blob: 374a00bb3f11ecb378f366f4980be58f0d05fa55 [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
2#ifndef ParagraphImpl_DEFINED
3#define ParagraphImpl_DEFINED
4
Julia Lavrova916a9042019-08-08 16:51:27 -04005#include <unicode/brkiter.h>
6#include <unicode/ubidi.h>
7#include <unicode/unistr.h>
8#include <unicode/urename.h>
Julia Lavrovaa3552c52019-05-30 16:12:56 -04009#include "include/core/SkPicture.h"
Mike Klein52337de2019-07-25 09:00:52 -050010#include "include/private/SkMutex.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040011#include "include/private/SkTHash.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -040012#include "modules/skparagraph/include/Paragraph.h"
13#include "modules/skparagraph/include/ParagraphStyle.h"
14#include "modules/skparagraph/include/TextStyle.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "modules/skparagraph/src/Run.h"
16#include "modules/skparagraph/src/TextLine.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -040017
18class SkCanvas;
19
20namespace skia {
21namespace textlayout {
22
23template <typename T> bool operator==(const SkSpan<T>& a, const SkSpan<T>& b) {
24 return a.size() == b.size() && a.begin() == b.begin();
25}
26
27template <typename T> bool operator<=(const SkSpan<T>& a, const SkSpan<T>& b) {
28 return a.begin() >= b.begin() && a.end() <= b.end();
29}
30
Julia Lavrova5207f352019-06-21 12:22:32 -040031template <typename TStyle>
32struct StyleBlock {
33 StyleBlock() : fRange(EMPTY_RANGE), fStyle() { }
34 StyleBlock(size_t start, size_t end, const TStyle& style) : fRange(start, end), fStyle(style) {}
35 StyleBlock(TextRange textRange, const TStyle& style) : fRange(textRange), fStyle(style) {}
36 void add(TextRange tail) {
37 SkASSERT(fRange.end == tail.start);
38 fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width());
39 }
40 TextRange fRange;
41 TStyle fStyle;
42};
43
Julia Lavrova2e30fde2019-10-09 09:43:02 -040044struct ResolvedFontDescriptor {
45
46 ResolvedFontDescriptor(TextIndex index, SkFont font)
47 : fFont(font), fTextStart(index) { }
48 SkFont fFont;
49 TextIndex fTextStart;
50};
51
Julia Lavrova916a9042019-08-08 16:51:27 -040052class TextBreaker {
53public:
54 TextBreaker() : fInitialized(false), fPos(-1) {}
55
56 bool initialize(SkSpan<const char> text, UBreakIteratorType type);
57
58 bool initialized() const { return fInitialized; }
59
60 size_t first() {
61 fPos = ubrk_first(fIterator.get());
62 return eof() ? fSize : fPos;
63 }
64
65 size_t next() {
66 fPos = ubrk_next(fIterator.get());
67 return eof() ? fSize : fPos;
68 }
69
70 size_t preceding(size_t offset) {
71 auto pos = ubrk_preceding(fIterator.get(), offset);
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040072 return pos == icu::BreakIterator::DONE ? 0 : pos;
Julia Lavrova916a9042019-08-08 16:51:27 -040073 }
74
75 size_t following(size_t offset) {
76 auto pos = ubrk_following(fIterator.get(), offset);
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040077 return pos == icu::BreakIterator::DONE ? fSize : pos;
Julia Lavrova916a9042019-08-08 16:51:27 -040078 }
79
80 int32_t status() { return ubrk_getRuleStatus(fIterator.get()); }
81
82 bool eof() { return fPos == icu::BreakIterator::DONE; }
83
84private:
Ben Wagner723a8772019-08-16 11:36:58 -040085 std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>> fIterator;
Julia Lavrova916a9042019-08-08 16:51:27 -040086 bool fInitialized;
87 int32_t fPos;
88 size_t fSize;
89};
90
Julia Lavrovaa3552c52019-05-30 16:12:56 -040091class ParagraphImpl final : public Paragraph {
Julia Lavrovac2228562019-08-08 16:51:27 -040092
Julia Lavrovaa3552c52019-05-30 16:12:56 -040093public:
Julia Lavrova5207f352019-06-21 12:22:32 -040094
Julia Lavrovaa3552c52019-05-30 16:12:56 -040095 ParagraphImpl(const SkString& text,
96 ParagraphStyle style,
Julia Lavrova5207f352019-06-21 12:22:32 -040097 SkTArray<Block, true> blocks,
Julia Lavrova916a9042019-08-08 16:51:27 -040098 SkTArray<Placeholder, true> placeholders,
Julia Lavrova35f88222019-06-21 12:22:32 -040099 sk_sp<FontCollection> fonts);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400100
101 ParagraphImpl(const std::u16string& utf16text,
Julia Lavrova916a9042019-08-08 16:51:27 -0400102 ParagraphStyle style,
103 SkTArray<Block, true> blocks,
104 SkTArray<Placeholder, true> placeholders,
105 sk_sp<FontCollection> fonts);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400106 ~ParagraphImpl() override;
107
108 void layout(SkScalar width) override;
109 void paint(SkCanvas* canvas, SkScalar x, SkScalar y) override;
110 std::vector<TextBox> getRectsForRange(unsigned start,
111 unsigned end,
112 RectHeightStyle rectHeightStyle,
113 RectWidthStyle rectWidthStyle) override;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400114 std::vector<TextBox> getRectsForPlaceholders() override;
115 void getLineMetrics(std::vector<LineMetrics>&) override;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400116 PositionWithAffinity getGlyphPositionAtCoordinate(SkScalar dx, SkScalar dy) override;
117 SkRange<size_t> getWordBoundary(unsigned offset) override;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400118
119 size_t lineNumber() override { return fLines.size(); }
120
Julia Lavrova5207f352019-06-21 12:22:32 -0400121 TextLine& addLine(SkVector offset, SkVector advance, TextRange text, TextRange textWithSpaces,
Julia Lavrova2ea20ea2020-01-22 10:56:53 -0500122 ClusterRange clusters, ClusterRange clustersWithGhosts, SkScalar widthWithSpaces,
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400123 InternalLineMetrics sizes);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400124
Julia Lavrovaa40db422019-08-21 13:49:15 -0400125 SkSpan<const char> text() const { return SkSpan<const char>(fText.c_str(), fText.size()); }
Julia Lavrova5207f352019-06-21 12:22:32 -0400126 InternalState state() const { return fState; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400127 SkSpan<Run> runs() { return SkSpan<Run>(fRuns.data(), fRuns.size()); }
Julia Lavrova5207f352019-06-21 12:22:32 -0400128 SkSpan<Block> styles() {
129 return SkSpan<Block>(fTextStyles.data(), fTextStyles.size());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400130 }
131 SkSpan<TextLine> lines() { return SkSpan<TextLine>(fLines.data(), fLines.size()); }
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400132 const ParagraphStyle& paragraphStyle() const { return fParagraphStyle; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400133 SkSpan<Cluster> clusters() { return SkSpan<Cluster>(fClusters.begin(), fClusters.size()); }
Julia Lavrovab7b0b3a2019-07-30 13:32:08 -0400134 sk_sp<FontCollection> fontCollection() const { return fFontCollection; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400135 void formatLines(SkScalar maxWidth);
136
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400137 void shiftCluster(ClusterIndex index, SkScalar shift, SkScalar lastShift) {
Julia Lavrova5207f352019-06-21 12:22:32 -0400138 auto& cluster = fClusters[index];
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400139 auto& runShift = fRunShifts[cluster.runIndex()];
140 auto& run = fRuns[cluster.runIndex()];
141 auto start = cluster.startPos();
142 auto end = cluster.endPos();
143 if (!run.leftToRight()) {
144 runShift.fShifts[start] = lastShift;
145 ++start;
146 ++end;
147 }
148 for (size_t pos = start; pos < end; ++pos) {
149 runShift.fShifts[pos] = shift;
Julia Lavrova5207f352019-06-21 12:22:32 -0400150 }
151 }
152
153 SkScalar posShift(RunIndex index, size_t pos) const {
154 if (fRunShifts.count() == 0) return 0.0;
155 return fRunShifts[index].fShifts[pos];
156 }
157
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400158 bool strutEnabled() const { return paragraphStyle().getStrutStyle().getStrutEnabled(); }
159 bool strutForceHeight() const {
160 return paragraphStyle().getStrutStyle().getForceStrutHeight();
161 }
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400162 bool strutHeightOverride() const {
163 return paragraphStyle().getStrutStyle().getHeightOverride();
164 }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400165 InternalLineMetrics strutMetrics() const { return fStrutMetrics; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400166
Julia Lavrova5207f352019-06-21 12:22:32 -0400167 SkSpan<const char> text(TextRange textRange);
168 SkSpan<Cluster> clusters(ClusterRange clusterRange);
169 Cluster& cluster(ClusterIndex clusterIndex);
170 Run& run(RunIndex runIndex);
Julia Lavrova526df262019-08-21 17:49:44 -0400171 Run& runByCluster(ClusterIndex clusterIndex);
Julia Lavrova5207f352019-06-21 12:22:32 -0400172 SkSpan<Block> blocks(BlockRange blockRange);
173 Block& block(BlockIndex blockIndex);
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400174 SkTArray<ResolvedFontDescriptor> resolvedFonts() const { return fFontSwitches; }
Julia Lavrova5207f352019-06-21 12:22:32 -0400175
176 void markDirty() override { fState = kUnknown; }
Julia Lavrova3281b962019-12-02 11:32:25 -0500177
178 int32_t unresolvedGlyphs() override;
179
Julia Lavrova5207f352019-06-21 12:22:32 -0400180 void setState(InternalState state);
Julia Lavrova5207f352019-06-21 12:22:32 -0400181 sk_sp<SkPicture> getPicture() { return fPicture; }
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400182 SkRect getBoundaries() const { return fOrigin; }
Julia Lavrova916a9042019-08-08 16:51:27 -0400183
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400184 void resetContext();
185 void resolveStrut();
Julia Lavrova5207f352019-06-21 12:22:32 -0400186 void resetRunShifts();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400187 void buildClusterTable();
Julia Lavrova5207f352019-06-21 12:22:32 -0400188 void markLineBreaks();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400189 bool shapeTextIntoEndlessLine();
190 void breakShapedTextIntoLines(SkScalar maxWidth);
191 void paintLinesIntoPicture();
192
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400193 void updateTextAlign(TextAlign textAlign) override;
194 void updateText(size_t from, SkString text) override;
195 void updateFontSize(size_t from, size_t to, SkScalar fontSize) override;
196 void updateForegroundPaint(size_t from, size_t to, SkPaint paint) override;
197 void updateBackgroundPaint(size_t from, size_t to, SkPaint paint) override;
198
Jason Simmons22bb52e2019-12-05 17:56:59 -0800199 InternalLineMetrics getEmptyMetrics() const { return fEmptyMetrics; }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400200 InternalLineMetrics getStrutMetrics() const { return fStrutMetrics; }
201
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500202 BlockRange findAllBlocks(TextRange textRange);
203
Julia Lavrova5207f352019-06-21 12:22:32 -0400204private:
205 friend class ParagraphBuilder;
206 friend class ParagraphCacheKey;
207 friend class ParagraphCacheValue;
208 friend class ParagraphCache;
209
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400210 friend class TextWrapper;
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400211 friend class OneLineShaper;
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400212
Julia Lavrova9bd83512020-01-15 14:46:35 -0500213 void calculateBoundaries();
Julia Lavrova5207f352019-06-21 12:22:32 -0400214 void extractStyles();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400215
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400216 void markGraphemes16();
Julia Lavrovac2228562019-08-08 16:51:27 -0400217 void markGraphemes();
218
Jason Simmons22bb52e2019-12-05 17:56:59 -0800219 void computeEmptyMetrics();
220
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400221 // Input
Julia Lavrova5207f352019-06-21 12:22:32 -0400222 SkTArray<StyleBlock<SkScalar>> fLetterSpaceStyles;
223 SkTArray<StyleBlock<SkScalar>> fWordSpaceStyles;
224 SkTArray<StyleBlock<SkPaint>> fBackgroundStyles;
225 SkTArray<StyleBlock<SkPaint>> fForegroundStyles;
226 SkTArray<StyleBlock<std::vector<TextShadow>>> fShadowStyles;
227 SkTArray<StyleBlock<Decoration>> fDecorationStyles;
228 SkTArray<Block, true> fTextStyles; // TODO: take out only the font stuff
Julia Lavrova916a9042019-08-08 16:51:27 -0400229 SkTArray<Placeholder, true> fPlaceholders;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400230 SkString fText;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400231
232 // Internal structures
Julia Lavrova5207f352019-06-21 12:22:32 -0400233 InternalState fState;
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400234 SkTArray<Run, false> fRuns; // kShaped
Julia Lavrova5207f352019-06-21 12:22:32 -0400235 SkTArray<Cluster, true> fClusters; // kClusterized (cached: text, word spacing, letter spacing, resolved fonts)
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400236 SkTArray<Grapheme, true> fGraphemes16;
Julia Lavrovac2228562019-08-08 16:51:27 -0400237 SkTArray<Codepoint, true> fCodePoints;
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400238 SkTHashSet<size_t> fGraphemes;
Julia Lavrova3281b962019-12-02 11:32:25 -0500239 size_t fUnresolvedGlyphs;
Julia Lavrova5207f352019-06-21 12:22:32 -0400240
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400241 SkTArray<RunShifts, false> fRunShifts;
Julia Lavrova5207f352019-06-21 12:22:32 -0400242 SkTArray<TextLine, true> fLines; // kFormatted (cached: width, max lines, ellipsis, text align)
243 sk_sp<SkPicture> fPicture; // kRecorded (cached: text styles)
244
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400245 SkTArray<ResolvedFontDescriptor> fFontSwitches;
246
Jason Simmons22bb52e2019-12-05 17:56:59 -0800247 InternalLineMetrics fEmptyMetrics;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400248 InternalLineMetrics fStrutMetrics;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400249
Julia Lavrova9af5cc42019-06-19 13:32:01 -0400250 SkScalar fOldWidth;
Julia Lavrova5207f352019-06-21 12:22:32 -0400251 SkScalar fOldHeight;
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400252 SkScalar fMaxWidthWithTrailingSpaces;
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400253 SkRect fOrigin;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400254 std::vector<size_t> fWords;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400255};
256} // namespace textlayout
257} // namespace skia
258
259#endif // ParagraphImpl_DEFINED