changeset 92151:664a09af03dc

(w32font_text_extents): Avoid getting HDC and selecting a font into it unless we have to.
author Jason Rumney <jasonr@gnu.org>
date Sun, 24 Feb 2008 15:07:46 +0000
parents 1c088baa9d2d
children 2c181d4110e5
files src/w32font.c
diffstat 1 files changed, 40 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/w32font.c	Sun Feb 24 13:36:39 2008 +0000
+++ b/src/w32font.c	Sun Feb 24 15:07:46 2008 +0000
@@ -290,8 +290,8 @@
      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 @@
      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 @@
               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 @@
         }
     }
 
+  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;