2019-09-25 12:36:44 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-10-03 16:20:13 +03:00
|
|
|
#include <AK/Utf8View.h>
|
2019-09-25 12:36:44 +03:00
|
|
|
#include <LibCore/CDirIterator.h>
|
|
|
|
#include <LibDraw/Font.h>
|
2019-09-25 12:40:37 +03:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-09-25 12:36:44 +03:00
|
|
|
#include <LibHTML/Layout/LayoutBlock.h>
|
2019-06-16 00:41:15 +03:00
|
|
|
#include <LibHTML/Layout/LayoutText.h>
|
2019-06-16 00:17:08 +03:00
|
|
|
#include <ctype.h>
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2019-10-04 13:12:39 +03:00
|
|
|
LayoutText::LayoutText(const Text& text)
|
2019-10-07 10:50:31 +03:00
|
|
|
: LayoutNode(&text)
|
2019-06-15 23:49:44 +03:00
|
|
|
{
|
2019-10-06 12:09:38 +03:00
|
|
|
set_inline(true);
|
2019-06-15 23:49:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LayoutText::~LayoutText()
|
|
|
|
{
|
|
|
|
}
|
2019-06-16 00:17:08 +03:00
|
|
|
|
|
|
|
static bool is_all_whitespace(const String& string)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < string.length(); ++i) {
|
|
|
|
if (!isspace(string[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-04 16:56:36 +03:00
|
|
|
const String& LayoutText::text_for_style(const StyleProperties& style) const
|
2019-06-16 00:17:08 +03:00
|
|
|
{
|
|
|
|
static String one_space = " ";
|
2019-10-04 13:12:39 +03:00
|
|
|
if (is_all_whitespace(node().data())) {
|
2019-10-08 16:34:19 +03:00
|
|
|
if (style.string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "normal")
|
2019-09-25 12:36:44 +03:00
|
|
|
return one_space;
|
2019-10-04 13:12:39 +03:00
|
|
|
}
|
2019-06-16 00:17:08 +03:00
|
|
|
return node().data();
|
|
|
|
}
|
2019-06-21 00:00:26 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
void LayoutText::render_fragment(RenderingContext& context, const LineBoxFragment& fragment) const
|
2019-09-25 12:36:44 +03:00
|
|
|
{
|
2019-10-03 16:20:13 +03:00
|
|
|
auto& painter = context.painter();
|
2019-10-06 12:23:58 +03:00
|
|
|
painter.set_font(style().font());
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-08 16:34:19 +03:00
|
|
|
auto color = style().color_or_fallback(CSS::PropertyID::Color, document(), Color::Black);
|
|
|
|
auto text_decoration = style().string_or_fallback(CSS::PropertyID::TextDecoration, "none");
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
bool is_underline = text_decoration == "underline";
|
|
|
|
if (is_underline)
|
2019-10-20 13:30:25 +03:00
|
|
|
painter.draw_line(enclosing_int_rect(fragment.rect()).bottom_left().translated(0, -1), enclosing_int_rect(fragment.rect()).bottom_right().translated(0, -1), color);
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-20 13:30:25 +03:00
|
|
|
painter.draw_text(enclosing_int_rect(fragment.rect()), m_text_for_rendering.substring_view(fragment.start(), fragment.length()), TextAlignment::TopLeft, color);
|
2019-09-25 12:36:44 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void LayoutText::for_each_word(Callback callback) const
|
2019-06-21 00:00:26 +03:00
|
|
|
{
|
2019-10-09 17:29:23 +03:00
|
|
|
Utf8View view(m_text_for_rendering);
|
2019-10-03 16:20:13 +03:00
|
|
|
if (view.is_empty())
|
2019-09-25 12:36:44 +03:00
|
|
|
return;
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
auto start_of_word = view.begin();
|
|
|
|
|
|
|
|
auto commit_word = [&](auto it) {
|
|
|
|
int start = view.byte_offset_of(start_of_word);
|
|
|
|
int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_word);
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
callback(view.substring_view(start, length), start, length);
|
2019-09-25 12:36:44 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
start_of_word = it;
|
|
|
|
};
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
bool last_was_space = isspace(*view.begin());
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
for (auto it = view.begin(); it != view.end();) {
|
|
|
|
bool is_space = isspace(*it);
|
|
|
|
if (is_space == last_was_space) {
|
|
|
|
++it;
|
2019-09-25 12:36:44 +03:00
|
|
|
continue;
|
2019-10-03 16:20:13 +03:00
|
|
|
}
|
|
|
|
last_was_space = is_space;
|
|
|
|
commit_word(it);
|
|
|
|
++it;
|
2019-09-25 12:36:44 +03:00
|
|
|
}
|
2019-10-03 16:20:13 +03:00
|
|
|
if (start_of_word != view.end())
|
|
|
|
commit_word(view.end());
|
2019-06-21 00:00:26 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void LayoutText::for_each_source_line(Callback callback) const
|
2019-06-21 00:00:26 +03:00
|
|
|
{
|
2019-10-09 17:29:23 +03:00
|
|
|
Utf8View view(m_text_for_rendering);
|
2019-10-03 16:20:13 +03:00
|
|
|
if (view.is_empty())
|
|
|
|
return;
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
auto start_of_line = view.begin();
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
auto commit_line = [&](auto it) {
|
|
|
|
int start = view.byte_offset_of(start_of_line);
|
|
|
|
int length = view.byte_offset_of(it) - view.byte_offset_of(start_of_line);
|
2019-09-25 12:36:44 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
if (length > 0) {
|
|
|
|
callback(view.substring_view(start, length), start, length);
|
|
|
|
}
|
|
|
|
};
|
2019-09-25 12:40:37 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
for (auto it = view.begin(); it != view.end();) {
|
2019-10-03 17:49:32 +03:00
|
|
|
bool did_commit = false;
|
|
|
|
if (*it == '\n') {
|
2019-10-03 16:20:13 +03:00
|
|
|
commit_line(it);
|
2019-10-03 17:49:32 +03:00
|
|
|
did_commit = true;
|
|
|
|
}
|
2019-10-03 16:20:13 +03:00
|
|
|
++it;
|
2019-10-03 17:49:32 +03:00
|
|
|
if (did_commit)
|
|
|
|
start_of_line = it;
|
2019-09-29 12:32:00 +03:00
|
|
|
}
|
2019-10-03 16:20:13 +03:00
|
|
|
if (start_of_line != view.end())
|
|
|
|
commit_line(view.end());
|
2019-09-29 12:32:00 +03:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
void LayoutText::split_into_lines(LayoutBlock& container)
|
2019-09-25 12:40:37 +03:00
|
|
|
{
|
2019-10-06 12:23:58 +03:00
|
|
|
auto& font = style().font();
|
|
|
|
int space_width = font.glyph_width(' ') + font.glyph_spacing();
|
2019-10-12 14:42:58 +03:00
|
|
|
int line_height = style().line_height();
|
2019-10-03 16:20:13 +03:00
|
|
|
|
|
|
|
auto& line_boxes = container.line_boxes();
|
|
|
|
if (line_boxes.is_empty())
|
|
|
|
line_boxes.append(LineBox());
|
2019-10-13 18:31:58 +03:00
|
|
|
int available_width = container.width() - line_boxes.last().width();
|
2019-10-03 16:20:13 +03:00
|
|
|
|
2019-10-08 16:34:19 +03:00
|
|
|
bool is_preformatted = style().string_or_fallback(CSS::PropertyID::WhiteSpace, "normal") == "pre";
|
2019-10-03 16:20:13 +03:00
|
|
|
if (is_preformatted) {
|
2019-10-09 17:29:23 +03:00
|
|
|
m_text_for_rendering = node().data();
|
2019-10-03 16:20:13 +03:00
|
|
|
for_each_source_line([&](const Utf8View& view, int start, int length) {
|
2019-10-06 12:23:58 +03:00
|
|
|
line_boxes.last().add_fragment(*this, start, length, font.width(view), line_height);
|
2019-10-03 16:20:13 +03:00
|
|
|
line_boxes.append(LineBox());
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2019-09-28 23:31:41 +03:00
|
|
|
|
2019-10-09 17:29:23 +03:00
|
|
|
// Collapse whitespace into single spaces
|
2019-10-18 23:50:44 +03:00
|
|
|
auto utf8_view = Utf8View(node().data());
|
|
|
|
StringBuilder builder(node().data().length());
|
|
|
|
for (auto it = utf8_view.begin(); it != utf8_view.end(); ++it) {
|
|
|
|
if (!isspace(*it)) {
|
|
|
|
builder.append(utf8_view.as_string().characters_without_null_termination() + utf8_view.byte_offset_of(it), it.codepoint_length_in_bytes());
|
2019-10-09 17:29:23 +03:00
|
|
|
} else {
|
|
|
|
builder.append(' ');
|
2019-10-18 23:50:44 +03:00
|
|
|
auto prev = it;
|
|
|
|
while (it != utf8_view.end() && isspace(*it)) {
|
|
|
|
prev = it;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
it = prev;
|
2019-10-09 17:29:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_text_for_rendering = builder.to_string();
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
struct Word {
|
|
|
|
Utf8View view;
|
|
|
|
int start;
|
|
|
|
int length;
|
|
|
|
};
|
|
|
|
Vector<Word> words;
|
2019-09-28 23:18:19 +03:00
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
for_each_word([&](const Utf8View& view, int start, int length) {
|
|
|
|
words.append({ Utf8View(view), start, length });
|
2019-09-29 12:32:00 +03:00
|
|
|
});
|
|
|
|
|
2019-10-03 16:20:13 +03:00
|
|
|
for (int i = 0; i < words.size(); ++i) {
|
|
|
|
auto& word = words[i];
|
|
|
|
|
|
|
|
int word_width;
|
2019-10-03 16:55:18 +03:00
|
|
|
bool is_whitespace = isspace(*word.view.begin());
|
|
|
|
|
|
|
|
if (is_whitespace)
|
2019-10-03 16:20:13 +03:00
|
|
|
word_width = space_width;
|
|
|
|
else
|
2019-10-06 12:23:58 +03:00
|
|
|
word_width = font.width(word.view) + font.glyph_spacing();
|
2019-10-03 16:20:13 +03:00
|
|
|
|
2019-10-19 22:34:15 +03:00
|
|
|
if (line_boxes.last().width() > 0 && word_width > available_width) {
|
2019-10-03 16:20:13 +03:00
|
|
|
line_boxes.append(LineBox());
|
2019-10-13 18:31:58 +03:00
|
|
|
available_width = container.width();
|
2019-09-29 12:32:00 +03:00
|
|
|
}
|
2019-10-03 16:20:13 +03:00
|
|
|
|
2019-10-03 16:55:18 +03:00
|
|
|
if (is_whitespace && line_boxes.last().fragments().is_empty())
|
|
|
|
continue;
|
|
|
|
|
2019-10-09 17:29:23 +03:00
|
|
|
line_boxes.last().add_fragment(*this, word.start, is_whitespace ? 1 : word.length, word_width, line_height);
|
2019-10-03 16:20:13 +03:00
|
|
|
available_width -= word_width;
|
2019-10-03 16:55:18 +03:00
|
|
|
|
|
|
|
if (available_width < 0) {
|
|
|
|
line_boxes.append(LineBox());
|
2019-10-13 18:31:58 +03:00
|
|
|
available_width = container.width();
|
2019-10-03 16:55:18 +03:00
|
|
|
}
|
2019-10-03 16:20:13 +03:00
|
|
|
}
|
2019-09-25 12:40:37 +03:00
|
|
|
}
|