1 // Copyright 2019 Google LLC.
2
3 #ifndef DartTypes_DEFINED
4 #define DartTypes_DEFINED
5
6 #include "include/core/SkRect.h"
7 #include "include/core/SkTypes.h"
8
9 #include <algorithm>
10 #include <iterator>
11 #include <limits>
12
13 namespace skia {
14 namespace textlayout {
15
16 enum Affinity { kUpstream, kDownstream };
17
18 enum class RectHeightStyle {
19 // Provide tight bounding boxes that fit heights per run.
20 kTight,
21
22 // The height of the boxes will be the maximum height of all runs in the
23 // line. All rects in the same line will be the same height.
24 kMax,
25
26 // Extends the top and/or bottom edge of the bounds to fully cover any line
27 // spacing. The top edge of each line should be the same as the bottom edge
28 // of the line above. There should be no gaps in vertical coverage given any
29 // ParagraphStyle line_height.
30 //
31 // The top and bottom of each rect will cover half of the
32 // space above and half of the space below the line.
33 kIncludeLineSpacingMiddle,
34 // The line spacing will be added to the top of the rect.
35 kIncludeLineSpacingTop,
36 // The line spacing will be added to the bottom of the rect.
37 kIncludeLineSpacingBottom,
38 //
39 kStrut
40 };
41
42 enum class RectWidthStyle {
43 // Provide tight bounding boxes that fit widths to the runs of each line
44 // independently.
45 kTight,
46
47 // Extends the width of the last rect of each line to match the position of
48 // the widest rect over all the lines.
49 kMax
50 };
51
52 enum class TextAlign {
53 kLeft,
54 kRight,
55 kCenter,
56 kJustify,
57 kStart,
58 kEnd,
59 };
60
61 enum class TextDirection {
62 kRtl,
63 kLtr,
64 };
65
66 struct PositionWithAffinity {
67 int32_t position;
68 Affinity affinity;
69
PositionWithAffinityPositionWithAffinity70 PositionWithAffinity() : position(0), affinity(kDownstream) {}
PositionWithAffinityPositionWithAffinity71 PositionWithAffinity(int32_t p, Affinity a) : position(p), affinity(a) {}
72 };
73
74 struct TextBox {
75 SkRect rect;
76 TextDirection direction;
77
TextBoxTextBox78 TextBox(SkRect r, TextDirection d) : rect(r), direction(d) {}
79 };
80
81 // -------------------------------------------------------------------
82 // --- Reversed iterable
83
84 template<typename C, typename UnaryFunction>
directional_for_each(C & c,bool forwards,UnaryFunction f)85 UnaryFunction directional_for_each(C& c, bool forwards, UnaryFunction f) {
86 return forwards
87 ? std::for_each(std::begin(c), std::end(c), f)
88 : std::for_each(std::rbegin(c), std::rend(c), f);
89 }
90
91 const size_t EMPTY_INDEX = std::numeric_limits<size_t>::max();
92 template <typename T> struct SkRange {
SkRangeSkRange93 SkRange() : start(), end() {}
SkRangeSkRange94 SkRange(T s, T e) : start(s), end(e) {}
95
96 using SignedT = std::make_signed_t<T>;
97
98 T start, end;
99
100 bool operator==(const SkRange<T>& other) const {
101 return start == other.start && end == other.end;
102 }
103
widthSkRange104 T width() const { return end - start; }
105
ShiftSkRange106 void Shift(SignedT delta) {
107 start += delta;
108 end += delta;
109 }
110
containsSkRange111 bool contains(SkRange<size_t> other) const {
112 return start <= other.start && end >= other.end;
113 }
114
intersectsSkRange115 bool intersects(SkRange<size_t> other) const {
116 return std::max(start, other.start) <= std::min(end, other.end);
117 }
118
intersectionSkRange119 SkRange<size_t> intersection(SkRange<size_t> other) const {
120 return SkRange<size_t>(std::max(start, other.start), std::min(end, other.end));
121 }
122
emptySkRange123 bool empty() const {
124 return start == EMPTY_INDEX && end == EMPTY_INDEX;
125 }
126 };
127
128 const SkRange<size_t> EMPTY_RANGE = SkRange<size_t>(EMPTY_INDEX, EMPTY_INDEX);
129
130
131 enum class TextBaseline {
132 kAlphabetic,
133 kIdeographic,
134 };
135
136 enum TextHeightBehavior {
137 kAll = 0x0,
138 kDisableFirstAscent = 0x1,
139 kDisableLastDescent = 0x2,
140 kDisableAll = 0x1 | 0x2,
141 };
142
143 enum class LineMetricStyle : uint8_t {
144 // Use ascent, descent, etc from a fixed baseline.
145 Typographic,
146 // Use ascent, descent, etc like css with the leading split and with height adjustments
147 CSS
148 };
149
150 } // namespace textlayout
151 } // namespace skia
152
153 #endif // DartTypes_DEFINED
154