ecere/gfx/LFBDisplayDriver: Fixed wrong returned 'ascent' on Font
[sdk] / ecere / src / gfx / drivers / LFBDisplayDriver.ec
index 3b820ac..b427193 100644 (file)
@@ -11,7 +11,7 @@ namespace gfx::drivers;
 #include <ft2build.h>
 #include FT_FREETYPE_H
 #include FT_TRUETYPE_TABLES_H
-#include <freetype/ttunpat.h>
+#include FT_UNPATENTED_HINTING_H
 #define property  _property
 #include "harfbuzz.h"
 #undef property
@@ -66,7 +66,7 @@ import "Direct3D9DisplayDriver"
 
 static HB_Script theCurrentScript;
 
-static unichar UTF16GetChar(uint16 *string, int * nw)
+static unichar UTF16GetChar(const uint16 *string, int * nw)
 {
    unichar ch;
    if(HB_IsHighSurrogate(string[0]) && HB_IsLowSurrogate(string[1]))
@@ -82,7 +82,7 @@ static unichar UTF16GetChar(uint16 *string, int * nw)
    return ch;
 }
 
-static HB_Bool hb_stringToGlyphs(HB_Font font, uint16 * string, uint length, HB_Glyph *glyphs, uint *numGlyphs, HB_Bool rightToLeft)
+static HB_Bool hb_stringToGlyphs(HB_Font font, const uint16 * string, uint length, HB_Glyph *glyphs, uint *numGlyphs, HB_Bool rightToLeft)
 {
    FT_Face face = ((FontEntry)font->userData).face;
    int glyph_pos = 0;
@@ -135,7 +135,7 @@ static void hb_getAdvances(HB_Font font, const HB_Glyph * glyphs, uint numGlyphs
    }
 }
 
-static HB_Bool hb_canRender(HB_Font font, uint16 * string, uint length)
+static HB_Bool hb_canRender(HB_Font font, const uint16 * string, uint length)
 {
    FT_Face face = ((FontEntry)font->userData).face;
    int c, nw;
@@ -265,7 +265,7 @@ static int numFonts;
 #undef CompareString
 static BinaryTree loadedFonts
 {
-   CompareKey = (void *)BinaryTree::CompareString 
+   CompareKey = (void *)BinaryTree::CompareString
 };
 
 class FontEntry : BTNode
@@ -276,7 +276,7 @@ class FontEntry : BTNode
 
    int used;
    byte * buffer;
-   
+
    //If we don't save the FT_Stream before sacrificing it to FreeType, the garbage collector (if one is used) will destroy it prematurely
    FT_Stream stream;
    Font font;
@@ -284,8 +284,7 @@ class FontEntry : BTNode
 
    ~FontEntry()
    {
-      char * fileName = (char *)key;
-      delete fileName;
+      delete (char *)key;
       delete buffer;
       if(hbFace)
          HB_FreeFace(hbFace);
@@ -360,8 +359,8 @@ class GlyphPack : BTNode
       FontEntry fontEntry = null;
       FT_Face faces[128];
       float scales[128];
-      bool isGlyph = (uint)key & 0x80000000;
-      int curScript = ((uint)key & 0x7F000000) >> 24;
+      bool isGlyph = ((uint)key & 0x80000000) != 0;
+      //int curScript = ((uint)key & 0x7F000000) >> 24;
       unichar testChar = 0;
       /*
       if(isGlyph)
@@ -373,7 +372,7 @@ class GlyphPack : BTNode
                // printf("\nRendering arabic in %s (%d)\n", font.faceName, key & 0xFFFFFF);
                break;
             case HB_Script_Devanagari:
-               testChar = 0x905; 
+               testChar = 0x905;
                break;
             case 60: testChar = 'あ'; break;
             case 61: testChar = 0x3400; break;
@@ -395,7 +394,7 @@ class GlyphPack : BTNode
       FT_Set_Char_Size( fontEntry.face, (int)(font.size * 64), (int)(font.size * 64), 96, 96);
       */
 
-      maxWidth = 0; 
+      maxWidth = 0;
       maxHeight = 0;
 
       for(c = 0; c < MAX_FONT_LINK_ENTRIES; c++)
@@ -405,7 +404,7 @@ class GlyphPack : BTNode
          {
             FT_Matrix matrix;
             FT_Vector pen = { 0, 0 };
-            
+
             if(font.fakeItalic)
             {
                matrix.xx = (FT_Fixed)( 1.0 * 0x10000L );
@@ -416,6 +415,8 @@ class GlyphPack : BTNode
             }
             //FT_Set_Char_Size(fontEntry.face, (int)(font.size * 64), (int)(font.size * 64), 96, 96);
             fontEntry.scale = FaceSetCharSize(fontEntry.face, font.size);
+            if(!font.scale)
+               font.scale = fontEntry.scale;
             if(!c)
             {
                if(!fontEntry.face->units_per_EM)
@@ -499,13 +500,13 @@ class GlyphPack : BTNode
             int glyphNo = isGlyph ? (((uint)key | c) & 0x00FFFFFF) : FT_Get_Char_Index(faces[c], (uint)key | c);
 
             FT_Load_Glyph(faces[c], glyphNo, /*FT_LOAD_DEFAULT | FT_LOAD_FORCE_AUTOHINT*/ FT_LOAD_DEFAULT /*FT_LOAD_NO_HINTING*/); // FT_LOAD_RENDER // FT_LOAD_NO_HINTING
-            
+
             FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL);
 
             x = sx;
             y = sy;
             //printf("%d, %d\n", maxHeight, faces[c]->size->metrics.height >> 6);
-            
+
             glyph->left = slot->bitmap_left;
             // glyph->top = ((64 + (64 - faces[c]->glyph->metrics.height & 0x3F)) >> 6) + (int)(ascender - slot->bitmap_top) + font.height - (faces[c]->size->metrics.height >> 6);
             // glyph->top = (int)(ascender - slot->bitmap_top) + 2 * (font.height - maxHeight);
@@ -519,7 +520,7 @@ class GlyphPack : BTNode
 
             //glyph->top = (int)(ascender - slot->bitmap_top); // + ((faces[c]->size->metrics.height >> 6) - (faces[0]->size->metrics.height >> 6)) + (font.height - (faces[c]->size->metrics.height >> 6));
             //glyph->top = (int)(ascender - slot->bitmap_top); // + ((faces[c]->size->metrics.height >> 6) - (faces[0]->size->metrics.height >> 6)) + (font.height - (faces[c]->size->metrics.height >> 6));
-            
+
             //glyph->top = (int)(ascender - slot->bitmap_top);// + (font.height - maxHeight);
             glyph->top = (int)(ascender - slot->bitmap_top) + (int)(font.height - (faces[c]->size->metrics.height >> 6)) / 2;
 
@@ -530,7 +531,7 @@ class GlyphPack : BTNode
             {
                int total = 0;
                int numPixels = 0;
-               int max;
+               //int max;
                if(slot->bitmap.pixel_mode != FT_PIXEL_MODE_MONO)
                {
                   for(j = y, q = 0; j<yMax; j++)
@@ -547,7 +548,7 @@ class GlyphPack : BTNode
                      q += slot->bitmap.pitch;
                   }
                }
-               max = numPixels ? (total / numPixels) : 1;
+               //max = numPixels ? (total / numPixels) : 1;
 
                for(j = y, q = 0; j<yMax; j++)
                {
@@ -558,7 +559,7 @@ class GlyphPack : BTNode
                      {
                         picture[j * bitmap.stride + i] = (slot->bitmap.buffer[q + p] & bit) ? 255 : 0;
                         bit >>= 1;
-                        if(!bit) { bit = 0x80; p++; }                     
+                        if(!bit) { bit = 0x80; p++; }
                      }
                      else
                      {
@@ -589,7 +590,7 @@ class GlyphPack : BTNode
             for(c = 0; c<256; c++)
                bitmap.palette[c] = ColorAlpha { 255, { (byte)c,(byte)c,(byte)c } };
             bitmap.pixelFormat = pixelFormat8;
-            
+
             /*
             //strcpy(fileName, faceName);
             if(flags)
@@ -644,14 +645,14 @@ static uint * shaping(FontEntry entry, uint16 * string, int len, HB_Script scrip
    shaper_item.num_glyphs = shaper_item.item.length;
    shaper_item.glyphIndicesPresent = 0;
    shaper_item.initialGlyphCount = 0;
-   shaper_item.num_glyphs = 0;   
-   shaper_item.glyphs = null;   
+   shaper_item.num_glyphs = 0;
+   shaper_item.glyphs = null;
 
    while(!HB_ShapeItem(&shaper_item))
    {
       if(shaper_item.num_glyphs > maxGlyphs)
       {
-         maxGlyphs = shaper_item.num_glyphs;                                                                                                            
+         maxGlyphs = shaper_item.num_glyphs;
          glyphs = shaper_item.glyphs = renew0 glyphs HB_Glyph[maxGlyphs];
          shaper_item.attributes   = renew0 shaper_item.attributes HB_GlyphAttributes[maxGlyphs];
          shaper_item.advances     = renew0 shaper_item.advances HB_Fixed[maxGlyphs];
@@ -662,11 +663,11 @@ static uint * shaping(FontEntry entry, uint16 * string, int len, HB_Script scrip
       {
          shaper_item.glyphs = glyphs;
          shaper_item.num_glyphs = maxGlyphs;
-      } 
+      }
   }
 
    *numGlyphs = shaper_item.num_glyphs;
-   *rightToLeft = shaper_item.item.bidiLevel % 2;
+   *rightToLeft = (bool)(shaper_item.item.bidiLevel % 2);
    return shaper_item.glyphs;
 }
 
@@ -706,6 +707,10 @@ public class Font : struct
       }
 #endif
    }
+   public property int ascent
+   {
+      get { return (int)(this ? ascent * scale : 0); }
+   }
 };
 
 public class LFBDisplay : struct
@@ -827,19 +832,19 @@ static int CALLBACK MyFontProc(ENUMLOGFONTEX * font, NEWTEXTMETRICEX *lpntme, in
       {
          if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts",0,KEY_READ,&key) ||
             !RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Fonts",0,KEY_READ,&key))
-         {  
+         {
             int value = 0;
             while(true)
             {
                char entryName[1024];
                char fontFileName[1024];
-               uint32 type;
-               int size = 1024;
-               int sizeFileName = 1024;
+               DWORD type;
+               DWORD size = 1024;
+               DWORD sizeFileName = 1024;
                char * occurence;
-               if(RegEnumValue(key, value++, entryName, (PDWORD)&size, null, (PDWORD)&type, (LPBYTE)fontFileName, (PDWORD)&sizeFileName) != ERROR_SUCCESS)
+               if(RegEnumValue(key, value++, entryName, &size, null, (PDWORD)&type, (LPBYTE)fontFileName, &sizeFileName) != ERROR_SUCCESS)
                   break;
-               if((occurence = SearchString((char *)entryName, 0, (char *)font->elfFullName, false, false)))
+               if((occurence = SearchString(entryName, 0, (const char *)font->elfFullName, false, false)))
                {
                   int c;
                   for(c = (int)(occurence - entryName) - 1; c >= 0; c--)
@@ -863,19 +868,21 @@ static int CALLBACK MyFontProc(ENUMLOGFONTEX * font, NEWTEXTMETRICEX *lpntme, in
                   strcpy(fileName, fontFileName);
                   RegCloseKey(key);
                   return 0;
-               }               
+               }
             }
             RegCloseKey(key);
             return 1;
          }
       }
    }
-   return 1;   
+   return 1;
 }
 #endif
 
+#if !defined(ECERE_NOTRUETYPE)
 static int utf16BufferSize = 0;
 static uint16 * utf16 = null;
+#endif
 
 public class LFBDisplayDriver : DisplayDriver
 {
@@ -911,7 +918,7 @@ public class LFBDisplayDriver : DisplayDriver
             case 2: stride = width;                    break;
          }
       }
-      bitmap.stride = stride;   
+      bitmap.stride = stride;
       bitmap.width = width;
       bitmap.height = height;
       bitmap.size = (uint32) stride * (uint32)height;
@@ -948,7 +955,7 @@ public class LFBDisplayDriver : DisplayDriver
    }
 
    bool ConvertBitmap(DisplaySystem displaySystem, Bitmap src, PixelFormat format, ColorAlpha * palette)
-   {  
+   {
       bool result = false;
       LFBSystem lfbSystem = displaySystem ? displaySystem.driverData : null;
 
@@ -961,7 +968,7 @@ public class LFBDisplayDriver : DisplayDriver
                /*
                DWORD c;
                DWORD * picture = (DWORD *)src.picture;
-               
+
                for(c = 0; c<src.size; c++, picture++)
                {
                   if(*picture & 0xFFFFFF)
@@ -1004,6 +1011,7 @@ public class LFBDisplayDriver : DisplayDriver
                   src.palette = bitmap.palette;
                   src.stride = bitmap.stride;
                   src.size = bitmap.size;
+                  src.sizeBytes = bitmap.sizeBytes;
                   src.pixelFormat = bitmap.pixelFormat;
 
                   result = true;
@@ -1232,7 +1240,7 @@ public class LFBDisplayDriver : DisplayDriver
       LFBDisplay lfbDisplay = display ? display.driverData : null;
       LFBSurface lfbSurface = surface.driverData;
       uint index;
-      if(display) color = color /*& 0xFFFFFF*/;
+      //if(display) color = color & 0xFFFFFF;
       lfbSurface.foregroundRgb = color;
 
       if(lfbSurface.font && lfbDisplay)
@@ -1254,7 +1262,7 @@ public class LFBDisplayDriver : DisplayDriver
          case pixelFormat565:  lfbSurface.foreground = (Color565)color; break;
          case pixelFormat888:  lfbSurface.foreground = color; break;
          case pixelFormatRGBA: lfbSurface.foreground = (ColorRGBA)color; break;
-         case pixelFormatText: 
+         case pixelFormatText:
             if(display)
                lfbSurface.foreground = BestColorMatch(lfbDisplay.bitmap.palette,0,15,color) << 8;
             else
@@ -1267,7 +1275,7 @@ public class LFBDisplayDriver : DisplayDriver
    {
       LFBDisplay lfbDisplay = display ? display.driverData : null;
       LFBSurface lfbSurface = surface.driverData;
-      color = color /*& 0xFFFFFF*/;
+      //color = color & 0xFFFFFF;
       switch(lfbSurface.bitmap.pixelFormat)
       {
          case pixelFormat8:
@@ -1281,7 +1289,7 @@ public class LFBDisplayDriver : DisplayDriver
          case pixelFormat565:  lfbSurface.background = (Color565)color; break;
          case pixelFormat888:  lfbSurface.background = color; break;
          case pixelFormatRGBA: lfbSurface.background = (ColorRGBA)color; break;
-         case pixelFormatText: 
+         case pixelFormatText:
             if(display)
                lfbSurface.background = BestColorMatch(lfbDisplay.bitmap.palette,0,15,color) << 12;
             else
@@ -1481,7 +1489,7 @@ public class LFBDisplayDriver : DisplayDriver
                         ((uint16 *)lfbSurface.bitmap.picture)[offset]=(uint16)color;
                      else
                      {
-                        ((uint16 *)lfbSurface.bitmap.picture)[offset] = 
+                        ((uint16 *)lfbSurface.bitmap.picture)[offset] =
                            (((uint16 *)lfbSurface.bitmap.picture)[offset] & 0xF000) | (uint16)color;
                      }
                   }
@@ -1512,7 +1520,7 @@ public class LFBDisplayDriver : DisplayDriver
                break;
          }
       }
-      else 
+      else
       {
          if(x2 >= x1)
          {
@@ -1697,14 +1705,14 @@ public class LFBDisplayDriver : DisplayDriver
    void memset_32_aligned(void *buf, int val, int dwords)
    {
             // Qword align
-            if ((int)(buf) & 4) 
+            if ((int)(buf) & 4)
             {
                     *((uint32 *)(buf)) = val;
                     buf = ((uint32 *)(buf))+1;
                     dwords--;
             }
-    
-            if (dwords > 1) 
+
+            if (dwords > 1)
             {
                     __asm {
                             cld
@@ -1724,7 +1732,7 @@ public class LFBDisplayDriver : DisplayDriver
                             emms
                     };
             }
-    
+
             // Final dword
             if (dwords & 1) *((int*)(buf)) = val;
    }*/
@@ -1816,21 +1824,21 @@ public class LFBDisplayDriver : DisplayDriver
    void memset_32(void *buf, uint32 val, uint32 dwords)
    {
       int align = 0;
-      if ((uint32)(buf) & 3) 
+      if ((uint32)(buf) & 3)
       {
          align = 4;
          dwords--;
-         
+
          if (((uint32)(buf) & 1))
          {
-            *(byte *)(buf) = (byte)(val&0xFF); 
+            *(byte *)(buf) = (byte)(val&0xFF);
             buf = ((byte *)(buf))+1;
             val = ((val& 0xFF) << 24) || ((val& 0xFFFFFF00) >> 8);
             align --;
          }
          if (((uint32)(buf) & 2))
          {
-            *(uint16 *)(buf) = (uint16)(val&0xFFFF); 
+            *(uint16 *)(buf) = (uint16)(val&0xFFFF);
             buf = ((uint16 *)(buf))+1;
             val = ((val& 0xFFFF) << 16) || ((val& 0xFFFF0000) >> 16);
             align-=2;
@@ -1841,12 +1849,12 @@ public class LFBDisplayDriver : DisplayDriver
       {
          if (align == 1)
          {
-            *(byte *)(buf) = (byte)(val&0xFF); 
+            *(byte *)(buf) = (byte)(val&0xFF);
          }
          else
          {
-            *(uint16 *)(buf) = (uint16)(val&0xFFFF); 
-            if (align & 1) *((byte *)(buf)+2) = (byte)((val>>16)&0xFF); 
+            *(uint16 *)(buf) = (uint16)(val&0xFFFF);
+            if (align & 1) *((byte *)(buf)+2) = (byte)((val>>16)&0xFF);
          }
       }
    }
@@ -1877,7 +1885,7 @@ public class LFBDisplayDriver : DisplayDriver
       {
          int w,y;
          byte * theOffset;
-         
+
          w = x2-x1+1;
          if(w<1) return;
 
@@ -1901,7 +1909,7 @@ public class LFBDisplayDriver : DisplayDriver
             }
             else
             {
-               if(surface.background.a == 255 || lfbSurface.clearing)
+               if(!surface.blend || surface.background.a == 255 || lfbSurface.clearing)
                {
                   switch(GetColorDepthShifts(lfbSurface.bitmap.pixelFormat))
                   {
@@ -1952,14 +1960,14 @@ public class LFBDisplayDriver : DisplayDriver
                               mov eax,color
                               mov edx,y
                               mov ecx,w
-                              
+
                               start:
                                  push ecx
-                                 
+
                                  rep stosd
                                  add edi,ebx
                                  pop ecx
-                                 dec edx                        
+                                 dec edx
                                  jnz start
                               pop edx
                               pop ebx
@@ -1998,8 +2006,8 @@ public class LFBDisplayDriver : DisplayDriver
                         {
                            int c;
                            for(c = 0; c < w; c++, dest++)
-                           {                              
-                              Color destColor;
+                           {
+                              Color destColor = 0;
                               if(pixelFormat == pixelFormat565)      { destColor = (Color)*(Color565 *)dest; }
                               else if(pixelFormat == pixelFormat555) { destColor = (Color)*(Color555 *)dest; }
                               else if(pixelFormat == pixelFormat444) { destColor = (Color)*(Color444 *)dest; }
@@ -2036,7 +2044,7 @@ public class LFBDisplayDriver : DisplayDriver
                         {
                            int c;
                            for(c = 0; c < w; c++, dest++)
-                           {                              
+                           {
                               int dr = dest->color.r;
                               int dg = dest->color.g;
                               int db = dest->color.b;
@@ -2352,7 +2360,7 @@ public class LFBDisplayDriver : DisplayDriver
       if(bitmap.pixelFormat != lfbDisplay.bitmap.pixelFormat || bitmap.width < w || bitmap.height < h)
       {
          bitmap.Free();
-         bitmap.Allocate(null, w,h,w, lfbDisplay.bitmap.pixelFormat, 
+         bitmap.Allocate(null, w,h,w, lfbDisplay.bitmap.pixelFormat,
             (lfbDisplay.bitmap.pixelFormat == pixelFormat8)?true:false);
       }
       if(bitmap)
@@ -2383,7 +2391,7 @@ public class LFBDisplayDriver : DisplayDriver
       {
          w = Abs(w);
          sw = Abs(sw);
-         flip = true; 
+         flip = true;
       }
 
       s2dw=(float)w / sw;
@@ -2459,7 +2467,7 @@ public class LFBDisplayDriver : DisplayDriver
             ColorAlpha * backsrc;
             ColorAlpha * source = ((ColorAlpha *) src.picture) + sy * addsource + sx;
             ColorAlpha * dest = ((ColorAlpha *) lfbSurface.bitmap.picture) + dy * adddest   + dx;
-            if(flip < 0) source += sw-1;
+            if(flip) source += sw-1;
             adddest -= w;
             yerr = 0;
             for(y=0; y<sh; y++)
@@ -2527,7 +2535,7 @@ public class LFBDisplayDriver : DisplayDriver
       {
          w = Abs(w);
          sw = Abs(sw);
-         flip = true; 
+         flip = true;
       }
 
       s2dw=(float)w / sw;
@@ -2597,10 +2605,7 @@ public class LFBDisplayDriver : DisplayDriver
          AlphaWriteMode alphaWrite = surface.alphaWrite;
          if(src.alphaBlend && surface.blend)
          {
-            int x, y;
-            uint xerr,yerr;
             uint adddest = lfbSurface.bitmap.stride, addsource = src.stride;
-            ColorAlpha * backsrc;
             ColorAlpha * source = ((ColorAlpha *) src.picture) + sy * addsource + sx;
             ColorAlpha * dest = ((ColorAlpha *) lfbSurface.bitmap.picture) + dy * adddest   + dx;
             float scaleX = (float)sw / w;
@@ -2626,7 +2631,6 @@ public class LFBDisplayDriver : DisplayDriver
                      int x0 = x * sw / w;
                      int x1 = Min(x0 + 1, sw - 1);
                      float beta = x * scaleX - x0;
-                     ColorAlpha color;
                      ColorAlpha src00, src01, src10, src11;
                      float a1,r1,g1,b1,a2,r2,g2,b2;
                      float a,r,g,b;
@@ -2665,12 +2669,12 @@ public class LFBDisplayDriver : DisplayDriver
                         else if(alphaWrite)
                            dest->a = (byte)a;
                      }
-                  } 
+                  }
                   dest += adddest;
                }
             }
-            else 
-            { 
+            else
+            {
                int y;
                for (y = 0; y < h; y++)
                {
@@ -2684,7 +2688,6 @@ public class LFBDisplayDriver : DisplayDriver
                      float a = 0, r = 0, g = 0, b = 0;
                      int numPixels = 0;
                      int i, j;
-                     ColorAlpha color;
                      for (i = y0; i <= y1; i++)
                         for (j = x0; j <= x1; j++)
                         {
@@ -2694,13 +2697,12 @@ public class LFBDisplayDriver : DisplayDriver
                            g += pixel.color.g;
                            b += pixel.color.b;
                            numPixels++;
-                        } 
+                        }
                      a /= numPixels;
                      r /= numPixels;
                      g /= numPixels;
                      b /= numPixels;
                      {
-                        ColorAlpha src = *source;
                         ColorAlpha dst = *dest;
                         int cr = (int)(a * r / 255 + ((255 - a) * dst.color.r / 255));
                         int cg = (int)(a * g / 255 + ((255 - a) * dst.color.g / 255));
@@ -2720,7 +2722,7 @@ public class LFBDisplayDriver : DisplayDriver
                      }
                   }
                   dest += adddest;
-               } 
+               }
             }
          }
          else if(!src.paletteShades && src.pixelFormat == lfbSurface.bitmap.pixelFormat)
@@ -2768,13 +2770,13 @@ public class LFBDisplayDriver : DisplayDriver
                   delete fontEntry;
                }
             }
-         } 
+         }
 #endif
          delete font;
       }
    }
 
-   Font LoadFont(DisplaySystem displaySystem, char * faceName, float size, FontFlags flags)
+   Font LoadFont(DisplaySystem displaySystem, const char * faceName, float size, FontFlags flags)
    {
       void * result = null;
 
@@ -2784,17 +2786,20 @@ public class LFBDisplayDriver : DisplayDriver
       {
          char fileName[MAX_LOCATION];
          bool fakeItalic = flags.italic;
-         char linkCfgPath[MAX_LOCATION];
          int fontID = 0;
 #if !defined(__WIN32__)
          File linkCfg;
 #endif
-         char * ecereFonts = getenv("ECERE_FONTS");
+         const char * ecereFonts = getenv("ECERE_FONTS");
          if(!ecereFonts) ecereFonts = "<:ecere>";
 #if !defined(__WIN32__)
-         strcpy(linkCfgPath, ecereFonts);
-         PathCat(linkCfgPath, "linking.cfg");
-         linkCfg = FileOpen(linkCfgPath, read);
+         {
+            char linkCfgPath[MAX_LOCATION];
+
+            strcpy(linkCfgPath, ecereFonts);
+            PathCat(linkCfgPath, "linking.cfg");
+            linkCfg = FileOpen(linkCfgPath, read);
+         }
 #endif
          strcpy(fileName, faceName);
          strcpy(font.faceName, faceName);
@@ -2849,27 +2854,27 @@ public class LFBDisplayDriver : DisplayDriver
                FontData fontData = { { 0 } };
                LOGFONT logFont = { 0 };
                HDC hdc = GetDC(0);
-      
+
                fakeItalic = false;
-      
+
                logFont.lfCharSet = DEFAULT_CHARSET;
                strcpy(logFont.lfFaceName, faceName);
                fontData.flags = flags;
-                  
-               EnumFontFamiliesEx(hdc, &logFont, (void *)MyFontProc, (DWORD)&fontData, 0);
+
+               EnumFontFamiliesEx(hdc, &logFont, (void *)MyFontProc, (LPARAM)&fontData, 0);
                if(!fontData.fileName[0] && flags.bold)
                {
                   fontData.forgive = true;
-                  EnumFontFamiliesEx(hdc, &logFont, (void *)MyFontProc, (DWORD)&fontData, 0);
+                  EnumFontFamiliesEx(hdc, &logFont, (void *)MyFontProc, (LPARAM)&fontData, 0);
                }
                if(!fontData.fileName[0])
                {
                   // Fake italic
                   fontData.flags.italic = false;
-                  EnumFontFamiliesEx(hdc, &logFont, (void *)MyFontProc, (DWORD)&fontData, 0);
+                  EnumFontFamiliesEx(hdc, &logFont, (void *)MyFontProc, (LPARAM)&fontData, 0);
                   fakeItalic = true;
                }
-      
+
                if(fontData.fileName[0])
                {
                   GetWindowsDirectory(fileName, MAX_LOCATION);
@@ -2877,7 +2882,7 @@ public class LFBDisplayDriver : DisplayDriver
                   PathCat(fileName, fontData.fileName);
                }
                ReleaseDC(0, hdc);
-            }  
+            }
    #elif !defined(ECERE_NOFONTCONFIG)
             {
                char * fileName2;
@@ -2899,7 +2904,7 @@ public class LFBDisplayDriver : DisplayDriver
 
                if(testChar)
                   FcCharSetAddChar(charSet, testChar);
-   
+
                pattern = FcPatternBuild(null,
                                //FC_SOURCE, FcTypeString, "freetype",
                                FC_FAMILY, FcTypeString, faceName,
@@ -2916,13 +2921,13 @@ public class LFBDisplayDriver : DisplayDriver
                // printf("Locating %s\n", faceName);
                if(matched)
                {
-                  FcPatternGetString(matched, FC_FAMILY, 0, &family);
+                  FcPatternGetString(matched, FC_FAMILY, 0, (FcChar8 **)&family);
                   //printf("Fontconfig returned %s\n", family);
                }
                if(matched && (result == FcResultMatch /*|| result == FcResultNoId*/) /*&& !strcmpi(family, faceName)*/)
                {
                   double fontSize;
-                  FcPatternGetString (matched, FC_FILE, 0, &fileName2);
+                  FcPatternGetString (matched, FC_FILE, 0, (FcChar8 **)&fileName2);
                   FcPatternGetInteger(matched, FC_INDEX, 0, &fontID);
                   FcPatternGetDouble(matched, FC_SIZE, 0, &fontSize);
                   strcpy(fileName, fileName2);
@@ -2956,11 +2961,11 @@ public class LFBDisplayDriver : DisplayDriver
             links[0] = 0;
             if(!RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\FontLink\\SystemLink",0,KEY_READ,&key) ||
                !RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\FontLink\\SystemLink",0,KEY_READ,&key))
-            {  
+            {
                // int value = 0;
-               uint32 type;
-               int size = 1024;
-               RegQueryValueEx(key, faceName, null, (LPDWORD)&type, (LPBYTE)links, (LPDWORD)&size);
+               DWORD type;
+               DWORD size = 1024;
+               RegQueryValueEx(key, faceName, null, &type, (LPBYTE)links, &size);
                memset(links + size, 0, 1024 - size);
                RegCloseKey(key);
             }
@@ -3002,7 +3007,7 @@ public class LFBDisplayDriver : DisplayDriver
                      FT_Open_Args args = { 0 };
                      FT_Parameter param = { FT_PARAM_TAG_UNPATENTED_HINTING };
                      FT_Stream stream = new0 FT_StreamRec[1];
-                     
+
                      if(!ftLibrary)
                         FT_Init_FreeType( &ftLibrary );
 
@@ -3126,14 +3131,14 @@ public class LFBDisplayDriver : DisplayDriver
                            matched = FcFontMatch (0, pattern, &result);
                            if(matched)
                            {
-                              FcPatternGetString(matched, FC_FAMILY, 0, &family);
+                              FcPatternGetString(matched, FC_FAMILY, 0, (FcChar8 **)&family);
                               // printf("Fontconfig returned %s\n", family);
                            }
                            if(matched && (result == FcResultMatch /*|| result == FcResultNoId*/) &&
-                              FcPatternGetString(matched, FC_FAMILY, 0, &family) == FcResultMatch /*&& !strcmpi(family, links + linksPos + c + 1)*/)
+                              FcPatternGetString(matched, FC_FAMILY, 0, (FcChar8 **)&family) == FcResultMatch /*&& !strcmpi(family, links + linksPos + c + 1)*/)
                            {
                               double fontSize;
-                              FcPatternGetString (matched, FC_FILE, 0, &fileName2);
+                              FcPatternGetString (matched, FC_FILE, 0, (FcChar8 **)&fileName2);
                               FcPatternGetInteger(matched, FC_INDEX, 0, &fontID);
                               FcPatternGetDouble(matched, FC_SIZE, 0, &fontSize);
                               strcpy(fileName, fileName2);
@@ -3149,7 +3154,7 @@ public class LFBDisplayDriver : DisplayDriver
                         }
                      }
 #endif
-                     
+
                   }
                   linksPos += c;
                   while(links[linksPos] && links[linksPos] != ',') linksPos++;
@@ -3157,7 +3162,7 @@ public class LFBDisplayDriver : DisplayDriver
                }
             }
          }
-            
+
          if(!result)
             UnloadFont(displaySystem, font);
          else
@@ -3173,7 +3178,7 @@ public class LFBDisplayDriver : DisplayDriver
    }
 
 #if !defined(ECERE_NOTRUETYPE)
-   void ::ProcessString(Font font, DisplaySystem displaySystem, byte * text, int len, 
+   void ::ProcessString(Font font, DisplaySystem displaySystem, const byte * text, int len,
                         void (* callback)(Surface surface, Display display, int x, int y, GlyphInfo * glyph, Bitmap bitmap),
                         Surface surface, Display display, int * x, int y)
    {
@@ -3184,19 +3189,19 @@ public class LFBDisplayDriver : DisplayDriver
          int c, nb, glyphIndex = 0;
          unichar lastPack = 0;
          GlyphPack pack = font.asciiPack;
-         int wc;
-         uint * glyphs;
+         int wc = 0;
+         uint * glyphs = null;
          int numGlyphs = 0;
          bool rightToLeft = false;
          int fontEntryNum = 0;
          int glyphScript = 0;
          FontEntry curFontEntry;
-         
+
          pack.bitmap.alphaBlend = true;
 
          for(c = 0; c < len || (numGlyphs && (rightToLeft ? (glyphIndex >= 0) : (glyphIndex < numGlyphs)));)
          {
-            uint glyphNo;
+            uint glyphNo = 0;
             uint packNo;
             if(numGlyphs && (rightToLeft ? (glyphIndex >= 0) : (glyphIndex < numGlyphs)))
             {
@@ -3206,18 +3211,20 @@ public class LFBDisplayDriver : DisplayDriver
             else
             {
                HB_Script curScript = HB_Script_Common;
-               byte * scriptStart = text + c;
-               unichar nonASCIIch = 0;
+               const byte * scriptStart = text + c;
+               //unichar nonASCIIch = 0;
                unichar ch;
                unichar ahead = 0;
                unichar testChar = 0;
-               char * testLang = null;
-               
+#if !defined(__WIN32__) && !defined(ECERE_NOFONTCONFIG)
+               const char * testLang = null;
+#endif
+
                while(true)
                {
                   HB_Script script = HB_Script_Common;
-                  ch = UTF8GetChar((char *)text + c, &nb);
-                  if(ch > 127) nonASCIIch = ch;
+                  ch = UTF8GetChar((const char *)text + c, &nb);
+                  //if(ch > 127) nonASCIIch = ch;
                   if(!nb) break;
                   if(ch == 32 && curScript)
                   {
@@ -3234,9 +3241,9 @@ public class LFBDisplayDriver : DisplayDriver
                         if(a < c + len)
                         {
                            int nb;
-                           unichar ahead = UTF8GetChar((char *)text + a, &nb);
+                           unichar ahead = UTF8GetChar((const char *)text + a, &nb);
                            if((ahead >= 0x590 && ahead <= 0x7C0) || (ahead >= 0xFB1D && ahead <= 0xFB4F) || (ahead >= 0xFB50 && ahead <= 0xFDFF))
-                              script = curScript;                           
+                              script = curScript;
                         }
                         else
                            script = curScript;
@@ -3285,7 +3292,7 @@ public class LFBDisplayDriver : DisplayDriver
                      if(c >= len)
                         break;
                   }
-                  else 
+                  else
                   {
                      if(!script || script > HB_ScriptCount) { c += nb; if(script > HB_ScriptCount) curScript = script; break; }
                      if(!script) { c += nb; break; }
@@ -3310,7 +3317,7 @@ public class LFBDisplayDriver : DisplayDriver
                      utf16 = renew utf16 uint16[max];
                      utf16BufferSize = max;
                   }
-                  wc = UTF8toUTF16BufferLen((char *)scriptStart, utf16, max, len);
+                  wc = UTF8toUTF16BufferLen((const char *)scriptStart, utf16, max, len);
                   theCurrentScript = glyphScript = curScript;
                }
                switch(curScript)
@@ -3318,7 +3325,10 @@ public class LFBDisplayDriver : DisplayDriver
                   case HB_Script_Arabic:        testChar = 0x621; /*testLang = "ar"; */
                      //printf("Arabic ");
                      break;
-                  case HB_Script_Devanagari:    testChar = 0x905; testLang = "sa"; 
+                  case HB_Script_Devanagari:    testChar = 0x905;
+#if !defined(__WIN32__) && !defined(ECERE_NOFONTCONFIG)
+                     testLang = "sa";
+#endif
                      //printf("Devanagari ");
                      break;
                   case HB_Script_Hebrew:        testChar = 0x05EA /*'ת'*/; /*testLang = "he"; */
@@ -3343,8 +3353,8 @@ public class LFBDisplayDriver : DisplayDriver
                         printf("Not found in %s\n", (char *)font.fontEntries[fontEntryNum].key);*/
                   }
                }
-               
-               if(fontEntryNum == MAX_FONT_LINK_ENTRIES) 
+
+               if(fontEntryNum == MAX_FONT_LINK_ENTRIES)
                {
 #if !defined(__WIN32__) && !defined(ECERE_NOFONTCONFIG)
                   int fontID = 0;
@@ -3356,14 +3366,13 @@ public class LFBDisplayDriver : DisplayDriver
                   char * family;
                   FontEntry fontEntry;
                   char * fileName = null;
-                  bool fakeItalic = false;
                   for(fontEntryNum = 0; fontEntryNum<MAX_FONT_LINK_ENTRIES; fontEntryNum++)
                      if(!font.fontEntries[fontEntryNum])
                         break;
                   if(fontEntryNum == MAX_FONT_LINK_ENTRIES)
 #endif
                      continue;
-            
+
 #if !defined(__WIN32__) && !defined(ECERE_NOFONTCONFIG)
                   {
                      charSet = FcCharSetCreate();
@@ -3387,12 +3396,12 @@ public class LFBDisplayDriver : DisplayDriver
                      matched = FcFontMatch (0, pattern, &result);
                      if(matched)
                      {
-                        FcPatternGetString(matched, FC_FAMILY, 0, &family);
+                        FcPatternGetString(matched, FC_FAMILY, 0, (FcChar8 **)&family);
                         //printf("Fontconfig returned %s\n", family);
                      }
-                     if(matched && (result == FcResultMatch) && FcPatternGetString(matched, FC_FAMILY, 0, &family) == FcResultMatch)
+                     if(matched && (result == FcResultMatch) && FcPatternGetString(matched, FC_FAMILY, 0, (FcChar8 **)&family) == FcResultMatch)
                      {
-                        FcPatternGetString (matched, FC_FILE, 0, &fileName);
+                        FcPatternGetString (matched, FC_FILE, 0, (FcChar8 **)&fileName);
                         FcPatternGetInteger(matched, FC_INDEX, 0, &fontID);
                         FcPatternGetDouble(matched, FC_SIZE, 0, &fontSize);
                         // printf("\nMatched to %s, %f\n", fileName, fontSize);
@@ -3411,16 +3420,16 @@ public class LFBDisplayDriver : DisplayDriver
                         if(file)
                         {
                            FileSize fileSize = file.GetSize();
-                           FT_Open_Args args = { 0 };                            
+                           FT_Open_Args args = { 0 };
                            FT_Parameter param = { FT_PARAM_TAG_UNPATENTED_HINTING };
                            FT_Stream stream = new0 FT_StreamRec[1];
-                           
+
                            if(!ftLibrary)
                               FT_Init_FreeType( &ftLibrary );
 
                            fontEntry = FontEntry { key = (uintptr)CopyString(fileName) };
                            fontEntry.stream = stream;
-                                                
+
                            //args.num_params = 1;
                            args.params = &param;
 
@@ -3441,7 +3450,7 @@ public class LFBDisplayDriver : DisplayDriver
                            // delete file;
                            if(fontEntry.face)
                            {
-                              fontEntry.hbFace = HB_NewFace(fontEntry.face, hb_getSFntTable);            
+                              fontEntry.hbFace = HB_NewFace(fontEntry.face, hb_getSFntTable);
                               fontEntry.hbFont.klass = &hb_fontClass;
                               fontEntry.hbFont.userData = fontEntry; //.face;
 
@@ -3463,7 +3472,7 @@ public class LFBDisplayDriver : DisplayDriver
                         fontEntry.used++;
                      }
                   }
-                  if(pattern) FcPatternDestroy(pattern);          
+                  if(pattern) FcPatternDestroy(pattern);
                   if(matched) FcPatternDestroy(matched);
                   if(charSet) FcCharSetDestroy(charSet);
 #endif
@@ -3488,7 +3497,7 @@ public class LFBDisplayDriver : DisplayDriver
 
             if(packNo != lastPack)
             {
-               if(glyphNo < 128) 
+               if(glyphNo < 128)
                   pack = font.asciiPack;
                else
                {
@@ -3544,11 +3553,11 @@ public class LFBDisplayDriver : DisplayDriver
    }
 
 #endif
-   void FontExtent(DisplaySystem displaySystem, Font font, byte * text, int len, int * width, int * height)
+   void FontExtent(DisplaySystem displaySystem, Font font, const char * text, int len, int * width, int * height)
    {
       if(displaySystem && displaySystem.flags.text && len)
       {
-         if(width) 
+         if(width)
          {
             int num = len;
             *width = num * textCellW;
@@ -3561,7 +3570,7 @@ public class LFBDisplayDriver : DisplayDriver
          {
             int w = 0;
 #if !defined(ECERE_NOTRUETYPE)
-            ProcessString(font, displaySystem, text, len, null, null, null, &w, 0);
+            ProcessString(font, displaySystem, (const byte *)text, len, null, null, null, &w, 0);
 #endif
             //*width = (w + 64 - w % 64) >> 6;
             *width = w >> 6;
@@ -3583,7 +3592,7 @@ public class LFBDisplayDriver : DisplayDriver
    }
 #endif
 
-   void WriteText(Display display, Surface surface, int x, int y, byte * text, int len)
+   void WriteText(Display display, Surface surface, int x, int y, const char * text, int len)
    {
       LFBSurface lfbSurface = surface.driverData;
       if(display && display.displaySystem.flags.text)
@@ -3595,7 +3604,7 @@ public class LFBDisplayDriver : DisplayDriver
          x /= textCellW;
          y /= textCellH;
 
-         if(y > surface.box.bottom || y < surface.box.top) 
+         if(y > surface.box.bottom || y < surface.box.top)
             return;
          coffset += (y+surface.offset.y) * lfbSurface.bitmap.stride + x + surface.offset.x;
          for(c=0; (c<len && x < surface.box.left); c++, x++,coffset++);
@@ -3607,17 +3616,17 @@ public class LFBDisplayDriver : DisplayDriver
                *coffset = (uint16) (((*coffset)&0xF000)|lfbSurface.foreground|text[c]);
          }
       }
-      else 
+      else
       {
          lfbSurface.writingText = true;
 #if !defined(ECERE_NOTRUETYPE)
          x <<= 6;
-         ProcessString(lfbSurface.font, surface.displaySystem, text, len, OutputGlyph, surface, display, &x, y);
+         ProcessString(lfbSurface.font, surface.displaySystem, (const byte *)text, len, OutputGlyph, surface, display, &x, y);
 #endif
          lfbSurface.writingText = false;
       }
    }
-   
+
    void TextFont(Display display, Surface surface, Font font)
    {
       LFBSurface lfbSurface = surface.driverData;
@@ -3626,11 +3635,10 @@ public class LFBDisplayDriver : DisplayDriver
 
    void TextOpacity(Display display, Surface surface, bool opaque)
    {
-      LFBSurface lfbSurface = surface.driverData;
 
    }
 
-   void TextExtent(Display display, Surface surface, byte * text, int len, int * width, int * height)
+   void TextExtent(Display display, Surface surface, const char * text, int len, int * width, int * height)
    {
       LFBSurface lfbSurface = surface.driverData;
       FontExtent(surface.displaySystem, lfbSurface.font, text, len, width, height);
@@ -3659,14 +3667,67 @@ public class LFBDisplayDriver : DisplayDriver
          delete mesh.texCoords;
    }
 
-   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh)
+   bool AllocateMesh(DisplaySystem displaySystem, Mesh mesh, MeshFeatures flags, int nVertices)
    {
       bool result = false;
-
-      if((!mesh.flags.vertices   || mesh.vertices  || (mesh.vertices  = new Vector3Df[mesh.nVertices])) &&
-         (!mesh.flags.normals    || mesh.normals   || (mesh.normals   = new Vector3Df[mesh.nVertices])) &&
-         (!mesh.flags.texCoords1 || mesh.texCoords || (mesh.texCoords = new Pointf  [mesh.nVertices])))
+      if(mesh.nVertices == nVertices)
+      {
+         result = true;
+         // Same number of vertices, adding features (Leaves the other features pointers alone)
+         if(mesh.flags != flags)
+         {
+            if(!mesh.flags.vertices && flags.vertices)
+            {
+               if(flags.doubleVertices)
+               {
+                  mesh.vertices = (Vector3Df *)new Vector3D[nVertices];
+               }
+               else
+                  mesh.vertices = new Vector3Df[nVertices];
+            }
+            if(!mesh.flags.normals && flags.normals)
+            {
+               if(flags.doubleNormals)
+               {
+                  mesh.normals = (Vector3Df *)new Vector3D[nVertices];
+               }
+               else
+                  mesh.normals = new Vector3Df[nVertices];
+            }
+            if(!mesh.flags.texCoords1 && flags.texCoords1)
+               mesh.texCoords = new Pointf[nVertices];
+            if(!mesh.flags.colors && flags.colors)
+               mesh.colors = new ColorRGBAf[nVertices];
+         }
+      }
+      else
+      {
          result = true;
+         // New number of vertices, reallocate all current and new features
+         flags |= mesh.flags;
+         if(flags.vertices)
+         {
+            if(flags.doubleVertices)
+            {
+               mesh.vertices = (Vector3Df *)renew mesh.vertices Vector3D[nVertices];
+            }
+            else
+               mesh.vertices = renew mesh.vertices Vector3Df[nVertices];
+         }
+         if(flags.normals)
+         {
+            if(flags.doubleNormals)
+            {
+               mesh.normals = (Vector3Df *)renew mesh.normals Vector3D[nVertices];
+            }
+            else
+               mesh.normals = renew mesh.normals Vector3Df[nVertices];
+         }
+         if(flags.texCoords1)
+            mesh.texCoords = renew mesh.texCoords Pointf[nVertices];
+         if(flags.colors)
+            mesh.colors = renew mesh.colors ColorRGBAf[nVertices];
+      }
       return result;
    }