Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | #ifndef ParagraphImpl_DEFINED |
| 3 | #define ParagraphImpl_DEFINED |
| 4 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 5 | #include "include/core/SkPicture.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 6 | #include "include/private/SkMutex.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 7 | #include "include/private/SkTHash.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 8 | #include "modules/skparagraph/include/Paragraph.h" |
| 9 | #include "modules/skparagraph/include/ParagraphStyle.h" |
| 10 | #include "modules/skparagraph/include/TextStyle.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 11 | #include "modules/skparagraph/src/FontResolver.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "modules/skparagraph/src/Run.h" |
| 13 | #include "modules/skparagraph/src/TextLine.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 14 | |
| 15 | class SkCanvas; |
| 16 | |
| 17 | namespace skia { |
| 18 | namespace textlayout { |
| 19 | |
| 20 | template <typename T> bool operator==(const SkSpan<T>& a, const SkSpan<T>& b) { |
| 21 | return a.size() == b.size() && a.begin() == b.begin(); |
| 22 | } |
| 23 | |
| 24 | template <typename T> bool operator<=(const SkSpan<T>& a, const SkSpan<T>& b) { |
| 25 | return a.begin() >= b.begin() && a.end() <= b.end(); |
| 26 | } |
| 27 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 28 | template <typename TStyle> |
| 29 | struct StyleBlock { |
| 30 | StyleBlock() : fRange(EMPTY_RANGE), fStyle() { } |
| 31 | StyleBlock(size_t start, size_t end, const TStyle& style) : fRange(start, end), fStyle(style) {} |
| 32 | StyleBlock(TextRange textRange, const TStyle& style) : fRange(textRange), fStyle(style) {} |
| 33 | void add(TextRange tail) { |
| 34 | SkASSERT(fRange.end == tail.start); |
| 35 | fRange = TextRange(fRange.start, fRange.start + fRange.width() + tail.width()); |
| 36 | } |
| 37 | TextRange fRange; |
| 38 | TStyle fStyle; |
| 39 | }; |
| 40 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 41 | class ParagraphImpl final : public Paragraph { |
| 42 | public: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 43 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 44 | ParagraphImpl(const SkString& text, |
| 45 | ParagraphStyle style, |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 46 | SkTArray<Block, true> blocks, |
Julia Lavrova | 35f8822 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 47 | sk_sp<FontCollection> fonts); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 48 | |
| 49 | ParagraphImpl(const std::u16string& utf16text, |
| 50 | ParagraphStyle style, |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 51 | SkTArray<Block, true> blocks, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 52 | sk_sp<FontCollection> fonts); |
| 53 | ~ParagraphImpl() override; |
| 54 | |
| 55 | void layout(SkScalar width) override; |
| 56 | void paint(SkCanvas* canvas, SkScalar x, SkScalar y) override; |
| 57 | std::vector<TextBox> getRectsForRange(unsigned start, |
| 58 | unsigned end, |
| 59 | RectHeightStyle rectHeightStyle, |
| 60 | RectWidthStyle rectWidthStyle) override; |
| 61 | PositionWithAffinity getGlyphPositionAtCoordinate(SkScalar dx, SkScalar dy) override; |
| 62 | SkRange<size_t> getWordBoundary(unsigned offset) override; |
| 63 | bool didExceedMaxLines() override { |
| 64 | return !fParagraphStyle.unlimited_lines() && fLines.size() > fParagraphStyle.getMaxLines(); |
| 65 | } |
| 66 | |
| 67 | size_t lineNumber() override { return fLines.size(); } |
| 68 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 69 | TextLine& addLine(SkVector offset, SkVector advance, TextRange text, TextRange textWithSpaces, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame^] | 70 | ClusterRange clusters, ClusterRange clustersWithGhosts, SkScalar AddLineToParagraph, |
| 71 | LineMetrics sizes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 72 | |
| 73 | SkSpan<const char> text() const { return fTextSpan; } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 74 | InternalState state() const { return fState; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 75 | SkSpan<Run> runs() { return SkSpan<Run>(fRuns.data(), fRuns.size()); } |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 76 | const SkTArray<FontDescr>& switches() const { return fFontResolver.switches(); } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 77 | SkSpan<Block> styles() { |
| 78 | return SkSpan<Block>(fTextStyles.data(), fTextStyles.size()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 79 | } |
| 80 | SkSpan<TextLine> lines() { return SkSpan<TextLine>(fLines.data(), fLines.size()); } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame^] | 81 | const ParagraphStyle& paragraphStyle() const { return fParagraphStyle; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 82 | SkSpan<Cluster> clusters() { return SkSpan<Cluster>(fClusters.begin(), fClusters.size()); } |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 83 | sk_sp<FontCollection> fontCollection() const { return fFontCollection; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 84 | void formatLines(SkScalar maxWidth); |
| 85 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 86 | void shiftCluster(ClusterIndex index, SkScalar shift) { |
| 87 | auto& cluster = fClusters[index]; |
| 88 | auto& run = fRunShifts[cluster.runIndex()]; |
| 89 | for (size_t pos = cluster.startPos(); pos < cluster.endPos(); ++pos) { |
| 90 | run.fShifts[pos] = shift; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | SkScalar posShift(RunIndex index, size_t pos) const { |
| 95 | if (fRunShifts.count() == 0) return 0.0; |
| 96 | return fRunShifts[index].fShifts[pos]; |
| 97 | } |
| 98 | |
| 99 | SkScalar lineShift(size_t index) { return fLines[index].shift(); } |
| 100 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 101 | bool strutEnabled() const { return paragraphStyle().getStrutStyle().getStrutEnabled(); } |
| 102 | bool strutForceHeight() const { |
| 103 | return paragraphStyle().getStrutStyle().getForceStrutHeight(); |
| 104 | } |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame^] | 105 | bool strutHeightOverride() const { |
| 106 | return paragraphStyle().getStrutStyle().getHeightOverride(); |
| 107 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 108 | LineMetrics strutMetrics() const { return fStrutMetrics; } |
| 109 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 110 | Measurement measurement() { |
| 111 | return { |
| 112 | fAlphabeticBaseline, |
| 113 | fIdeographicBaseline, |
| 114 | fHeight, |
| 115 | fWidth, |
| 116 | fMaxIntrinsicWidth, |
| 117 | fMinIntrinsicWidth, |
| 118 | }; |
| 119 | } |
| 120 | void setMeasurement(Measurement m) { |
| 121 | fAlphabeticBaseline = m.fAlphabeticBaseline; |
| 122 | fIdeographicBaseline = m.fIdeographicBaseline; |
| 123 | fHeight = m.fHeight; |
| 124 | fWidth = m.fWidth; |
| 125 | fMaxIntrinsicWidth = m.fMaxIntrinsicWidth; |
| 126 | fMinIntrinsicWidth = m.fMinIntrinsicWidth; |
| 127 | } |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 128 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 129 | SkSpan<const char> text(TextRange textRange); |
| 130 | SkSpan<Cluster> clusters(ClusterRange clusterRange); |
| 131 | Cluster& cluster(ClusterIndex clusterIndex); |
| 132 | Run& run(RunIndex runIndex); |
| 133 | SkSpan<Block> blocks(BlockRange blockRange); |
| 134 | Block& block(BlockIndex blockIndex); |
| 135 | |
| 136 | void markDirty() override { fState = kUnknown; } |
Julia Lavrova | b7b0b3a | 2019-07-30 13:32:08 -0400 | [diff] [blame] | 137 | FontResolver& getResolver() { return fFontResolver; } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 138 | void setState(InternalState state); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 139 | sk_sp<SkPicture> getPicture() { return fPicture; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 140 | |
| 141 | void resetContext(); |
| 142 | void resolveStrut(); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 143 | void resetRunShifts(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 144 | void buildClusterTable(); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 145 | void markLineBreaks(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 146 | bool shapeTextIntoEndlessLine(); |
| 147 | void breakShapedTextIntoLines(SkScalar maxWidth); |
| 148 | void paintLinesIntoPicture(); |
| 149 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 150 | private: |
| 151 | friend class ParagraphBuilder; |
| 152 | friend class ParagraphCacheKey; |
| 153 | friend class ParagraphCacheValue; |
| 154 | friend class ParagraphCache; |
| 155 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame^] | 156 | friend class TextWrapper; |
| 157 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 158 | BlockRange findAllBlocks(TextRange textRange); |
| 159 | void extractStyles(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 160 | |
| 161 | // Input |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 162 | SkTArray<StyleBlock<SkScalar>> fLetterSpaceStyles; |
| 163 | SkTArray<StyleBlock<SkScalar>> fWordSpaceStyles; |
| 164 | SkTArray<StyleBlock<SkPaint>> fBackgroundStyles; |
| 165 | SkTArray<StyleBlock<SkPaint>> fForegroundStyles; |
| 166 | SkTArray<StyleBlock<std::vector<TextShadow>>> fShadowStyles; |
| 167 | SkTArray<StyleBlock<Decoration>> fDecorationStyles; |
| 168 | SkTArray<Block, true> fTextStyles; // TODO: take out only the font stuff |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 169 | SkString fText; |
| 170 | SkSpan<const char> fTextSpan; |
| 171 | |
| 172 | // Internal structures |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 173 | InternalState fState; |
| 174 | SkTArray<Run> fRuns; // kShaped |
| 175 | SkTArray<Cluster, true> fClusters; // kClusterized (cached: text, word spacing, letter spacing, resolved fonts) |
| 176 | |
| 177 | SkTArray<RunShifts, true> fRunShifts; |
| 178 | SkTArray<TextLine, true> fLines; // kFormatted (cached: width, max lines, ellipsis, text align) |
| 179 | sk_sp<SkPicture> fPicture; // kRecorded (cached: text styles) |
| 180 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 181 | LineMetrics fStrutMetrics; |
Julia Lavrova | 35f8822 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 182 | FontResolver fFontResolver; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 183 | |
Julia Lavrova | 9af5cc4 | 2019-06-19 13:32:01 -0400 | [diff] [blame] | 184 | SkScalar fOldWidth; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 185 | SkScalar fOldHeight; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame^] | 186 | SkScalar fMaxWidthWithTrailingSpaces; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 187 | }; |
| 188 | } // namespace textlayout |
| 189 | } // namespace skia |
| 190 | |
| 191 | #endif // ParagraphImpl_DEFINED |