TTFFontEncoder.cpp 9.9 KB

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