TTFFontEncoder.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "Base.h"
  2. #include "TTFFontEncoder.h"
  3. #include "GPBFile.h"
  4. #include "StringUtil.h"
  5. namespace gameplay
  6. {
  7. static void drawBitmap(unsigned char* dstBitmap, int x, int y, int dstWidth, unsigned char* srcBitmap, int srcWidth, int srcHeight)
  8. {
  9. // offset dst bitmap by x,y.
  10. dstBitmap += (x + (y * dstWidth));
  11. for (int i = 0; i < srcHeight; ++i)
  12. {
  13. memcpy(dstBitmap, (const void*)srcBitmap, srcWidth);
  14. srcBitmap += srcWidth;
  15. dstBitmap += dstWidth;
  16. }
  17. }
  18. static void writeUint(FILE* fp, unsigned int i)
  19. {
  20. fwrite(&i, sizeof(unsigned int), 1, fp);
  21. }
  22. static void writeString(FILE* fp, const char* str)
  23. {
  24. unsigned int len = strlen(str);
  25. fwrite(&len, sizeof(unsigned int), 1, fp);
  26. if (len > 0)
  27. {
  28. fwrite(str, 1, len, fp);
  29. }
  30. }
  31. int writeFont(const char* inFilePath, const char* outFilePath, unsigned int fontSize, const char* id, bool fontpreview = false)
  32. {
  33. Glyph glyphArray[END_INDEX - START_INDEX];
  34. // Initialize freetype library.
  35. FT_Library library;
  36. FT_Error error = FT_Init_FreeType(&library);
  37. if (error)
  38. {
  39. LOG(1, "FT_Init_FreeType error: %d \n", error);
  40. return -1;
  41. }
  42. // Initialize font face.
  43. FT_Face face;
  44. error = FT_New_Face(library, inFilePath, 0, &face);
  45. if (error)
  46. {
  47. LOG(1, "FT_New_Face error: %d \n", error);
  48. return -1;
  49. }
  50. // Set the pixel size.
  51. error = FT_Set_Char_Size(
  52. face, // handle to face object.
  53. 0, // char_width in 1/64th of points.
  54. fontSize * 64, // char_height in 1/64th of points.
  55. 0, // horizontal device resolution (defaults to 72 dpi if resolution (0, 0)).
  56. 0 ); // vertical device resolution.
  57. if (error)
  58. {
  59. LOG(1, "FT_Set_Char_Size error: %d \n", error);
  60. return -1;
  61. }
  62. /*
  63. error = FT_Set_Pixel_Sizes(face, FONT_SIZE, 0);
  64. if (error)
  65. {
  66. LOG(1, "FT_Set_Pixel_Sizes error : %d \n", error);
  67. exit(1);
  68. }
  69. */
  70. // Save glyph information (slot contains the actual glyph bitmap).
  71. FT_GlyphSlot slot = face->glyph;
  72. int actualfontHeight = 0;
  73. int rowSize = 0; // Stores the total number of rows required to all glyphs.
  74. // Find the width of the image.
  75. for (unsigned char ascii = START_INDEX; ascii < END_INDEX; ++ascii)
  76. {
  77. // Load glyph image into the slot (erase previous one)
  78. error = FT_Load_Char(face, ascii, FT_LOAD_RENDER);
  79. if (error)
  80. {
  81. LOG(1, "FT_Load_Char error : %d \n", error);
  82. }
  83. int bitmapRows = slot->bitmap.rows;
  84. actualfontHeight = (actualfontHeight < bitmapRows) ? bitmapRows : actualfontHeight;
  85. if (slot->bitmap.rows > slot->bitmap_top)
  86. {
  87. bitmapRows += (slot->bitmap.rows - slot->bitmap_top);
  88. }
  89. rowSize = (rowSize < bitmapRows) ? bitmapRows : rowSize;
  90. }
  91. // Include padding in the rowSize.
  92. rowSize += GLYPH_PADDING;
  93. // Initialize with padding.
  94. int penX = 0;
  95. int penY = 0;
  96. int row = 0;
  97. double powerOf2 = 2;
  98. unsigned int imageWidth = 0;
  99. unsigned int imageHeight = 0;
  100. bool textureSizeFound = false;
  101. int advance;
  102. int i;
  103. while (textureSizeFound == false)
  104. {
  105. imageWidth = (unsigned int)pow(2.0, powerOf2);
  106. imageHeight = (unsigned int)pow(2.0, powerOf2);
  107. penX = 0;
  108. penY = 0;
  109. row = 0;
  110. // Find out the squared texture size that would fit all the require font glyphs.
  111. i = 0;
  112. for (unsigned char ascii = START_INDEX; ascii < END_INDEX; ++ascii)
  113. {
  114. // Load glyph image into the slot (erase the previous one).
  115. error = FT_Load_Char(face, ascii, FT_LOAD_RENDER);
  116. if (error)
  117. {
  118. LOG(1, "FT_Load_Char error : %d \n", error);
  119. }
  120. // Glyph image.
  121. int glyphWidth = slot->bitmap.pitch;
  122. int glyphHeight = slot->bitmap.rows;
  123. advance = glyphWidth + GLYPH_PADDING; //((int)slot->advance.x >> 6) + GLYPH_PADDING;
  124. // If we reach the end of the image wrap aroud to the next row.
  125. if ((penX + advance) > (int)imageWidth)
  126. {
  127. penX = 0;
  128. row += 1;
  129. penY = row * rowSize;
  130. if (penY + rowSize > (int)imageHeight)
  131. {
  132. powerOf2++;
  133. break;
  134. }
  135. }
  136. // penY should include the glyph offsets.
  137. penY += (actualfontHeight - glyphHeight) + (glyphHeight - slot->bitmap_top);
  138. // Set the pen position for the next glyph
  139. penX += advance; // Move X to next glyph position
  140. // Move Y back to the top of the row.
  141. penY = row * rowSize;
  142. if (ascii == (END_INDEX-1))
  143. {
  144. textureSizeFound = true;
  145. }
  146. i++;
  147. }
  148. }
  149. // Try further to find a tighter texture size.
  150. powerOf2 = 1;
  151. for (;;)
  152. {
  153. if ((penY + rowSize) >= pow(2.0, powerOf2))
  154. {
  155. powerOf2++;
  156. }
  157. else
  158. {
  159. imageHeight = (int)pow(2.0, powerOf2);
  160. break;
  161. }
  162. }
  163. // Allocate temporary image buffer to draw the glyphs into.
  164. unsigned char* imageBuffer = (unsigned char *)malloc(imageWidth * imageHeight);
  165. memset(imageBuffer, 0, imageWidth * imageHeight);
  166. penX = 0;
  167. penY = 0;
  168. row = 0;
  169. i = 0;
  170. for (unsigned char ascii = START_INDEX; ascii < END_INDEX; ++ascii)
  171. {
  172. // Load glyph image into the slot (erase the previous one).
  173. error = FT_Load_Char(face, ascii, FT_LOAD_RENDER);
  174. if (error)
  175. {
  176. LOG(1, "FT_Load_Char error : %d \n", error);
  177. }
  178. // Glyph image.
  179. unsigned char* glyphBuffer = slot->bitmap.buffer;
  180. int glyphWidth = slot->bitmap.pitch;
  181. int glyphHeight = slot->bitmap.rows;
  182. advance = glyphWidth + GLYPH_PADDING;//((int)slot->advance.x >> 6) + GLYPH_PADDING;
  183. // If we reach the end of the image wrap aroud to the next row.
  184. if ((penX + advance) > (int)imageWidth)
  185. {
  186. penX = 0;
  187. row += 1;
  188. penY = row * rowSize;
  189. if (penY + rowSize > (int)imageHeight)
  190. {
  191. LOG(1, "Image size exceeded!");
  192. return -1;
  193. }
  194. }
  195. // penY should include the glyph offsets.
  196. penY += (actualfontHeight - glyphHeight) + (glyphHeight - slot->bitmap_top);
  197. // Draw the glyph to the bitmap with a one pixel padding.
  198. drawBitmap(imageBuffer, penX, penY, imageWidth, glyphBuffer, glyphWidth, glyphHeight);
  199. // Move Y back to the top of the row.
  200. penY = row * rowSize;
  201. glyphArray[i].index = ascii;
  202. glyphArray[i].width = advance - GLYPH_PADDING;
  203. // Generate UV coords.
  204. glyphArray[i].uvCoords[0] = (float)penX / (float)imageWidth;
  205. glyphArray[i].uvCoords[1] = (float)penY / (float)imageHeight;
  206. glyphArray[i].uvCoords[2] = (float)(penX + advance - GLYPH_PADDING) / (float)imageWidth;
  207. glyphArray[i].uvCoords[3] = (float)(penY + rowSize) / (float)imageHeight;
  208. // Set the pen position for the next glyph
  209. penX += advance; // Move X to next glyph position
  210. i++;
  211. }
  212. FILE *gpbFp = fopen(outFilePath, "wb");
  213. // File header and version.
  214. char fileHeader[9] = {'«', 'G', 'P', 'B', '»', '\r', '\n', '\x1A', '\n'};
  215. fwrite(fileHeader, sizeof(char), 9, gpbFp);
  216. fwrite(gameplay::GPB_VERSION, sizeof(char), 2, gpbFp);
  217. // Write Ref table (for a single font)
  218. writeUint(gpbFp, 1); // Ref[] count
  219. writeString(gpbFp, id); // Ref id
  220. writeUint(gpbFp, 128); // Ref type
  221. writeUint(gpbFp, ftell(gpbFp) + 4); // Ref offset (current pos + 4 bytes)
  222. // Write Font object.
  223. // Family name.
  224. writeString(gpbFp, face->family_name);
  225. // Style.
  226. // TODO: Switch based on TTF style name and write appropriate font style unsigned int
  227. // For now just hardcoding to 0.
  228. //char* style = face->style_name;
  229. writeUint(gpbFp, 0); // 0 == PLAIN
  230. // Font size.
  231. writeUint(gpbFp, rowSize);
  232. // Character set.
  233. // TODO: Empty for now
  234. writeString(gpbFp, "");
  235. // Glyphs.
  236. unsigned int glyphSetSize = END_INDEX - START_INDEX;
  237. writeUint(gpbFp, glyphSetSize);
  238. fwrite(&glyphArray, sizeof(Glyph), glyphSetSize, gpbFp);
  239. // Texture.
  240. unsigned int textureSize = imageWidth * imageHeight;
  241. writeUint(gpbFp, imageWidth);
  242. writeUint(gpbFp, imageHeight);
  243. writeUint(gpbFp, textureSize);
  244. fwrite(imageBuffer, sizeof(unsigned char), textureSize, gpbFp);
  245. // Close file.
  246. fclose(gpbFp);
  247. LOG(1, "%s.gpb created successfully. \n", getBaseName(outFilePath).c_str());
  248. if (fontpreview)
  249. {
  250. // Write out font map to an image.
  251. std::string pgmFilePath = getFilenameNoExt(outFilePath);
  252. pgmFilePath.append(".pgm");
  253. FILE *imageFp = fopen(pgmFilePath.c_str(), "wb");
  254. fprintf(imageFp, "P5 %d %d 255\n", imageWidth, imageHeight);
  255. fwrite((const char *)imageBuffer, sizeof(unsigned char), imageWidth * imageHeight, imageFp);
  256. fclose(imageFp);
  257. }
  258. // Cleanup resources.
  259. free(imageBuffer);
  260. FT_Done_Face(face);
  261. FT_Done_FreeType(library);
  262. return 0;
  263. }
  264. }