1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include "cru/platform/graphics/cairo/PangoTextLayout.h"
#include "cru/base/StringUtil.h"
#include "cru/platform/Check.h"
#include "cru/platform/GraphicsBase.h"
#include "cru/platform/graphics/Base.h"
#include "cru/platform/graphics/cairo/CairoGraphicsFactory.h"
#include "cru/platform/graphics/cairo/PangoFont.h"
#include <pango/pangocairo.h>
namespace cru::platform::graphics::cairo {
namespace {
Rect ConvertFromPango(const Rect& rect) {
auto result = rect;
result.left /= PANGO_SCALE;
result.top /= PANGO_SCALE;
result.width /= PANGO_SCALE;
result.height /= PANGO_SCALE;
return result;
}
} // namespace
PangoTextLayout::PangoTextLayout(CairoGraphicsFactory* factory,
std::shared_ptr<IFont> font)
: CairoResource(factory) {
Expects(font);
font_ = CheckPlatform<PangoFont>(font, GetPlatformId());
pango_layout_ = pango_cairo_create_layout(factory->GetDefaultCairo());
pango_layout_set_font_description(pango_layout_,
font_->GetPangoFontDescription());
};
PangoTextLayout::~PangoTextLayout() { g_object_unref(pango_layout_); }
String PangoTextLayout::GetText() { return text_; }
void PangoTextLayout::SetText(String new_text) {
text_ = std::move(new_text);
utf8_text_ = text_.ToUtf8();
pango_layout_set_text(pango_layout_, utf8_text_.c_str(), utf8_text_.size());
}
std::shared_ptr<IFont> PangoTextLayout::GetFont() { return font_; }
void PangoTextLayout::SetFont(std::shared_ptr<IFont> font) {
Expects(font);
font_ = CheckPlatform<PangoFont>(font, GetPlatformId());
pango_layout_set_font_description(pango_layout_,
font_->GetPangoFontDescription());
}
void PangoTextLayout::SetMaxWidth(float max_width) {
return pango_layout_set_width(pango_layout_, max_width * PANGO_SCALE);
}
void PangoTextLayout::SetMaxHeight(float max_height) {
return pango_layout_set_height(pango_layout_, max_height * PANGO_SCALE);
}
bool PangoTextLayout::IsEditMode() { return edit_mode_; }
void PangoTextLayout::SetEditMode(bool enable) { edit_mode_ = enable; }
Index PangoTextLayout::GetLineIndexFromCharIndex(Index char_index) {
int line;
pango_layout_index_to_line_x(pango_layout_,
FromUtf16IndexToUtf8Index(char_index), false,
&line, nullptr);
return line;
}
Index PangoTextLayout::GetLineCount() {
return pango_layout_get_line_count(pango_layout_);
}
float PangoTextLayout::GetLineHeight(Index line_index) {
auto line = pango_layout_get_line_readonly(pango_layout_, line_index);
int height;
pango_layout_line_get_height(line, &height);
return static_cast<float>(height) / PANGO_SCALE;
}
Index PangoTextLayout::FromUtf8IndexToUtf16Index(Index index) {
Utf8CodePointIterator iter(utf8_text_.data(), utf8_text_.size());
int cp_count = 0;
while ((!iter.IsPastEnd()) && iter.GetPosition() < index) {
++iter;
cp_count++;
}
return text_.IndexFromCodePointToCodeUnit(cp_count);
}
Index PangoTextLayout::FromUtf16IndexToUtf8Index(Index index) {
Index cp_index = text_.IndexFromCodeUnitToCodePoint(index);
Utf8CodePointIterator iter(utf8_text_.data(), utf8_text_.size());
for (Index i = 0; i < cp_index; ++i) {
++iter;
}
return iter.GetPosition();
}
Rect PangoTextLayout::GetTextBounds(bool includingTrailingSpace) {
PangoRectangle rectangle;
pango_layout_get_extents(pango_layout_, &rectangle, nullptr);
return ConvertFromPango(
Rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height));
}
std::vector<Rect> PangoTextLayout::TextRangeRect(const TextRange& text_range) {
auto tr = text_range.Normalize();
auto utf8_start_index = FromUtf16IndexToUtf8Index(tr.GetStart());
auto utf8_end_index = FromUtf16IndexToUtf8Index(tr.GetEnd());
int start_line_index, end_line_index, start_x_pos, end_x_pos;
pango_layout_index_to_line_x(pango_layout_, utf8_start_index, false,
&start_line_index, &start_x_pos);
pango_layout_index_to_line_x(pango_layout_, utf8_end_index, false,
&end_line_index, &end_x_pos);
if (start_line_index == end_line_index) {
auto line = pango_layout_get_line(pango_layout_, start_line_index);
PangoRectangle rectangle;
pango_layout_line_get_extents(line, &rectangle, nullptr);
return {ConvertFromPango(Rect(rectangle.x + start_x_pos, rectangle.y,
end_x_pos - start_x_pos, rectangle.height))};
} else {
std::vector<Rect> result;
PangoRectangle rectangle;
auto start_line = pango_layout_get_line(pango_layout_, start_line_index);
pango_layout_line_get_extents(start_line, &rectangle, nullptr);
result.push_back(Rect(rectangle.x + start_x_pos, rectangle.y,
rectangle.width - start_x_pos, rectangle.height));
for (int line_index = start_line_index + 1; line_index < end_line_index;
line_index++) {
auto line = pango_layout_get_line(pango_layout_, line_index);
pango_layout_line_get_extents(line, &rectangle, nullptr);
result.push_back(
Rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height));
}
auto end_line = pango_layout_get_line(pango_layout_, end_line_index);
pango_layout_line_get_extents(end_line, &rectangle, nullptr);
result.push_back(
Rect(rectangle.x, rectangle.y, end_x_pos, rectangle.height));
for (auto& r : result) {
r = ConvertFromPango(r);
}
return result;
}
}
Rect PangoTextLayout::TextSinglePoint(Index position, bool trailing) {
auto utf8_index = FromUtf16IndexToUtf8Index(position);
int line_index, x_pos;
pango_layout_index_to_line_x(pango_layout_, utf8_index, trailing, &line_index,
&x_pos);
auto line = pango_layout_get_line(pango_layout_, line_index);
PangoRectangle rectangle;
pango_layout_line_get_extents(line, &rectangle, nullptr);
return ConvertFromPango(
Rect(rectangle.x + x_pos, rectangle.y, 0, rectangle.height));
}
TextHitTestResult PangoTextLayout::HitTest(const Point& point) {
int index, trailing;
auto inside_text =
pango_layout_xy_to_index(pango_layout_, point.x * PANGO_SCALE,
point.y * PANGO_SCALE, &index, &trailing);
return TextHitTestResult{FromUtf8IndexToUtf16Index(index), trailing != 0,
inside_text != 0};
}
} // namespace cru::platform::graphics::cairo
|