Skia
2D Graphics Library
TextShaper.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 SkottieTextShaper_DEFINED
9 #define SkottieTextShaper_DEFINED
10 
11 #include "include/core/SkFont.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/private/base/SkTypeTraits.h"
15 #include "include/utils/SkTextUtils.h"
16 
17 #include <vector>
18 
19 class SkCanvas;
20 class SkFontMgr;
21 class SkTypeface;
22 class SkString;
23 
24 struct SkRect;
25 
26 namespace skottie {
27 
28 // Helper implementing After Effects text shaping semantics on top of SkShaper.
29 
30 class Shaper final {
31 public:
32  struct RunRec {
34  size_t fSize;
35 
36  static_assert(::sk_is_trivially_relocatable<decltype(fFont)>::value);
37 
38  using sk_is_trivially_relocatable = std::true_type;
39  };
40 
41  struct ShapedGlyphs {
42  std::vector<RunRec> fRuns;
43 
44  // Consolidated storage for all runs.
45  std::vector<SkGlyphID> fGlyphIDs;
46  std::vector<SkPoint> fGlyphPos;
47 
48  // fClusters[i] is an input string index, pointing to the start of the UTF sequence
49  // associated with fGlyphs[i]. The number of entries matches the number of glyphs.
50  // Only available with Flags::kClusters.
51  std::vector<size_t> fClusters;
52 
53  enum class BoundsType { kConservative, kTight };
55 
56  void draw(SkCanvas*, const SkPoint& origin, const SkPaint&) const;
57  };
58 
59  struct Fragment {
61  SkPoint fOrigin;
62 
63  // Only valid for kFragmentGlyphs
64  float fAdvance,
66  uint32_t fLineIndex; // 0-based index for the line this fragment belongs to.
67  bool fIsWhitespace; // True if the first code point in the corresponding
68  // cluster is whitespace.
69  };
70 
71  struct Result {
72  std::vector<Fragment> fFragments;
73  size_t fMissingGlyphCount = 0;
74  // Relative text size scale, when using an auto-scaling ResizePolicy
75  // (otherwise 1.0). This is informative of the final text size, and is
76  // not required to render the Result.
77  float fScale = 1.0f;
78 
80  };
81 
82  enum class VAlign : uint8_t {
83  // Align the first line typographical top with the text box top (AE box text).
84  kTop,
85  // Align the first line typographical baseline with the text box top (AE point text).
87 
88  // Skottie vertical alignment extensions
89 
90  // These are based on a hybrid extent box defined (in Y) as
91  //
92  // ------------------------------------------------------
93  // MIN(visual_top_extent , typographical_top_extent )
94  //
95  // ...
96  //
97  // MAX(visual_bottom_extent, typographical_bottom_extent)
98  // ------------------------------------------------------
99  kHybridTop, // extent box top -> text box top
100  kHybridCenter, // extent box center -> text box center
101  kHybridBottom, // extent box bottom -> text box bottom
102 
103  // Visual alignement modes -- these are using tight visual bounds for the paragraph.
104  kVisualTop, // visual top -> text box top
105  kVisualCenter, // visual center -> text box center
106  kVisualBottom, // visual bottom -> text box bottom
107  };
108 
109  enum class ResizePolicy : uint8_t {
110  // Use the specified text size.
111  kNone,
112  // Resize the text such that the extent box fits (snuggly) in the text box,
113  // both horizontally and vertically.
114  kScaleToFit,
115  // Same kScaleToFit if the text doesn't fit at the specified font size.
116  // Otherwise, same as kNone.
118  };
119 
120  enum class LinebreakPolicy : uint8_t {
121  // Break lines such that they fit in a non-empty paragraph box, horizontally.
122  kParagraph,
123  // Only break lines when requested explicitly (\r), regardless of paragraph box dimensions.
124  kExplicit,
125  };
126 
127  // Initial text direction.
128  enum class Direction : uint8_t { kLTR, kRTL };
129 
130  enum class Capitalization {
131  kNone,
132  kUpperCase,
133  };
134 
135  enum Flags : uint32_t {
136  kNone = 0x00,
137 
138  // Split out individual glyphs into separate Fragments
139  // (useful when the caller intends to manipulate glyphs independently).
141 
142  // Compute the advance and ascent for each fragment.
144 
145  // Return cluster information.
146  kClusters = 0x04,
147  };
148 
149  struct TextDesc {
152  fMinTextSize = 0, // when auto-sizing
153  fMaxTextSize = 0, // when auto-sizing
156  fAscent = 0;
157  SkTextUtils::Align fHAlign = SkTextUtils::kLeft_Align;
163  size_t fMaxLines = 0; // when auto-sizing, 0 -> no max
164  uint32_t fFlags = 0;
165  const char* fLocale = nullptr;
166  };
167 
168  // Performs text layout along an infinite horizontal line, starting at |point|.
169  // Only explicit line breaks (\r) are observed.
170  static Result Shape(const SkString& text, const TextDesc& desc, const SkPoint& point,
171  const sk_sp<SkFontMgr>&);
172 
173  // Performs text layout within |box|, injecting line breaks as needed to ensure
174  // horizontal fitting. The result is *not* guaranteed to fit vertically (it may extend
175  // below the box bottom).
176  static Result Shape(const SkString& text, const TextDesc& desc, const SkRect& box,
177  const sk_sp<SkFontMgr>&);
178 
179 private:
180  Shaper() = delete;
181 };
182 
183 } // namespace skottie
184 
185 #endif // SkottieTextShaper_DEFINED
float SkScalar
Definition: SkScalar.h:14
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
Definition: SkFontMgr.h:36
SkFont controls options applied when drawing and measuring text.
Definition: SkFont.h:36
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
Light weight class for managing strings.
Definition: SkString.h:118
The SkTypeface class specifies the typeface and intrinsic style of a font.
Definition: SkTypeface.h:52
Definition: TextShaper.h:30
Direction
Definition: TextShaper.h:128
ResizePolicy
Definition: TextShaper.h:109
VAlign
Definition: TextShaper.h:82
Capitalization
Definition: TextShaper.h:130
LinebreakPolicy
Definition: TextShaper.h:120
Flags
Definition: TextShaper.h:135
@ kFragmentGlyphs
Definition: TextShaper.h:140
@ kClusters
Definition: TextShaper.h:146
@ kNone
Definition: TextShaper.h:136
@ kTrackFragmentAdvanceAscent
Definition: TextShaper.h:143
static Result Shape(const SkString &text, const TextDesc &desc, const SkPoint &point, const sk_sp< SkFontMgr > &)
static Result Shape(const SkString &text, const TextDesc &desc, const SkRect &box, const sk_sp< SkFontMgr > &)
Definition: ExternalLayer.h:16
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
Definition: TextShaper.h:59
SkPoint fOrigin
Definition: TextShaper.h:61
ShapedGlyphs fGlyphs
Definition: TextShaper.h:60
float fAdvance
Definition: TextShaper.h:64
float fAscent
Definition: TextShaper.h:65
bool fIsWhitespace
Definition: TextShaper.h:67
uint32_t fLineIndex
Definition: TextShaper.h:66
Definition: TextShaper.h:71
float fScale
Definition: TextShaper.h:77
std::vector< Fragment > fFragments
Definition: TextShaper.h:72
size_t fMissingGlyphCount
Definition: TextShaper.h:73
SkRect computeVisualBounds() const
Definition: TextShaper.h:32
SkFont fFont
Definition: TextShaper.h:33
std::true_type sk_is_trivially_relocatable
Definition: TextShaper.h:38
size_t fSize
Definition: TextShaper.h:34
Definition: TextShaper.h:41
std::vector< SkGlyphID > fGlyphIDs
Definition: TextShaper.h:45
BoundsType
Definition: TextShaper.h:53
SkRect computeBounds(BoundsType) const
std::vector< SkPoint > fGlyphPos
Definition: TextShaper.h:46
std::vector< size_t > fClusters
Definition: TextShaper.h:51
std::vector< RunRec > fRuns
Definition: TextShaper.h:42
void draw(SkCanvas *, const SkPoint &origin, const SkPaint &) const
Definition: TextShaper.h:149
size_t fMaxLines
Definition: TextShaper.h:163
uint32_t fFlags
Definition: TextShaper.h:164
SkScalar fLineHeight
Definition: TextShaper.h:154
LinebreakPolicy fLinebreak
Definition: TextShaper.h:160
SkTextUtils::Align fHAlign
Definition: TextShaper.h:157
const char * fLocale
Definition: TextShaper.h:165
Direction fDirection
Definition: TextShaper.h:161
const sk_sp< SkTypeface > & fTypeface
Definition: TextShaper.h:150
ResizePolicy fResize
Definition: TextShaper.h:159
SkScalar fMaxTextSize
Definition: TextShaper.h:153
SkScalar fTextSize
Definition: TextShaper.h:151
Capitalization fCapitalization
Definition: TextShaper.h:162
SkScalar fMinTextSize
Definition: TextShaper.h:152
SkScalar fLineShift
Definition: TextShaper.h:155
SkScalar fAscent
Definition: TextShaper.h:156
VAlign fVAlign
Definition: TextShaper.h:158