ladybird/Userland/Libraries/LibGfx/Font/Font.cpp
MacDue ada48a1daf 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.
2023-01-05 12:09:35 +01:00

29 lines
865 B
C++

/*
* 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 } };
}
}