• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum class EllipsisModal {
67     HEAD = 0,
68     MIDDLE = 1,
69     TAIL = 2,
70 };
71 
72 struct PositionWithAffinity {
73     int32_t position;
74     Affinity affinity;
75 
PositionWithAffinityPositionWithAffinity76     PositionWithAffinity() : position(0), affinity(kDownstream) {}
PositionWithAffinityPositionWithAffinity77     PositionWithAffinity(int32_t p, Affinity a) : position(p), affinity(a) {}
78 };
79 
80 struct TextBox {
81     SkRect rect;
82     TextDirection direction;
83 
TextBoxTextBox84     TextBox(SkRect r, TextDirection d) : rect(r), direction(d) {}
85 };
86 
87 // -------------------------------------------------------------------
88 // --- Reversed iterable
89 
90 template<typename C, typename UnaryFunction>
directional_for_each(C & c,bool forwards,UnaryFunction f)91 UnaryFunction directional_for_each(C& c, bool forwards, UnaryFunction f) {
92     return forwards
93               ? std::for_each(std::begin(c), std::end(c), f)
94               : std::for_each(std::rbegin(c), std::rend(c), f);
95 }
96 
97 const size_t EMPTY_INDEX = std::numeric_limits<size_t>::max();
98 template <typename T> struct SkRange {
SkRangeSkRange99     SkRange() : start(), end() {}
SkRangeSkRange100     SkRange(T s, T e) : start(s), end(e) {}
101 
102     using SignedT = std::make_signed_t<T>;
103 
104     T start, end;
105 
106     bool operator==(const SkRange<T>& other) const {
107         return start == other.start && end == other.end;
108     }
109 
widthSkRange110     T width() const { return end - start; }
111 
ShiftSkRange112     void Shift(SignedT delta) {
113         start += delta;
114         end += delta;
115     }
116 
containsSkRange117     bool contains(SkRange<size_t> other) const {
118         return start <= other.start && end >= other.end;
119     }
120 
intersectsSkRange121     bool intersects(SkRange<size_t> other) const {
122         return std::max(start, other.start) <= std::min(end, other.end);
123     }
124 
intersectionSkRange125     SkRange<size_t> intersection(SkRange<size_t> other) const {
126         return SkRange<size_t>(std::max(start, other.start), std::min(end, other.end));
127     }
128 
emptySkRange129     bool empty() const {
130         return start == EMPTY_INDEX && end == EMPTY_INDEX;
131     }
132 };
133 
134 const SkRange<size_t> EMPTY_RANGE = SkRange<size_t>(EMPTY_INDEX, EMPTY_INDEX);
135 
136 
137 enum class TextBaseline {
138     kAlphabetic,
139     kIdeographic,
140 };
141 
142 enum TextHeightBehavior {
143     kAll = 0x0,
144     kDisableFirstAscent = 0x1,
145     kDisableLastDescent = 0x2,
146     kDisableAll = 0x1 | 0x2,
147 };
148 
149 enum class LineMetricStyle : uint8_t {
150     // Use ascent, descent, etc from a fixed baseline.
151     Typographic,
152     // Use ascent, descent, etc like css with the leading split and with height adjustments
153     CSS
154 };
155 
156 }  // namespace textlayout
157 }  // namespace skia
158 
159 #endif  // DartTypes_DEFINED
160