From 70ac013544f6ff67079a235ef5c67551654beea5 Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Fri, 16 Dec 2022 12:15:39 +0800 Subject: PangoWin32: Avoid initializing DirectWrite repeatedly Make sure that we use the GPrivate to grab the DirectWrite boilerplate items that we might have setup, and only attempt to initialize DirectWrite if the items have not been previously setup. --- pango/pangowin32-dwrite-fontmap.cpp | 2 +- pango/pangowin32.c | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pango/pangowin32-dwrite-fontmap.cpp b/pango/pangowin32-dwrite-fontmap.cpp index a2c03675..8f3a3095 100644 --- a/pango/pangowin32-dwrite-fontmap.cpp +++ b/pango/pangowin32-dwrite-fontmap.cpp @@ -424,7 +424,7 @@ pango_win32_font_description_from_logfontw_dwrite (const LOGFONTW *logfontw) PangoStretch stretch; PangoWin32DWriteItems *dwrite_items; - dwrite_items = pango_win32_init_direct_write (); + dwrite_items = pango_win32_get_direct_write_items (); if (dwrite_items == NULL) return NULL; diff --git a/pango/pangowin32.c b/pango/pangowin32.c index 5a26ee98..7b6325a5 100644 --- a/pango/pangowin32.c +++ b/pango/pangowin32.c @@ -133,7 +133,6 @@ HDC _pango_win32_get_display_dc (void) { HDC hdc = g_private_get (&display_dc_key); - PangoWin32DWriteItems *items; if (hdc == NULL) { @@ -154,12 +153,8 @@ _pango_win32_get_display_dc (void) #endif } - items = g_private_get (&dwrite_items); - if (items == NULL) - { - items = pango_win32_init_direct_write (); - g_private_set (&dwrite_items, items); - } + /* ensure DirectWrite is initialized */ + pango_win32_get_direct_write_items (); return hdc; } @@ -167,7 +162,15 @@ _pango_win32_get_display_dc (void) PangoWin32DWriteItems * pango_win32_get_direct_write_items (void) { - return g_private_get (&dwrite_items); + PangoWin32DWriteItems *items = g_private_get (&dwrite_items); + + if (items == NULL) + { + items = pango_win32_init_direct_write (); + g_private_set (&dwrite_items, items); + } + + return items; } /** -- cgit v1.2.1 From 3345bafa6f3c49afd6902449d298b583343abdfa Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Fri, 16 Dec 2022 17:50:20 +0800 Subject: pangowin32-dwrite-fontmap.cpp: Clean up code a bit We can just insert the IDWriteFont into our PangoWin32FontMap if we do need to create one. Just bail out if the IDWriteFont could not be created, which should be unlikely. --- pango/pangowin32-dwrite-fontmap.cpp | 72 ++++++++++++++----------------------- 1 file changed, 27 insertions(+), 45 deletions(-) diff --git a/pango/pangowin32-dwrite-fontmap.cpp b/pango/pangowin32-dwrite-fontmap.cpp index 8f3a3095..2db0972a 100644 --- a/pango/pangowin32-dwrite-fontmap.cpp +++ b/pango/pangowin32-dwrite-fontmap.cpp @@ -103,6 +103,32 @@ _pango_win32_get_dwrite_font_face_from_dwrite_font (IDWriteFont *font) return NULL; } +static IDWriteFont * +get_dwrite_font_from_pango_win32_font (PangoWin32Font *font) +{ + PangoWin32DWriteItems *dwrite_items = pango_win32_get_direct_write_items (); + IDWriteFont *dwrite_font = NULL; + PangoWin32FontMap *fontmap = PANGO_WIN32_FONT_MAP (font->fontmap); + + dwrite_font = (IDWriteFont *) g_hash_table_lookup (fontmap->dwrite_fonts, + &font->logfontw); + + /* create the IDWriteFont from the logfont underlying the PangoWin32Font if needed */ + if (dwrite_font == NULL) + { + if (SUCCEEDED (dwrite_items->gdi_interop->CreateFontFromLOGFONT (&font->logfontw, + &dwrite_font)) && + dwrite_font != NULL) + { + g_hash_table_insert (fontmap->dwrite_fonts, + &font->logfontw, + dwrite_font); + } + } + + return dwrite_font; +} + void * pango_win32_font_get_dwrite_font_face (PangoWin32Font *font) { @@ -111,7 +137,7 @@ pango_win32_font_get_dwrite_font_face (PangoWin32Font *font) IDWriteFont *dwrite_font = NULL; IDWriteFontFace *face = NULL; - dwrite_font = (IDWriteFont *) g_hash_table_lookup (win32fontmap->dwrite_fonts, &win32font->logfontw); + dwrite_font = get_dwrite_font_from_pango_win32_font (font); if (dwrite_font != NULL) return (void *)_pango_win32_get_dwrite_font_face_from_dwrite_font (dwrite_font); @@ -452,30 +478,6 @@ pango_win32_font_description_from_logfontw_dwrite (const LOGFONTW *logfontw) return desc; } -static IDWriteFont * -get_dwrite_font_from_pango_win32_font (PangoWin32Font *font, - gboolean *is_cleanup_dwrite_font) -{ - PangoWin32DWriteItems *dwrite_items = pango_win32_get_direct_write_items (); - IDWriteFont *dwrite_font = NULL; - - dwrite_font = (IDWriteFont *) g_hash_table_lookup (PANGO_WIN32_FONT_MAP (font->fontmap)->dwrite_fonts, - &font->logfontw); - - /* create the IDWriteFont from the logfont underlying the PangoWin32Font if needed */ - if (dwrite_font == NULL) - { - if (SUCCEEDED (dwrite_items->gdi_interop->CreateFontFromLOGFONT (&font->logfontw, - &dwrite_font)) && - dwrite_font != NULL) - *is_cleanup_dwrite_font = TRUE; - else - dwrite_font = NULL; - } - - return dwrite_font; -} - /* macros to help parse the 'gasp' font table, referring to FreeType2 */ #define DWRITE_UCHAR_USHORT( p, i, s ) ( (unsigned short)( ((const unsigned char *)(p))[(i)] ) << (s) ) @@ -490,28 +492,11 @@ get_dwrite_font_from_pango_win32_font (PangoWin32Font *font, gboolean pango_win32_dwrite_font_check_is_hinted (PangoWin32Font *font) { - IDWriteFont *dwrite_font = NULL; IDWriteFontFace *dwrite_font_face = NULL; - gboolean failed = FALSE; gboolean result = FALSE; - gboolean dwrite_font_release = FALSE; dwrite_font_face = (IDWriteFontFace *)pango_win32_font_get_dwrite_font_face (font); - if (dwrite_font_face == NULL) - { - dwrite_font = get_dwrite_font_from_pango_win32_font (font, &dwrite_font_release); - - if (dwrite_font != NULL) - dwrite_font_face = _pango_win32_get_dwrite_font_face_from_dwrite_font (dwrite_font); - else - { - g_warning ("Failed to retrieve IDWriteFont from PangoWin32Font"); - - return FALSE; - } - } - if (dwrite_font_face != NULL) { UINT32 gasp_tag = DWRITE_MAKE_OPENTYPE_TAG ('g', 'a', 's', 'p'); @@ -553,9 +538,6 @@ pango_win32_dwrite_font_check_is_hinted (PangoWin32Font *font) dwrite_font_face->Release (); } - if (dwrite_font_release) - dwrite_font->Release (); - return result; } -- cgit v1.2.1 From c7d7c9eab24ef52596f43f0e698bb6e7cb7f027b Mon Sep 17 00:00:00 2001 From: Chun-wei Fan Date: Fri, 16 Dec 2022 17:52:02 +0800 Subject: pangocairo-win32font.c: Drop an unused variable --- pango/pangocairo-win32font.c | 1 - 1 file changed, 1 deletion(-) diff --git a/pango/pangocairo-win32font.c b/pango/pangocairo-win32font.c index 0141c414..9d52dbcf 100644 --- a/pango/pangocairo-win32font.c +++ b/pango/pangocairo-win32font.c @@ -76,7 +76,6 @@ pango_cairo_win32_font_create_font_face (PangoCairoFont *font) PangoCairoWin32Font *cwfont = PANGO_CAIRO_WIN32_FONT (font); PangoWin32Font *win32font = &cwfont->font; void *dwrite_font_face = NULL; - gpointer dwrite_font = NULL; #ifdef HAVE_CAIRO_WIN32_DIRECTWRITE dwrite_font_face = pango_win32_font_get_dwrite_font_face (win32font); -- cgit v1.2.1