Browse Source

Corrected issue with text measure on Latin-1 chars

Considering chars inputs come in UTF8 form!
raysan5 6 years ago
parent
commit
1038e79b36
1 changed files with 19 additions and 2 deletions
  1. 19 2
      src/text.c

+ 19 - 2
src/text.c

@@ -935,13 +935,30 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing
     float textHeight = (float)font.baseSize;
     float textHeight = (float)font.baseSize;
     float scaleFactor = fontSize/(float)font.baseSize;
     float scaleFactor = fontSize/(float)font.baseSize;
 
 
+    unsigned char letter = 0;       // Current character
+    int index = 0;                  // Index position in sprite font
+    
     for (int i = 0; i < len; i++)
     for (int i = 0; i < len; i++)
     {
     {
         lenCounter++;
         lenCounter++;
-
+        
         if (text[i] != '\n')
         if (text[i] != '\n')
         {
         {
-            int index = GetGlyphIndex(font, (int)text[i]);
+            if ((unsigned char)text[i] == 0xc2)         // UTF-8 encoding identification
+            {
+                // Support UTF-8 encoded values from [0xc2 0x80] -> [0xc2 0xbf](¿)
+                letter = (unsigned char)text[i + 1];
+                index = GetGlyphIndex(font, (int)letter);
+                i++;
+            }
+            else if ((unsigned char)text[i] == 0xc3)    // UTF-8 encoding identification
+            {
+                // Support UTF-8 encoded values from [0xc3 0x80](À) -> [0xc3 0xbf](ÿ)
+                letter = (unsigned char)text[i + 1];
+                index = GetGlyphIndex(font, (int)letter + 64);
+                i++;
+            }
+            else index = GetGlyphIndex(font, (unsigned char)text[i]);
 
 
             if (font.chars[index].advanceX != 0) textWidth += font.chars[index].advanceX;
             if (font.chars[index].advanceX != 0) textWidth += font.chars[index].advanceX;
             else textWidth += (font.chars[index].rec.width + font.chars[index].offsetX);
             else textWidth += (font.chars[index].rec.width + font.chars[index].offsetX);