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