import-font.cpp 10 KB

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