1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:47:34 +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

@ -86,6 +86,29 @@ private:
float m_ascent;
};
struct GlyphSubpixelOffset {
u8 x;
u8 y;
// TODO: Allow setting this at runtime via some config?
static constexpr int subpixel_divisions() { return 3; }
FloatPoint to_float_point() const { return FloatPoint(x / float(subpixel_divisions()), y / float(subpixel_divisions())); }
bool operator==(GlyphSubpixelOffset const&) const = default;
};
struct GlyphRasterPosition {
// Where the glyph bitmap should be drawn/blitted.
IntPoint blit_position;
// A subpixel offset to be used when rendering the glyph.
// This improves kerning and alignment at the expense of caching a few extra bitmaps.
// This is (currently) snapped to thirds of a subpixel (i.e. 0, 0.33, 0.66).
GlyphSubpixelOffset subpixel_offset;
static GlyphRasterPosition get_nearest_fit_for(FloatPoint position);
};
struct FontPixelMetrics {
float size { 0 };
float x_height { 0 };
@ -124,8 +147,10 @@ public:
virtual u16 weight() const = 0;
virtual Glyph glyph(u32 code_point) const = 0;
virtual Glyph glyph(u32 code_point, GlyphSubpixelOffset) const = 0;
virtual bool contains_glyph(u32 code_point) const = 0;
virtual float glyph_left_bearing(u32 code_point) const = 0;
virtual float glyph_width(u32 code_point) const = 0;
virtual float glyph_or_emoji_width(u32 code_point) const = 0;
virtual float glyphs_horizontal_kerning(u32 left_code_point, u32 right_code_point) const = 0;