SkFontHost_FreeType_common.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2006-2012 The Android Open Source Project
  3. * Copyright 2012 Mozilla Foundation
  4. *
  5. * Use of this source code is governed by a BSD-style license that can be
  6. * found in the LICENSE file.
  7. */
  8. #ifndef SKFONTHOST_FREETYPE_COMMON_H_
  9. #define SKFONTHOST_FREETYPE_COMMON_H_
  10. #include "include/core/SkTypeface.h"
  11. #include "include/core/SkTypes.h"
  12. #include "include/private/SkMutex.h"
  13. #include "src/core/SkGlyph.h"
  14. #include "src/core/SkScalerContext.h"
  15. #include "src/utils/SkCharToGlyphCache.h"
  16. #include "include/core/SkFontMgr.h"
  17. // These are forward declared to avoid pimpl but also hide the FreeType implementation.
  18. typedef struct FT_LibraryRec_* FT_Library;
  19. typedef struct FT_FaceRec_* FT_Face;
  20. typedef struct FT_StreamRec_* FT_Stream;
  21. typedef signed long FT_Pos;
  22. #ifdef SK_DEBUG
  23. const char* SkTraceFtrGetError(int);
  24. #define SK_TRACEFTR(ERR, MSG, ...) \
  25. SkDebugf("%s:%lu:1: error: 0x%x '%s' " MSG "\n", __FILE__, __LINE__, ERR, \
  26. SkTraceFtrGetError((int)(ERR)), __VA_ARGS__)
  27. #else
  28. #define SK_TRACEFTR(ERR, ...) do { sk_ignore_unused_variable(ERR); } while (false)
  29. #endif
  30. class SkScalerContext_FreeType_Base : public SkScalerContext {
  31. protected:
  32. // See http://freetype.sourceforge.net/freetype2/docs/reference/ft2-bitmap_handling.html#FT_Bitmap_Embolden
  33. // This value was chosen by eyeballing the result in Firefox and trying to match it.
  34. static const FT_Pos kBitmapEmboldenStrength = 1 << 6;
  35. SkScalerContext_FreeType_Base(sk_sp<SkTypeface> typeface, const SkScalerContextEffects& effects,
  36. const SkDescriptor *desc)
  37. : INHERITED(std::move(typeface), effects, desc)
  38. {}
  39. void generateGlyphImage(FT_Face face, const SkGlyph& glyph, const SkMatrix& bitmapTransform);
  40. bool generateGlyphPath(FT_Face face, SkPath* path);
  41. bool generateFacePath(FT_Face face, SkGlyphID glyphID, SkPath* path);
  42. private:
  43. typedef SkScalerContext INHERITED;
  44. };
  45. class SkTypeface_FreeType : public SkTypeface {
  46. public:
  47. /** For SkFontMgrs to make use of our ability to extract
  48. * name and style from a stream, using FreeType's API.
  49. */
  50. class Scanner : ::SkNoncopyable {
  51. public:
  52. Scanner();
  53. ~Scanner();
  54. struct AxisDefinition {
  55. SkFourByteTag fTag;
  56. SkFixed fMinimum;
  57. SkFixed fDefault;
  58. SkFixed fMaximum;
  59. };
  60. using AxisDefinitions = SkSTArray<4, AxisDefinition, true>;
  61. bool recognizedFont(SkStreamAsset* stream, int* numFonts) const;
  62. bool scanFont(SkStreamAsset* stream, int ttcIndex,
  63. SkString* name, SkFontStyle* style, bool* isFixedPitch,
  64. AxisDefinitions* axes) const;
  65. static void computeAxisValues(
  66. AxisDefinitions axisDefinitions,
  67. const SkFontArguments::VariationPosition position,
  68. SkFixed* axisValues,
  69. const SkString& name);
  70. static bool GetAxes(FT_Face face, AxisDefinitions* axes);
  71. private:
  72. FT_Face openFace(SkStreamAsset* stream, int ttcIndex, FT_Stream ftStream) const;
  73. FT_Library fLibrary;
  74. mutable SkMutex fLibraryMutex;
  75. };
  76. /** Fetch units/EM from "head" table if needed (ie for bitmap fonts) */
  77. static int GetUnitsPerEm(FT_Face face);
  78. protected:
  79. SkTypeface_FreeType(const SkFontStyle& style, bool isFixedPitch)
  80. : INHERITED(style, isFixedPitch)
  81. {}
  82. std::unique_ptr<SkFontData> cloneFontData(const SkFontArguments&) const;
  83. virtual SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
  84. const SkDescriptor*) const override;
  85. void onFilterRec(SkScalerContextRec*) const override;
  86. void getGlyphToUnicodeMap(SkUnichar*) const override;
  87. std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
  88. void getPostScriptGlyphNames(SkString* dstArray) const override;
  89. int onGetUPEM() const override;
  90. bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
  91. int32_t adjustments[]) const override;
  92. void onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphID glyphs[]) const override;
  93. int onCountGlyphs() const override;
  94. LocalizedStrings* onCreateFamilyNameIterator() const override;
  95. int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
  96. int coordinateCount) const override;
  97. int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
  98. int parameterCount) const override;
  99. int onGetTableTags(SkFontTableTag tags[]) const override;
  100. size_t onGetTableData(SkFontTableTag, size_t offset,
  101. size_t length, void* data) const override;
  102. sk_sp<SkData> onCopyTableData(SkFontTableTag) const override;
  103. private:
  104. mutable SkMutex fC2GCacheMutex;
  105. mutable SkCharToGlyphCache fC2GCache;
  106. typedef SkTypeface INHERITED;
  107. };
  108. #endif // SKFONTHOST_FREETYPE_COMMON_H_