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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/StringBuilder.h>
#include <LibGfx/Painter.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/Layout/BlockContainer.h>
#include <LibWeb/Layout/InlineFormattingContext.h>
#include <LibWeb/Layout/Label.h>
#include <LibWeb/Layout/TextNode.h>
namespace Web::Layout {
TextNode::TextNode(DOM::Document& document, DOM::Text& text)
: Node(document, &text)
{
set_inline(true);
}
TextNode::~TextNode()
{
}
static bool is_all_whitespace(StringView string)
{
for (size_t i = 0; i < string.length(); ++i) {
if (!is_ascii_space(string[i]))
return false;
}
return true;
}
void TextNode::paint_text_decoration(Gfx::Painter& painter, LineBoxFragment const& fragment) const
{
Gfx::IntPoint line_start_point {};
Gfx::IntPoint line_end_point {};
auto& font = fragment.layout_node().font();
auto fragment_box = enclosing_int_rect(fragment.absolute_rect());
auto glyph_height = font.glyph_height();
auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height;
switch (computed_values().text_decoration_line()) {
case CSS::TextDecorationLine::None:
return;
case CSS::TextDecorationLine::Underline:
line_start_point = fragment_box.top_left().translated(0, baseline + 2);
line_end_point = fragment_box.top_right().translated(0, baseline + 2);
break;
case CSS::TextDecorationLine::Overline:
line_start_point = fragment_box.top_left().translated(0, baseline - glyph_height);
line_end_point = fragment_box.top_right().translated(0, baseline - glyph_height);
break;
case CSS::TextDecorationLine::LineThrough: {
auto x_height = font.x_height();
line_start_point = fragment_box.top_left().translated(0, baseline - x_height / 2);
line_end_point = fragment_box.top_right().translated(0, baseline - x_height / 2);
break;
}
case CSS::TextDecorationLine::Blink:
// Conforming user agents may simply not blink the text
return;
}
painter.draw_line(line_start_point, line_end_point, computed_values().color());
}
void TextNode::paint_fragment(PaintContext& context, const LineBoxFragment& fragment, PaintPhase phase) const
{
auto& painter = context.painter();
if (phase == PaintPhase::Foreground) {
painter.set_font(font());
if (document().inspected_node() == &dom_node())
context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), Color::Magenta);
paint_text_decoration(painter, fragment);
// FIXME: text-transform should be done already in layout, since uppercase glyphs may be wider than lowercase, etc.
auto text = m_text_for_rendering;
auto text_transform = computed_values().text_transform();
if (text_transform == CSS::TextTransform::Uppercase)
text = m_text_for_rendering.to_uppercase();
if (text_transform == CSS::TextTransform::Lowercase)
text = m_text_for_rendering.to_lowercase();
painter.draw_text(enclosing_int_rect(fragment.absolute_rect()), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, computed_values().color());
auto selection_rect = fragment.selection_rect(font());
if (!selection_rect.is_empty()) {
painter.fill_rect(enclosing_int_rect(selection_rect), context.palette().selection());
Gfx::PainterStateSaver saver(painter);
painter.add_clip_rect(enclosing_int_rect(selection_rect));
painter.draw_text(enclosing_int_rect(fragment.absolute_rect()), text.substring_view(fragment.start(), fragment.length()), Gfx::TextAlignment::CenterLeft, context.palette().selection_text());
}
paint_cursor_if_needed(context, fragment);
}
}
void TextNode::paint_cursor_if_needed(PaintContext& context, const LineBoxFragment& fragment) const
{
if (!browsing_context().is_focused_context())
return;
if (!browsing_context().cursor_blink_state())
return;
if (browsing_context().cursor_position().node() != &dom_node())
return;
// NOTE: This checks if the cursor is before the start or after the end of the fragment. If it is at the end, after all text, it should still be painted.
if (browsing_context().cursor_position().offset() < (unsigned)fragment.start() || browsing_context().cursor_position().offset() > (unsigned)(fragment.start() + fragment.length()))
return;
if (!fragment.layout_node().dom_node() || !fragment.layout_node().dom_node()->is_editable())
return;
auto fragment_rect = fragment.absolute_rect();
float cursor_x = fragment_rect.x() + font().width(fragment.text().substring_view(0, browsing_context().cursor_position().offset() - fragment.start()));
float cursor_top = fragment_rect.top();
float cursor_height = fragment_rect.height();
Gfx::IntRect cursor_rect(cursor_x, cursor_top, 1, cursor_height);
context.painter().draw_rect(cursor_rect, computed_values().color());
}
// NOTE: This collapes whitespace into a single ASCII space if collapse is true. If previous_is_empty_or_ends_in_whitespace, it also strips leading whitespace.
void TextNode::compute_text_for_rendering(bool collapse, bool previous_is_empty_or_ends_in_whitespace)
{
auto& data = dom_node().data();
if (!collapse || data.is_empty()) {
m_text_for_rendering = data;
return;
}
// NOTE: A couple fast returns to avoid unnecessarily allocating a StringBuilder.
if (data.length() == 1) {
if (is_ascii_space(data[0])) {
if (previous_is_empty_or_ends_in_whitespace)
m_text_for_rendering = String::empty();
else {
static String s_single_space_string = " ";
m_text_for_rendering = s_single_space_string;
}
} else {
m_text_for_rendering = data;
}
return;
}
bool contains_space = false;
for (auto& c : data) {
if (is_ascii_space(c)) {
contains_space = true;
break;
}
}
if (!contains_space) {
m_text_for_rendering = data;
return;
}
StringBuilder builder(data.length());
size_t index = 0;
auto skip_over_whitespace = [&index, &data] {
while (index < data.length() && is_ascii_space(data[index]))
++index;
};
if (previous_is_empty_or_ends_in_whitespace)
skip_over_whitespace();
while (index < data.length()) {
if (is_ascii_space(data[index])) {
builder.append(' ');
++index;
skip_over_whitespace();
} else {
builder.append(data[index]);
++index;
}
}
m_text_for_rendering = builder.to_string();
}
bool TextNode::wants_mouse_events() const
{
return parent() && is<Label>(parent());
}
void TextNode::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
{
if (!parent() || !is<Label>(*parent()))
return;
verify_cast<Label>(*parent()).handle_mousedown_on_label({}, position, button);
browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
}
void TextNode::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
{
if (!parent() || !is<Label>(*parent()))
return;
// NOTE: Changing the state of the DOM node may run arbitrary JS, which could disappear this node.
NonnullRefPtr protect = *this;
verify_cast<Label>(*parent()).handle_mouseup_on_label({}, position, button);
browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
}
void TextNode::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
{
if (!parent() || !is<Label>(*parent()))
return;
verify_cast<Label>(*parent()).handle_mousemove_on_label({}, position, button);
}
TextNode::ChunkIterator::ChunkIterator(StringView text, LayoutMode layout_mode, bool wrap_lines, bool respect_linebreaks)
: m_layout_mode(layout_mode)
, m_wrap_lines(wrap_lines)
, m_respect_linebreaks(respect_linebreaks)
, m_utf8_view(text)
, m_iterator(m_utf8_view.begin())
{
m_last_was_space = !text.is_empty() && is_ascii_space(*m_utf8_view.begin());
}
Optional<TextNode::Chunk> TextNode::ChunkIterator::next()
{
if (m_iterator == m_utf8_view.end())
return {};
auto start_of_chunk = m_iterator;
while (m_iterator != m_utf8_view.end()) {
++m_iterator;
if (m_last_was_newline) {
// NOTE: This expression looks out for the case where we have
// multiple newlines in a row. Because every output next()
// that's a newline newline must be prepared for in advance by
// the previous next() call, we need to check whether the next
// character is a newline here as well. Otherwise, the newline
// becomes part of the next expression and causes rendering
// issues.
m_last_was_newline = m_iterator != m_utf8_view.end() && *m_iterator == '\n';
if (auto result = try_commit_chunk(start_of_chunk, m_iterator, true); result.has_value())
return result.release_value();
}
if (m_layout_mode == LayoutMode::AllPossibleLineBreaks) {
if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value()) {
return result.release_value();
}
}
// NOTE: The checks after this need to look at the current iterator
// position, which depends on not being at the end.
if (m_iterator == m_utf8_view.end())
break;
// NOTE: When we're supposed to stop on linebreaks, we're actually
// supposed to output two chunks: "content" and "\n". Since we
// can't output two chunks at once, we store this information as a
// flag to output the newline immediately at the earliest
// opportunity.
if (m_respect_linebreaks && *m_iterator == '\n') {
m_last_was_newline = true;
if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value()) {
return result.release_value();
}
}
if (m_wrap_lines) {
bool is_space = is_ascii_space(*m_iterator);
if (is_space != m_last_was_space) {
m_last_was_space = is_space;
if (auto result = try_commit_chunk(start_of_chunk, m_iterator, false); result.has_value()) {
return result.release_value();
}
}
}
}
if (start_of_chunk != m_utf8_view.end()) {
// Try to output whatever's left at the end of the text node.
if (auto result = try_commit_chunk(start_of_chunk, m_utf8_view.end(), false, true); result.has_value())
return result.release_value();
}
return {};
}
Optional<TextNode::Chunk> TextNode::ChunkIterator::try_commit_chunk(Utf8View::Iterator const& start, Utf8View::Iterator const& end, bool has_breaking_newline, bool must_commit)
{
if (m_layout_mode == LayoutMode::OnlyRequiredLineBreaks && !must_commit)
return {};
auto byte_offset = m_utf8_view.byte_offset_of(start);
auto byte_length = m_utf8_view.byte_offset_of(end) - byte_offset;
if (byte_length > 0) {
auto chunk_view = m_utf8_view.substring_view(byte_offset, byte_length);
return Chunk {
.view = chunk_view,
.start = byte_offset,
.length = byte_length,
.has_breaking_newline = has_breaking_newline,
.is_all_whitespace = is_all_whitespace(chunk_view.as_string()),
};
}
return {};
}
void TextNode::paint(PaintContext&, PaintPhase)
{
}
}
|