Coder Social home page Coder Social logo

wcwidth's Introduction

A wcwidth implementation in C

Travis build status

A simple implementation of wcwidth(3) in C as a port of wcwidth.py.

Used in Termux as part of the libandroid-support.so shared library.

wcwidth's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

wcwidth's Issues

Faster preselection on zero-width and 'negative'-width

It would be my first contribution to a project which is not my own. So, no clue how to do.

I've forked the project: https://github.com/PitWD/termux_wcwidth
The changes are made inside the wcwidth() function. And I added two functions to test_wcwidth.c for a simple time measurement.

Please check the changes and if you are fine with them, I would love to get a little introduction how to prepare them for a regular commit.

1st edit:
~10% faster. Binary size is a little smaller, RAM usage the same.
2nd edit:
Even RAM usage is a little smaller...

Take (xft) font(s) into consideration?

I am looking into rxvt-unicode's handling of wide characters once again, and
found the following:

The NerdFileTypes font for example seems to use private use codepoints:

E.g. 0xe612 (๎˜’) is defined as:

StartChar: uniE612
Encoding: 58898 58898 924
Width: 2592
GlyphClass: 2
Flags: MW

Source: https://github.com/ryanoasis/nerd-fonts/blob/72ca7b7c1f9e8e3c5e319e0703fd95fc827bf3b9/glyph-source-fonts/original-source.otf.

According to https://codepoints.net/U+e612 this is "U+E612 PRIVATE USE CODEPOINT", with Ambiguous East Asian Width, where http://unicode.org/reports/tr11/#Ambiguous says

Private-use character codes and the replacement character have
ambiguous width, because they may stand in for characters of any width.

This could be interpreted as "let the font decide", and therefore I've looked
into using the following method in rxvt-unicode to ask the font for the actual
width:

int rxvt_font_xft::get_wcwidth (unicode_t unicode)
{
  FcChar32 ch = unicode;
  XGlyphInfo g;
  XftTextExtents32 (term->dpy, f, &ch, 1, &g);

  int w = g.width - g.x;
  // int wcw = max (WCWIDTH (unicode), 1);
  int r = (w + term->fwidth - 1) / term->fwidth;
  printf("get_wcwidth: fwidth=%d, g.width=%d-g.x=%d => w=%d, xOff=%d => %d\n", term->fwidth, g.width, g.x, w, g.xOff, r);
  r = (g.xOff + term->fwidth - 1 - term->fwidth/2) / term->fwidth;
  return r;
}

Using this in rxvt_term::scr_add_lines then:

int width = WCWIDTH (c);
rxvt_fontset *fs = FONTSET (rstyle);
rxvt_font *f = (*fs)[fs->find_font_idx (c)];
int wcwidth = f->get_wcwidth(c);
printf("c: %c, f: %d, width=%d, wcwidth=%d\n", c, f, width, wcwidth);
width = wcwidth;

While this method works well for rxvt-unicode (but is slow, of course), it does not play well with the shell/programs being used therein (just like some custom wcwidth implementation).

Therefore this would need to be provided through wcwidth itself.

Do you think it would be feasible to add support for something like this to this library/project?

I could imagine this being opt-in, e.g. through defining some environment variable (containing a list of fonts, since for rxvt-unicode only a defined list would be used).
Also this would need to have some caching in place then probably.

The obvious workaround is to patch (this) wcwidth to just return what you expect, which works well already.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.