From 10090acb534055447244ac348229ecc738af64ce Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Fri, 15 Aug 2025 18:42:11 +0200 Subject: [PATCH] add support for font substitution in the sidebar --- application/Sidebar.c | 2 +- application/nfont.c | 70 ++++++++++++++++++++++++++++++++++++++++++- application/nfont.h | 17 +++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/application/Sidebar.c b/application/Sidebar.c index fa08602..481caf2 100644 --- a/application/Sidebar.c +++ b/application/Sidebar.c @@ -316,7 +316,7 @@ static void sidebar_expose(Widget widget, XEvent *event, Region region) { XftDrawRect(s->sidebar.d, &s->sidebar.select2_bg, 0, i.index*height, s->core.width, height); } - XftDrawString8(s->sidebar.d, cg, s->sidebar.font->fonts->font, 10, i.index*height + xftFont->ascent, (const FcChar8*)name, strlen(name)); + FontDrawString8(s->sidebar.d, cg, s->sidebar.font, 10, i.index*height + xftFont->ascent, (const FcChar8*)name, strlen(name)); } } diff --git a/application/nfont.c b/application/nfont.c index bf677a0..cce3666 100644 --- a/application/nfont.c +++ b/application/nfont.c @@ -212,4 +212,72 @@ void FontUnref(NFont *font) { if(--font->ref == 0) { FontDestroy(font); } -} \ No newline at end of file +} + +void FontDrawString8( + XftDraw *draw, + const XftColor *color, + NFont *font, + int x, + int y, + const FcChar8 *string, + int len) +{ + 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 extents; + XftTextExtents32(XftDrawDisplay(draw), currentFont, segStr, segLen, &extents); + + // draw previous characters + XftDrawString32(draw, color, currentFont, x, y, segStr, segLen); + + x += extents.xOff; + pos = i; + } + currentFont = cFont; + } + + if(pos < len) { + const FcChar32 *segStr = string+pos; + int segLen = len-pos; + // draw previous characters + XftDrawString32(draw, color, currentFont, x, y, segStr, segLen); + } + +} diff --git a/application/nfont.h b/application/nfont.h index 179d289..cbefd86 100644 --- a/application/nfont.h +++ b/application/nfont.h @@ -63,6 +63,23 @@ void FontDestroy(NFont *f); NFont *FontRef(NFont *font); void FontUnref(NFont *font); +void FontDrawString8( + XftDraw *draw, + const XftColor *color, + NFont *font, + int x, + int y, + const FcChar8 *string, + int len); + +void FontDrawString32( + XftDraw *draw, + const XftColor *color, + NFont *font, + int x, + int y, + const FcChar32 *string, + int len); #ifdef __cplusplus } -- 2.47.3