From ac0eb346b746b1dd2b3698d8b03dbba9d584ef36 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Sat, 16 Aug 2025 13:47:52 +0200 Subject: [PATCH] add nfont FontTextExtents functions --- application/nfont.c | 76 +++++++++++++++++++++++++++++++++++++++++++++ application/nfont.h | 14 +++++++++ 2 files changed, 90 insertions(+) diff --git a/application/nfont.c b/application/nfont.c index cce3666..ab8c921 100644 --- a/application/nfont.c +++ b/application/nfont.c @@ -281,3 +281,79 @@ void FontDrawString32( } } + +void FontTextExtents8( + Display *dpy, + NFont *font, + const FcChar8 *string, + int len, + XGlyphInfo *extents) +{ + FcChar32 static_buffer[128]; + FcChar32 *buf = static_buffer; + if(len >= 128) { + buf = calloc(len, sizeof(FcChar32)); + } + + // convert string from utf8 to utf32 + int c = 0; + for(int i=0;i pos) { + const FcChar32 *segStr = string+pos; + int segLen = i-pos; + + // get extents of string segment + XGlyphInfo ext; + XftTextExtents32(dpy, currentFont, segStr, segLen, &ext); + + if(ext.height > ret.height) { + ret.height = ext.height; + } + ret.width += ext.width; + ret.xOff += ext.xOff; + + pos = i; + } + currentFont = cFont; + } + + if(pos < len) { + const FcChar32 *segStr = string+pos; + int segLen = len-pos; + // get extents of string segment + XGlyphInfo ext; + XftTextExtents32(dpy, currentFont, segStr, segLen, &ext); + + if(ext.height > ret.height) { + ret.height = ext.height; + } + ret.width += ext.width; + ret.xOff += ext.xOff; + } + + *extents = ret; +} diff --git a/application/nfont.h b/application/nfont.h index cbefd86..65c720b 100644 --- a/application/nfont.h +++ b/application/nfont.h @@ -81,6 +81,20 @@ void FontDrawString32( const FcChar32 *string, int len); +void FontTextExtents8( + Display *dpy, + NFont *font, + const FcChar8 *string, + int len, + XGlyphInfo *extents); + +void FontTextExtents32( + Display *dpy, + NFont *font, + const FcChar32 *string, + int len, + XGlyphInfo *extents); + #ifdef __cplusplus } #endif -- 2.47.3