Font: Allow setting dpi internally

This fixes some unexpected changes in text rendering for OpenFL which rely on a private function in Lime. Previously we defaulted at 72 dpi, which apparently is expected to layout the text properly. Most displays are 96 dpi or higher today, so we should probably look into this. For RenderGlyph, which was previously broken over several versions, we will now use a dpi of 96 for now. In the next version of lime, we absolutely should alter the function signature to allow for renderGlyph to accept a dpi argument.
This commit is contained in:
Chris Speciale
2024-10-30 12:49:30 -04:00
parent dbfd6615c0
commit 579efa5351
5 changed files with 27 additions and 19 deletions

View File

@@ -59,7 +59,7 @@ namespace lime {
int GetUnitsPerEM ();
int RenderGlyph (int index, Bytes *bytes, int offset = 0);
int RenderGlyphs (value indices, Bytes *bytes);
void SetSize (size_t size);
void SetSize (size_t size, size_t dpi);
void* library;
void* face;

View File

@@ -1602,21 +1602,21 @@ namespace lime {
}
void lime_font_set_size (value fontHandle, int fontSize) {
void lime_font_set_size (value fontHandle, int fontSize, int dpi) {
#ifdef LIME_FREETYPE
Font *font = (Font*)val_data (fontHandle);
font->SetSize (fontSize);
font->SetSize (fontSize, dpi);
#endif
}
HL_PRIM void HL_NAME(hl_font_set_size) (HL_CFFIPointer* fontHandle, int fontSize) {
HL_PRIM void HL_NAME(hl_font_set_size) (HL_CFFIPointer* fontHandle, int fontSize, int dpi) {
#ifdef LIME_FREETYPE
Font *font = (Font*)fontHandle->ptr;
font->SetSize (fontSize);
font->SetSize (fontSize, dpi);
#endif
}
@@ -4153,7 +4153,7 @@ namespace lime {
DEFINE_HL_PRIM (_DYN, hl_font_outline_decompose, _TCFFIPOINTER _I32);
DEFINE_HL_PRIM (_TBYTES, hl_font_render_glyph, _TCFFIPOINTER _I32 _TBYTES);
DEFINE_HL_PRIM (_TBYTES, hl_font_render_glyphs, _TCFFIPOINTER _ARR _TBYTES);
DEFINE_HL_PRIM (_VOID, hl_font_set_size, _TCFFIPOINTER _I32);
DEFINE_HL_PRIM (_VOID, hl_font_set_size, _TCFFIPOINTER _I32 _I32);
DEFINE_HL_PRIM (_VOID, hl_gamepad_add_mappings, _ARR);
DEFINE_HL_PRIM (_VOID, hl_gamepad_event_manager_register, _FUN(_VOID, _NO_ARG) _TGAMEPAD_EVENT);
DEFINE_HL_PRIM (_BYTES, hl_gamepad_get_device_guid, _I32);

View File

@@ -1039,14 +1039,22 @@ namespace lime {
return totalOffset;
}
void Font::SetSize(size_t size, size_t dpi)
{
//We changed the function signature to include a dpi argument which changes this from
//the default value of 72 for dpi. Any public api that uses this should probably be changed
//to allow setting the dpi in an appropriate future release.
size_t hdpi = dpi;
size_t vdpi = dpi;
void Font::SetSize (size_t size) {
size_t hdpi = 72;
size_t vdpi = 72;
FT_Set_Char_Size ((FT_Face)face, (int)(size*64), (int)(size*64), hdpi, vdpi);
FT_Set_Char_Size(
(FT_Face)face, //Handle to the target face object
0, //Char width in 1/64th of points (0 means same as height)
static_cast<int>(size * 64), //Char height in 1/64th of points
hdpi, //Horizontal DPI
vdpi //Vertical DPI
);
mSize = size;
}

View File

@@ -149,7 +149,7 @@ class NativeCFFI
@:cffi private static function lime_font_render_glyphs(handle:Dynamic, indices:Dynamic, data:Dynamic):Dynamic;
@:cffi private static function lime_font_set_size(handle:Dynamic, size:Int):Void;
@:cffi private static function lime_font_set_size(handle:Dynamic, size:Int, dpi:Int):Void;
@:cffi private static function lime_gamepad_add_mappings(mappings:Dynamic):Void;
@@ -439,7 +439,7 @@ class NativeCFFI
"lime_font_render_glyph", "oioo", false));
private static var lime_font_render_glyphs = new cpp.Callable<cpp.Object->cpp.Object->cpp.Object->cpp.Object>(cpp.Prime._loadPrime("lime",
"lime_font_render_glyphs", "oooo", false));
private static var lime_font_set_size = new cpp.Callable<cpp.Object->Int->cpp.Void>(cpp.Prime._loadPrime("lime", "lime_font_set_size", "oiv", false));
private static var lime_font_set_size = new cpp.Callable<cpp.Object->Int->Int->ccpp.Void>(cpp.Prime._loadPrime("lime", "lime_font_set_size", "oiiv", false));
private static var lime_gamepad_add_mappings = new cpp.Callable<cpp.Object->cpp.Void>(cpp.Prime._loadPrime("lime", "lime_gamepad_add_mappings", "ov",
false));
private static var lime_gamepad_get_device_guid = new cpp.Callable<Int->cpp.Object>(cpp.Prime._loadPrime("lime", "lime_gamepad_get_device_guid", "io",
@@ -672,7 +672,7 @@ class NativeCFFI
private static var lime_font_outline_decompose = CFFI.load("lime", "lime_font_outline_decompose", 2);
private static var lime_font_render_glyph = CFFI.load("lime", "lime_font_render_glyph", 3);
private static var lime_font_render_glyphs = CFFI.load("lime", "lime_font_render_glyphs", 3);
private static var lime_font_set_size = CFFI.load("lime", "lime_font_set_size", 2);
private static var lime_font_set_size = CFFI.load("lime", "lime_font_set_size", 3);
private static var lime_gamepad_add_mappings = CFFI.load("lime", "lime_gamepad_add_mappings", 1);
private static var lime_gamepad_get_device_guid = CFFI.load("lime", "lime_gamepad_get_device_guid", 1);
private static var lime_gamepad_get_device_name = CFFI.load("lime", "lime_gamepad_get_device_name", 1);
@@ -993,7 +993,7 @@ class NativeCFFI
return null;
}
@:hlNative("lime", "hl_font_set_size") private static function lime_font_set_size(handle:CFFIPointer, size:Int):Void {}
@:hlNative("lime", "hl_font_set_size") private static function lime_font_set_size(handle:CFFIPointer, size:Int, dpi:Int):Void {}
@:hlNative("lime", "hl_gamepad_add_mappings") private static function lime_gamepad_add_mappings(mappings:hl.NativeArray<String>):Void {}

View File

@@ -317,7 +317,7 @@ class Font
public function renderGlyph(glyph:Glyph, fontSize:Int):Image
{
#if (lime_cffi && !macro)
__setSize(fontSize);
__setSize(fontSize, 96);
// Allocate an estimated buffer size - adjust if necessary
var bytes:Bytes = Bytes.alloc(0); // Allocate some reasonable initial size
@@ -714,7 +714,7 @@ class Font
}
#end
@:noCompletion private function __setSize(size:Int):Void
@:noCompletion private function __setSize(size:Int, dpi:Int):Void
{
#if (lime_cffi && !macro)
NativeCFFI.lime_font_set_size(src, size);