changeset 1713:4d87e55c0bbb

Add utf8 uppercasing to small-caps For some reason utf8decode() didn't work for me, so I used mbrtowc() funcs.
author Jorge Arellano Cid <jcid@dillo.org>
date Thu, 07 Oct 2010 13:06:30 -0400
parents 9a71c8d886ef
children 80f90763d2c3
files dw/fltkplatform.cc dw/fltkviewbase.cc src/dillo.cc
diffstat 3 files changed, 63 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/dw/fltkplatform.cc	Thu Oct 07 13:02:49 2010 -0400
+++ b/dw/fltkplatform.cc	Thu Oct 07 13:06:30 2010 -0400
@@ -17,6 +17,9 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <stdio.h>
+#include <wchar.h>
+#include <wctype.h>
 
 #include "../lout/msg.h"
 #include "fltkcore.hh"
@@ -28,7 +31,6 @@
 #include <fltk/InvisibleBox.h>
 #include <fltk/Tooltip.h>
 #include <fltk/utf.h>
-#include <stdio.h>
 
 namespace dw {
 namespace fltk {
@@ -351,20 +353,50 @@
 int FltkPlatform::textWidth (core::style::Font *font, const char *text,
                              int len)
 {
-   int width;
+   char chbuf[MB_CUR_MAX];
+   wchar_t wc, wcu;
+   mbstate_t st1, st2;
+   int width = 0;
    FltkFont *ff = (FltkFont*) font;
-   setfont (ff->font, ff->size);
-   width = (int) getwidth (text, len);
-
-   if (font->letterSpacing) {
-      int curr = 0, next = 0;
-
-      while (next < len) {
+   int curr = 0, next = 0, nb;
+   
+   if (font->fontVariant == 1) {
+      int sc_fontsize, sc_letterSpacing;
+      sc_fontsize = lout::misc::roundInt(ff->size * 0.78);
+      sc_letterSpacing = lout::misc::roundInt(font->letterSpacing * 0.78);
+      memset (&st1, '\0', sizeof (mbstate_t));
+      memset (&st2, '\0', sizeof (mbstate_t));
+      for (curr = 0; next < len; curr = next) {
          next = nextGlyph(text, curr);
-         width += font->letterSpacing;
-         curr = next;
+         nb = (int)mbrtowc(&wc, text + curr, next - curr, &st1);
+         if ((wcu = towupper(wc)) == wc) {
+            /* already uppercase, just draw the character */
+            setfont(ff->font, ff->size);
+            width += font->letterSpacing;
+            width += (int)getwidth(text + curr, next - curr);
+         } else {
+            /* make utf8 string for converted char */
+            nb = wcrtomb(chbuf, wcu, &st2);
+            setfont(ff->font, sc_fontsize);
+            width += sc_letterSpacing;
+            width += (int)getwidth(chbuf, nb);
+         }
+      }
+   } else {
+      setfont (ff->font, ff->size);
+      width = (int) getwidth (text, len);
+   
+      if (font->letterSpacing) {
+         int curr = 0, next = 0;
+   
+         while (next < len) {
+            next = nextGlyph(text, curr);
+            width += font->letterSpacing;
+            curr = next;
+         }
       }
    }
+
    return width;
 }
 
--- a/dw/fltkviewbase.cc	Thu Oct 07 13:02:49 2010 -0400
+++ b/dw/fltkviewbase.cc	Thu Oct 07 13:06:30 2010 -0400
@@ -29,6 +29,8 @@
 #include <fltk/run.h>
 
 #include <stdio.h>
+#include <wchar.h>
+#include <wctype.h>
 #include "../lout/msg.h"
 
 using namespace fltk;
@@ -490,31 +492,35 @@
       /* Nonzero letter spacing adjustment, draw each glyph individually */
       int viewX = translateCanvasXToViewX (x),
           viewY = translateCanvasYToViewY (y);
-      int curr = 0, next = 0;
-      char *text2 = NULL;
+      int curr = 0, next = 0, cb;
+      char chbuf[MB_CUR_MAX];
+      wchar_t wc, wcu;
+      mbstate_t st1, st2;
 
       if (font->fontVariant == 1) {
          int sc_fontsize, sc_letterSpacing;
          sc_fontsize = lout::misc::roundInt(ff->size * 0.78);
          sc_letterSpacing = lout::misc::roundInt(font->letterSpacing * 0.78);
-         text2 = strdup(text);
-         while(next < len) {
+         memset (&st1, '\0', sizeof (mbstate_t));
+         memset (&st2, '\0', sizeof (mbstate_t));
+         for (curr = 0; next < len; curr = next) {
             next = theLayout->nextGlyph(text, curr);
-            if (isupper(text[curr])) {
+            cb = (int)mbrtowc(&wc, text + curr, next - curr, &st1);
+            if ((wcu = towupper(wc)) == wc) {
+               /* already uppercase, just draw the character */
                setfont(ff->font, ff->size);
                drawtext(text + curr, next - curr, viewX, viewY);
                viewX += font->letterSpacing;
                viewX += (int)getwidth(text + curr, next - curr);
             } else {
-               text2[curr] = toupper(text2[curr]);
+               /* make utf8 string for converted char */
+               int n = wcrtomb(chbuf, wcu, &st2);
                setfont(ff->font, sc_fontsize);
-               drawtext(text2 + curr, next - curr, viewX, viewY);
+               drawtext(chbuf, n, viewX, viewY);
                viewX += sc_letterSpacing;
-               viewX += (int)getwidth(text2 + curr, next - curr);
+               viewX += (int)getwidth(chbuf, n);
             }
-            curr = next;
          }
-         free(text2);
       } else {
          while (next < len) {
             next = theLayout->nextGlyph(text, curr);
--- a/src/dillo.cc	Thu Oct 07 13:02:49 2010 -0400
+++ b/src/dillo.cc	Thu Oct 07 13:06:30 2010 -0400
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <signal.h>
+#include <locale.h>
 
 #include <fltk/Window.h>
 #include <fltk/TabGroup.h>
@@ -368,6 +369,9 @@
       }
    }
 
+   /* We use utf-8 internally, force character conversions follow suit */
+   setlocale (LC_CTYPE, "en_US.utf8");
+
    fltk::run();
 
    /*