On 21 Sep 2011, at 18:20, Matthias Melcher wrote:
>
> This should work (untested):
>
> int findFirstNCharacters(const char *str, int n)
> {
> int bytes = 0;
> int maxBytes = strlen(str);
> while (n>0 != *str!=0) {
> int bytesInChar = fl_utf_nb_char(*str, maxBytes);
> if (bytesInChar==-1) break; // error in UTF-8
> bytes += bytesInChar;
> maxBytes -= bytesInChar;
> str += bytesInChar;
> n--;
> }
> return bytes;
> }
If the intent is to trim glyphs off the end of a string until it only has the
required number of glyphs left, then I think you could do something useful
using:
/* F2: Move backward to the previous valid UTF8 sequence start */
FL_EXPORT const char* fl_utf8back(const char* p, const char* start, const char*
end);
Starting one byte in from the end, I think you can use this to walk backwards
through the string until you have removed the necessary number of glyphs...
Say you have used:
int num_glyphs = fl_utf_nb_char(const unsigned char *buf, int len);
To determine that there are 10 glyphs in your string, and you know you only
have room for 6 on the screen, then you could use
const char *glyph_begin = fl_utf8back(buf, ...);
4 times to walk back to the start of glyph 7, then by comparing the values of
"buf" and "glyph_begin" you know exactly how many bytes in your string are
required to create the 6 glyphs that you can fit on the display...
Well, something like that, anyway...
_______________________________________________
fltk-dev mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk-dev