[RICHED20] Sync with Wine Staging 4.18. CORE-16441
[reactos.git] / dll / win32 / riched20 / style.c
index c3f3fc3..8b9c212 100644 (file)
@@ -85,6 +85,8 @@ BOOL cf2w_to_cfany(CHARFORMAT2W *to, const CHARFORMAT2W *from)
     CopyMemory(t, from, FIELD_OFFSET(CHARFORMATA, szFaceName));
     WideCharToMultiByte(CP_ACP, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), NULL, NULL);
     t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
+    t->dwMask &= CFM_ALL;
+    t->dwEffects &= CFM_EFFECTS;
     return TRUE;
   }
   if (to->cbSize == sizeof(CHARFORMATW))
@@ -92,6 +94,8 @@ BOOL cf2w_to_cfany(CHARFORMAT2W *to, const CHARFORMAT2W *from)
     CHARFORMATW *t = (CHARFORMATW *)to;
     CopyMemory(t, from, sizeof(*t));
     t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
+    t->dwMask &= CFM_ALL;
+    t->dwEffects &= CFM_EFFECTS;
     return TRUE;
   }
   if (to->cbSize == sizeof(CHARFORMAT2A))
@@ -348,62 +352,11 @@ static BOOL ME_IsFontEqual(const LOGFONTW *p1, const LOGFONTW *p2)
 {
   if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
     return FALSE;
-  if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
+  if (wcscmp(p1->lfFaceName, p2->lfFaceName))
     return FALSE;
   return TRUE;
 }
 
-HFONT ME_SelectStyleFont(ME_Context *c, ME_Style *s)
-{
-  HFONT hOldFont;
-  LOGFONTW lf;
-  int i, nEmpty, nAge = 0x7FFFFFFF;
-  ME_FontCacheItem *item;
-  assert(s);
-  
-  ME_LogFontFromStyle(c, &lf, s);
-  
-  for (i=0; i<HFONT_CACHE_SIZE; i++)
-    c->editor->pFontCache[i].nAge++;
-  for (i=0, nEmpty=-1, nAge=0; i<HFONT_CACHE_SIZE; i++)
-  {
-    item = &c->editor->pFontCache[i];
-    if (!item->nRefs)
-    {
-      if (item->nAge > nAge)
-        nEmpty = i, nAge = item->nAge;
-    }
-    if (item->hFont && ME_IsFontEqual(&item->lfSpecs, &lf))
-      break;
-  }
-  if (i < HFONT_CACHE_SIZE) /* found */
-  {
-    item = &c->editor->pFontCache[i];
-    TRACE_(richedit_style)("font reused %d\n", i);
-    item->nRefs++;
-  }
-  else
-  {
-    item = &c->editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
-
-    assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
-    if (item->hFont) {
-      TRACE_(richedit_style)("font deleted %d\n", nEmpty);
-      DeleteObject(item->hFont);
-      item->hFont = NULL;
-    }
-    item->hFont = CreateFontIndirectW(&lf);
-    TRACE_(richedit_style)("font created %d\n", nEmpty);
-    item->nRefs = 1;
-    item->lfSpecs = lf;
-  }
-  s->font_cache = item;
-  hOldFont = SelectObject(c->hDC, item->hFont);
-  /* should be cached too, maybe ? */
-  GetTextMetricsW(c->hDC, &s->tm);
-  return hOldFont;
-}
-
 static void release_font_cache(ME_FontCacheItem *item)
 {
     if (item->nRefs > 0)
@@ -413,11 +366,77 @@ static void release_font_cache(ME_FontCacheItem *item)
     }
 }
 
-void ME_UnselectStyleFont(ME_Context *c, ME_Style *s, HFONT hOldFont)
+void select_style( ME_Context *c, ME_Style *s )
 {
-  SelectObject(c->hDC, hOldFont);
-  release_font_cache(s->font_cache);
-  s->font_cache = NULL;
+    HFONT old_font;
+    LOGFONTW lf;
+    int i, empty, age = 0x7FFFFFFF;
+    ME_FontCacheItem *item;
+
+    if (c->current_style == s) return;
+
+    if (s)
+    {
+        ME_LogFontFromStyle( c, &lf, s );
+
+        for (i = 0; i < HFONT_CACHE_SIZE; i++)
+            c->editor->pFontCache[i].nAge++;
+        for (i = 0, empty = -1, age = 0; i < HFONT_CACHE_SIZE; i++)
+        {
+            item = &c->editor->pFontCache[i];
+            if (!item->nRefs)
+            {
+                if (item->nAge > age)
+                {
+                    empty = i;
+                    age = item->nAge;
+                }
+            }
+
+            if (item->hFont && ME_IsFontEqual( &item->lfSpecs, &lf ))
+                break;
+        }
+
+        if (i < HFONT_CACHE_SIZE) /* found */
+        {
+            item = &c->editor->pFontCache[i];
+            TRACE_(richedit_style)( "font reused %d\n", i );
+            item->nRefs++;
+        }
+        else
+        {
+            assert(empty != -1);
+            item = &c->editor->pFontCache[empty];
+            if (item->hFont)
+            {
+                TRACE_(richedit_style)( "font deleted %d\n", empty );
+                DeleteObject(item->hFont);
+                item->hFont = NULL;
+            }
+            item->hFont = CreateFontIndirectW( &lf );
+            TRACE_(richedit_style)( "font created %d\n", empty );
+            item->nRefs = 1;
+            item->lfSpecs = lf;
+        }
+        s->font_cache = item;
+        old_font = SelectObject( c->hDC, item->hFont );
+        GetTextMetricsW( c->hDC, &s->tm );
+        if (!c->orig_font) c->orig_font = old_font;
+    }
+    else
+    {
+        SelectObject( c->hDC, c->orig_font );
+        c->orig_font = NULL;
+    }
+
+    if (c->current_style)
+    {
+        release_font_cache( c->current_style->font_cache );
+        c->current_style->font_cache = NULL;
+    }
+    c->current_style = s;
+
+    return;
 }
 
 void ME_DestroyStyle(ME_Style *s)