From 368f18016c96a1516e9e72bc151e523ec94622a8 Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Sat, 13 Mar 2021 19:21:25 +0100 Subject: Fast pango (#201) * Cache single unicode character widths under Xft+Pango --- .../Xlib/Fl_Xlib_Graphics_Driver_font_xft.cxx | 49 +++++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'src/drivers/Xlib/Fl_Xlib_Graphics_Driver_font_xft.cxx') diff --git a/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_font_xft.cxx b/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_font_xft.cxx index a8039df46..a755d881f 100644 --- a/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_font_xft.cxx +++ b/src/drivers/Xlib/Fl_Xlib_Graphics_Driver_font_xft.cxx @@ -1012,6 +1012,10 @@ float Fl_Xlib_Graphics_Driver::scale_bitmap_for_PostScript() { Fl_Xlib_Font_Descriptor::~Fl_Xlib_Font_Descriptor() { if (this == fl_graphics_driver->font_descriptor()) fl_graphics_driver->font_descriptor(NULL); // XftFontClose(fl_display, font); +#if USE_PANGO + if (width) for (int i = 0; i < 64; i++) delete[] width[i]; + delete[] width; +#endif } @@ -1347,7 +1351,45 @@ void Fl_Xlib_Graphics_Driver::do_draw(int from_right, const char *str, int n, in (y - y_correction - lheight + desc) * PANGO_SCALE ); // 1.8 } +// cache the widths of single Unicode characters +double Fl_Xlib_Graphics_Driver::width_unscaled(unsigned int utf32) { + unsigned r=0; + Fl_Xlib_Font_Descriptor *desc = NULL; + if (utf32 <= 0xFFFF) { + desc = (Fl_Xlib_Font_Descriptor*)font_descriptor(); + r = (utf32 & 0xFC00) >> 10; + if (!desc->width) { + desc->width = (int**)new int*[64]; + memset(desc->width, 0, 64*sizeof(int*)); + } + if (!desc->width[r]) { + desc->width[r] = (int*)new int[0x0400]; + for (int i = 0; i < 0x0400; i++) desc->width[r][i] = -1; + } else { + if ( desc->width[r][utf32&0x03FF] >= 0 ) { // already cached + return double(desc->width[r][utf32 & 0x03FF]); + } + } + } + char buf4[4]; + int n = fl_utf8encode(utf32, buf4); + double width = do_width_unscaled_(buf4, n); + if (utf32 <= 0xFFFF) { + desc->width[r][utf32 & 0x03FF] = (int)width; + } + return width; +} + double Fl_Xlib_Graphics_Driver::width_unscaled(const char* str, int n) { + if (n == fl_utf8len(*str)) { // str contains a single unicode character + int l; + unsigned c = fl_utf8decode(str, str+n, &l); + return width_unscaled(c); // that character's width may have been cached + } + return do_width_unscaled_(str, n); // full width computation for multi-char strings +} + +double Fl_Xlib_Graphics_Driver::do_width_unscaled_(const char* str, int n) { if (!n) return 0; if (!fl_display || size_ == 0) return -1; if (!playout_) context(); @@ -1373,12 +1415,6 @@ int Fl_Xlib_Graphics_Driver::height_unscaled() { else return -1; } -double Fl_Xlib_Graphics_Driver::width_unscaled(unsigned int c) { - char buf4[4]; - int n = fl_utf8encode(c, buf4); - return width_unscaled(buf4, n); -} - int Fl_Xlib_Graphics_Driver::descent_unscaled() { if (font_descriptor()) return ((Fl_Xlib_Font_Descriptor*)font_descriptor())->descent_; else return -1; @@ -1458,6 +1494,7 @@ Fl_Xlib_Font_Descriptor::Fl_Xlib_Font_Descriptor(const char* name, Fl_Fontsize f angle = fangle; height_ = 0; descent_ = 0; + width = NULL; } #endif // USE_PANGO -- cgit v1.2.3