From 2a36efcfc245388b81913d2b192ee9ca74cb4a04 Mon Sep 17 00:00:00 2001 From: Jason Rumney Date: Sun, 24 Feb 2008 15:08:06 +0000 Subject: [PATCH] (w32font_text_extents): Avoid getting HDC and selecting a font into it unless we have to. --- src/ChangeLog | 5 ++++ src/w32font.c | 64 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 29a4e7ba636..afddcca381b 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2008-02-24 Jason Rumney + + * w32font.c (w32font_text_extents): Avoid getting HDC and selecting + a font into it unless we have to. + 2008-02-19 Stefan Monnier * intervals.h (INT_LISPLIKE): Remove. It may misfire. diff --git a/src/w32font.c b/src/w32font.c index 5776c95d0a7..d0cbe508d4f 100644 --- a/src/w32font.c +++ b/src/w32font.c @@ -290,8 +290,8 @@ w32font_text_extents (font, code, nglyphs, metrics) struct font_metrics *metrics; { int i; - HFONT old_font; - HDC dc; + HFONT old_font = NULL; + HDC dc = NULL; struct frame * f; int total_width = 0; WORD *wcode = alloca(nglyphs * sizeof (WORD)); @@ -302,9 +302,6 @@ w32font_text_extents (font, code, nglyphs, metrics) until the API is updated to pass in a frame. */ f = XFRAME (selected_frame); - dc = get_frame_dc (f); - old_font = SelectObject (dc, ((W32FontStruct *)(font->font.font))->hfont); - if (metrics) { GLYPHMETRICS gm; @@ -339,33 +336,45 @@ w32font_text_extents (font, code, nglyphs, metrics) metrics->ascent = max (metrics->ascent, char_metric->ascent); metrics->descent = max (metrics->descent, char_metric->descent); } - else if (GetGlyphOutlineW (dc, *(code + i), GGO_METRICS, &gm, 0, - NULL, &transform) != GDI_ERROR) - { - int new_val = metrics->width + gm.gmBlackBoxX - + gm.gmptGlyphOrigin.x; - metrics->rbearing = max (metrics->rbearing, new_val); - new_val = -gm.gmptGlyphOrigin.x - metrics->width; - metrics->lbearing = max (metrics->lbearing, new_val); - metrics->width += gm.gmCellIncX; - new_val = -gm.gmptGlyphOrigin.y; - metrics->ascent = max (metrics->ascent, new_val); - new_val = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y; - metrics->descent = max (metrics->descent, new_val); - } else { - /* Rely on an estimate based on the overall font metrics. */ - break; + if (dc == NULL) + { + dc = get_frame_dc (f); + old_font = SelectObject (dc, ((W32FontStruct *) + (font->font.font))->hfont); + } + if (GetGlyphOutlineW (dc, *(code + i), GGO_METRICS, &gm, 0, + NULL, &transform) != GDI_ERROR) + { + int new_val = metrics->width + gm.gmBlackBoxX + + gm.gmptGlyphOrigin.x; + metrics->rbearing = max (metrics->rbearing, new_val); + new_val = -gm.gmptGlyphOrigin.x - metrics->width; + metrics->lbearing = max (metrics->lbearing, new_val); + metrics->width += gm.gmCellIncX; + new_val = -gm.gmptGlyphOrigin.y; + metrics->ascent = max (metrics->ascent, new_val); + new_val = gm.gmBlackBoxY + gm.gmptGlyphOrigin.y; + metrics->descent = max (metrics->descent, new_val); + } + else + { + /* Rely on an estimate based on the overall font metrics. */ + break; + } } } /* If we got through everything, return. */ if (i == nglyphs) { - /* Restore state and release DC. */ - SelectObject (dc, old_font); - release_frame_dc (f, dc); + if (dc != NULL) + { + /* Restore state and release DC. */ + SelectObject (dc, old_font); + release_frame_dc (f, dc); + } return metrics->width; } @@ -382,6 +391,13 @@ w32font_text_extents (font, code, nglyphs, metrics) } } + if (dc == NULL) + { + dc = get_frame_dc (f); + old_font = SelectObject (dc, ((W32FontStruct *) + (font->font.font))->hfont); + } + if (GetTextExtentPoint32W (dc, wcode, nglyphs, &size)) { total_width = size.cx; -- 2.39.2