import-font.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "import-font.h"
  2. #include <cstdlib>
  3. #include <queue>
  4. #include <ft2build.h>
  5. #include FT_FREETYPE_H
  6. #include FT_OUTLINE_H
  7. #if defined(_WIN32) && !defined(MSDFGEN_CMAKE_BUILD)
  8. #pragma comment(lib, "freetype.lib")
  9. #endif
  10. namespace msdfgen {
  11. #define REQUIRE(cond) { if (!(cond)) return false; }
  12. #define F26DOT6_TO_DOUBLE(x) (1/64.*double(x))
  13. class FreetypeHandle {
  14. friend FreetypeHandle * initializeFreetype();
  15. friend void deinitializeFreetype(FreetypeHandle *library);
  16. friend FontHandle * loadFont(FreetypeHandle *library, const char *filename);
  17. friend FontHandle * loadFontData(FreetypeHandle *library, const byte *data, int length);
  18. FT_Library library;
  19. };
  20. class FontHandle {
  21. friend FontHandle * adoptFreetypeFont(FT_Face ftFace);
  22. friend FontHandle * loadFont(FreetypeHandle *library, const char *filename);
  23. friend FontHandle * loadFontData(FreetypeHandle *library, const byte *data, int length);
  24. friend void destroyFont(FontHandle *font);
  25. friend bool getFontMetrics(FontMetrics &metrics, FontHandle *font);
  26. friend bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font);
  27. friend bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode);
  28. friend bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *advance);
  29. friend bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *advance);
  30. friend bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex1, GlyphIndex glyphIndex2);
  31. friend bool getKerning(double &output, FontHandle *font, unicode_t unicode1, unicode_t unicode2);
  32. FT_Face face;
  33. bool ownership;
  34. };
  35. struct FtContext {
  36. Point2 position;
  37. Shape *shape;
  38. Contour *contour;
  39. };
  40. static Point2 ftPoint2(const FT_Vector &vector) {
  41. return Point2(F26DOT6_TO_DOUBLE(vector.x), F26DOT6_TO_DOUBLE(vector.y));
  42. }
  43. static int ftMoveTo(const FT_Vector *to, void *user) {
  44. FtContext *context = reinterpret_cast<FtContext *>(user);
  45. if (!(context->contour && context->contour->edges.empty()))
  46. context->contour = &context->shape->addContour();
  47. context->position = ftPoint2(*to);
  48. return 0;
  49. }
  50. static int ftLineTo(const FT_Vector *to, void *user) {
  51. FtContext *context = reinterpret_cast<FtContext *>(user);
  52. Point2 endpoint = ftPoint2(*to);
  53. if (endpoint != context->position) {
  54. context->contour->addEdge(new LinearSegment(context->position, endpoint));
  55. context->position = endpoint;
  56. }
  57. return 0;
  58. }
  59. static int ftConicTo(const FT_Vector *control, const FT_Vector *to, void *user) {
  60. FtContext *context = reinterpret_cast<FtContext *>(user);
  61. context->contour->addEdge(new QuadraticSegment(context->position, ftPoint2(*control), ftPoint2(*to)));
  62. context->position = ftPoint2(*to);
  63. return 0;
  64. }
  65. static int ftCubicTo(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  66. FtContext *context = reinterpret_cast<FtContext *>(user);
  67. context->contour->addEdge(new CubicSegment(context->position, ftPoint2(*control1), ftPoint2(*control2), ftPoint2(*to)));
  68. context->position = ftPoint2(*to);
  69. return 0;
  70. }
  71. GlyphIndex::GlyphIndex(unsigned index) : index(index) { }
  72. unsigned GlyphIndex::getIndex() const {
  73. return index;
  74. }
  75. bool GlyphIndex::operator!() const {
  76. return index == 0;
  77. }
  78. FreetypeHandle * initializeFreetype() {
  79. FreetypeHandle *handle = new FreetypeHandle;
  80. FT_Error error = FT_Init_FreeType(&handle->library);
  81. if (error) {
  82. delete handle;
  83. return NULL;
  84. }
  85. return handle;
  86. }
  87. void deinitializeFreetype(FreetypeHandle *library) {
  88. FT_Done_FreeType(library->library);
  89. delete library;
  90. }
  91. FontHandle * adoptFreetypeFont(FT_Face ftFace) {
  92. FontHandle *handle = new FontHandle;
  93. handle->face = ftFace;
  94. handle->ownership = false;
  95. return handle;
  96. }
  97. FontHandle * loadFont(FreetypeHandle *library, const char *filename) {
  98. if (!library)
  99. return NULL;
  100. FontHandle *handle = new FontHandle;
  101. FT_Error error = FT_New_Face(library->library, filename, 0, &handle->face);
  102. if (error) {
  103. delete handle;
  104. return NULL;
  105. }
  106. handle->ownership = true;
  107. return handle;
  108. }
  109. FontHandle * loadFontData(FreetypeHandle *library, const byte *data, int length) {
  110. if (!library)
  111. return NULL;
  112. FontHandle *handle = new FontHandle;
  113. FT_Error error = FT_New_Memory_Face(library->library, data, length, 0, &handle->face);
  114. if (error) {
  115. delete handle;
  116. return NULL;
  117. }
  118. handle->ownership = true;
  119. return handle;
  120. }
  121. void destroyFont(FontHandle *font) {
  122. if (font->ownership)
  123. FT_Done_Face(font->face);
  124. delete font;
  125. }
  126. bool getFontMetrics(FontMetrics &metrics, FontHandle *font) {
  127. metrics.emSize = F26DOT6_TO_DOUBLE(font->face->units_per_EM);
  128. metrics.ascenderY = F26DOT6_TO_DOUBLE(font->face->ascender);
  129. metrics.descenderY = F26DOT6_TO_DOUBLE(font->face->descender);
  130. metrics.lineHeight = F26DOT6_TO_DOUBLE(font->face->height);
  131. metrics.underlineY = F26DOT6_TO_DOUBLE(font->face->underline_position);
  132. metrics.underlineThickness = F26DOT6_TO_DOUBLE(font->face->underline_thickness);
  133. return true;
  134. }
  135. bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font) {
  136. FT_Error error = FT_Load_Char(font->face, ' ', FT_LOAD_NO_SCALE);
  137. if (error)
  138. return false;
  139. spaceAdvance = F26DOT6_TO_DOUBLE(font->face->glyph->advance.x);
  140. error = FT_Load_Char(font->face, '\t', FT_LOAD_NO_SCALE);
  141. if (error)
  142. return false;
  143. tabAdvance = F26DOT6_TO_DOUBLE(font->face->glyph->advance.x);
  144. return true;
  145. }
  146. bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode) {
  147. glyphIndex = GlyphIndex(FT_Get_Char_Index(font->face, unicode));
  148. return glyphIndex.getIndex() != 0;
  149. }
  150. bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *advance) {
  151. if (!font)
  152. return false;
  153. FT_Error error = FT_Load_Glyph(font->face, glyphIndex.getIndex(), FT_LOAD_NO_SCALE);
  154. if (error)
  155. return false;
  156. output.contours.clear();
  157. output.inverseYAxis = false;
  158. if (advance)
  159. *advance = F26DOT6_TO_DOUBLE(font->face->glyph->advance.x);
  160. FtContext context = { };
  161. context.shape = &output;
  162. FT_Outline_Funcs ftFunctions;
  163. ftFunctions.move_to = &ftMoveTo;
  164. ftFunctions.line_to = &ftLineTo;
  165. ftFunctions.conic_to = &ftConicTo;
  166. ftFunctions.cubic_to = &ftCubicTo;
  167. ftFunctions.shift = 0;
  168. ftFunctions.delta = 0;
  169. error = FT_Outline_Decompose(&font->face->glyph->outline, &ftFunctions, &context);
  170. if (error)
  171. return false;
  172. if (!output.contours.empty() && output.contours.back().edges.empty())
  173. output.contours.pop_back();
  174. return true;
  175. }
  176. bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *advance) {
  177. return loadGlyph(output, font, GlyphIndex(FT_Get_Char_Index(font->face, unicode)), advance);
  178. }
  179. bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex1, GlyphIndex glyphIndex2) {
  180. FT_Vector kerning;
  181. if (FT_Get_Kerning(font->face, glyphIndex1.getIndex(), glyphIndex2.getIndex(), FT_KERNING_UNSCALED, &kerning)) {
  182. output = 0;
  183. return false;
  184. }
  185. output = F26DOT6_TO_DOUBLE(kerning.x);
  186. return true;
  187. }
  188. bool getKerning(double &output, FontHandle *font, unicode_t unicode1, unicode_t unicode2) {
  189. return getKerning(output, font, GlyphIndex(FT_Get_Char_Index(font->face, unicode1)), GlyphIndex(FT_Get_Char_Index(font->face, unicode2)));
  190. }
  191. }