SkShaper.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2016 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 SkShaper_DEFINED
  8. #define SkShaper_DEFINED
  9. #include "include/core/SkPoint.h"
  10. #include "include/core/SkRefCnt.h"
  11. #include "include/core/SkScalar.h"
  12. #include "include/core/SkTextBlob.h"
  13. #include "include/core/SkTypes.h"
  14. #include <memory>
  15. class SkFont;
  16. class SkFontMgr;
  17. /**
  18. Shapes text using HarfBuzz and places the shaped text into a
  19. client-managed buffer.
  20. If compiled without HarfBuzz, fall back on SkPaint::textToGlyphs.
  21. */
  22. class SkShaper {
  23. public:
  24. static std::unique_ptr<SkShaper> MakePrimitive();
  25. #ifdef SK_SHAPER_HARFBUZZ_AVAILABLE
  26. static std::unique_ptr<SkShaper> MakeShaperDrivenWrapper();
  27. static std::unique_ptr<SkShaper> MakeShapeThenWrap();
  28. static std::unique_ptr<SkShaper> MakeShapeDontWrapOrReorder();
  29. #endif
  30. static std::unique_ptr<SkShaper> Make();
  31. SkShaper();
  32. virtual ~SkShaper();
  33. class RunIterator {
  34. public:
  35. virtual ~RunIterator() = default;
  36. /** Set state to that of current run and move iterator to end of that run. */
  37. virtual void consume() = 0;
  38. /** Offset to one past the last (utf8) element in the current run. */
  39. virtual size_t endOfCurrentRun() const = 0;
  40. /** Return true if consume should no longer be called. */
  41. virtual bool atEnd() const = 0;
  42. };
  43. private:
  44. template <typename RunIteratorSubclass>
  45. class TrivialRunIterator : public RunIteratorSubclass {
  46. public:
  47. static_assert(std::is_base_of<RunIterator, RunIteratorSubclass>::value, "");
  48. TrivialRunIterator(size_t utf8Bytes) : fEnd(utf8Bytes), fAtEnd(false) {}
  49. void consume() override { fAtEnd = true; }
  50. size_t endOfCurrentRun() const override { return fAtEnd ? fEnd : 0; }
  51. bool atEnd() const override { return fAtEnd; }
  52. private:
  53. size_t fEnd;
  54. bool fAtEnd;
  55. };
  56. public:
  57. class FontRunIterator : public RunIterator {
  58. public:
  59. virtual const SkFont& currentFont() const = 0;
  60. };
  61. static std::unique_ptr<FontRunIterator>
  62. MakeFontMgrRunIterator(const char* utf8, size_t utf8Bytes,
  63. const SkFont& font, sk_sp<SkFontMgr> fallback);
  64. class TrivialFontRunIterator : public TrivialRunIterator<FontRunIterator> {
  65. public:
  66. TrivialFontRunIterator(const SkFont& font, size_t utf8Bytes)
  67. : TrivialRunIterator(utf8Bytes), fFont(font) {}
  68. const SkFont& currentFont() const override { return fFont; }
  69. private:
  70. SkFont fFont;
  71. };
  72. class BiDiRunIterator : public RunIterator {
  73. public:
  74. /** The unicode bidi embedding level (even ltr, odd rtl) */
  75. virtual uint8_t currentLevel() const = 0;
  76. };
  77. #ifdef SK_SHAPER_HARFBUZZ_AVAILABLE
  78. static std::unique_ptr<BiDiRunIterator>
  79. MakeIcuBiDiRunIterator(const char* utf8, size_t utf8Bytes, uint8_t bidiLevel);
  80. #endif
  81. class TrivialBiDiRunIterator : public TrivialRunIterator<BiDiRunIterator> {
  82. public:
  83. TrivialBiDiRunIterator(uint8_t bidiLevel, size_t utf8Bytes)
  84. : TrivialRunIterator(utf8Bytes), fBidiLevel(bidiLevel) {}
  85. uint8_t currentLevel() const override { return fBidiLevel; }
  86. private:
  87. uint8_t fBidiLevel;
  88. };
  89. class ScriptRunIterator : public RunIterator {
  90. public:
  91. /** Should be iso15924 codes. */
  92. virtual SkFourByteTag currentScript() const = 0;
  93. };
  94. #ifdef SK_SHAPER_HARFBUZZ_AVAILABLE
  95. static std::unique_ptr<ScriptRunIterator>
  96. MakeHbIcuScriptRunIterator(const char* utf8, size_t utf8Bytes);
  97. #endif
  98. class TrivialScriptRunIterator : public TrivialRunIterator<ScriptRunIterator> {
  99. public:
  100. TrivialScriptRunIterator(SkFourByteTag script, size_t utf8Bytes)
  101. : TrivialRunIterator(utf8Bytes), fScript(script) {}
  102. SkFourByteTag currentScript() const override { return fScript; }
  103. private:
  104. SkFourByteTag fScript;
  105. };
  106. class LanguageRunIterator : public RunIterator {
  107. public:
  108. /** Should be BCP-47, c locale names may also work. */
  109. virtual const char* currentLanguage() const = 0;
  110. };
  111. static std::unique_ptr<LanguageRunIterator>
  112. MakeStdLanguageRunIterator(const char* utf8, size_t utf8Bytes);
  113. class TrivialLanguageRunIterator : public TrivialRunIterator<LanguageRunIterator> {
  114. public:
  115. TrivialLanguageRunIterator(const char* language, size_t utf8Bytes)
  116. : TrivialRunIterator(utf8Bytes), fLanguage(language) {}
  117. const char* currentLanguage() const override { return fLanguage.c_str(); }
  118. private:
  119. SkString fLanguage;
  120. };
  121. class RunHandler {
  122. public:
  123. virtual ~RunHandler() = default;
  124. struct Range {
  125. constexpr Range() : fBegin(0), fSize(0) {}
  126. constexpr Range(size_t begin, size_t size) : fBegin(begin), fSize(size) {}
  127. size_t fBegin;
  128. size_t fSize;
  129. constexpr size_t begin() const { return fBegin; }
  130. constexpr size_t end() const { return begin() + size(); }
  131. constexpr size_t size() const { return fSize; }
  132. };
  133. struct RunInfo {
  134. const SkFont& fFont;
  135. uint8_t fBidiLevel;
  136. SkVector fAdvance;
  137. size_t glyphCount;
  138. Range utf8Range;
  139. };
  140. struct Buffer {
  141. SkGlyphID* glyphs; // required
  142. SkPoint* positions; // required, if (!offsets) put glyphs[i] at positions[i]
  143. // if ( offsets) positions[i+1]-positions[i] are advances
  144. SkPoint* offsets; // optional, if ( offsets) put glyphs[i] at positions[i]+offsets[i]
  145. uint32_t* clusters; // optional, utf8+clusters[i] starts run which produced glyphs[i]
  146. SkPoint point; // offset to add to all positions
  147. };
  148. /** Called when beginning a line. */
  149. virtual void beginLine() = 0;
  150. /** Called once for each run in a line. Can compute baselines and offsets. */
  151. virtual void runInfo(const RunInfo&) = 0;
  152. /** Called after all runInfo calls for a line. */
  153. virtual void commitRunInfo() = 0;
  154. /** Called for each run in a line after commitRunInfo. The buffer will be filled out. */
  155. virtual Buffer runBuffer(const RunInfo&) = 0;
  156. /** Called after each runBuffer is filled out. */
  157. virtual void commitRunBuffer(const RunInfo&) = 0;
  158. /** Called when ending a line. */
  159. virtual void commitLine() = 0;
  160. };
  161. virtual void shape(const char* utf8, size_t utf8Bytes,
  162. const SkFont& srcFont,
  163. bool leftToRight,
  164. SkScalar width,
  165. RunHandler*) const = 0;
  166. virtual void shape(const char* utf8, size_t utf8Bytes,
  167. FontRunIterator&,
  168. BiDiRunIterator&,
  169. ScriptRunIterator&,
  170. LanguageRunIterator&,
  171. SkScalar width,
  172. RunHandler*) const = 0;
  173. private:
  174. SkShaper(const SkShaper&) = delete;
  175. SkShaper& operator=(const SkShaper&) = delete;
  176. };
  177. /**
  178. * Helper for shaping text directly into a SkTextBlob.
  179. */
  180. class SkTextBlobBuilderRunHandler final : public SkShaper::RunHandler {
  181. public:
  182. SkTextBlobBuilderRunHandler(const char* utf8Text, SkPoint offset)
  183. : fUtf8Text(utf8Text)
  184. , fOffset(offset) {}
  185. sk_sp<SkTextBlob> makeBlob();
  186. SkPoint endPoint() { return fOffset; }
  187. void beginLine() override;
  188. void runInfo(const RunInfo&) override;
  189. void commitRunInfo() override;
  190. Buffer runBuffer(const RunInfo&) override;
  191. void commitRunBuffer(const RunInfo&) override;
  192. void commitLine() override;
  193. private:
  194. SkTextBlobBuilder fBuilder;
  195. char const * const fUtf8Text;
  196. uint32_t* fClusters;
  197. int fClusterOffset;
  198. int fGlyphCount;
  199. SkScalar fMaxRunAscent;
  200. SkScalar fMaxRunDescent;
  201. SkScalar fMaxRunLeading;
  202. SkPoint fCurrentPosition;
  203. SkPoint fOffset;
  204. };
  205. #endif // SkShaper_DEFINED