import-font.cpp 9.8 KB

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