import-font.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <cstddef>
  3. #include "../core/Shape.h"
  4. namespace msdfgen {
  5. typedef unsigned char byte;
  6. typedef unsigned unicode_t;
  7. class FreetypeHandle;
  8. class FontHandle;
  9. class GlyphIndex {
  10. public:
  11. explicit GlyphIndex(unsigned index = 0);
  12. unsigned getIndex() const;
  13. private:
  14. unsigned index;
  15. };
  16. /// Global metrics of a typeface (in font units).
  17. struct FontMetrics {
  18. /// The size of one EM.
  19. double emSize;
  20. /// The vertical position of the ascender and descender relative to the baseline.
  21. double ascenderY, descenderY;
  22. /// The vertical difference between consecutive baselines.
  23. double lineHeight;
  24. /// The vertical position and thickness of the underline.
  25. double underlineY, underlineThickness;
  26. };
  27. /// A structure to model a given axis of a variable font.
  28. struct FontVariationAxis {
  29. /// The name of the variation axis.
  30. const char *name;
  31. /// The axis's minimum coordinate value.
  32. double minValue;
  33. /// The axis's maximum coordinate value.
  34. double maxValue;
  35. /// The axis's default coordinate value. FreeType computes meaningful default values for Adobe MM fonts.
  36. double defaultValue;
  37. };
  38. /// Initializes the FreeType library.
  39. FreetypeHandle *initializeFreetype();
  40. /// Deinitializes the FreeType library.
  41. void deinitializeFreetype(FreetypeHandle *library);
  42. #ifdef FT_LOAD_DEFAULT // FreeType included
  43. /// Creates a FontHandle from FT_Face that was loaded by the user. destroyFont must still be called but will not affect the FT_Face.
  44. FontHandle *adoptFreetypeFont(FT_Face ftFace);
  45. /// Converts the geometry of FreeType's FT_Outline to a Shape object.
  46. FT_Error readFreetypeOutline(Shape &output, FT_Outline *outline);
  47. #endif
  48. /// Loads a font file and returns its handle.
  49. FontHandle *loadFont(FreetypeHandle *library, const char *filename);
  50. /// Loads a font from binary data and returns its handle.
  51. FontHandle *loadFontData(FreetypeHandle *library, const byte *data, int length);
  52. /// Unloads a font file.
  53. void destroyFont(FontHandle *font);
  54. /// Outputs the metrics of a font file.
  55. bool getFontMetrics(FontMetrics &metrics, FontHandle *font);
  56. /// Outputs the width of the space and tab characters.
  57. bool getFontWhitespaceWidth(double &spaceAdvance, double &tabAdvance, FontHandle *font);
  58. /// Outputs the glyph index corresponding to the specified Unicode character.
  59. bool getGlyphIndex(GlyphIndex &glyphIndex, FontHandle *font, unicode_t unicode);
  60. /// Loads the geometry of a glyph from a font file.
  61. bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *advance = NULL);
  62. bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *advance = NULL);
  63. /// Outputs the kerning distance adjustment between two specific glyphs.
  64. bool getKerning(double &output, FontHandle *font, GlyphIndex glyphIndex1, GlyphIndex glyphIndex2);
  65. bool getKerning(double &output, FontHandle *font, unicode_t unicode1, unicode_t unicode2);
  66. #ifndef MSDFGEN_DISABLE_VARIABLE_FONTS
  67. /// Sets a single variation axis of a variable font.
  68. bool setFontVariationAxis(FreetypeHandle *library, FontHandle *font, const char *name, double coordinate);
  69. /// Lists names and ranges of variation axes of a variable font.
  70. bool listFontVariationAxes(std::vector<FontVariationAxis> &axes, FreetypeHandle *library, FontHandle *font);
  71. #endif
  72. }