2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-05-29 12:38:28 +02:00
|
|
|
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
2025-04-19 19:10:01 +02:00
|
|
|
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
2025-04-19 19:10:01 +02:00
|
|
|
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2018-10-11 23:45:57 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2025-04-19 19:10:01 +02:00
|
|
|
#include <AK/FlyString.h>
|
|
|
|
|
#include <LibGfx/Font/Font.h>
|
|
|
|
|
#include <LibGfx/Font/Typeface.h>
|
2020-02-06 11:56:38 +01:00
|
|
|
|
2025-04-19 19:10:01 +02:00
|
|
|
class SkFont;
|
2024-08-13 17:05:43 +01:00
|
|
|
struct hb_font_t;
|
|
|
|
|
|
2020-02-06 11:56:38 +01:00
|
|
|
namespace Gfx {
|
2018-10-11 23:45:57 +02:00
|
|
|
|
2022-03-28 12:03:44 +02:00
|
|
|
struct FontPixelMetrics {
|
2021-09-23 13:12:11 +02:00
|
|
|
float size { 0 };
|
|
|
|
|
float x_height { 0 };
|
2022-03-28 12:01:10 +02:00
|
|
|
float advance_of_ascii_zero { 0 };
|
2022-03-28 12:17:18 +02:00
|
|
|
|
|
|
|
|
// Number of pixels the font extends above the baseline.
|
|
|
|
|
float ascent { 0 };
|
|
|
|
|
|
|
|
|
|
// Number of pixels the font descends below the baseline.
|
|
|
|
|
float descent { 0 };
|
|
|
|
|
|
|
|
|
|
// Line gap specified by font.
|
|
|
|
|
float line_gap { 0 };
|
|
|
|
|
|
2023-01-04 19:53:00 +01:00
|
|
|
float line_spacing() const { return ascent + descent + line_gap; }
|
2021-09-23 13:12:11 +02:00
|
|
|
};
|
|
|
|
|
|
2023-02-04 23:00:34 +03:00
|
|
|
// https://learn.microsoft.com/en-us/typography/opentype/spec/os2#uswidthclass
|
|
|
|
|
enum FontWidth {
|
|
|
|
|
UltraCondensed = 1,
|
|
|
|
|
ExtraCondensed = 2,
|
|
|
|
|
Condensed = 3,
|
|
|
|
|
SemiCondensed = 4,
|
|
|
|
|
Normal = 5,
|
|
|
|
|
SemiExpanded = 6,
|
|
|
|
|
Expanded = 7,
|
|
|
|
|
ExtraExpanded = 8,
|
|
|
|
|
UltraExpanded = 9
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-13 17:05:43 +01:00
|
|
|
constexpr float text_shaping_resolution = 64;
|
|
|
|
|
|
2019-06-21 15:29:31 +02:00
|
|
|
class Font : public RefCounted<Font> {
|
2018-10-11 23:45:57 +02:00
|
|
|
public:
|
2025-04-19 19:10:01 +02:00
|
|
|
Font(NonnullRefPtr<Typeface const>, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI);
|
|
|
|
|
ScaledFontMetrics metrics() const;
|
|
|
|
|
~Font();
|
|
|
|
|
|
|
|
|
|
float point_size() const;
|
|
|
|
|
float pixel_size() const;
|
2025-06-17 14:32:20 +02:00
|
|
|
FontPixelMetrics const& pixel_metrics() const { return m_pixel_metrics; }
|
2025-04-19 19:10:01 +02:00
|
|
|
u8 slope() const { return m_typeface->slope(); }
|
|
|
|
|
u16 weight() const { return m_typeface->weight(); }
|
|
|
|
|
bool contains_glyph(u32 code_point) const { return m_typeface->glyph_id_for_code_point(code_point) > 0; }
|
|
|
|
|
float glyph_width(u32 code_point) const;
|
|
|
|
|
u32 glyph_id_for_code_point(u32 code_point) const { return m_typeface->glyph_id_for_code_point(code_point); }
|
|
|
|
|
float preferred_line_height() const { return metrics().height() + metrics().line_gap; }
|
|
|
|
|
int x_height() const { return m_point_height; } // FIXME: Read from font
|
|
|
|
|
u8 baseline() const { return m_point_height; } // FIXME: Read from font
|
|
|
|
|
float width(StringView) const;
|
|
|
|
|
float width(Utf8View const&) const;
|
2025-07-25 09:05:18 -04:00
|
|
|
float width(Utf16View const&) const;
|
2025-04-19 19:10:01 +02:00
|
|
|
FlyString const& family() const { return m_typeface->family(); }
|
|
|
|
|
|
|
|
|
|
NonnullRefPtr<Font> scaled_with_size(float point_size) const;
|
|
|
|
|
NonnullRefPtr<Font> with_size(float point_size) const;
|
|
|
|
|
|
|
|
|
|
Typeface const& typeface() const { return m_typeface; }
|
|
|
|
|
|
|
|
|
|
SkFont skia_font(float scale) const;
|
2023-02-09 11:55:05 +03:00
|
|
|
|
2021-07-20 02:14:29 +02:00
|
|
|
Font const& bold_variant() const;
|
2024-08-13 17:05:43 +01:00
|
|
|
hb_font_t* harfbuzz_font() const;
|
2021-07-20 02:14:29 +02:00
|
|
|
|
|
|
|
|
private:
|
2025-04-19 19:10:01 +02:00
|
|
|
mutable RefPtr<Font const> m_bold_variant;
|
2024-08-13 17:05:43 +01:00
|
|
|
mutable hb_font_t* m_harfbuzz_font { nullptr };
|
2025-04-19 19:10:01 +02:00
|
|
|
|
|
|
|
|
NonnullRefPtr<Typeface const> m_typeface;
|
|
|
|
|
float m_x_scale { 0.0f };
|
|
|
|
|
float m_y_scale { 0.0f };
|
|
|
|
|
float m_point_width { 0.0f };
|
|
|
|
|
float m_point_height { 0.0f };
|
|
|
|
|
FontPixelMetrics m_pixel_metrics;
|
|
|
|
|
|
|
|
|
|
float m_pixel_size { 0.0f };
|
2018-10-11 23:45:57 +02:00
|
|
|
};
|
2020-02-06 11:56:38 +01:00
|
|
|
|
|
|
|
|
}
|