import-font.cpp 6.9 KB

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