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 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2023-11-04 21:41:24 +00:00
|
|
|
#include <AK/CharacterTypes.h>
|
2022-09-14 00:56:13 +02: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 09:28:38 +02:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2023-12-09 23:45:57 +01:00
|
|
|
#include <LibGfx/FontCascadeList.h>
|
2022-09-14 00:56:13 +02:00
|
|
|
#include <LibGfx/Forward.h>
|
2021-07-25 21:20:11 +00:00
|
|
|
#include <LibGfx/Rect.h>
|
|
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
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-05 00:17:04 +01:00
|
|
|
struct DrawGlyph {
|
2023-11-04 21:41:24 +00:00
|
|
|
FloatPoint position;
|
2023-11-05 00:17:04 +01:00
|
|
|
u32 code_point;
|
2023-12-09 23:14:07 +01:00
|
|
|
NonnullRefPtr<Font const> font;
|
2023-12-29 06:10:32 +01:00
|
|
|
|
|
|
|
|
void translate_by(FloatPoint const& delta)
|
|
|
|
|
{
|
|
|
|
|
position.translate_by(delta);
|
|
|
|
|
}
|
2023-11-04 21:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
2023-11-05 00:17:04 +01:00
|
|
|
struct DrawEmoji {
|
2023-12-02 18:38:05 +01:00
|
|
|
FloatPoint position;
|
2023-11-05 00:17:04 +01:00
|
|
|
Gfx::Bitmap const* emoji;
|
2023-12-09 23:14:07 +01:00
|
|
|
NonnullRefPtr<Font const> font;
|
2023-12-29 06:10:32 +01:00
|
|
|
|
|
|
|
|
void translate_by(FloatPoint const& delta)
|
|
|
|
|
{
|
|
|
|
|
position.translate_by(delta);
|
|
|
|
|
}
|
2023-11-05 00:17:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using DrawGlyphOrEmoji = Variant<DrawGlyph, DrawEmoji>;
|
|
|
|
|
|
2024-03-01 16:37:44 +01:00
|
|
|
class GlyphRun : public RefCounted<GlyphRun> {
|
|
|
|
|
public:
|
|
|
|
|
GlyphRun() = default;
|
|
|
|
|
GlyphRun(Vector<Gfx::DrawGlyphOrEmoji>&& glyphs)
|
|
|
|
|
: m_glyphs(move(glyphs))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[[nodiscard]] Vector<Gfx::DrawGlyphOrEmoji> const& glyphs() const { return m_glyphs; }
|
|
|
|
|
[[nodiscard]] bool is_empty() const { return m_glyphs.is_empty(); }
|
|
|
|
|
|
|
|
|
|
void append(Gfx::DrawGlyphOrEmoji glyph) { m_glyphs.append(glyph); }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Vector<Gfx::DrawGlyphOrEmoji> m_glyphs;
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-05 00:17:04 +01:00
|
|
|
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 23:45:57 +01: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
|
|
|
{
|
2024-01-08 00:10:10 +01:00
|
|
|
auto const& space_glyph_font = font_list.font_for_code_point(' ');
|
2024-06-04 07:02:22 +02:00
|
|
|
float space_width = space_glyph_font.glyph_width(' ');
|
2023-11-04 21:41:24 +00:00
|
|
|
|
|
|
|
|
u32 last_code_point = 0;
|
|
|
|
|
|
2023-11-05 00:17:04 +01:00
|
|
|
auto point = baseline_start;
|
|
|
|
|
for (auto code_point_iterator = string.begin(); code_point_iterator != string.end(); ++code_point_iterator) {
|
2023-12-09 23:45:57 +01: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 23:45:57 +01: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 23:45:57 +01: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);
|
|
|
|
|
|
2024-06-04 07:02:22 +02:00
|
|
|
auto glyph_width = font->glyph_or_emoji_width(it);
|
2023-12-09 23:45:57 +01:00
|
|
|
auto glyph_or_emoji = prepare_draw_glyph_or_emoji(point, code_point_iterator, *font);
|
2023-11-05 00:17:04 +01:00
|
|
|
if (include_left_bearing == IncludeLeftBearing::Yes) {
|
|
|
|
|
if (glyph_or_emoji.has<DrawGlyph>())
|
2023-12-09 23:45:57 +01:00
|
|
|
glyph_or_emoji.get<DrawGlyph>().position += FloatPoint(font->glyph_left_bearing(code_point), 0);
|
2023-11-05 00:17:04 +01:00
|
|
|
}
|
2023-11-04 21:41:24 +00:00
|
|
|
|
2023-11-05 00:17:04 +01: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 18:38:05 +01:00
|
|
|
if (width.has_value())
|
2024-06-04 07:02:22 +02:00
|
|
|
*width = point.x();
|
2023-12-02 18:38:05 +01:00
|
|
|
}
|
2023-11-05 00:17:04 +01:00
|
|
|
|
2021-07-25 21:20:11 +00:00
|
|
|
}
|