TestSVGTypeface.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef TestSVGTypeface_DEFINED
  8. #define TestSVGTypeface_DEFINED
  9. #include "include/core/SkFontArguments.h"
  10. #include "include/core/SkFontMetrics.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkRect.h"
  14. #include "include/core/SkRefCnt.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkStream.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTypeface.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/pathops/SkPathOps.h"
  21. #include "include/private/SkMutex.h"
  22. #include "include/private/SkTArray.h"
  23. #include "include/private/SkTHash.h"
  24. #include "src/core/SkSpan.h"
  25. #include <memory>
  26. class SkCanvas;
  27. class SkDescriptor;
  28. class SkFontDescriptor;
  29. class SkFontStyle;
  30. class SkGlyph;
  31. class SkPath;
  32. class SkScalerContext;
  33. class SkSVGDOM;
  34. class SkWStream;
  35. struct SkAdvancedTypefaceMetrics;
  36. struct SkScalerContextEffects;
  37. struct SkScalerContextRec;
  38. struct SkSVGTestTypefaceGlyphData {
  39. const char* fSvgResourcePath;
  40. SkPoint fOrigin; // y-down
  41. SkScalar fAdvance;
  42. SkUnichar fUnicode; // TODO: this limits to 1:1
  43. };
  44. class TestSVGTypeface : public SkTypeface {
  45. public:
  46. TestSVGTypeface(const char* name,
  47. int upem,
  48. const SkFontMetrics& metrics,
  49. SkSpan<const SkSVGTestTypefaceGlyphData> data,
  50. const SkFontStyle& style);
  51. ~TestSVGTypeface() override;
  52. void getAdvance(SkGlyph* glyph) const;
  53. void getFontMetrics(SkFontMetrics* metrics) const;
  54. static sk_sp<TestSVGTypeface> Default();
  55. static sk_sp<TestSVGTypeface> Planets();
  56. void exportTtxCbdt(SkWStream*, SkSpan<unsigned> strikeSizes) const;
  57. void exportTtxSbix(SkWStream*, SkSpan<unsigned> strikeSizes) const;
  58. void exportTtxColr(SkWStream*) const;
  59. virtual bool getPathOp(SkColor, SkPathOp*) const = 0;
  60. struct GlyfLayerInfo {
  61. GlyfLayerInfo(int layerColorIndex, SkIRect bounds)
  62. : fLayerColorIndex(layerColorIndex), fBounds(bounds) {}
  63. int fLayerColorIndex;
  64. SkIRect fBounds;
  65. };
  66. struct GlyfInfo {
  67. GlyfInfo() : fBounds(SkIRect::MakeEmpty()) {}
  68. SkIRect fBounds;
  69. SkTArray<GlyfLayerInfo> fLayers;
  70. };
  71. protected:
  72. void exportTtxCommon(SkWStream*, const char* type, const SkTArray<GlyfInfo>* = nullptr) const;
  73. SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
  74. const SkDescriptor* desc) const override;
  75. void onFilterRec(SkScalerContextRec* rec) const override;
  76. void getGlyphToUnicodeMap(SkUnichar*) const override;
  77. std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override;
  78. std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override { return nullptr; }
  79. sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override {
  80. return sk_ref_sp(this);
  81. }
  82. void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override;
  83. void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override;
  84. void getPostScriptGlyphNames(SkString*) const override {}
  85. int onCountGlyphs() const override { return fGlyphCount; }
  86. int onGetUPEM() const override { return fUpem; }
  87. void onGetFamilyName(SkString* familyName) const override;
  88. SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
  89. int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],
  90. int coordinateCount) const override {
  91. return 0;
  92. }
  93. int onGetVariationDesignParameters(SkFontParameters::Variation::Axis parameters[],
  94. int parameterCount) const override {
  95. return 0;
  96. }
  97. int onGetTableTags(SkFontTableTag tags[]) const override { return 0; }
  98. size_t onGetTableData(SkFontTableTag tag,
  99. size_t offset,
  100. size_t length,
  101. void* data) const override {
  102. return 0;
  103. }
  104. private:
  105. struct Glyph {
  106. Glyph();
  107. ~Glyph();
  108. SkPoint fOrigin;
  109. SkScalar fAdvance;
  110. const char* fResourcePath;
  111. SkSize size() const;
  112. void render(SkCanvas*) const;
  113. private:
  114. // Lazily parses the SVG from fResourcePath, and manages mutex locking.
  115. template <typename Fn> void withSVG(Fn&&) const;
  116. // The mutex guards lazy parsing of the SVG, but also predates that.
  117. // Must be SkSVGDOM::render() is not thread safe?
  118. // If not, an SkOnce is enough here.
  119. mutable SkMutex fSvgMutex;
  120. mutable bool fParsedSvg = false;
  121. mutable sk_sp<SkSVGDOM> fSvg;
  122. };
  123. SkString fName;
  124. int fUpem;
  125. const SkFontMetrics fFontMetrics;
  126. std::unique_ptr<Glyph[]> fGlyphs;
  127. int fGlyphCount;
  128. SkTHashMap<SkUnichar, SkGlyphID> fCMap;
  129. friend class SkTestSVGScalerContext;
  130. };
  131. #endif