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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2023-11-04 21:41:24 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2022-09-13 22:56:13 +00:00
|
|
|
#include <AK/Forward.h>
|
2021-07-25 21:20:11 +00:00
|
|
|
#include <AK/Utf32View.h>
|
|
|
|
#include <AK/Utf8View.h>
|
|
|
|
#include <AK/Vector.h>
|
2022-04-09 07:28:38 +00:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2023-12-09 22:45:57 +00:00
|
|
|
#include <LibGfx/FontCascadeList.h>
|
2022-09-13 22:56:13 +00:00
|
|
|
#include <LibGfx/Forward.h>
|
2021-07-25 21:20:11 +00:00
|
|
|
#include <LibGfx/Rect.h>
|
|
|
|
#include <LibGfx/TextElision.h>
|
|
|
|
#include <LibGfx/TextWrapping.h>
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
// FIXME: This currently isn't an ideal way of doing things; ideally, TextLayout
|
|
|
|
// would be doing the rendering by painting individual glyphs. However, this
|
|
|
|
// would regress our Unicode bidirectional text support. Therefore, fixing this
|
|
|
|
// requires:
|
|
|
|
// - Moving the bidirectional algorithm either here, or some place TextLayout
|
|
|
|
// can access;
|
|
|
|
// - Making TextLayout render the given text into something like a Vector<Line>
|
|
|
|
// where:
|
|
|
|
// using Line = Vector<DirectionalRun>;
|
|
|
|
// struct DirectionalRun {
|
|
|
|
// Utf32View glyphs;
|
|
|
|
// Vector<int> advance;
|
|
|
|
// TextDirection direction;
|
|
|
|
// };
|
|
|
|
// - Either;
|
|
|
|
// a) Making TextLayout output these Lines directly using a given Painter, or
|
|
|
|
// b) Taking the Lines from TextLayout and painting each glyph.
|
|
|
|
class TextLayout {
|
|
|
|
public:
|
2023-01-05 19:41:19 +00:00
|
|
|
TextLayout(Gfx::Font const& font, Utf8View const& text, FloatRect const& rect)
|
2021-07-25 21:20:11 +00:00
|
|
|
: m_font(font)
|
2023-01-06 10:55:23 +00:00
|
|
|
, m_font_metrics(font.pixel_metrics())
|
2021-07-25 21:20:11 +00:00
|
|
|
, m_text(text)
|
|
|
|
, m_rect(rect)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<ByteString, 32> lines(TextElision elision, TextWrapping wrapping) const
|
2021-07-25 21:20:11 +00:00
|
|
|
{
|
2023-01-06 10:55:23 +00:00
|
|
|
return wrap_lines(elision, wrapping);
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 10:55:23 +00:00
|
|
|
FloatRect bounding_rect(TextWrapping) const;
|
2021-07-25 21:20:11 +00:00
|
|
|
|
|
|
|
private:
|
2023-12-16 14:19:34 +00:00
|
|
|
Vector<ByteString, 32> wrap_lines(TextElision, TextWrapping) const;
|
|
|
|
ByteString elide_text_from_right(Utf8View) const;
|
2021-07-25 21:20:11 +00:00
|
|
|
|
2023-01-05 19:41:19 +00:00
|
|
|
Font const& m_font;
|
2023-01-06 10:55:23 +00:00
|
|
|
FontPixelMetrics m_font_metrics;
|
2021-07-25 21:20:11 +00:00
|
|
|
Utf8View m_text;
|
2023-01-01 18:42:00 +00:00
|
|
|
FloatRect m_rect;
|
2021-07-25 21:20:11 +00:00
|
|
|
};
|
|
|
|
|
2023-11-04 21:41:24 +00:00
|
|
|
inline bool should_paint_as_space(u32 code_point)
|
|
|
|
{
|
|
|
|
return is_ascii_space(code_point) || code_point == 0xa0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class IncludeLeftBearing {
|
|
|
|
Yes,
|
|
|
|
No
|
|
|
|
};
|
|
|
|
|
2023-11-04 23:17:04 +00:00
|
|
|
struct DrawGlyph {
|
2023-11-04 21:41:24 +00:00
|
|
|
FloatPoint position;
|
2023-11-04 23:17:04 +00:00
|
|
|
u32 code_point;
|
2023-12-09 22:14:07 +00:00
|
|
|
NonnullRefPtr<Font const> font;
|
2023-11-04 21:41:24 +00:00
|
|
|
};
|
|
|
|
|
2023-11-04 23:17:04 +00:00
|
|
|
struct DrawEmoji {
|
2023-12-02 17:38:05 +00:00
|
|
|
FloatPoint position;
|
2023-11-04 23:17:04 +00:00
|
|
|
Gfx::Bitmap const* emoji;
|
2023-12-09 22:14:07 +00:00
|
|
|
NonnullRefPtr<Font const> font;
|
2023-11-04 23:17:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using DrawGlyphOrEmoji = Variant<DrawGlyph, DrawEmoji>;
|
|
|
|
|
|
|
|
Variant<DrawGlyph, DrawEmoji> prepare_draw_glyph_or_emoji(FloatPoint point, Utf8CodePointIterator& it, Font const& font);
|
|
|
|
|
2023-11-04 21:41:24 +00:00
|
|
|
template<typename Callback>
|
2023-12-09 22:45:57 +00:00
|
|
|
void for_each_glyph_position(FloatPoint baseline_start, Utf8View string, FontCascadeList const& font_list, Callback callback, IncludeLeftBearing include_left_bearing = IncludeLeftBearing::No, Optional<float&> width = {})
|
2023-11-04 21:41:24 +00:00
|
|
|
{
|
2023-12-09 22:45:57 +00:00
|
|
|
float space_width = font_list.first().glyph_width(' ') + font_list.first().glyph_spacing();
|
2023-11-04 21:41:24 +00:00
|
|
|
|
|
|
|
u32 last_code_point = 0;
|
|
|
|
|
2023-11-04 23:17:04 +00:00
|
|
|
auto point = baseline_start;
|
|
|
|
for (auto code_point_iterator = string.begin(); code_point_iterator != string.end(); ++code_point_iterator) {
|
2023-12-09 22:45:57 +00:00
|
|
|
auto it = code_point_iterator; // The callback function will advance the iterator, so create a copy for this lookup.
|
2023-11-04 21:41:24 +00:00
|
|
|
auto code_point = *code_point_iterator;
|
2023-12-09 22:45:57 +00:00
|
|
|
RefPtr<Gfx::Font const> font = font_list.font_for_code_point(code_point);
|
|
|
|
|
|
|
|
point.set_y(baseline_start.y() - font->pixel_metrics().ascent);
|
|
|
|
|
2023-11-04 21:41:24 +00:00
|
|
|
if (should_paint_as_space(code_point)) {
|
|
|
|
point.translate_by(space_width, 0);
|
|
|
|
last_code_point = code_point;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-09 22:45:57 +00:00
|
|
|
auto kerning = font->glyphs_horizontal_kerning(last_code_point, code_point);
|
2023-11-04 21:41:24 +00:00
|
|
|
if (kerning != 0.0f)
|
|
|
|
point.translate_by(kerning, 0);
|
|
|
|
|
2023-12-09 22:45:57 +00:00
|
|
|
auto glyph_width = font->glyph_or_emoji_width(it) + font->glyph_spacing();
|
|
|
|
auto glyph_or_emoji = prepare_draw_glyph_or_emoji(point, code_point_iterator, *font);
|
2023-11-04 23:17:04 +00:00
|
|
|
if (include_left_bearing == IncludeLeftBearing::Yes) {
|
|
|
|
if (glyph_or_emoji.has<DrawGlyph>())
|
2023-12-09 22:45:57 +00:00
|
|
|
glyph_or_emoji.get<DrawGlyph>().position += FloatPoint(font->glyph_left_bearing(code_point), 0);
|
2023-11-04 23:17:04 +00:00
|
|
|
}
|
2023-11-04 21:41:24 +00:00
|
|
|
|
2023-11-04 23:17:04 +00:00
|
|
|
callback(glyph_or_emoji);
|
2023-11-04 21:41:24 +00:00
|
|
|
|
|
|
|
point.translate_by(glyph_width, 0);
|
|
|
|
last_code_point = code_point;
|
|
|
|
}
|
|
|
|
|
2023-12-02 17:38:05 +00:00
|
|
|
if (width.has_value())
|
2023-12-09 22:45:57 +00:00
|
|
|
*width = point.x() - font_list.first().glyph_spacing();
|
2023-12-02 17:38:05 +00:00
|
|
|
}
|
2023-11-04 23:17:04 +00:00
|
|
|
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|