Hi, all!

I'm not sure is it a bug or wrong docs, but there is a problem.

I tried to use fl_draw() with explicit length of UTF-8 text (MS Win).
Comments in fl_draw.H say "Draws an array of n characters starting at the given 
location." But in fact it doesn't work correctly, because the function 
fl_utf8toUtf16() which is used to convert encodings accepts count in BYTES! But 
we know byte!=symbol in UTF-8. Thereby the fl_draw() prints only part of 
characters from the source text. And last printed symbol is incorrect in many 
cases.

To solve the problem we can convert all string to UTF-16 and then restrict its 
by the specified length.

For example (fl_font_win32.cxx):

 void Fl_GDI_Graphics_Driver::draw(const char* str, int n, int x, int y) {
   COLORREF oldColor = SetTextColor(fl_gc, fl_RGB());
   SelectObject(fl_gc, font_descriptor()->fid);
-  int wn = fl_utf8toUtf16(str, n, wstr, wstr_len);
+  int wn = fl_utf8toUtf16(str, strlen(str), wstr, wstr_len);
   if(wn >= wstr_len) {
     wstr = (unsigned short*) realloc(wstr, sizeof(unsigned short) * (wn + 1));
     wstr_len = wn + 1;
     wn = fl_utf8toUtf16(str, n, wstr, wstr_len);
   }
+  wn = (wn < n) ? wn : n;
   TextOutW(fl_gc, x, y, (WCHAR*)wstr, wn);
   SetTextColor(fl_gc, oldColor); // restore initial state
 }

It works fine, but this solution treats the input line like null-terminated 
one. It can be wrong in some cases. What to do? I don't know exactly. May be we 
should set maximal conversion size (in several times more than input size).
Or we can modify the function fl_draw() to work with UTF-16 (add new one). 
Because in order to evaluate right length of text, user has to convert source 
line to the UTF-16, restrict size and convert into UTF-8 again to invoke 
fl_draw(s,x,y) where string will be converted one more time.  Thus at the 
moment there is two unnecessary conversions.

BR
Nikita
_______________________________________________
fltk-dev mailing list
fltk-dev@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to