• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkottieShaper_DEFINED
9 #define SkottieShaper_DEFINED
10 
11 #include "include/core/SkPoint.h"
12 #include "include/utils/SkTextUtils.h"
13 
14 #include <vector>
15 
16 class SkFontMgr;
17 class SkTextBlob;
18 
19 namespace skottie {
20 
21 // Helper implementing After Effects text shaping semantics on top of SkShaper.
22 
23 class Shaper final {
24 public:
25     struct Fragment {
26         sk_sp<SkTextBlob> fBlob;
27         SkPoint           fPos;
28 
29         // Only valid for kFragmentGlyphs
30         uint32_t          fLineIndex;    // 0-based index for the line this fragment belongs to.
31         bool              fIsWhitespace; // True if the first code point in the corresponding
32                                          // cluster is whitespace.
33     };
34 
35     struct Result {
36         std::vector<Fragment> fFragments;
37         size_t                fMissingGlyphCount = 0;
38 
39         SkRect computeVisualBounds() const;
40     };
41 
42     enum class VAlign : uint8_t {
43         // Align the first line typographical top with the text box top (AE box text).
44         kTop,
45         // Align the first line typographical baseline with the text box top (AE point text).
46         kTopBaseline,
47 
48         // Skottie vertical alignment extensions: these are based on an extent box defined (in Y) as
49         //
50         //   ------------------------------------------------------
51         //   MIN(visual_top_extent   , typographical_top_extent   )
52         //
53         //                         ...
54         //
55         //   MAX(visual_bottom_extent, typographical_bottom_extent)
56         //   ------------------------------------------------------
57 
58         // extent box top -> text box top
59         kVisualTop,
60         // extent box center -> text box center
61         kVisualCenter,
62         // extent box bottom -> text box bottom
63         kVisualBottom,
64         // Resize the text such that the extent box fits (snuggly) in the text box.
65         kVisualResizeToFit,
66     };
67 
68     enum Flags : uint32_t {
69         kNone           = 0x00,
70 
71         // Split out individual glyphs into separate Fragments
72         // (useful when the caller intends to manipulate glyphs independently).
73         kFragmentGlyphs = 0x01,
74     };
75 
76     struct TextDesc {
77         const sk_sp<SkTypeface>&  fTypeface;
78         SkScalar                  fTextSize,
79                                   fLineHeight,
80                                   fAscent;
81         SkTextUtils::Align        fHAlign;
82         VAlign                    fVAlign;
83         uint32_t                  fFlags;
84     };
85 
86     // Performs text layout along an infinite horizontal line, starting at |textPoint|.
87     // Only explicit line breaks (\r) are observed.
88     static Result Shape(const SkString& text, const TextDesc& desc, const SkPoint& textPoint,
89                         const sk_sp<SkFontMgr>&);
90 
91     // Performs text layout within |textBox|, injecting line breaks as needed to ensure
92     // horizontal fitting.  The result is *not* guaranteed to fit vertically (it may extend
93     // below the box bottom).
94     static Result Shape(const SkString& text, const TextDesc& desc, const SkRect& textBox,
95                         const sk_sp<SkFontMgr>&);
96 
97 private:
98     Shaper() = delete;
99 };
100 
101 } // namespace skottie
102 
103 #endif // SkottieShaper_DEFINED
104