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 8 #ifndef SkShaper_DEFINED 9 #define SkShaper_DEFINED 10 11 #include <memory> 12 13 #include "SkPoint.h" 14 #include "SkSpan.h" 15 #include "SkTextBlob.h" 16 #include "SkTypeface.h" 17 18 class SkFont; 19 20 /** 21 Shapes text using HarfBuzz and places the shaped text into a 22 client-managed buffer. 23 24 If compiled without HarfBuzz, fall back on SkPaint::textToGlyphs. 25 */ 26 class SkShaper { 27 public: 28 static std::unique_ptr<SkShaper> MakePrimitive(); 29 #ifdef SK_SHAPER_HARFBUZZ_AVAILABLE 30 static std::unique_ptr<SkShaper> MakeHarfBuzz(); 31 #endif 32 33 static std::unique_ptr<SkShaper> Make(); 34 35 SkShaper(); 36 virtual ~SkShaper(); 37 38 class RunHandler { 39 public: 40 virtual ~RunHandler() = default; 41 42 struct RunInfo { 43 SkVector fAdvance; 44 SkScalar fAscent, 45 fDescent, 46 fLeading; 47 }; 48 49 struct Buffer { 50 SkGlyphID* glyphs; // required 51 SkPoint* positions; // required 52 uint32_t* clusters; // optional 53 }; 54 55 // Callback per glyph run. 56 virtual Buffer newRunBuffer(const RunInfo&, const SkFont&, int glyphCount, 57 SkSpan<const char> utf8) = 0; 58 59 // Called after run information is filled out. 60 virtual void commitRun() = 0; 61 // Callback per line. 62 virtual void commitLine() = 0; 63 }; 64 65 virtual SkPoint shape(RunHandler* handler, 66 const SkFont& srcFont, 67 const char* utf8text, 68 size_t textBytes, 69 bool leftToRight, 70 SkPoint point, 71 SkScalar width) const = 0; 72 73 private: 74 SkShaper(const SkShaper&) = delete; 75 SkShaper& operator=(const SkShaper&) = delete; 76 }; 77 78 /** 79 * Helper for shaping text directly into a SkTextBlob. 80 */ 81 class SkTextBlobBuilderRunHandler final : public SkShaper::RunHandler { 82 public: SkTextBlobBuilderRunHandler(const char * utf8Text)83 SkTextBlobBuilderRunHandler(const char* utf8Text) : fUtf8Text(utf8Text) {} 84 sk_sp<SkTextBlob> makeBlob(); 85 86 SkShaper::RunHandler::Buffer newRunBuffer(const RunInfo&, const SkFont&, int, SkSpan<const char>) override; 87 void commitRun() override; commitLine()88 void commitLine() override {} 89 90 private: 91 SkTextBlobBuilder fBuilder; 92 char const * const fUtf8Text; 93 uint32_t* fClusters; 94 int fClusterOffset; 95 int fGlyphCount; 96 }; 97 98 #endif // SkShaper_DEFINED 99