import-font.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #include "../core/Shape.h"
  3. namespace msdfgen {
  4. #define MSDFGEN_LEGACY_FONT_COORDINATE_SCALE (1/64.)
  5. typedef unsigned unicode_t;
  6. class FreetypeHandle;
  7. class FontHandle;
  8. class GlyphIndex {
  9. public:
  10. explicit GlyphIndex(unsigned index = 0);
  11. unsigned getIndex() const;
  12. private:
  13. unsigned index;
  14. };
  15. /// Global metrics of a typeface (in font units).
  16. struct FontMetrics {
  17. /// The size of one EM.
  18. double emSize;
  19. /// The vertical position of the ascender and descender relative to the baseline.
  20. double ascenderY, descenderY;
  21. /// The vertical difference between consecutive baselines.
  22. double lineHeight;
  23. /// The vertical position and thickness of the underline.
  24. double underlineY, underlineThickness;
  25. };
  26. /// A structure to model a given axis of a variable font.
  27. struct FontVariationAxis {
  28. /// The name of the variation axis.
  29. const char *name;
  30. /// The axis's minimum coordinate value.
  31. double minValue;
  32. /// The axis's maximum coordinate value.
  33. double maxValue;
  34. /// The axis's default coordinate value. FreeType computes meaningful default values for Adobe MM fonts.
  35. double defaultValue;
  36. };
  37. /// The scaling applied to font glyph coordinates when loading a glyph
  38. enum FontCoordinateScaling {
  39. /// The coordinates are kept as the integer values native to the font file
  40. FONT_SCALING_NONE,
  41. /// The coordinates will be normalized to the em size, i.e. 1 = 1 em
  42. FONT_SCALING_EM_NORMALIZED,
  43. /// The incorrect legacy version that was in effect before version 1.12, coordinate values are divided by 64 - DO NOT USE - for backwards compatibility only
  44. FONT_SCALING_LEGACY
  45. };
  46. /// Initializes the FreeType library.
  47. FreetypeHandle *initializeFreetype();
  48. /// Deinitializes the FreeType library.
  49. void deinitializeFreetype(FreetypeHandle *library);
  50. #ifdef FT_LOAD_DEFAULT // FreeType included
  51. /// Creates a FontHandle from FT_Face that was loaded by the user. destroyFont must still be called but will not affect the FT_Face.
  52. FontHandle *adoptFreetypeFont(FT_Face ftFace);
  53. /// Converts the geometry of FreeType's FT_Outline to a Shape object.
  54. FT_Error readFreetypeOutline(Shape &output, FT_Outline *outline, double scale = MSDFGEN_LEGACY_FONT_COORDINATE_SCALE);
  55. #endif
  56. /// Loads a font file and returns its handle.
  57. FontHandle *loadFont(FreetypeHandle *library, const char *filename);
  58. /// Loads a font from binary data and returns its handle.
  59. FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length);
  60. /// Unloads a font.
  61. void destroyFont(FontHandle *font);
  62. /// Outputs the metrics of a font.
  63. bool getFontMetrics(FontMetrics &metrics, FontHandle *font, FontCoordinateScaling coordinateScaling = FONT_SCALING_LEGACY);
  64. /// Outputs the width of the space and tab characters.
  65. bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font, FontCoordinateScaling coordinateScaling = FONT_SCALING_LEGACY);
  66. /// Outputs the total number of glyphs available in the font.
  67. bool getGlyphCount(unsigned &output, FontHandle *font);
  68. /// Outputs the glyph index corresponding to the specified Unicode character.
  69. bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode);
  70. /// Loads the geometry of a glyph from a font.
  71. bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, FontCoordinateScaling coordinateScaling, double *outAdvance = NULL);
  72. bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, FontCoordinateScaling coordinateScaling, double *outAdvance = NULL);
  73. // Legacy API - FontCoordinateScaling is LEGACY
  74. bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *outAdvance = NULL);
  75. bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *outAdvance = NULL);
  76. /// Outputs the kerning distance adjustment between two specific glyphs.
  77. bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex0, GlyphIndex glyphIndex1, FontCoordinateScaling coordinateScaling = FONT_SCALING_LEGACY);
  78. bool getKerning(double &output, FontHandle *font, unicode_t unicode0, unicode_t unicode1, FontCoordinateScaling coordinateScaling = FONT_SCALING_LEGACY);
  79. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  80. /// Sets a single variation axis of a variable font.
  81. bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate);
  82. /// Lists names and ranges of variation axes of a variable font.
  83. bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font);
  84. #endif
  85. }