summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/Font/ScaledFont.h
diff options
context:
space:
mode:
authorMacDue <macdue@dueutil.tech>2023-01-02 20:10:00 +0100
committerAndreas Kling <kling@serenityos.org>2023-01-05 12:09:35 +0100
commitada48a1daf24203b90730f178f4466d2393702c5 (patch)
treed56810dbb7c2697985e63e13491014e3e165d672 /Userland/Libraries/LibGfx/Font/ScaledFont.h
parenta1726b1ba5aa6d19949f682936b3ee95b368a370 (diff)
downloadserenity-ada48a1daf24203b90730f178f4466d2393702c5.zip
LibGfx: Add ability to request glyphs at subpixel offsets to fonts
This adds the option to pass a subpixel offset when fetching a glyph from a font, this offset is currently snapped to thirds of a pixel (i.e. 0, 0.33, 0.66). This is then used when rasterizing the glyph, which is then cached like usual. Note that when using subpixel offsets you're trading a bit of space for accuracy. With the current third of a pixel offsets you can end up with up to 9 bitmaps per glyph.
Diffstat (limited to 'Userland/Libraries/LibGfx/Font/ScaledFont.h')
-rw-r--r--Userland/Libraries/LibGfx/Font/ScaledFont.h25
1 files changed, 23 insertions, 2 deletions
diff --git a/Userland/Libraries/LibGfx/Font/ScaledFont.h b/Userland/Libraries/LibGfx/Font/ScaledFont.h
index 15d3fa7d40..e346c7b581 100644
--- a/Userland/Libraries/LibGfx/Font/ScaledFont.h
+++ b/Userland/Libraries/LibGfx/Font/ScaledFont.h
@@ -16,6 +16,13 @@
namespace Gfx {
+struct GlyphIndexWithSubpixelOffset {
+ u32 glyph_id;
+ GlyphSubpixelOffset subpixel_offset;
+
+ bool operator==(GlyphIndexWithSubpixelOffset const&) const = default;
+};
+
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)
@@ -30,7 +37,7 @@ public:
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;
+ RefPtr<Gfx::Bitmap> rasterize_glyph(u32 glyph_id, GlyphSubpixelOffset) const;
// ^Gfx::Font
virtual NonnullRefPtr<Font> clone() const override { return MUST(try_clone()); } // FIXME: clone() should not need to be implemented
@@ -42,6 +49,8 @@ public:
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 float glyph_left_bearing(u32 code_point) const override;
+ virtual Glyph glyph(u32 code_point, GlyphSubpixelOffset) const override;
virtual bool contains_glyph(u32 code_point) const override { return m_font->glyph_id_for_code_point(code_point) > 0; }
virtual float glyph_width(u32 code_point) const override;
virtual float glyph_or_emoji_width(u32 code_point) const override;
@@ -72,10 +81,22 @@ private:
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;
+ mutable HashMap<GlyphIndexWithSubpixelOffset, RefPtr<Gfx::Bitmap>> m_cached_glyph_bitmaps;
template<typename T>
float unicode_view_width(T const& view) const;
};
}
+
+namespace AK {
+
+template<>
+struct Traits<Gfx::GlyphIndexWithSubpixelOffset> : public GenericTraits<Gfx::GlyphIndexWithSubpixelOffset> {
+ static unsigned hash(Gfx::GlyphIndexWithSubpixelOffset const& index)
+ {
+ return pair_int_hash(index.glyph_id, (index.subpixel_offset.x << 8) | index.subpixel_offset.y);
+ }
+};
+
+}