فهرست منبع

Read and write array of TTFGlyph platform independently.

Michal Srb 11 سال پیش
والد
کامیت
fbc764ffcf
2فایلهای تغییر یافته به همراه48 افزوده شده و 5 حذف شده
  1. 40 4
      gameplay/src/Bundle.cpp
  2. 8 1
      tools/encoder/src/TTFFontEncoder.cpp

+ 40 - 4
gameplay/src/Bundle.cpp

@@ -1698,11 +1698,47 @@ Font* Bundle::loadFont(const char* id)
         }
 
         Font::Glyph* glyphs = new Font::Glyph[glyphCount];
-        if (_stream->read(glyphs, sizeof(Font::Glyph), glyphCount) != glyphCount)
+        for (unsigned j = 0; j < glyphCount; j++)
         {
-            GP_ERROR("Failed to read glyphs for font '%s'.", id);
-            SAFE_DELETE_ARRAY(glyphs);
-            return NULL;
+            if (_stream->read(&glyphs[j].code, 4, 1) != 1)
+            {
+                GP_ERROR("Failed to read glyph #%d code for font '%s'.", j, id);
+                SAFE_DELETE_ARRAY(glyphs);
+                return NULL;
+            }
+            if (_stream->read(&glyphs[j].width, 4, 1) != 1)
+            {
+                GP_ERROR("Failed to read glyph #%d width for font '%s'.", j, id);
+                SAFE_DELETE_ARRAY(glyphs);
+                return NULL;
+            }
+            if (getVersionMajor() >= 1 && getVersionMinor() >= 5)
+            {
+                if (_stream->read(&glyphs[j].bearingX, 4, 1) != 1)
+                {
+                    GP_ERROR("Failed to read glyph #%d bearingX for font '%s'.", j, id);
+                    SAFE_DELETE_ARRAY(glyphs);
+                    return NULL;
+                }
+                if (_stream->read(&glyphs[j].advance, 4, 1) != 1)
+                {
+                    GP_ERROR("Failed to read glyph #%d advance for font '%s'.", j, id);
+                    SAFE_DELETE_ARRAY(glyphs);
+                    return NULL;
+                }
+            }
+            else
+            {
+                // Fallback values for older GBP format.
+                glyphs[j].bearingX = 0;
+                glyphs[j].advance = glyphs[j].width;
+            }
+            if (_stream->read(&glyphs[j].uvs, 4, 4) != 4)
+            {
+                GP_ERROR("Failed to read glyph #%d uvs for font '%s'.", j, id);
+                SAFE_DELETE_ARRAY(glyphs);
+                return NULL;
+            }
         }
 
         // Read texture attributes.

+ 8 - 1
tools/encoder/src/TTFFontEncoder.cpp

@@ -417,7 +417,14 @@ int writeFont(const char* inFilePath, const char* outFilePath, std::vector<unsig
         // Glyphs.
         unsigned int glyphSetSize = END_INDEX - START_INDEX;
         writeUint(gpbFp, glyphSetSize);
-        fwrite(&font->glyphArray, sizeof(TTFGlyph), glyphSetSize, gpbFp);
+        for (unsigned int j = 0; j < glyphSetSize; j++)
+        {
+            writeUint(gpbFp, font->glyphArray[j].index);
+            writeUint(gpbFp, font->glyphArray[j].width);
+            fwrite(&font->glyphArray[j].bearingX, sizeof(int), 1, gpbFp);
+            writeUint(gpbFp, font->glyphArray[j].advance);
+            fwrite(&font->glyphArray[j].uvCoords, sizeof(float), 4, gpbFp);
+        }
 
         // Image dimensions
         unsigned int imageSize = font->imageWidth * font->imageHeight;