[PATCH 1/3] riched20: Store the font cache entry rather than the HFONT.

Huw Davies huw at codeweavers.com
Mon Nov 9 10:11:41 CST 2015


This also removes an incorrect DeleteObject() from DestroyStyle().

Signed-off-by: Huw Davies <huw at codeweavers.com>
---
 dlls/riched20/editstr.h | 22 +++++++++++-----------
 dlls/riched20/style.c   | 46 ++++++++++++++++++++--------------------------
 2 files changed, 31 insertions(+), 37 deletions(-)

diff --git a/dlls/riched20/editstr.h b/dlls/riched20/editstr.h
index 90f7fa0..3e98dac 100644
--- a/dlls/riched20/editstr.h
+++ b/dlls/riched20/editstr.h
@@ -60,11 +60,21 @@ typedef struct tagME_String
   int nLen, nBuffer;
 } ME_String;
 
+typedef struct tagME_FontCacheItem
+{
+  LOGFONTW lfSpecs;
+  HFONT hFont;
+  int nRefs;
+  int nAge;
+} ME_FontCacheItem;
+
+#define HFONT_CACHE_SIZE 10
+
 typedef struct tagME_Style
 {
   CHARFORMAT2W fmt;
 
-  HFONT hFont; /* cached font for the style */
+  ME_FontCacheItem *font_cache; /* cached font for the style */
   TEXTMETRICW tm; /* cached font metrics for the style */
   int nRefs; /* reference count */
   SCRIPT_CACHE script_cache;
@@ -372,16 +382,6 @@ typedef struct tagME_OutStream {
   UINT nNestingLevel;
 } ME_OutStream;
 
-typedef struct tagME_FontCacheItem
-{
-  LOGFONTW lfSpecs;
-  HFONT hFont;
-  int nRefs;
-  int nAge;
-} ME_FontCacheItem;
-
-#define HFONT_CACHE_SIZE 10
-
 typedef struct tagME_TextEditor
 {
   HWND hWnd, hwndParent;
diff --git a/dlls/riched20/style.c b/dlls/riched20/style.c
index 0817ea8..0e990e2 100644
--- a/dlls/riched20/style.c
+++ b/dlls/riched20/style.c
@@ -146,7 +146,7 @@ ME_Style *ME_MakeStyle(CHARFORMAT2W *style)
   assert(style->cbSize == sizeof(CHARFORMAT2W));
   s->fmt = *style;
   s->nRefs = 1;
-  s->hFont = NULL;
+  s->font_cache = NULL;
   memset(&s->tm, 0, sizeof(s->tm));
   s->tm.tmAscent = -1;
   s->script_cache = NULL;
@@ -383,8 +383,6 @@ HFONT ME_SelectStyleFont(ME_Context *c, ME_Style *s)
   {
     item = &c->editor->pFontCache[i];
     TRACE_(richedit_style)("font reused %d\n", i);
-
-    s->hFont = item->hFont;
     item->nRefs++;
   }
   else
@@ -397,44 +395,40 @@ HFONT ME_SelectStyleFont(ME_Context *c, ME_Style *s)
       DeleteObject(item->hFont);
       item->hFont = NULL;
     }
-    s->hFont = CreateFontIndirectW(&lf);
-    assert(s->hFont);
+    item->hFont = CreateFontIndirectW(&lf);
     TRACE_(richedit_style)("font created %d\n", nEmpty);
-    item->hFont = s->hFont;
     item->nRefs = 1;
     item->lfSpecs = lf;
   }
-  hOldFont = SelectObject(c->hDC, s->hFont);
+  s->font_cache = item;
+  hOldFont = SelectObject(c->hDC, item->hFont);
   /* should be cached too, maybe ? */
   GetTextMetricsW(c->hDC, &s->tm);
   return hOldFont;
 }
 
-void ME_UnselectStyleFont(ME_Context *c, ME_Style *s, HFONT hOldFont)
+static void release_font_cache(ME_FontCacheItem *item)
 {
-  int i;
-  
-  assert(s);
-  SelectObject(c->hDC, hOldFont);
-  for (i=0; i<HFONT_CACHE_SIZE; i++)
-  {
-    ME_FontCacheItem *pItem = &c->editor->pFontCache[i];
-    if (pItem->hFont == s->hFont && pItem->nRefs > 0)
+    if (item->nRefs > 0)
     {
-      pItem->nRefs--;
-      pItem->nAge = 0;
-      s->hFont = NULL;
-      return;
+        item->nRefs--;
+        item->nAge = 0;
     }
-  }
-  assert(0 == "UnselectStyleFont without SelectStyleFont");
 }
 
-static void ME_DestroyStyle(ME_Style *s) {
-  if (s->hFont)
+void ME_UnselectStyleFont(ME_Context *c, ME_Style *s, HFONT hOldFont)
+{
+  SelectObject(c->hDC, hOldFont);
+  release_font_cache(s->font_cache);
+  s->font_cache = NULL;
+}
+
+static void ME_DestroyStyle(ME_Style *s)
+{
+  if (s->font_cache)
   {
-    DeleteObject(s->hFont);
-    s->hFont = NULL;
+    release_font_cache( s->font_cache );
+    s->font_cache = NULL;
   }
   ScriptFreeCache( &s->script_cache );
   FREE_OBJ(s);
-- 
1.8.0




More information about the wine-patches mailing list