import-font.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "import-font.h"
  2. #include <cstring>
  3. #include <vector>
  4. #include <ft2build.h>
  5. #include FT_FREETYPE_H
  6. #include FT_OUTLINE_H
  7. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  8. #include FT_MULTIPLE_MASTERS_H
  9. #endif
  10. namespace msdfgen {
  11. #define F26DOT6_TO_DOUBLE(x) (1/64.*double(x))
  12. #define F16DOT16_TO_DOUBLE(x) (1/65536.*double(x))
  13. #define DOUBLE_TO_F16DOT16(x) FT_Fixed(65536.*x)
  14. class FreetypeHandle {
  15. friend FreetypeHandle *initializeFreetype();
  16. friend void deinitializeFreetype(FreetypeHandle *library);
  17. friend FontHandle *loadFont(FreetypeHandle *library, const char *filename);
  18. friend FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length);
  19. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  20. friend bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate);
  21. friend bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font);
  22. #endif
  23. FT_Library library;
  24. };
  25. class FontHandle {
  26. friend FontHandle *adoptFreetypeFont(FT_Face ftFace);
  27. friend FontHandle *loadFont(FreetypeHandle *library, const char *filename);
  28. friend FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length);
  29. friend void destroyFont(FontHandle *font);
  30. friend bool getFontMetrics(FontMetrics &metrics, FontHandle *font);
  31. friend bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font);
  32. friend bool getGlyphCount(unsigned &output, FontHandle *font);
  33. friend bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode);
  34. friend bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *advance);
  35. friend bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *advance);
  36. friend bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex0, GlyphIndex glyphIndex1);
  37. friend bool getKerning(double &output, FontHandle *font, unicode_t unicode0, unicode_t unicode1);
  38. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  39. friend bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate);
  40. friend bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font);
  41. #endif
  42. FT_Face face;
  43. bool ownership;
  44. };
  45. struct FtContext {
  46. Point2 position;
  47. Shape *shape;
  48. Contour *contour;
  49. };
  50. static Point2 ftPoint2(const FT_Vector &vector) {
  51. return Point2(F26DOT6_TO_DOUBLE(vector.x), F26DOT6_TO_DOUBLE(vector.y));
  52. }
  53. static int ftMoveTo(const FT_Vector *to, void *user) {
  54. FtContext *context = reinterpret_cast<FtContext *>(user);
  55. if (!(context->contour && context->contour->edges.empty()))
  56. context->contour = &context->shape->addContour();
  57. context->position = ftPoint2(*to);
  58. return 0;
  59. }
  60. static int ftLineTo(const FT_Vector *to, void *user) {
  61. FtContext *context = reinterpret_cast<FtContext *>(user);
  62. Point2 endpoint = ftPoint2(*to);
  63. if (endpoint != context->position) {
  64. context->contour->addEdge(EdgeHolder(context->position, endpoint));
  65. context->position = endpoint;
  66. }
  67. return 0;
  68. }
  69. static int ftConicTo(const FT_Vector *control, const FT_Vector *to, void *user) {
  70. FtContext *context = reinterpret_cast<FtContext *>(user);
  71. Point2 endpoint = ftPoint2(*to);
  72. if (endpoint != context->position) {
  73. context->contour->addEdge(EdgeHolder(context->position, ftPoint2(*control), endpoint));
  74. context->position = endpoint;
  75. }
  76. return 0;
  77. }
  78. static int ftCubicTo(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  79. FtContext *context = reinterpret_cast<FtContext *>(user);
  80. Point2 endpoint = ftPoint2(*to);
  81. if (endpoint != context->position || crossProduct(ftPoint2(*control1)-endpoint, ftPoint2(*control2)-endpoint)) {
  82. context->contour->addEdge(EdgeHolder(context->position, ftPoint2(*control1), ftPoint2(*control2), endpoint));
  83. context->position = endpoint;
  84. }
  85. return 0;
  86. }
  87. GlyphIndex::GlyphIndex(unsigned index) : index(index) { }
  88. unsigned GlyphIndex::getIndex() const {
  89. return index;
  90. }
  91. FreetypeHandle *initializeFreetype() {
  92. FreetypeHandle *handle = new FreetypeHandle;
  93. FT_Error error = FT_Init_FreeType(&handle->library);
  94. if (error) {
  95. delete handle;
  96. return NULL;
  97. }
  98. return handle;
  99. }
  100. void deinitializeFreetype(FreetypeHandle *library) {
  101. FT_Done_FreeType(library->library);
  102. delete library;
  103. }
  104. FontHandle *adoptFreetypeFont(FT_Face ftFace) {
  105. FontHandle *handle = new FontHandle;
  106. handle->face = ftFace;
  107. handle->ownership = false;
  108. return handle;
  109. }
  110. FT_Error readFreetypeOutline(Shape &output, FT_Outline *outline) {
  111. output.contours.clear();
  112. output.inverseYAxis = false;
  113. FtContext context = { };
  114. context.shape = &output;
  115. FT_Outline_Funcs ftFunctions;
  116. ftFunctions.move_to = &ftMoveTo;
  117. ftFunctions.line_to = &ftLineTo;
  118. ftFunctions.conic_to = &ftConicTo;
  119. ftFunctions.cubic_to = &ftCubicTo;
  120. ftFunctions.shift = 0;
  121. ftFunctions.delta = 0;
  122. FT_Error error = FT_Outline_Decompose(outline, &ftFunctions, &context);
  123. if (!output.contours.empty() && output.contours.back().edges.empty())
  124. output.contours.pop_back();
  125. return error;
  126. }
  127. FontHandle *loadFont(FreetypeHandle *library, const char *filename) {
  128. if (!library)
  129. return NULL;
  130. FontHandle *handle = new FontHandle;
  131. FT_Error error = FT_New_Face(library->library, filename, 0, &handle->face);
  132. if (error) {
  133. delete handle;
  134. return NULL;
  135. }
  136. handle->ownership = true;
  137. return handle;
  138. }
  139. FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length) {
  140. if (!library)
  141. return NULL;
  142. FontHandle *handle = new FontHandle;
  143. FT_Error error = FT_New_Memory_Face(library->library, data, length, 0, &handle->face);
  144. if (error) {
  145. delete handle;
  146. return NULL;
  147. }
  148. handle->ownership = true;
  149. return handle;
  150. }
  151. void destroyFont(FontHandle *font) {
  152. if (font->ownership)
  153. FT_Done_Face(font->face);
  154. delete font;
  155. }
  156. bool getFontMetrics(FontMetrics &metrics, FontHandle *font) {
  157. metrics.emSize = F26DOT6_TO_DOUBLE(font->face->units_per_EM);
  158. metrics.ascenderY = F26DOT6_TO_DOUBLE(font->face->ascender);
  159. metrics.descenderY = F26DOT6_TO_DOUBLE(font->face->descender);
  160. metrics.lineHeight = F26DOT6_TO_DOUBLE(font->face->height);
  161. metrics.underlineY = F26DOT6_TO_DOUBLE(font->face->underline_position);
  162. metrics.underlineThickness = F26DOT6_TO_DOUBLE(font->face->underline_thickness);
  163. return true;
  164. }
  165. bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font) {
  166. FT_Error error = FT_Load_Char(font->face, ' ', FT_LOAD_NO_SCALE);
  167. if (error)
  168. return false;
  169. spaceAdvance = F26DOT6_TO_DOUBLE(font->face->glyph->advance.x);
  170. error = FT_Load_Char(font->face, '\t', FT_LOAD_NO_SCALE);
  171. if (error)
  172. return false;
  173. tabAdvance = F26DOT6_TO_DOUBLE(font->face->glyph->advance.x);
  174. return true;
  175. }
  176. bool getGlyphCount(unsigned &output, FontHandle *font) {
  177. output = (unsigned) font->face->num_glyphs;
  178. return true;
  179. }
  180. bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode) {
  181. glyphIndex = GlyphIndex(FT_Get_Char_Index(font->face, unicode));
  182. return glyphIndex.getIndex() != 0;
  183. }
  184. bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *advance) {
  185. if (!font)
  186. return false;
  187. FT_Error error = FT_Load_Glyph(font->face, glyphIndex.getIndex(), FT_LOAD_NO_SCALE);
  188. if (error)
  189. return false;
  190. if (advance)
  191. *advance = F26DOT6_TO_DOUBLE(font->face->glyph->advance.x);
  192. return !readFreetypeOutline(output, &font->face->glyph->outline);
  193. }
  194. bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *advance) {
  195. return loadGlyph(output, font, GlyphIndex(FT_Get_Char_Index(font->face, unicode)), advance);
  196. }
  197. bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex0, GlyphIndex glyphIndex1) {
  198. FT_Vector kerning;
  199. if (FT_Get_Kerning(font->face, glyphIndex0.getIndex(), glyphIndex1.getIndex(), FT_KERNING_UNSCALED, &kerning)) {
  200. output = 0;
  201. return false;
  202. }
  203. output = F26DOT6_TO_DOUBLE(kerning.x);
  204. return true;
  205. }
  206. bool getKerning(double &output, FontHandle *font, unicode_t unicode0, unicode_t unicode1) {
  207. return getKerning(output, font, GlyphIndex(FT_Get_Char_Index(font->face, unicode0)), GlyphIndex(FT_Get_Char_Index(font->face, unicode1)));
  208. }
  209. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  210. bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate) {
  211. bool success = false;
  212. if (font->face->face_flags&FT_FACE_FLAG_MULTIPLE_MASTERS) {
  213. FT_MM_Var *master = NULL;
  214. if (FT_Get_MM_Var(font->face, &master))
  215. return false;
  216. if (master && master->num_axis) {
  217. std::vector<FT_Fixed> coords(master->num_axis);
  218. if (!FT_Get_Var_Design_Coordinates(font->face, FT_UInt(coords.size()), &coords[0])) {
  219. for (FT_UInt i = 0; i < master->num_axis; ++i) {
  220. if (!strcmp(name, master->axis[i].name)) {
  221. coords[i] = DOUBLE_TO_F16DOT16(coordinate);
  222. success = true;
  223. break;
  224. }
  225. }
  226. }
  227. if (FT_Set_Var_Design_Coordinates(font->face, FT_UInt(coords.size()), &coords[0]))
  228. success = false;
  229. }
  230. FT_Done_MM_Var(library->library, master);
  231. }
  232. return success;
  233. }
  234. bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font) {
  235. if (font->face->face_flags&FT_FACE_FLAG_MULTIPLE_MASTERS) {
  236. FT_MM_Var *master = NULL;
  237. if (FT_Get_MM_Var(font->face, &master))
  238. return false;
  239. axes.resize(master->num_axis);
  240. for (FT_UInt i = 0; i < master->num_axis; ++i) {
  241. FontVariationAxis &axis = axes[i];
  242. axis.name = master->axis[i].name;
  243. axis.minValue = F16DOT16_TO_DOUBLE(master->axis[i].minimum);
  244. axis.maxValue = F16DOT16_TO_DOUBLE(master->axis[i].maximum);
  245. axis.defaultValue = F16DOT16_TO_DOUBLE(master->axis[i].def);
  246. }
  247. FT_Done_MM_Var(library->library, master);
  248. return true;
  249. }
  250. return false;
  251. }
  252. #endif
  253. }