Flutter Engine
The Flutter Engine
GlyphRun.h
Go to the documentation of this file.
1/*
2 * Copyright 2018 The Android Open Source Project
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 SkGlyphRun_DEFINED
9#define SkGlyphRun_DEFINED
10
11#include "include/core/SkFont.h"
14#include "include/core/SkRect.h"
16#include "include/core/SkSpan.h"
20#include "src/base/SkZip.h"
21
22#include <algorithm>
23#include <cstddef>
24#include <cstdint>
25#include <iterator>
26#include <optional>
27#include <tuple>
28#include <vector>
29
30class SkPaint;
31struct SkRSXform;
32
33namespace sktext {
34class GlyphRunBuilder;
35
36class GlyphRun {
37public:
38 GlyphRun(const SkFont& font,
44
45 GlyphRun(const GlyphRun& glyphRun, const SkFont& font);
46
47 size_t runSize() const { return fSource.size(); }
48 SkSpan<const SkPoint> positions() const { return fSource.get<1>(); }
49 SkSpan<const SkGlyphID> glyphsIDs() const { return fSource.get<0>(); }
51 const SkFont& font() const { return fFont; }
52 SkSpan<const uint32_t> clusters() const { return fClusters; }
53 SkSpan<const char> text() const { return fText; }
54 SkSpan<const SkVector> scaledRotations() const { return fScaledRotations; }
55
56private:
57 // GlyphIDs and positions.
59 // Original text from SkTextBlob if present. Will be empty of not present.
60 const SkSpan<const char> fText;
61 // Original clusters from SkTextBlob if present. Will be empty if not present.
62 const SkSpan<const uint32_t> fClusters;
63 // Possible RSXForm information
64 const SkSpan<const SkVector> fScaledRotations;
65 // Font for this run modified to have glyph encoding and left alignment.
66 SkFont fFont;
67};
68
70 SkSpan<const GlyphRun> fGlyphRuns;
71
72public:
73 // Blob maybe null.
77 SkSpan<const GlyphRun> glyphRunList,
79
80 GlyphRunList(const GlyphRun& glyphRun,
81 const SkRect& bounds,
84 uint64_t uniqueID() const;
85 bool anyRunsLCD() const;
86 void temporaryShuntBlobNotifyAddedToCache(uint32_t cacheID, SkTextBlob::PurgeDelegate) const;
87
88 bool canCache() const { return fOriginalTextBlob != nullptr; }
89 size_t runCount() const { return fGlyphRuns.size(); }
90 size_t totalGlyphCount() const {
91 size_t glyphCount = 0;
92 for (const GlyphRun& run : *this) {
93 glyphCount += run.runSize();
94 }
95 return glyphCount;
96 }
97 size_t maxGlyphRunSize() const {
98 size_t size = 0;
99 for (const GlyphRun& run : *this) {
100 size = std::max(run.runSize(), size);
101 }
102 return size;
103 }
104
105 bool hasRSXForm() const {
106 for (const GlyphRun& run : *this) {
107 if (!run.scaledRotations().empty()) { return true; }
108 }
109 return false;
110 }
111
113
114 SkPoint origin() const { return fOrigin; }
115 SkRect sourceBounds() const { return fSourceBounds; }
116 SkRect sourceBoundsWithOrigin() const { return fSourceBounds.makeOffset(fOrigin); }
117 const SkTextBlob* blob() const { return fOriginalTextBlob; }
118 GlyphRunBuilder* builder() const { return fBuilder; }
119
120 auto begin() -> decltype(fGlyphRuns.begin()) { return fGlyphRuns.begin(); }
121 auto end() -> decltype(fGlyphRuns.end()) { return fGlyphRuns.end(); }
122 auto begin() const -> decltype(std::cbegin(fGlyphRuns)) { return std::cbegin(fGlyphRuns); }
123 auto end() const -> decltype(std::cend(fGlyphRuns)) { return std::cend(fGlyphRuns); }
124 auto size() const -> decltype(fGlyphRuns.size()) { return fGlyphRuns.size(); }
125 auto empty() const -> decltype(fGlyphRuns.empty()) { return fGlyphRuns.empty(); }
126 auto operator [] (size_t i) const -> decltype(fGlyphRuns[i]) { return fGlyphRuns[i]; }
127
128private:
129 // The text blob is needed to hook up the call back that the SkTextBlob destructor calls. It
130 // should be used for nothing else.
131 const SkTextBlob* fOriginalTextBlob{nullptr};
132 const SkRect fSourceBounds{SkRect::MakeEmpty()};
133 const SkPoint fOrigin = {0, 0};
134 GlyphRunBuilder* const fBuilder;
135};
136
138public:
141 const SkPaint& paint,
142 const void* bytes,
143 size_t byteLength,
144 SkPoint origin,
146 const GlyphRunList& blobToGlyphRunList(const SkTextBlob& blob, SkPoint origin);
147 std::tuple<SkSpan<const SkPoint>, SkSpan<const SkVector>>
149
150 bool empty() const { return fGlyphRunListStorage.empty(); }
151
152private:
153 void initialize(const SkTextBlob& blob);
154 void prepareBuffers(int positionCount, int RSXFormCount);
155
156 SkSpan<const SkGlyphID> textToGlyphIDs(
157 const SkFont& font, const void* bytes, size_t byteLength, SkTextEncoding);
158
159 void makeGlyphRun(
160 const SkFont& font,
162 SkSpan<const SkPoint> positions,
164 SkSpan<const uint32_t> clusters,
165 SkSpan<const SkVector> scaledRotations);
166
167 const GlyphRunList& setGlyphRunList(
168 const SkTextBlob* blob, const SkRect& bounds, SkPoint origin);
169
170 int fMaxTotalRunSize{0};
172 int fMaxScaledRotations{0};
174
175 std::vector<GlyphRun> fGlyphRunListStorage;
176 std::optional<GlyphRunList> fGlyphRunList; // Defaults to no value;
177
178 // Used as a temporary for preparing using utfN text. This implies that only one run of
179 // glyph ids will ever be needed because blobs are already glyph based.
180 std::vector<SkGlyphID> fScratchGlyphIDs;
181
182};
183} // namespace sktext
184
185#endif // SkGlyphRun_DEFINED
SkTextEncoding
Definition: SkFontTypes.h:11
@ kUTF8
uses bytes to represent UTF-8 or ASCII
Definition: SkFont.h:35
constexpr T * begin() const
Definition: SkSpan_impl.h:90
constexpr T * end() const
Definition: SkSpan_impl.h:91
constexpr bool empty() const
Definition: SkSpan_impl.h:96
constexpr size_t size() const
Definition: SkSpan_impl.h:95
Definition: SkZip.h:25
std::tuple< SkSpan< const SkPoint >, SkSpan< const SkVector > > convertRSXForm(SkSpan< const SkRSXform > xforms)
Definition: GlyphRun.cpp:294
bool empty() const
Definition: GlyphRun.h:150
GlyphRunList makeGlyphRunList(const GlyphRun &run, const SkPaint &paint, SkPoint origin)
Definition: GlyphRun.cpp:189
const GlyphRunList & blobToGlyphRunList(const SkTextBlob &blob, SkPoint origin)
Definition: GlyphRun.cpp:233
const GlyphRunList & textToGlyphRunList(const SkFont &font, const SkPaint &paint, const void *bytes, size_t byteLength, SkPoint origin, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition: GlyphRun.cpp:211
auto end() -> decltype(fGlyphRuns.end())
Definition: GlyphRun.h:121
uint64_t uniqueID() const
Definition: GlyphRun.cpp:67
SkRect sourceBounds() const
Definition: GlyphRun.h:115
const SkTextBlob * blob() const
Definition: GlyphRun.h:117
size_t maxGlyphRunSize() const
Definition: GlyphRun.h:97
auto empty() const -> decltype(fGlyphRuns.empty())
Definition: GlyphRun.h:125
bool hasRSXForm() const
Definition: GlyphRun.h:105
SkPoint origin() const
Definition: GlyphRun.h:114
SkRect sourceBoundsWithOrigin() const
Definition: GlyphRun.h:116
size_t runCount() const
Definition: GlyphRun.h:89
auto begin() -> decltype(fGlyphRuns.begin())
Definition: GlyphRun.h:120
auto end() const -> decltype(std::cend(fGlyphRuns))
Definition: GlyphRun.h:123
auto size() const -> decltype(fGlyphRuns.size())
Definition: GlyphRun.h:124
sk_sp< SkTextBlob > makeBlob() const
Definition: GlyphRun.cpp:88
size_t totalGlyphCount() const
Definition: GlyphRun.h:90
void temporaryShuntBlobNotifyAddedToCache(uint32_t cacheID, SkTextBlob::PurgeDelegate) const
Definition: GlyphRun.cpp:81
bool canCache() const
Definition: GlyphRun.h:88
bool anyRunsLCD() const
Definition: GlyphRun.cpp:72
auto operator[](size_t i) const -> decltype(fGlyphRuns[i])
Definition: GlyphRun.h:126
GlyphRunBuilder * builder() const
Definition: GlyphRun.h:118
GlyphRunList(const SkTextBlob *blob, SkRect bounds, SkPoint origin, SkSpan< const GlyphRun > glyphRunList, GlyphRunBuilder *builder)
Definition: GlyphRun.cpp:46
auto begin() const -> decltype(std::cbegin(fGlyphRuns))
Definition: GlyphRun.h:122
SkSpan< const SkVector > scaledRotations() const
Definition: GlyphRun.h:54
SkSpan< const char > text() const
Definition: GlyphRun.h:53
const SkFont & font() const
Definition: GlyphRun.h:51
SkZip< const SkGlyphID, const SkPoint > source() const
Definition: GlyphRun.h:50
size_t runSize() const
Definition: GlyphRun.h:47
SkSpan< const uint32_t > clusters() const
Definition: GlyphRun.h:52
SkSpan< const SkGlyphID > glyphsIDs() const
Definition: GlyphRun.h:49
SkSpan< const SkPoint > positions() const
Definition: GlyphRun.h:48
GlyphRun(const SkFont &font, SkSpan< const SkPoint > positions, SkSpan< const SkGlyphID > glyphIDs, SkSpan< const char > text, SkSpan< const uint32_t > clusters, SkSpan< const SkVector > scaledRotations)
Definition: GlyphRun.cpp:27
const Paint & paint
Definition: color_source.cc:38
static float max(float r, float g, float b)
Definition: hsl.cpp:49
std::u16string text
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkRSXform > xforms
Definition: SkRecords.h:332
font
Font Metadata and Metrics.
Definition: run.py:1
Definition: ref_ptr.h:256
static constexpr SkRect MakeEmpty()
Definition: SkRect.h:595
constexpr SkRect makeOffset(float dx, float dy) const
Definition: SkRect.h:965