Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | #ifndef TextLine_DEFINED |
| 3 | #define TextLine_DEFINED |
| 4 | |
Ben Wagner | 4ca7a81 | 2020-05-28 13:48:18 -0400 | [diff] [blame] | 5 | #include "include/core/SkPoint.h" |
| 6 | #include "include/core/SkRect.h" |
| 7 | #include "include/core/SkScalar.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 8 | #include "include/private/SkTArray.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 9 | #include "modules/skparagraph/include/DartTypes.h" |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 10 | #include "modules/skparagraph/include/Metrics.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 11 | #include "modules/skparagraph/include/TextStyle.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "modules/skparagraph/src/Run.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 13 | |
Ben Wagner | 4ca7a81 | 2020-05-28 13:48:18 -0400 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <functional> |
Ben Wagner | 0b13cec | 2020-05-18 10:52:23 -0400 | [diff] [blame] | 16 | #include <memory> |
Ben Wagner | 4ca7a81 | 2020-05-28 13:48:18 -0400 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | class SkCanvas; |
| 20 | class SkString; |
Ben Wagner | 0b13cec | 2020-05-18 10:52:23 -0400 | [diff] [blame] | 21 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 22 | namespace skia { |
| 23 | namespace textlayout { |
| 24 | |
Ben Wagner | 4ca7a81 | 2020-05-28 13:48:18 -0400 | [diff] [blame] | 25 | class ParagraphImpl; |
| 26 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 27 | class TextLine { |
| 28 | public: |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 29 | |
| 30 | struct ClipContext { |
| 31 | const Run* run; |
| 32 | size_t pos; |
| 33 | size_t size; |
| 34 | SkScalar fTextShift; // Shifts the text inside the run so it's placed at the right position |
| 35 | SkRect clip; |
| 36 | bool clippingNeeded; |
| 37 | }; |
| 38 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 39 | TextLine() = default; |
Ben Wagner | c087221 | 2020-05-27 22:13:55 -0400 | [diff] [blame] | 40 | TextLine(const TextLine&) = delete; |
| 41 | TextLine& operator=(const TextLine&) = delete; |
| 42 | TextLine(TextLine&&) = default; |
| 43 | TextLine& operator=(TextLine&&) = default; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 44 | ~TextLine() = default; |
| 45 | |
Julia Lavrova | 2aa7245 | 2020-08-03 15:18:41 -0400 | [diff] [blame] | 46 | TextLine(ParagraphImpl* owner, |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 47 | SkVector offset, |
| 48 | SkVector advance, |
| 49 | BlockRange blocks, |
| 50 | TextRange text, |
| 51 | TextRange textWithSpaces, |
| 52 | ClusterRange clusters, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 53 | ClusterRange clustersWithGhosts, |
| 54 | SkScalar widthWithSpaces, |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 55 | InternalLineMetrics sizes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 56 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 57 | TextRange trimmedText() const { return fTextRange; } |
| 58 | TextRange textWithSpaces() const { return fTextWithWhitespacesRange; } |
| 59 | ClusterRange clusters() const { return fClusterRange; } |
| 60 | ClusterRange clustersWithSpaces() { return fGhostClusterRange; } |
| 61 | Run* ellipsis() const { return fEllipsis.get(); } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 62 | InternalLineMetrics sizes() const { return fSizes; } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 63 | bool empty() const { return fTextRange.empty(); } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 64 | |
Julia Lavrova | d3a32c5 | 2020-02-03 09:43:52 -0500 | [diff] [blame] | 65 | SkScalar spacesWidth() { return fWidthWithSpaces - width(); } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 66 | SkScalar height() const { return fAdvance.fY; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 67 | SkScalar width() const { |
| 68 | return fAdvance.fX + (fEllipsis != nullptr ? fEllipsis->fAdvance.fX : 0); |
| 69 | } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 70 | SkScalar shift() const { return fShift; } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 71 | SkVector offset() const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 72 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 73 | SkScalar alphabeticBaseline() const { return fSizes.alphabeticBaseline(); } |
| 74 | SkScalar ideographicBaseline() const { return fSizes.ideographicBaseline(); } |
| 75 | SkScalar baseline() const { return fSizes.baseline(); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 76 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 77 | using RunVisitor = std::function<bool(const Run* run, SkScalar runOffset, TextRange textRange, SkScalar* width)>; |
| 78 | void iterateThroughVisualRuns(bool includingGhostSpaces, const RunVisitor& runVisitor) const; |
| 79 | using RunStyleVisitor = std::function<void(TextRange textRange, const TextStyle& style, const ClipContext& context)>; |
| 80 | SkScalar iterateThroughSingleRunByStyles(const Run* run, SkScalar runOffset, TextRange textRange, |
| 81 | StyleType styleType, const RunStyleVisitor& visitor) const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 82 | |
Julia Lavrova | f4cf893 | 2020-04-22 14:33:20 -0400 | [diff] [blame] | 83 | using ClustersVisitor = std::function<bool(const Cluster* cluster, bool ghost)>; |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 84 | void iterateThroughClustersInGlyphsOrder(bool reverse, bool includeGhosts, const ClustersVisitor& visitor) const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 85 | |
Julia Lavrova | 6bdbd3d | 2020-05-06 12:03:17 -0400 | [diff] [blame] | 86 | void format(TextAlign align, SkScalar maxWidth); |
Julia Lavrova | b6030fb | 2020-09-08 11:01:49 -0400 | [diff] [blame^] | 87 | SkRect paint(SkCanvas* canvas, SkScalar x, SkScalar y); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 88 | |
| 89 | void createEllipsis(SkScalar maxWidth, const SkString& ellipsis, bool ltr); |
| 90 | |
| 91 | // For testing internal structures |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 92 | void scanStyles(StyleType style, const RunStyleVisitor& visitor); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 93 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 94 | void setMaxRunMetrics(const InternalLineMetrics& metrics) { fMaxRunMetrics = metrics; } |
| 95 | InternalLineMetrics getMaxRunMetrics() const { return fMaxRunMetrics; } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 96 | |
Julia Lavrova | 8335ab6 | 2020-04-27 15:49:53 -0400 | [diff] [blame] | 97 | bool isFirstLine(); |
| 98 | bool isLastLine(); |
| 99 | void getRectsForRange(TextRange textRange, RectHeightStyle rectHeightStyle, RectWidthStyle rectWidthStyle, std::vector<TextBox>& boxes); |
| 100 | void getRectsForPlaceholders(std::vector<TextBox>& boxes); |
| 101 | PositionWithAffinity getGlyphPositionAtCoordinate(SkScalar dx); |
| 102 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 103 | ClipContext measureTextInsideOneRun(TextRange textRange, |
| 104 | const Run* run, |
| 105 | SkScalar runOffsetInLine, |
| 106 | SkScalar textOffsetInRunInLine, |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 107 | bool includeGhostSpaces, |
| 108 | bool limitToClusters) const; |
| 109 | |
| 110 | LineMetrics getMetrics() const; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 111 | |
Julia Lavrova | 89e678d | 2020-01-28 10:43:31 -0500 | [diff] [blame] | 112 | SkRect extendHeight(const ClipContext& context) const; |
| 113 | |
Julia Lavrova | 9588a64 | 2020-04-30 11:31:25 -0400 | [diff] [blame] | 114 | SkScalar metricsWithoutMultiplier(TextHeightBehavior correction); |
| 115 | void shiftVertically(SkScalar shift) { fOffset.fY += shift; } |
| 116 | |
Julia Lavrova | 6bdbd3d | 2020-05-06 12:03:17 -0400 | [diff] [blame] | 117 | bool endsWithHardLineBreak() const; |
| 118 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 119 | private: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 120 | |
Ben Wagner | 0b13cec | 2020-05-18 10:52:23 -0400 | [diff] [blame] | 121 | std::unique_ptr<Run> shapeEllipsis(const SkString& ellipsis, Run* run); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 122 | void justify(SkScalar maxWidth); |
| 123 | |
Julia Lavrova | b6030fb | 2020-09-08 11:01:49 -0400 | [diff] [blame^] | 124 | SkRect paintText(SkCanvas* canvas, SkScalar x, SkScalar y, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
| 125 | void paintBackground(SkCanvas* canvas, SkScalar x, SkScalar y, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
| 126 | SkRect paintShadow(SkCanvas* canvas, SkScalar x, SkScalar y, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
| 127 | void paintDecorations(SkCanvas* canvas, SkScalar x, SkScalar y, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 128 | |
Julia Lavrova | a0708e8 | 2020-02-28 12:14:58 -0500 | [diff] [blame] | 129 | void shiftCluster(const Cluster* cluster, SkScalar shift, SkScalar prevShift); |
| 130 | |
Julia Lavrova | 2aa7245 | 2020-08-03 15:18:41 -0400 | [diff] [blame] | 131 | ParagraphImpl* fOwner; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 132 | BlockRange fBlockRange; |
| 133 | TextRange fTextRange; |
| 134 | TextRange fTextWithWhitespacesRange; |
| 135 | ClusterRange fClusterRange; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 136 | ClusterRange fGhostClusterRange; |
Ben Wagner | c087221 | 2020-05-27 22:13:55 -0400 | [diff] [blame] | 137 | // Avoid the malloc/free in the common case of one run per line |
| 138 | SkSTArray<1, size_t, true> fRunsInVisualOrder; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 139 | SkVector fAdvance; // Text size |
| 140 | SkVector fOffset; // Text position |
Julia Lavrova | 6bdbd3d | 2020-05-06 12:03:17 -0400 | [diff] [blame] | 141 | SkScalar fShift; // Let right |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 142 | SkScalar fWidthWithSpaces; |
Ben Wagner | 0b13cec | 2020-05-18 10:52:23 -0400 | [diff] [blame] | 143 | std::unique_ptr<Run> fEllipsis; // In case the line ends with the ellipsis |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 144 | InternalLineMetrics fSizes; // Line metrics as a max of all run metrics and struts |
| 145 | InternalLineMetrics fMaxRunMetrics; // No struts - need it for GetRectForRange(max height) |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 146 | bool fHasBackground; |
| 147 | bool fHasShadows; |
| 148 | bool fHasDecorations; |
Julia Lavrova | dd1de25 | 2020-05-08 11:53:19 -0400 | [diff] [blame] | 149 | |
| 150 | LineMetricStyle fAscentStyle; |
| 151 | LineMetricStyle fDescentStyle; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 152 | }; |
| 153 | } // namespace textlayout |
| 154 | } // namespace skia |
| 155 | |
| 156 | #endif // TextLine_DEFINED |