mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 02:47:35 +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:
parent
a1726b1ba5
commit
ada48a1daf
14 changed files with 121 additions and 21 deletions
29
Userland/Libraries/LibGfx/Font/Font.cpp
Normal file
29
Userland/Libraries/LibGfx/Font/Font.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGfx/Font/Font.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
GlyphRasterPosition GlyphRasterPosition::get_nearest_fit_for(FloatPoint position)
|
||||
{
|
||||
constexpr auto subpixel_divisions = GlyphSubpixelOffset::subpixel_divisions();
|
||||
auto fit = [](float pos, int& blit_pos, u8& subpixel_offset) {
|
||||
blit_pos = floorf(pos);
|
||||
subpixel_offset = round_to<u8>((pos - blit_pos) / (1.0f / subpixel_divisions));
|
||||
if (subpixel_offset >= subpixel_divisions) {
|
||||
blit_pos += 1;
|
||||
subpixel_offset = 0;
|
||||
}
|
||||
};
|
||||
int blit_x, blit_y;
|
||||
u8 subpixel_x, subpixel_y;
|
||||
fit(position.x(), blit_x, subpixel_x);
|
||||
fit(position.y(), blit_y, subpixel_y);
|
||||
return GlyphRasterPosition { { blit_x, blit_y }, { subpixel_x, subpixel_y } };
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue