2021-07-25 21:20:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "TextLayout.h"
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
2021-07-27 19:50:11 +00:00
|
|
|
enum class BlockType {
|
|
|
|
Newline,
|
|
|
|
Whitespace,
|
|
|
|
Word
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Block {
|
|
|
|
BlockType type;
|
|
|
|
Utf8View characters;
|
|
|
|
};
|
2021-07-25 21:20:11 +00:00
|
|
|
|
2023-01-06 11:55:23 +01:00
|
|
|
FloatRect TextLayout::bounding_rect(TextWrapping wrapping) const
|
2021-07-25 21:20:11 +00:00
|
|
|
{
|
2023-01-06 11:55:23 +01:00
|
|
|
auto lines = wrap_lines(TextElision::None, wrapping);
|
|
|
|
if (lines.is_empty()) {
|
2021-07-25 21:20:11 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:42:00 +01:00
|
|
|
FloatRect bounding_rect = {
|
2023-01-06 11:55:23 +01:00
|
|
|
0, 0, 0, (static_cast<float>(lines.size()) * (m_font_metrics.ascent + m_font_metrics.descent + m_font_metrics.line_gap)) - m_font_metrics.line_gap
|
2021-07-25 21:20:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& line : lines) {
|
2023-01-05 20:41:19 +01:00
|
|
|
auto line_width = m_font.width(line);
|
2021-07-25 21:20:11 +00:00
|
|
|
if (line_width > bounding_rect.width())
|
|
|
|
bounding_rect.set_width(line_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bounding_rect;
|
|
|
|
}
|
|
|
|
|
2023-01-06 11:55:23 +01:00
|
|
|
Vector<DeprecatedString, 32> TextLayout::wrap_lines(TextElision elision, TextWrapping wrapping) const
|
2021-07-25 21:20:11 +00:00
|
|
|
{
|
2021-07-27 19:50:11 +00:00
|
|
|
Vector<Block> blocks;
|
|
|
|
|
|
|
|
Optional<BlockType> current_block_type;
|
2021-07-27 22:08:46 +02:00
|
|
|
size_t block_start_offset = 0;
|
2021-07-25 21:20:11 +00:00
|
|
|
|
2021-07-27 19:50:11 +00:00
|
|
|
size_t offset = 0;
|
2021-07-25 21:20:11 +00:00
|
|
|
for (auto it = m_text.begin(); !it.done(); ++it) {
|
2021-07-27 19:50:11 +00:00
|
|
|
offset = m_text.iterator_offset(it);
|
2021-07-25 21:20:11 +00:00
|
|
|
|
|
|
|
switch (*it) {
|
|
|
|
case '\t':
|
|
|
|
case ' ': {
|
2021-07-27 19:50:11 +00:00
|
|
|
if (current_block_type.has_value() && current_block_type.value() != BlockType::Whitespace) {
|
|
|
|
blocks.append({
|
|
|
|
current_block_type.value(),
|
|
|
|
m_text.substring_view(block_start_offset, offset - block_start_offset),
|
|
|
|
});
|
|
|
|
current_block_type.clear();
|
|
|
|
}
|
2021-07-25 21:20:11 +00:00
|
|
|
|
2021-07-27 19:50:11 +00:00
|
|
|
if (!current_block_type.has_value()) {
|
|
|
|
current_block_type = BlockType::Whitespace;
|
|
|
|
block_start_offset = offset;
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-27 19:50:11 +00:00
|
|
|
case '\n':
|
|
|
|
case '\r': {
|
|
|
|
if (current_block_type.has_value()) {
|
|
|
|
blocks.append({
|
|
|
|
current_block_type.value(),
|
|
|
|
m_text.substring_view(block_start_offset, offset - block_start_offset),
|
|
|
|
});
|
|
|
|
current_block_type.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
blocks.append({ BlockType::Newline, Utf8View {} });
|
|
|
|
continue;
|
|
|
|
}
|
2021-07-25 21:20:11 +00:00
|
|
|
default: {
|
2021-07-27 19:50:11 +00:00
|
|
|
if (current_block_type.has_value() && current_block_type.value() != BlockType::Word) {
|
|
|
|
blocks.append({
|
|
|
|
current_block_type.value(),
|
|
|
|
m_text.substring_view(block_start_offset, offset - block_start_offset),
|
|
|
|
});
|
|
|
|
current_block_type.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current_block_type.has_value()) {
|
|
|
|
current_block_type = BlockType::Word;
|
|
|
|
block_start_offset = offset;
|
|
|
|
}
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:50:11 +00:00
|
|
|
if (current_block_type.has_value()) {
|
|
|
|
blocks.append({
|
|
|
|
current_block_type.value(),
|
|
|
|
m_text.substring_view(block_start_offset, m_text.byte_length() - block_start_offset),
|
|
|
|
});
|
|
|
|
}
|
2021-07-25 21:20:11 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> lines;
|
2021-07-25 21:20:11 +00:00
|
|
|
StringBuilder builder;
|
2023-01-01 19:42:00 +01:00
|
|
|
float line_width = 0;
|
2021-07-27 21:24:47 +00:00
|
|
|
size_t current_block = 0;
|
2021-07-27 19:50:11 +00:00
|
|
|
for (Block& block : blocks) {
|
|
|
|
switch (block.type) {
|
|
|
|
case BlockType::Newline: {
|
2022-12-06 01:12:49 +00:00
|
|
|
lines.append(builder.to_deprecated_string());
|
2021-07-25 21:20:11 +00:00
|
|
|
builder.clear();
|
|
|
|
line_width = 0;
|
2021-07-27 21:24:47 +00:00
|
|
|
current_block++;
|
2021-07-27 19:50:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case BlockType::Whitespace:
|
|
|
|
case BlockType::Word: {
|
2023-01-05 20:41:19 +01:00
|
|
|
float block_width = m_font.width(block.characters);
|
2021-07-27 21:24:47 +00:00
|
|
|
// FIXME: This should look at the specific advance amount of the
|
|
|
|
// last character, but we don't support that yet.
|
|
|
|
if (current_block != blocks.size() - 1) {
|
2023-01-05 20:41:19 +01:00
|
|
|
block_width += m_font.glyph_spacing();
|
2021-07-27 21:24:47 +00:00
|
|
|
}
|
2021-07-25 21:20:11 +00:00
|
|
|
|
2023-01-01 19:42:00 +01:00
|
|
|
if (wrapping == TextWrapping::Wrap && line_width + block_width > m_rect.width()) {
|
2022-12-06 01:12:49 +00:00
|
|
|
lines.append(builder.to_deprecated_string());
|
2021-07-27 21:11:05 +00:00
|
|
|
builder.clear();
|
|
|
|
line_width = 0;
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|
2021-07-27 19:50:11 +00:00
|
|
|
|
|
|
|
builder.append(block.characters.as_string());
|
|
|
|
line_width += block_width;
|
2021-07-27 21:24:47 +00:00
|
|
|
current_block++;
|
2021-07-27 19:50:11 +00:00
|
|
|
}
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 11:55:23 +01:00
|
|
|
auto last_line = builder.to_deprecated_string();
|
|
|
|
if (!last_line.is_empty())
|
|
|
|
lines.append(last_line);
|
2021-07-25 21:20:11 +00:00
|
|
|
|
|
|
|
switch (elision) {
|
|
|
|
case TextElision::None:
|
|
|
|
break;
|
|
|
|
case TextElision::Right: {
|
2023-01-06 11:55:23 +01:00
|
|
|
lines.at(lines.size() - 1) = elide_text_from_right(Utf8View { lines.at(lines.size() - 1) });
|
2021-07-25 21:20:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2023-01-06 11:55:23 +01:00
|
|
|
DeprecatedString TextLayout::elide_text_from_right(Utf8View text) const
|
2021-07-25 21:20:11 +00:00
|
|
|
{
|
2023-01-05 20:41:19 +01:00
|
|
|
float text_width = m_font.width(text);
|
2023-01-06 11:55:23 +01:00
|
|
|
if (text_width > static_cast<float>(m_rect.width())) {
|
2023-01-05 20:41:19 +01:00
|
|
|
float ellipsis_width = m_font.width("..."sv);
|
2023-01-01 19:42:00 +01:00
|
|
|
float current_width = ellipsis_width;
|
2023-01-05 20:41:19 +01:00
|
|
|
size_t glyph_spacing = m_font.glyph_spacing();
|
2021-07-25 21:20:11 +00:00
|
|
|
|
|
|
|
// FIXME: This code will break when the font has glyphs with advance
|
|
|
|
// amounts different from the actual width of the glyph
|
|
|
|
// (which is the case with many TrueType fonts).
|
|
|
|
if (ellipsis_width < text_width) {
|
|
|
|
size_t offset = 0;
|
|
|
|
for (auto it = text.begin(); !it.done(); ++it) {
|
2023-02-20 13:15:06 -05:00
|
|
|
auto glyph_width = m_font.glyph_or_emoji_width(it);
|
2021-07-25 21:20:11 +00:00
|
|
|
// NOTE: Glyph spacing should not be added after the last glyph on the line,
|
|
|
|
// but since we are here because the last glyph does not actually fit on the line,
|
|
|
|
// we don't have to worry about spacing.
|
2023-01-01 19:42:00 +01:00
|
|
|
auto width_with_this_glyph_included = current_width + glyph_width + glyph_spacing;
|
2021-07-25 21:20:11 +00:00
|
|
|
if (width_with_this_glyph_included > m_rect.width())
|
|
|
|
break;
|
|
|
|
current_width += glyph_width + glyph_spacing;
|
|
|
|
offset = text.iterator_offset(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(text.substring_view(0, offset).as_string());
|
2022-07-11 17:32:29 +00:00
|
|
|
builder.append("..."sv);
|
2022-12-06 01:12:49 +00:00
|
|
|
return builder.to_deprecated_string();
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.as_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|