blob: 36b8abf53ada9fc3b795c28bf5f68d5442b060a0 [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 Lavrovac88a3bc2020-01-23 10:16:26 -050052struct BidiRegion {
53 BidiRegion(size_t start, size_t end, uint8_t dir)
54 : text(start, end), direction(dir) { }
55 TextRange text;
56 uint8_t direction;
57};
58
Julia Lavrova916a9042019-08-08 16:51:27 -040059class TextBreaker {
60public:
61 TextBreaker() : fInitialized(false), fPos(-1) {}
62
63 bool initialize(SkSpan<const char> text, UBreakIteratorType type);
64
65 bool initialized() const { return fInitialized; }
66
67 size_t first() {
68 fPos = ubrk_first(fIterator.get());
69 return eof() ? fSize : fPos;
70 }
71
72 size_t next() {
73 fPos = ubrk_next(fIterator.get());
74 return eof() ? fSize : fPos;
75 }
76
77 size_t preceding(size_t offset) {
78 auto pos = ubrk_preceding(fIterator.get(), offset);
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040079 return pos == icu::BreakIterator::DONE ? 0 : pos;
Julia Lavrova916a9042019-08-08 16:51:27 -040080 }
81
82 size_t following(size_t offset) {
83 auto pos = ubrk_following(fIterator.get(), offset);
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040084 return pos == icu::BreakIterator::DONE ? fSize : pos;
Julia Lavrova916a9042019-08-08 16:51:27 -040085 }
86
87 int32_t status() { return ubrk_getRuleStatus(fIterator.get()); }
88
89 bool eof() { return fPos == icu::BreakIterator::DONE; }
90
91private:
Ben Wagner723a8772019-08-16 11:36:58 -040092 std::unique_ptr<UBreakIterator, SkFunctionWrapper<decltype(ubrk_close), ubrk_close>> fIterator;
Julia Lavrova916a9042019-08-08 16:51:27 -040093 bool fInitialized;
94 int32_t fPos;
95 size_t fSize;
96};
97
Julia Lavrovaa3552c52019-05-30 16:12:56 -040098class ParagraphImpl final : public Paragraph {
Julia Lavrovac2228562019-08-08 16:51:27 -040099
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400100public:
Julia Lavrova5207f352019-06-21 12:22:32 -0400101
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400102 ParagraphImpl(const SkString& text,
103 ParagraphStyle style,
Julia Lavrova5207f352019-06-21 12:22:32 -0400104 SkTArray<Block, true> blocks,
Julia Lavrova916a9042019-08-08 16:51:27 -0400105 SkTArray<Placeholder, true> placeholders,
Julia Lavrova35f88222019-06-21 12:22:32 -0400106 sk_sp<FontCollection> fonts);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400107
108 ParagraphImpl(const std::u16string& utf16text,
Julia Lavrova916a9042019-08-08 16:51:27 -0400109 ParagraphStyle style,
110 SkTArray<Block, true> blocks,
111 SkTArray<Placeholder, true> placeholders,
112 sk_sp<FontCollection> fonts);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400113 ~ParagraphImpl() override;
114
115 void layout(SkScalar width) override;
116 void paint(SkCanvas* canvas, SkScalar x, SkScalar y) override;
117 std::vector<TextBox> getRectsForRange(unsigned start,
118 unsigned end,
119 RectHeightStyle rectHeightStyle,
120 RectWidthStyle rectWidthStyle) override;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400121 std::vector<TextBox> getRectsForPlaceholders() override;
122 void getLineMetrics(std::vector<LineMetrics>&) override;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400123 PositionWithAffinity getGlyphPositionAtCoordinate(SkScalar dx, SkScalar dy) override;
124 SkRange<size_t> getWordBoundary(unsigned offset) override;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400125
126 size_t lineNumber() override { return fLines.size(); }
127
Julia Lavrova5207f352019-06-21 12:22:32 -0400128 TextLine& addLine(SkVector offset, SkVector advance, TextRange text, TextRange textWithSpaces,
Julia Lavrova2ea20ea2020-01-22 10:56:53 -0500129 ClusterRange clusters, ClusterRange clustersWithGhosts, SkScalar widthWithSpaces,
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400130 InternalLineMetrics sizes);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400131
Julia Lavrovaa40db422019-08-21 13:49:15 -0400132 SkSpan<const char> text() const { return SkSpan<const char>(fText.c_str(), fText.size()); }
Julia Lavrova5207f352019-06-21 12:22:32 -0400133 InternalState state() const { return fState; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400134 SkSpan<Run> runs() { return SkSpan<Run>(fRuns.data(), fRuns.size()); }
Julia Lavrova5207f352019-06-21 12:22:32 -0400135 SkSpan<Block> styles() {
136 return SkSpan<Block>(fTextStyles.data(), fTextStyles.size());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400137 }
Julia Lavrovac0360582020-02-05 10:17:53 -0500138 SkSpan<Placeholder> placeholders() {
139 return SkSpan<Placeholder>(fPlaceholders.data(), fPlaceholders.size());
140 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400141 SkSpan<TextLine> lines() { return SkSpan<TextLine>(fLines.data(), fLines.size()); }
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400142 const ParagraphStyle& paragraphStyle() const { return fParagraphStyle; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400143 SkSpan<Cluster> clusters() { return SkSpan<Cluster>(fClusters.begin(), fClusters.size()); }
Julia Lavrovab7b0b3a2019-07-30 13:32:08 -0400144 sk_sp<FontCollection> fontCollection() const { return fFontCollection; }
Julia Lavrovaa169b002020-03-23 13:39:52 -0400145 const SkTHashSet<size_t>& graphemes() const { return fGraphemes; }
Julia Lavrova8335ab62020-04-27 15:49:53 -0400146 SkSpan<Codepoint> codepoints(){ return SkSpan<Codepoint>(fCodePoints.begin(), fCodePoints.size()); }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400147 void formatLines(SkScalar maxWidth);
148
149 bool strutEnabled() const { return paragraphStyle().getStrutStyle().getStrutEnabled(); }
150 bool strutForceHeight() const {
151 return paragraphStyle().getStrutStyle().getForceStrutHeight();
152 }
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400153 bool strutHeightOverride() const {
154 return paragraphStyle().getStrutStyle().getHeightOverride();
155 }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400156 InternalLineMetrics strutMetrics() const { return fStrutMetrics; }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400157
Julia Lavrova5207f352019-06-21 12:22:32 -0400158 SkSpan<const char> text(TextRange textRange);
159 SkSpan<Cluster> clusters(ClusterRange clusterRange);
160 Cluster& cluster(ClusterIndex clusterIndex);
161 Run& run(RunIndex runIndex);
Julia Lavrova526df262019-08-21 17:49:44 -0400162 Run& runByCluster(ClusterIndex clusterIndex);
Julia Lavrova5207f352019-06-21 12:22:32 -0400163 SkSpan<Block> blocks(BlockRange blockRange);
164 Block& block(BlockIndex blockIndex);
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400165 SkTArray<ResolvedFontDescriptor> resolvedFonts() const { return fFontSwitches; }
Julia Lavrova5207f352019-06-21 12:22:32 -0400166
167 void markDirty() override { fState = kUnknown; }
Julia Lavrova3281b962019-12-02 11:32:25 -0500168
169 int32_t unresolvedGlyphs() override;
170
Julia Lavrova5207f352019-06-21 12:22:32 -0400171 void setState(InternalState state);
Julia Lavrova5207f352019-06-21 12:22:32 -0400172 sk_sp<SkPicture> getPicture() { return fPicture; }
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400173 SkRect getBoundaries() const { return fOrigin; }
Julia Lavrova916a9042019-08-08 16:51:27 -0400174
Julia Lavrova8335ab62020-04-27 15:49:53 -0400175 SkScalar widthWithTrailingSpaces() { return fMaxWidthWithTrailingSpaces; }
176
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400177 void resetContext();
178 void resolveStrut();
179 void buildClusterTable();
Julia Lavrova5207f352019-06-21 12:22:32 -0400180 void markLineBreaks();
Julia Lavrova8335ab62020-04-27 15:49:53 -0400181 void spaceGlyphs();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400182 bool shapeTextIntoEndlessLine();
183 void breakShapedTextIntoLines(SkScalar maxWidth);
184 void paintLinesIntoPicture();
185
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400186 void updateTextAlign(TextAlign textAlign) override;
187 void updateText(size_t from, SkString text) override;
188 void updateFontSize(size_t from, size_t to, SkScalar fontSize) override;
189 void updateForegroundPaint(size_t from, size_t to, SkPaint paint) override;
190 void updateBackgroundPaint(size_t from, size_t to, SkPaint paint) override;
191
Jason Simmons22bb52e2019-12-05 17:56:59 -0800192 InternalLineMetrics getEmptyMetrics() const { return fEmptyMetrics; }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400193 InternalLineMetrics getStrutMetrics() const { return fStrutMetrics; }
194
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500195 BlockRange findAllBlocks(TextRange textRange);
196
Julia Lavrovaa0708e82020-02-28 12:14:58 -0500197 void resetShifts() {
198 for (auto& run : fRuns) {
199 run.resetJustificationShifts();
200 run.resetShifts();
201 }
202 }
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 Lavrovaa3552c52019-05-30 16:12:56 -0400214
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400215 void markGraphemes16();
Julia Lavrovac2228562019-08-08 16:51:27 -0400216 void markGraphemes();
217
Jason Simmons22bb52e2019-12-05 17:56:59 -0800218 void computeEmptyMetrics();
219
Julia Lavrovac88a3bc2020-01-23 10:16:26 -0500220 bool calculateBidiRegions(SkTArray<BidiRegion>* regions);
221
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400222 // Input
Julia Lavrova5207f352019-06-21 12:22:32 -0400223 SkTArray<StyleBlock<SkScalar>> fLetterSpaceStyles;
224 SkTArray<StyleBlock<SkScalar>> fWordSpaceStyles;
225 SkTArray<StyleBlock<SkPaint>> fBackgroundStyles;
226 SkTArray<StyleBlock<SkPaint>> fForegroundStyles;
227 SkTArray<StyleBlock<std::vector<TextShadow>>> fShadowStyles;
228 SkTArray<StyleBlock<Decoration>> fDecorationStyles;
229 SkTArray<Block, true> fTextStyles; // TODO: take out only the font stuff
Julia Lavrova916a9042019-08-08 16:51:27 -0400230 SkTArray<Placeholder, true> fPlaceholders;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400231 SkString fText;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400232
233 // Internal structures
Julia Lavrova5207f352019-06-21 12:22:32 -0400234 InternalState fState;
Julia Lavrovaa0708e82020-02-28 12:14:58 -0500235 SkTArray<Run, false> fRuns; // kShaped
Julia Lavrova5207f352019-06-21 12:22:32 -0400236 SkTArray<Cluster, true> fClusters; // kClusterized (cached: text, word spacing, letter spacing, resolved fonts)
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400237 SkTArray<Grapheme, true> fGraphemes16;
Julia Lavrovac2228562019-08-08 16:51:27 -0400238 SkTArray<Codepoint, true> fCodePoints;
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400239 SkTHashSet<size_t> fGraphemes;
Julia Lavrova3281b962019-12-02 11:32:25 -0500240 size_t fUnresolvedGlyphs;
Julia Lavrova5207f352019-06-21 12:22:32 -0400241
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