1 | /* |
2 | * Copyright 2017 Google Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #ifndef LIB_TXT_SRC_PARAGRAPH_STYLE_H_ |
18 | #define LIB_TXT_SRC_PARAGRAPH_STYLE_H_ |
19 | |
20 | #include <climits> |
21 | #include <string> |
22 | |
23 | #include "font_style.h" |
24 | #include "font_weight.h" |
25 | #include "minikin/LineBreaker.h" |
26 | #include "text_style.h" |
27 | |
28 | namespace txt { |
29 | |
30 | enum class TextAlign { |
31 | left, |
32 | right, |
33 | center, |
34 | justify, |
35 | start, |
36 | end, |
37 | }; |
38 | |
39 | enum class TextDirection { |
40 | rtl, |
41 | ltr, |
42 | }; |
43 | |
44 | // Allows disabling height adjustments to first line's ascent and the |
45 | // last line's descent. If disabled, the line will use the default font |
46 | // metric provided ascent/descent and ParagraphStyle.height will not take |
47 | // effect. |
48 | // |
49 | // The default behavior is kAll where height adjustments are enabled for all |
50 | // lines. |
51 | // |
52 | // Multiple behaviors can be applied at once with a bitwise | operator. For |
53 | // example, disabling first ascent and last descent can achieved with: |
54 | // |
55 | // (kDisableFirstAscent | kDisableLastDescent). |
56 | enum TextHeightBehavior { |
57 | kAll = 0x0, |
58 | kDisableFirstAscent = 0x1, |
59 | kDisableLastDescent = 0x2, |
60 | kDisableAll = 0x1 | 0x2, |
61 | }; |
62 | |
63 | class ParagraphStyle { |
64 | public: |
65 | // Default TextStyle. Used in GetTextStyle() to obtain the base TextStyle to |
66 | // inherit off of. |
67 | FontWeight font_weight = FontWeight::w400; |
68 | FontStyle font_style = FontStyle::normal; |
69 | std::string font_family = "" ; |
70 | double font_size = 14; |
71 | double height = 1; |
72 | size_t text_height_behavior = TextHeightBehavior::kAll; |
73 | bool has_height_override = false; |
74 | |
75 | // Strut properties. strut_enabled must be set to true for the rest of the |
76 | // properties to take effect. |
77 | // TODO(garyq): Break the strut properties into a separate class. |
78 | bool strut_enabled = false; |
79 | FontWeight strut_font_weight = FontWeight::w400; |
80 | FontStyle strut_font_style = FontStyle::normal; |
81 | std::vector<std::string> strut_font_families; |
82 | double strut_font_size = 14; |
83 | double strut_height = 1; |
84 | bool strut_has_height_override = false; |
85 | double strut_leading = -1; // Negative to use font's default leading. [0,inf) |
86 | // to use custom leading as a ratio of font size. |
87 | bool force_strut_height = false; |
88 | |
89 | // General paragraph properties. |
90 | TextAlign text_align = TextAlign::start; |
91 | TextDirection text_direction = TextDirection::ltr; |
92 | size_t max_lines = std::numeric_limits<size_t>::max(); |
93 | std::u16string ellipsis; |
94 | std::string locale; |
95 | |
96 | // Default strategy is kBreakStrategy_Greedy. Sometimes, |
97 | // kBreakStrategy_HighQuality will produce more desirable layouts (e.g., very |
98 | // long words are more likely to be reasonably placed). |
99 | // kBreakStrategy_Balanced will balance between the two. |
100 | minikin::BreakStrategy break_strategy = |
101 | minikin::BreakStrategy::kBreakStrategy_Greedy; |
102 | |
103 | TextStyle GetTextStyle() const; |
104 | |
105 | bool unlimited_lines() const; |
106 | bool ellipsized() const; |
107 | |
108 | // Return a text alignment value that is not dependent on the text direction. |
109 | TextAlign effective_align() const; |
110 | }; |
111 | |
112 | } // namespace txt |
113 | |
114 | #endif // LIB_TXT_SRC_PARAGRAPH_STYLE_H_ |
115 | |