1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:27:45 +00:00

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.
This commit is contained in:
MacDue 2023-01-02 20:10:00 +01:00 committed by Andreas Kling
parent a1726b1ba5
commit ada48a1daf
14 changed files with 121 additions and 21 deletions

View file

@ -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);
}
};
}