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 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 5 | #include "include/core/SkCanvas.h" |
| 6 | #include "include/private/SkTArray.h" |
| 7 | #include "include/private/SkTHash.h" |
| 8 | #include "modules/skparagraph/include/DartTypes.h" |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 9 | #include "modules/skparagraph/include/Metrics.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 10 | #include "modules/skparagraph/include/TextStyle.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 11 | #include "modules/skparagraph/src/Run.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 12 | #include "src/core/SkSpan.h" |
| 13 | |
| 14 | namespace skia { |
| 15 | namespace textlayout { |
| 16 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 17 | class TextLine { |
| 18 | public: |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 19 | |
| 20 | struct ClipContext { |
| 21 | const Run* run; |
| 22 | size_t pos; |
| 23 | size_t size; |
| 24 | SkScalar fTextShift; // Shifts the text inside the run so it's placed at the right position |
| 25 | SkRect clip; |
| 26 | bool clippingNeeded; |
| 27 | }; |
| 28 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 29 | TextLine() = default; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 30 | ~TextLine() = default; |
| 31 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 32 | TextLine(ParagraphImpl* master, |
| 33 | SkVector offset, |
| 34 | SkVector advance, |
| 35 | BlockRange blocks, |
| 36 | TextRange text, |
| 37 | TextRange textWithSpaces, |
| 38 | ClusterRange clusters, |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 39 | ClusterRange clustersWithGhosts, |
| 40 | SkScalar widthWithSpaces, |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 41 | InternalLineMetrics sizes); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 42 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 43 | void setMaster(ParagraphImpl* master) { fMaster = master; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 44 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 45 | TextRange trimmedText() const { return fTextRange; } |
| 46 | TextRange textWithSpaces() const { return fTextWithWhitespacesRange; } |
| 47 | ClusterRange clusters() const { return fClusterRange; } |
| 48 | ClusterRange clustersWithSpaces() { return fGhostClusterRange; } |
| 49 | Run* ellipsis() const { return fEllipsis.get(); } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 50 | InternalLineMetrics sizes() const { return fSizes; } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 51 | bool empty() const { return fTextRange.empty(); } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 52 | |
Julia Lavrova | d3a32c5 | 2020-02-03 09:43:52 -0500 | [diff] [blame] | 53 | SkScalar spacesWidth() { return fWidthWithSpaces - width(); } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 54 | SkScalar height() const { return fAdvance.fY; } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 55 | SkScalar width() const { |
| 56 | return fAdvance.fX + (fEllipsis != nullptr ? fEllipsis->fAdvance.fX : 0); |
| 57 | } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 58 | SkScalar shift() const { return fShift; } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 59 | SkVector offset() const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 60 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 61 | SkScalar alphabeticBaseline() const { return fSizes.alphabeticBaseline(); } |
| 62 | SkScalar ideographicBaseline() const { return fSizes.ideographicBaseline(); } |
| 63 | SkScalar baseline() const { return fSizes.baseline(); } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 64 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 65 | using RunVisitor = std::function<bool(const Run* run, SkScalar runOffset, TextRange textRange, SkScalar* width)>; |
| 66 | void iterateThroughVisualRuns(bool includingGhostSpaces, const RunVisitor& runVisitor) const; |
| 67 | using RunStyleVisitor = std::function<void(TextRange textRange, const TextStyle& style, const ClipContext& context)>; |
| 68 | SkScalar iterateThroughSingleRunByStyles(const Run* run, SkScalar runOffset, TextRange textRange, |
| 69 | StyleType styleType, const RunStyleVisitor& visitor) const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 70 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 71 | |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 72 | using ClustersVisitor = std::function<bool(const Cluster* cluster, ClusterIndex index, bool leftToRight, bool ghost)>; |
| 73 | void iterateThroughClustersInGlyphsOrder(bool reverse, bool includeGhosts, const ClustersVisitor& visitor) const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 74 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 75 | void format(TextAlign effectiveAlign, SkScalar maxWidth); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 76 | void paint(SkCanvas* canvas); |
| 77 | |
| 78 | void createEllipsis(SkScalar maxWidth, const SkString& ellipsis, bool ltr); |
| 79 | |
| 80 | // For testing internal structures |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 81 | void scanStyles(StyleType style, const RunStyleVisitor& visitor); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 82 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 83 | TextAlign assumedTextAlign() const; |
| 84 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 85 | void setMaxRunMetrics(const InternalLineMetrics& metrics) { fMaxRunMetrics = metrics; } |
| 86 | InternalLineMetrics getMaxRunMetrics() const { return fMaxRunMetrics; } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 87 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 88 | ClipContext measureTextInsideOneRun(TextRange textRange, |
| 89 | const Run* run, |
| 90 | SkScalar runOffsetInLine, |
| 91 | SkScalar textOffsetInRunInLine, |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 92 | bool includeGhostSpaces, |
| 93 | bool limitToClusters) const; |
| 94 | |
| 95 | LineMetrics getMetrics() const; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 96 | |
Julia Lavrova | 9bd8351 | 2020-01-15 14:46:35 -0500 | [diff] [blame] | 97 | SkRect calculateBoundaries(); |
| 98 | |
Julia Lavrova | 89e678d | 2020-01-28 10:43:31 -0500 | [diff] [blame] | 99 | SkRect extendHeight(const ClipContext& context) const; |
| 100 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 101 | private: |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 102 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 103 | Run* shapeEllipsis(const SkString& ellipsis, Run* run); |
| 104 | void justify(SkScalar maxWidth); |
| 105 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 106 | void paintText(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
| 107 | void paintBackground(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
| 108 | void paintShadow(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
| 109 | void paintDecorations(SkCanvas* canvas, TextRange textRange, const TextStyle& style, const ClipContext& context) const; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 110 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 111 | void computeDecorationPaint(SkPaint& paint, SkRect clip, const TextStyle& style, SkScalar thickness, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 112 | SkPath& path) const; |
| 113 | |
| 114 | bool contains(const Cluster* cluster) const { |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 115 | return fTextRange.contains(cluster->textRange()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 118 | ParagraphImpl* fMaster; |
| 119 | BlockRange fBlockRange; |
| 120 | TextRange fTextRange; |
| 121 | TextRange fTextWithWhitespacesRange; |
| 122 | ClusterRange fClusterRange; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 123 | ClusterRange fGhostClusterRange; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 124 | |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 125 | SkTArray<size_t, true> fRunsInVisualOrder; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 126 | SkVector fAdvance; // Text size |
| 127 | SkVector fOffset; // Text position |
| 128 | SkScalar fShift; // Left right |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 129 | SkScalar fWidthWithSpaces; |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 130 | std::shared_ptr<Run> fEllipsis; // In case the line ends with the ellipsis |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 131 | InternalLineMetrics fSizes; // Line metrics as a max of all run metrics and struts |
| 132 | InternalLineMetrics fMaxRunMetrics; // No struts - need it for GetRectForRange(max height) |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 133 | bool fHasBackground; |
| 134 | bool fHasShadows; |
| 135 | bool fHasDecorations; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 136 | }; |
| 137 | } // namespace textlayout |
| 138 | } // namespace skia |
| 139 | |
| 140 | #endif // TextLine_DEFINED |