TTFFontEncoder.cpp 9.9 KB

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