diff options
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGfx/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/ScaledFont.cpp | 107 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/ScaledFont.h | 80 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/TrueType/Font.cpp | 104 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/TrueType/Font.h | 111 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/Typeface.cpp | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Font/VectorFont.h | 49 | ||||
-rw-r--r-- | Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 3 |
9 files changed, 260 insertions, 199 deletions
diff --git a/Userland/Libraries/LibGfx/CMakeLists.txt b/Userland/Libraries/LibGfx/CMakeLists.txt index 02ea23e353..55d1b31b84 100644 --- a/Userland/Libraries/LibGfx/CMakeLists.txt +++ b/Userland/Libraries/LibGfx/CMakeLists.txt @@ -17,6 +17,7 @@ set(SOURCES Font/BitmapFont.cpp Font/Emoji.cpp Font/FontDatabase.cpp + Font/ScaledFont.cpp Font/TrueType/Font.cpp Font/TrueType/Glyf.cpp Font/TrueType/Cmap.cpp diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.cpp b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp new file mode 100644 index 0000000000..8f5e9dc889 --- /dev/null +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <AK/Utf32View.h> +#include <AK/Utf8View.h> +#include <LibGfx/Font/ScaledFont.h> + +namespace Gfx { + +int ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); } +int ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); } +int ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); } + +template<typename T> +ALWAYS_INLINE int ScaledFont::unicode_view_width(T const& view) const +{ + if (view.is_empty()) + return 0; + int width = 0; + int longest_width = 0; + u32 last_code_point = 0; + for (auto code_point : view) { + if (code_point == '\n' || code_point == '\r') { + longest_width = max(width, longest_width); + width = 0; + last_code_point = code_point; + continue; + } + u32 glyph_id = glyph_id_for_code_point(code_point); + auto kerning = glyphs_horizontal_kerning(last_code_point, code_point); + width += kerning + glyph_metrics(glyph_id).advance_width; + last_code_point = code_point; + } + longest_width = max(width, longest_width); + return longest_width; +} + +RefPtr<Gfx::Bitmap> ScaledFont::rasterize_glyph(u32 glyph_id) const +{ + auto glyph_iterator = m_cached_glyph_bitmaps.find(glyph_id); + if (glyph_iterator != m_cached_glyph_bitmaps.end()) + return glyph_iterator->value; + + auto glyph_bitmap = m_font->rasterize_glyph(glyph_id, m_x_scale, m_y_scale); + m_cached_glyph_bitmaps.set(glyph_id, glyph_bitmap); + return glyph_bitmap; +} + +Gfx::Glyph ScaledFont::glyph(u32 code_point) const +{ + auto id = glyph_id_for_code_point(code_point); + auto bitmap = rasterize_glyph(id); + auto metrics = glyph_metrics(id); + return Gfx::Glyph(bitmap, metrics.left_side_bearing, metrics.advance_width, metrics.ascender); +} + +u8 ScaledFont::glyph_width(u32 code_point) const +{ + auto id = glyph_id_for_code_point(code_point); + auto metrics = glyph_metrics(id); + return metrics.advance_width; +} + +int ScaledFont::glyph_or_emoji_width(u32 code_point) const +{ + auto id = glyph_id_for_code_point(code_point); + auto metrics = glyph_metrics(id); + return metrics.advance_width; +} + +float ScaledFont::glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const +{ + if (left_code_point == 0 || right_code_point == 0) + return 0.f; + + auto left_glyph_id = glyph_id_for_code_point(left_code_point); + auto right_glyph_id = glyph_id_for_code_point(right_code_point); + if (left_glyph_id == 0 || right_glyph_id == 0) + return 0.f; + + return m_font->glyphs_horizontal_kerning(left_glyph_id, right_glyph_id, m_x_scale); +} + +u8 ScaledFont::glyph_fixed_width() const +{ + return glyph_metrics(glyph_id_for_code_point(' ')).advance_width; +} + +Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const +{ + auto metrics = m_font->metrics(m_x_scale, m_y_scale); + + return Gfx::FontPixelMetrics { + .size = (float)pixel_size(), + .x_height = (float)x_height(), + .advance_of_ascii_zero = (float)glyph_width('0'), + .glyph_spacing = (float)glyph_spacing(), + .ascent = metrics.ascender, + .descent = -metrics.descender, + .line_gap = metrics.line_gap, + }; +} + +} diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.h b/Userland/Libraries/LibGfx/Font/ScaledFont.h new file mode 100644 index 0000000000..95e19395ef --- /dev/null +++ b/Userland/Libraries/LibGfx/Font/ScaledFont.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/HashMap.h> +#include <LibGfx/Bitmap.h> +#include <LibGfx/Font/Font.h> +#include <LibGfx/Font/VectorFont.h> + +#define POINTS_PER_INCH 72.0f +#define DEFAULT_DPI 96 + +namespace Gfx { + +class ScaledFont : public Gfx::Font { +public: + ScaledFont(NonnullRefPtr<VectorFont> font, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI) + : m_font(move(font)) + , m_point_width(point_width) + , m_point_height(point_height) + { + float units_per_em = m_font->units_per_em(); + m_x_scale = (point_width * dpi_x) / (POINTS_PER_INCH * units_per_em); + m_y_scale = (point_height * dpi_y) / (POINTS_PER_INCH * units_per_em); + } + u32 glyph_id_for_code_point(u32 code_point) const { return m_font->glyph_id_for_code_point(code_point); } + ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); } + ScaledGlyphMetrics glyph_metrics(u32 glyph_id) const { return m_font->glyph_metrics(glyph_id, m_x_scale, m_y_scale); } + RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id) const; + + // ^Gfx::Font + virtual NonnullRefPtr<Font> clone() const override { return *this; } // FIXME: clone() should not need to be implemented + virtual u8 presentation_size() const override { return m_point_height; } + virtual int pixel_size() const override { return m_point_height * 1.33333333f; } + virtual float point_size() const override { return m_point_height; } + virtual Gfx::FontPixelMetrics pixel_metrics() const override; + virtual u8 slope() const override { return m_font->slope(); } + virtual u16 weight() const override { return m_font->weight(); } + virtual Gfx::Glyph glyph(u32 code_point) const override; + virtual bool contains_glyph(u32 code_point) const override { return m_font->glyph_id_for_code_point(code_point) > 0; } + virtual u8 glyph_width(u32 code_point) const override; + virtual int glyph_or_emoji_width(u32 code_point) const override; + virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const override; + virtual int preferred_line_height() const override { return metrics().height() + metrics().line_gap; } + virtual u8 glyph_height() const override { return m_point_height; } + virtual int x_height() const override { return m_point_height; } // FIXME: Read from font + virtual u8 min_glyph_width() const override { return 1; } // FIXME: Read from font + virtual u8 max_glyph_width() const override { return m_point_width; } // FIXME: Read from font + virtual u8 glyph_fixed_width() const override; + virtual u8 baseline() const override { return m_point_height; } // FIXME: Read from font + virtual u8 mean_line() const override { return m_point_height; } // FIXME: Read from font + virtual int width(StringView) const override; + virtual int width(Utf8View const&) const override; + virtual int width(Utf32View const&) const override; + virtual String name() const override { return String::formatted("{} {}", family(), variant()); } + virtual bool is_fixed_width() const override { return m_font->is_fixed_width(); } + virtual u8 glyph_spacing() const override { return 0; } + virtual size_t glyph_count() const override { return m_font->glyph_count(); } + virtual String family() const override { return m_font->family(); } + virtual String variant() const override { return m_font->variant(); } + virtual String qualified_name() const override { return String::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope()); } + virtual String human_readable_name() const override { return String::formatted("{} {} {}", family(), variant(), presentation_size()); } + +private: + NonnullRefPtr<VectorFont> m_font; + float m_x_scale { 0.0f }; + float m_y_scale { 0.0f }; + float m_point_width { 0.0f }; + float m_point_height { 0.0f }; + mutable HashMap<u32, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps; + + template<typename T> + int unicode_view_width(T const& view) const; +}; + +} diff --git a/Userland/Libraries/LibGfx/Font/TrueType/Font.cpp b/Userland/Libraries/LibGfx/Font/TrueType/Font.cpp index 2cf2dea9d0..5a73f5cd50 100644 --- a/Userland/Libraries/LibGfx/Font/TrueType/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/TrueType/Font.cpp @@ -8,8 +8,6 @@ #include <AK/Checked.h> #include <AK/Try.h> -#include <AK/Utf32View.h> -#include <AK/Utf8View.h> #include <LibCore/MappedFile.h> #include <LibGfx/Font/TrueType/Cmap.h> #include <LibGfx/Font/TrueType/Font.h> @@ -526,13 +524,13 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3 return adopt_ref(*new Font(move(buffer), move(head), move(name), move(hhea), move(maxp), move(hmtx), move(cmap), move(loca), move(glyf), move(os2), move(kern))); } -ScaledFontMetrics Font::metrics([[maybe_unused]] float x_scale, float y_scale) const +Gfx::ScaledFontMetrics Font::metrics([[maybe_unused]] float x_scale, float y_scale) const { auto ascender = m_hhea.ascender() * y_scale; auto descender = m_hhea.descender() * y_scale; auto line_gap = m_hhea.line_gap() * y_scale; - return ScaledFontMetrics { + return Gfx::ScaledFontMetrics { .ascender = ascender, .descender = descender, .line_gap = line_gap, @@ -540,7 +538,7 @@ ScaledFontMetrics Font::metrics([[maybe_unused]] float x_scale, float y_scale) c } // FIXME: "loca" and "glyf" are not available for CFF fonts. -ScaledGlyphMetrics Font::glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const +Gfx::ScaledGlyphMetrics Font::glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const { if (glyph_id >= glyph_count()) { glyph_id = 0; @@ -550,7 +548,7 @@ ScaledGlyphMetrics Font::glyph_metrics(u32 glyph_id, float x_scale, float y_scal auto glyph = m_glyf.glyph(glyph_offset); int ascender = glyph.ascender(); int descender = glyph.descender(); - return ScaledGlyphMetrics { + return Gfx::ScaledGlyphMetrics { .ascender = (int)roundf(ascender * y_scale), .descender = (int)roundf(descender * y_scale), .advance_width = (int)roundf(horizontal_metrics.advance_width * x_scale), @@ -644,100 +642,6 @@ bool Font::is_fixed_width() const return glyph_metrics(glyph_id_for_code_point('.'), 1, 1).advance_width == glyph_metrics(glyph_id_for_code_point('X'), 1, 1).advance_width; } -int ScaledFont::width(StringView view) const { return unicode_view_width(Utf8View(view)); } -int ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); } -int ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); } - -template<typename T> -ALWAYS_INLINE int ScaledFont::unicode_view_width(T const& view) const -{ - if (view.is_empty()) - return 0; - int width = 0; - int longest_width = 0; - u32 last_code_point = 0; - for (auto code_point : view) { - if (code_point == '\n' || code_point == '\r') { - longest_width = max(width, longest_width); - width = 0; - last_code_point = code_point; - continue; - } - u32 glyph_id = glyph_id_for_code_point(code_point); - auto kerning = glyphs_horizontal_kerning(last_code_point, code_point); - width += kerning + glyph_metrics(glyph_id).advance_width; - last_code_point = code_point; - } - longest_width = max(width, longest_width); - return longest_width; -} - -RefPtr<Gfx::Bitmap> ScaledFont::rasterize_glyph(u32 glyph_id) const -{ - auto glyph_iterator = m_cached_glyph_bitmaps.find(glyph_id); - if (glyph_iterator != m_cached_glyph_bitmaps.end()) - return glyph_iterator->value; - - auto glyph_bitmap = m_font->rasterize_glyph(glyph_id, m_x_scale, m_y_scale); - m_cached_glyph_bitmaps.set(glyph_id, glyph_bitmap); - return glyph_bitmap; -} - -Gfx::Glyph ScaledFont::glyph(u32 code_point) const -{ - auto id = glyph_id_for_code_point(code_point); - auto bitmap = rasterize_glyph(id); - auto metrics = glyph_metrics(id); - return Gfx::Glyph(bitmap, metrics.left_side_bearing, metrics.advance_width, metrics.ascender); -} - -u8 ScaledFont::glyph_width(u32 code_point) const -{ - auto id = glyph_id_for_code_point(code_point); - auto metrics = glyph_metrics(id); - return metrics.advance_width; -} - -int ScaledFont::glyph_or_emoji_width(u32 code_point) const -{ - auto id = glyph_id_for_code_point(code_point); - auto metrics = glyph_metrics(id); - return metrics.advance_width; -} - -float ScaledFont::glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const -{ - if (left_code_point == 0 || right_code_point == 0) - return 0.f; - - auto left_glyph_id = glyph_id_for_code_point(left_code_point); - auto right_glyph_id = glyph_id_for_code_point(right_code_point); - if (left_glyph_id == 0 || right_glyph_id == 0) - return 0.f; - - return m_font->glyphs_horizontal_kerning(left_glyph_id, right_glyph_id, m_x_scale); -} - -u8 ScaledFont::glyph_fixed_width() const -{ - return glyph_metrics(glyph_id_for_code_point(' ')).advance_width; -} - -Gfx::FontPixelMetrics ScaledFont::pixel_metrics() const -{ - auto metrics = m_font->metrics(m_x_scale, m_y_scale); - - return Gfx::FontPixelMetrics { - .size = (float)pixel_size(), - .x_height = (float)x_height(), - .advance_of_ascii_zero = (float)glyph_width('0'), - .glyph_spacing = (float)glyph_spacing(), - .ascent = metrics.ascender, - .descent = -metrics.descender, - .line_gap = metrics.line_gap, - }; -} - u16 OS2::weight_class() const { return be_u16(m_slice.offset_pointer((u32)Offsets::WeightClass)); diff --git a/Userland/Libraries/LibGfx/Font/TrueType/Font.h b/Userland/Libraries/LibGfx/Font/TrueType/Font.h index 17ed391468..1b6177f348 100644 --- a/Userland/Libraries/LibGfx/Font/TrueType/Font.h +++ b/Userland/Libraries/LibGfx/Font/TrueType/Font.h @@ -6,7 +6,6 @@ #pragma once -#include <AK/HashMap.h> #include <AK/Noncopyable.h> #include <AK/RefCounted.h> #include <AK/StringView.h> @@ -15,50 +14,29 @@ #include <LibGfx/Font/TrueType/Cmap.h> #include <LibGfx/Font/TrueType/Glyf.h> #include <LibGfx/Font/TrueType/Tables.h> -#include <LibGfx/Size.h> - -#define POINTS_PER_INCH 72.0f -#define DEFAULT_DPI 96 +#include <LibGfx/Font/VectorFont.h> namespace TTF { -struct ScaledFontMetrics { - float ascender { 0 }; - float descender { 0 }; - float line_gap { 0 }; - - int height() const - { - return ascender - descender; - } -}; - -struct ScaledGlyphMetrics { - int ascender; - int descender; - int advance_width; - int left_side_bearing; -}; - -class Font : public RefCounted<Font> { +class Font : public Gfx::VectorFont { AK_MAKE_NONCOPYABLE(Font); public: static ErrorOr<NonnullRefPtr<Font>> try_load_from_file(String path, unsigned index = 0); static ErrorOr<NonnullRefPtr<Font>> try_load_from_externally_owned_memory(ReadonlyBytes bytes, unsigned index = 0); - ScaledFontMetrics metrics(float x_scale, float y_scale) const; - ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const; - float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const; - RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale) const; - u32 glyph_count() const; - u16 units_per_em() const; - u32 glyph_id_for_code_point(u32 code_point) const { return m_cmap.glyph_id_for_code_point(code_point); } - String family() const; - String variant() const; - u16 weight() const; - u8 slope() const; - bool is_fixed_width() const; + virtual Gfx::ScaledFontMetrics metrics(float x_scale, float y_scale) const override; + virtual Gfx::ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const override; + virtual float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const override; + virtual RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale) const override; + virtual u32 glyph_count() const override; + virtual u16 units_per_em() const override; + virtual u32 glyph_id_for_code_point(u32 code_point) const override { return m_cmap.glyph_id_for_code_point(code_point); } + virtual String family() const override; + virtual String variant() const override; + virtual u16 weight() const override; + virtual u8 slope() const override; + virtual bool is_fixed_width() const override; private: enum class Offsets { @@ -106,65 +84,4 @@ private: Optional<Kern> m_kern; }; -class ScaledFont : public Gfx::Font { -public: - ScaledFont(NonnullRefPtr<TTF::Font> font, float point_width, float point_height, unsigned dpi_x = DEFAULT_DPI, unsigned dpi_y = DEFAULT_DPI) - : m_font(move(font)) - , m_point_width(point_width) - , m_point_height(point_height) - { - float units_per_em = m_font->units_per_em(); - m_x_scale = (point_width * dpi_x) / (POINTS_PER_INCH * units_per_em); - m_y_scale = (point_height * dpi_y) / (POINTS_PER_INCH * units_per_em); - } - u32 glyph_id_for_code_point(u32 code_point) const { return m_font->glyph_id_for_code_point(code_point); } - ScaledFontMetrics metrics() const { return m_font->metrics(m_x_scale, m_y_scale); } - ScaledGlyphMetrics glyph_metrics(u32 glyph_id) const { return m_font->glyph_metrics(glyph_id, m_x_scale, m_y_scale); } - RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id) const; - - // ^Gfx::Font - virtual NonnullRefPtr<Font> clone() const override { return *this; } // FIXME: clone() should not need to be implemented - virtual u8 presentation_size() const override { return m_point_height; } - virtual int pixel_size() const override { return m_point_height * 1.33333333f; } - virtual float point_size() const override { return m_point_height; } - virtual Gfx::FontPixelMetrics pixel_metrics() const override; - virtual u8 slope() const override { return m_font->slope(); } - virtual u16 weight() const override { return m_font->weight(); } - virtual Gfx::Glyph glyph(u32 code_point) const override; - virtual bool contains_glyph(u32 code_point) const override { return m_font->glyph_id_for_code_point(code_point) > 0; } - virtual u8 glyph_width(u32 code_point) const override; - virtual int glyph_or_emoji_width(u32 code_point) const override; - virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const override; - virtual int preferred_line_height() const override { return metrics().height() + metrics().line_gap; } - virtual u8 glyph_height() const override { return m_point_height; } - virtual int x_height() const override { return m_point_height; } // FIXME: Read from font - virtual u8 min_glyph_width() const override { return 1; } // FIXME: Read from font - virtual u8 max_glyph_width() const override { return m_point_width; } // FIXME: Read from font - virtual u8 glyph_fixed_width() const override; - virtual u8 baseline() const override { return m_point_height; } // FIXME: Read from font - virtual u8 mean_line() const override { return m_point_height; } // FIXME: Read from font - virtual int width(StringView) const override; - virtual int width(Utf8View const&) const override; - virtual int width(Utf32View const&) const override; - virtual String name() const override { return String::formatted("{} {}", family(), variant()); } - virtual bool is_fixed_width() const override { return m_font->is_fixed_width(); } - virtual u8 glyph_spacing() const override { return 0; } - virtual size_t glyph_count() const override { return m_font->glyph_count(); } - virtual String family() const override { return m_font->family(); } - virtual String variant() const override { return m_font->variant(); } - virtual String qualified_name() const override { return String::formatted("{} {} {} {}", family(), presentation_size(), weight(), slope()); } - virtual String human_readable_name() const override { return String::formatted("{} {} {}", family(), variant(), presentation_size()); } - -private: - NonnullRefPtr<TTF::Font> m_font; - float m_x_scale { 0.0f }; - float m_y_scale { 0.0f }; - float m_point_width { 0.0f }; - float m_point_height { 0.0f }; - mutable HashMap<u32, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps; - - template<typename T> - int unicode_view_width(T const& view) const; -}; - } diff --git a/Userland/Libraries/LibGfx/Font/Typeface.cpp b/Userland/Libraries/LibGfx/Font/Typeface.cpp index 0094b39404..a65dd311c1 100644 --- a/Userland/Libraries/LibGfx/Font/Typeface.cpp +++ b/Userland/Libraries/LibGfx/Font/Typeface.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibGfx/Font/ScaledFont.h> #include <LibGfx/Font/Typeface.h> namespace Gfx { @@ -53,7 +54,7 @@ RefPtr<Font> Typeface::get_font(float point_size, Font::AllowInexactSizeMatch al VERIFY(point_size > 0); if (m_ttf_font) - return adopt_ref(*new TTF::ScaledFont(*m_ttf_font, point_size, point_size)); + return adopt_ref(*new Gfx::ScaledFont(*m_ttf_font, point_size, point_size)); RefPtr<BitmapFont> best_match; int size = roundf(point_size); diff --git a/Userland/Libraries/LibGfx/Font/VectorFont.h b/Userland/Libraries/LibGfx/Font/VectorFont.h new file mode 100644 index 0000000000..4932818f4d --- /dev/null +++ b/Userland/Libraries/LibGfx/Font/VectorFont.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Noncopyable.h> +#include <AK/RefCounted.h> + +namespace Gfx { + +struct ScaledFontMetrics { + float ascender { 0 }; + float descender { 0 }; + float line_gap { 0 }; + + int height() const + { + return ascender - descender; + } +}; + +struct ScaledGlyphMetrics { + int ascender; + int descender; + int advance_width; + int left_side_bearing; +}; + +class VectorFont : public RefCounted<VectorFont> { +public: + virtual ~VectorFont() { } + virtual ScaledFontMetrics metrics(float x_scale, float y_scale) const = 0; + virtual ScaledGlyphMetrics glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const = 0; + virtual float glyphs_horizontal_kerning(u32 left_glyph_id, u32 right_glyph_id, float x_scale) const = 0; + virtual RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, float x_scale, float y_scale) const = 0; + virtual u32 glyph_count() const = 0; + virtual u16 units_per_em() const = 0; + virtual u32 glyph_id_for_code_point(u32 code_point) const = 0; + virtual String family() const = 0; + virtual String variant() const = 0; + virtual u16 weight() const = 0; + virtual u8 slope() const = 0; + virtual bool is_fixed_width() const = 0; +}; + +} diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp index 9a04255873..69ce4d3ac7 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibGfx/Font/ScaledFont.h> #include <LibPDF/CommonNames.h> #include <LibPDF/Fonts/TrueTypeFont.h> #include <LibPDF/Fonts/Type1Font.h> diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index aa39bf39b8..7f7f41f2dc 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -12,6 +12,7 @@ #include <LibGfx/Font/Font.h> #include <LibGfx/Font/FontDatabase.h> #include <LibGfx/Font/FontStyleMapping.h> +#include <LibGfx/Font/ScaledFont.h> #include <LibGfx/Font/TrueType/Font.h> #include <LibWeb/CSS/CSSFontFaceRule.h> #include <LibWeb/CSS/CSSStyleRule.h> @@ -65,7 +66,7 @@ public: { if (!m_ttf_font) return nullptr; - return adopt_ref(*new TTF::ScaledFont(*m_ttf_font, point_size, point_size)); + return adopt_ref(*new Gfx::ScaledFont(*m_ttf_font, point_size, point_size)); } private: |