fontgen.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <ft2build.h>
  4. #include FT_FREETYPE_H
  5. #include FT_GLYPH_H
  6. #include "Image.h"
  7. #include "TGAImageLoader.h"
  8. #include "FileStream.h"
  9. #include "List.h"
  10. using namespace Crown;
  11. typedef List<uint> CodePointRangeList;
  12. CodePointRangeList mCodePointRangeList;
  13. void AddCodePointRange(uint min, uint max);
  14. Image* BuildFontImage(const char* ttfFont, ushort ttfSize, ushort ttfResolution);
  15. int main(int argc, char** argv)
  16. {
  17. Image* fontImage;
  18. if (argc == 4)
  19. {
  20. fontImage = BuildFontImage(argv[1], atoi(argv[2]), atoi(argv[3]));
  21. }
  22. if (fontImage == NULL)
  23. {
  24. return -1;
  25. }
  26. fontImage->ConvertToRGBA8();
  27. TGAImageLoader tgaLoader;
  28. tgaLoader.SaveFile(fontImage, Str(argv[1]) + Str(".tga"));
  29. delete fontImage;
  30. FileStream fileStream(SOM_READ, Str(argv[1]) + Str(".txt"));
  31. TextReader textReader(&fileStream);
  32. return 0;
  33. }
  34. Image* BuildFontImage(const char* ttfFont, ushort ttfSize, ushort ttfResolution)
  35. {
  36. uint mMaxTextHeight;
  37. uint mMaxCharacterHeight;
  38. uint mMaxCharacterWidth;
  39. FileStream* font = new FileStream(SOM_READ, ttfFont);
  40. if (font == NULL)
  41. {
  42. printf("Error: Unable to open file: %s\n", ttfFont);
  43. return NULL;
  44. }
  45. // Start reading from the beginning
  46. font->Seek(0, SM_SeekFromBegin);
  47. uchar* fontBuffer = new uchar[font->GetSize()];
  48. font->ReadDataBlock(fontBuffer, font->GetSize());
  49. FT_Library library;
  50. if (FT_Init_FreeType(&library))
  51. {
  52. printf("Error: Could not initialize FreeType library.");
  53. return NULL;
  54. }
  55. FT_Face face;
  56. uint error;
  57. error = FT_New_Memory_Face(library, fontBuffer, font->GetSize(), 0, &face);
  58. // Done with font stream
  59. delete font;
  60. if (error == FT_Err_Unknown_File_Format)
  61. {
  62. printf("Error: Unknown file format.");
  63. return NULL;
  64. }
  65. else if (error)
  66. {
  67. printf("Error: Could not load font file.");
  68. return NULL;
  69. }
  70. if ((face->face_flags & FT_FACE_FLAG_SCALABLE) == 0)
  71. {
  72. printf("Error: The font file is not scalable.");
  73. return NULL;
  74. }
  75. FT_Set_Char_Size(face, ttfSize << 6, ttfSize << 6, ttfResolution, ttfResolution);
  76. if (mCodePointRangeList.IsEmpty())
  77. {
  78. AddCodePointRange(32, 126); // ASCII printable characters
  79. }
  80. int maxGlyphWidth = 0;
  81. int maxGlyphHeight = 0;
  82. int maxHoriBearingY = 0;
  83. uint glyphCount = 0;
  84. // Find the biggest character
  85. // For each range
  86. CE_ASSERT(mCodePointRangeList.GetSize() % 2 == 0);
  87. for (int i = 0; i < mCodePointRangeList.GetSize(); i += 2)
  88. {
  89. uint j = mCodePointRangeList[i + 0];
  90. uint k = mCodePointRangeList[i + 1];
  91. // For each character in range
  92. for (; j <= k; j++)
  93. {
  94. FT_Load_Glyph(face, FT_Get_Char_Index(face, j), FT_LOAD_RENDER);
  95. FT_Bitmap& bitmap = face->glyph->bitmap;
  96. FT_Glyph_Metrics& metrics = face->glyph->metrics;
  97. if (maxGlyphWidth < (bitmap.width + (metrics.horiBearingX >> 6)))
  98. {
  99. maxGlyphWidth = (bitmap.width + (metrics.horiBearingX >> 6));
  100. }
  101. if (maxGlyphHeight < (2 * bitmap.rows - (metrics.horiBearingY >> 6)))
  102. {
  103. maxGlyphHeight = (2 * bitmap.rows - (metrics.horiBearingY >> 6));
  104. }
  105. if (maxHoriBearingY < (metrics.horiBearingY >> 6))
  106. {
  107. maxHoriBearingY = (metrics.horiBearingY >> 6);
  108. }
  109. glyphCount++;
  110. }
  111. }
  112. mMaxCharacterHeight = maxGlyphHeight;
  113. mMaxCharacterWidth = maxGlyphWidth;
  114. // Additional spacing between characters to avoid artifacts
  115. uint glyphSpacing = 3;
  116. uint glyphPerRow = (uint)(Math::Ceil(Math::Sqrt((float)(glyphCount * maxGlyphHeight / maxGlyphWidth))));
  117. uint bufferWidth = glyphPerRow * (maxGlyphWidth + glyphSpacing);
  118. uint bufferHeight = (uint)Math::Ceil((float) glyphCount / glyphPerRow) * (maxGlyphHeight + glyphSpacing);
  119. bufferWidth = Math::NextPow2(bufferWidth);
  120. bufferHeight = Math::NextPow2(bufferHeight);
  121. uchar* buffer = new uchar[bufferWidth * bufferHeight * 2];
  122. uint advanceX = 0;
  123. uint advanceY = 0;
  124. uint totalAdvance = 0;
  125. uint glyphRowCount = 0;
  126. int glyphBaseline = maxGlyphHeight - maxHoriBearingY;
  127. for (uint i = 0; i < bufferWidth * bufferHeight * 2; i++)
  128. {
  129. buffer[i] = 0;
  130. }
  131. mMaxTextHeight = 0;
  132. FileStream fileStream(SOM_WRITE, Str(ttfFont) + Str(".txt"));
  133. TextWriter fileWriter(&fileStream);
  134. CE_ASSERT(mCodePointRangeList.GetSize() % 2 == 0);
  135. // For each range
  136. for (int i = 0; i < mCodePointRangeList.GetSize(); i += 2)
  137. {
  138. uint j = mCodePointRangeList[i + 0];
  139. uint k = mCodePointRangeList[i + 1];
  140. // For each character in range
  141. for (; j <= k; j++)
  142. {
  143. FT_Load_Glyph(face, FT_Get_Char_Index(face, j), FT_LOAD_RENDER);
  144. FT_Bitmap& bitmap = face->glyph->bitmap;
  145. FT_Glyph_Metrics& metrics = face->glyph->metrics;
  146. // If we have reached the maximum glyphs per row
  147. if (glyphRowCount == glyphPerRow)
  148. {
  149. glyphRowCount = 0;
  150. advanceX = 0;
  151. advanceY += maxGlyphHeight + glyphSpacing;
  152. }
  153. int horiz = (metrics.horiBearingY >> 6) - bitmap.rows;
  154. totalAdvance = advanceX + (metrics.horiBearingX >> 6) + (advanceY + glyphBaseline + horiz) * bufferWidth;
  155. // Glyph drawing
  156. for (int h = 0; h < bitmap.rows; h++)
  157. {
  158. for (int w = 0; w < bitmap.width; w++)
  159. {
  160. uchar value = bitmap.buffer[w + bitmap.pitch * (bitmap.rows - h -1)];
  161. buffer[2 * (w + h * bufferWidth + totalAdvance)] = 255;
  162. buffer[2 * (w + h * bufferWidth + totalAdvance) + 1] = value;
  163. }
  164. }
  165. float baseline = (float) (metrics.horiBearingY >> 6);
  166. // fileWriter.WriteFormatted("%d %f %f %f %f %f %f %f %f\n",
  167. // j,
  168. // (1.0f / (float) bufferWidth) * (float) advanceX,
  169. // (1.0f / (float) bufferWidth) * ((float) advanceX + maxGlyphWidth),
  170. // (1.0f / (float) bufferHeight) * (float) advanceY,
  171. // (1.0f / (float) bufferHeight) * ((float) advanceY + maxGlyphHeight),
  172. // (float) bitmap.width,
  173. // (float) bitmap.rows,
  174. // (float) (face->glyph->advance.x >> 6),
  175. // baseline);
  176. uint totalHeight = bitmap.rows + (int)Math::Abs(bitmap.rows - baseline);
  177. mMaxTextHeight = Math::Max<uint>(mMaxTextHeight, totalHeight);
  178. advanceX += maxGlyphWidth + glyphSpacing;
  179. glyphRowCount++;
  180. }
  181. }
  182. FT_Done_Face(face);
  183. FT_Done_FreeType(library);
  184. if (fontBuffer)
  185. {
  186. delete[] fontBuffer;
  187. }
  188. // Done with FreeType stuff
  189. Image* fontImage = new Image();
  190. fontImage->DestroyImage();
  191. fontImage->CreateImage(PF_LA_8, bufferWidth, bufferHeight, buffer);
  192. return fontImage;
  193. }
  194. void AddCodePointRange(uint min, uint max)
  195. {
  196. CE_ASSERT(min <= max);
  197. mCodePointRangeList.Append(min);
  198. mCodePointRangeList.Append(max);
  199. }