1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "gfxGDIFont.h"
7 
8 #include "mozilla/MemoryReporting.h"
9 #include "mozilla/Sprintf.h"
10 #include "mozilla/WindowsVersion.h"
11 
12 #include <algorithm>
13 #include "gfxWindowsPlatform.h"
14 #include "gfxContext.h"
15 #include "mozilla/Preferences.h"
16 #include "nsUnicodeProperties.h"
17 #include "gfxFontConstants.h"
18 #include "gfxTextRun.h"
19 
20 #include "cairo-win32.h"
21 
22 #define ROUND(x) floor((x) + 0.5)
23 
24 using namespace mozilla;
25 using namespace mozilla::gfx;
26 using namespace mozilla::unicode;
27 
gfxGDIFont(GDIFontEntry * aFontEntry,const gfxFontStyle * aFontStyle,AntialiasOption anAAOption)28 gfxGDIFont::gfxGDIFont(GDIFontEntry* aFontEntry, const gfxFontStyle* aFontStyle,
29                        AntialiasOption anAAOption)
30     : gfxFont(nullptr, aFontEntry, aFontStyle, anAAOption),
31       mFont(nullptr),
32       mMetrics(nullptr),
33       mIsBitmap(false),
34       mScriptCache(nullptr) {
35   mNeedsSyntheticBold = aFontStyle->NeedsSyntheticBold(aFontEntry);
36 
37   Initialize();
38 
39   if (mFont) {
40     mUnscaledFont = aFontEntry->LookupUnscaledFont(mFont);
41   }
42 }
43 
~gfxGDIFont()44 gfxGDIFont::~gfxGDIFont() {
45   if (mFont) {
46     ::DeleteObject(mFont);
47   }
48   if (mScriptCache) {
49     ScriptFreeCache(&mScriptCache);
50   }
51   delete mMetrics;
52 }
53 
CopyWithAntialiasOption(AntialiasOption anAAOption)54 UniquePtr<gfxFont> gfxGDIFont::CopyWithAntialiasOption(
55     AntialiasOption anAAOption) {
56   auto entry = static_cast<GDIFontEntry*>(mFontEntry.get());
57   return MakeUnique<gfxGDIFont>(entry, &mStyle, anAAOption);
58 }
59 
ShapeText(DrawTarget * aDrawTarget,const char16_t * aText,uint32_t aOffset,uint32_t aLength,Script aScript,nsAtom * aLanguage,bool aVertical,RoundingFlags aRounding,gfxShapedText * aShapedText)60 bool gfxGDIFont::ShapeText(DrawTarget* aDrawTarget, const char16_t* aText,
61                            uint32_t aOffset, uint32_t aLength, Script aScript,
62                            nsAtom* aLanguage, bool aVertical,
63                            RoundingFlags aRounding,
64                            gfxShapedText* aShapedText) {
65   if (!mIsValid) {
66     NS_WARNING("invalid font! expect incorrect text rendering");
67     return false;
68   }
69 
70   return gfxFont::ShapeText(aDrawTarget, aText, aOffset, aLength, aScript,
71                             aLanguage, aVertical, aRounding, aShapedText);
72 }
73 
GetHorizontalMetrics()74 const gfxFont::Metrics& gfxGDIFont::GetHorizontalMetrics() { return *mMetrics; }
75 
GetScaledFont(DrawTarget * aTarget)76 already_AddRefed<ScaledFont> gfxGDIFont::GetScaledFont(DrawTarget* aTarget) {
77   if (!mAzureScaledFont) {
78     LOGFONT lf;
79     GetObject(GetHFONT(), sizeof(LOGFONT), &lf);
80 
81     mAzureScaledFont = Factory::CreateScaledFontForGDIFont(
82         &lf, GetUnscaledFont(), GetAdjustedSize());
83     InitializeScaledFont();
84   }
85 
86   RefPtr<ScaledFont> scaledFont(mAzureScaledFont);
87   return scaledFont.forget();
88 }
89 
Measure(const gfxTextRun * aTextRun,uint32_t aStart,uint32_t aEnd,BoundingBoxType aBoundingBoxType,DrawTarget * aRefDrawTarget,Spacing * aSpacing,gfx::ShapedTextFlags aOrientation)90 gfxFont::RunMetrics gfxGDIFont::Measure(const gfxTextRun* aTextRun,
91                                         uint32_t aStart, uint32_t aEnd,
92                                         BoundingBoxType aBoundingBoxType,
93                                         DrawTarget* aRefDrawTarget,
94                                         Spacing* aSpacing,
95                                         gfx::ShapedTextFlags aOrientation) {
96   gfxFont::RunMetrics metrics =
97       gfxFont::Measure(aTextRun, aStart, aEnd, aBoundingBoxType, aRefDrawTarget,
98                        aSpacing, aOrientation);
99 
100   // if aBoundingBoxType is LOOSE_INK_EXTENTS
101   // and the underlying cairo font may be antialiased,
102   // we can't trust Windows to have considered all the pixels
103   // so we need to add "padding" to the bounds.
104   // (see bugs 475968, 439831, compare also bug 445087)
105   if (aBoundingBoxType == LOOSE_INK_EXTENTS &&
106       mAntialiasOption != kAntialiasNone && metrics.mBoundingBox.Width() > 0) {
107     metrics.mBoundingBox.MoveByX(-aTextRun->GetAppUnitsPerDevUnit());
108     metrics.mBoundingBox.SetWidth(metrics.mBoundingBox.Width() +
109                                   aTextRun->GetAppUnitsPerDevUnit() * 3);
110   }
111 
112   return metrics;
113 }
114 
Initialize()115 void gfxGDIFont::Initialize() {
116   NS_ASSERTION(!mMetrics, "re-creating metrics? this will leak");
117 
118   LOGFONTW logFont;
119 
120   if (mAdjustedSize == 0.0) {
121     mAdjustedSize = GetAdjustedSize();
122     if (FontSizeAdjust::Tag(mStyle.sizeAdjustBasis) !=
123         FontSizeAdjust::Tag::None) {
124       if (mStyle.sizeAdjust > 0.0 && mAdjustedSize > 0.0) {
125         // to implement font-size-adjust, we first create the "unadjusted" font
126         FillLogFont(logFont, mAdjustedSize);
127         mFont = ::CreateFontIndirectW(&logFont);
128 
129         // initialize its metrics so we can calculate size adjustment
130         Initialize();
131 
132         // Unless the font was so small that GDI metrics rounded to zero,
133         // calculate the properly adjusted size, and then proceed
134         // to recreate mFont and recalculate metrics
135         if (mMetrics->emHeight > 0.0) {
136           gfxFloat aspect;
137           switch (FontSizeAdjust::Tag(mStyle.sizeAdjustBasis)) {
138             default:
139               MOZ_ASSERT_UNREACHABLE("unhandled sizeAdjustBasis?");
140               aspect = 0.0;
141               break;
142             case FontSizeAdjust::Tag::ExHeight:
143               aspect = mMetrics->xHeight / mMetrics->emHeight;
144               break;
145             case FontSizeAdjust::Tag::CapHeight:
146               aspect = mMetrics->capHeight / mMetrics->emHeight;
147               break;
148             case FontSizeAdjust::Tag::ChWidth: {
149               gfxFloat advance = GetCharAdvance('0');
150               aspect = advance > 0.0 ? advance / mMetrics->emHeight : 0.5;
151               break;
152             }
153             case FontSizeAdjust::Tag::IcWidth:
154             case FontSizeAdjust::Tag::IcHeight: {
155               bool vertical = FontSizeAdjust::Tag(mStyle.sizeAdjustBasis) ==
156                               FontSizeAdjust::Tag::IcHeight;
157               gfxFloat advance = GetCharAdvance(0x6C34, vertical);
158               aspect = advance > 0.0 ? advance / mMetrics->emHeight : 1.0;
159               break;
160             }
161           }
162           if (aspect > 0.0) {
163             // If we created a shaper above (to measure glyphs), discard it so
164             // we get a new one for the adjusted scaling.
165             mHarfBuzzShaper = nullptr;
166             mAdjustedSize = mStyle.GetAdjustedSize(aspect);
167           }
168         }
169 
170         // delete the temporary font and metrics
171         ::DeleteObject(mFont);
172         mFont = nullptr;
173         delete mMetrics;
174         mMetrics = nullptr;
175       } else {
176         mAdjustedSize = 0.0;
177       }
178     }
179   }
180 
181   // (bug 724231) for local user fonts, we don't use GDI's synthetic bold,
182   // as it could lead to a different, incompatible face being used
183   // but instead do our own multi-striking
184   if (mNeedsSyntheticBold && GetFontEntry()->IsLocalUserFont()) {
185     mApplySyntheticBold = true;
186   }
187 
188   // this may end up being zero
189   mAdjustedSize = ROUND(mAdjustedSize);
190   FillLogFont(logFont, mAdjustedSize);
191   mFont = ::CreateFontIndirectW(&logFont);
192 
193   mMetrics = new gfxFont::Metrics;
194   ::memset(mMetrics, 0, sizeof(*mMetrics));
195 
196   if (!mFont) {
197     NS_WARNING("Failed creating GDI font");
198     mIsValid = false;
199     return;
200   }
201 
202   AutoDC dc;
203   SetGraphicsMode(dc.GetDC(), GM_ADVANCED);
204   AutoSelectFont selectFont(dc.GetDC(), mFont);
205 
206   // Get font metrics if size > 0
207   if (mAdjustedSize > 0.0) {
208     OUTLINETEXTMETRIC oMetrics;
209     TEXTMETRIC& metrics = oMetrics.otmTextMetrics;
210 
211     if (0 < GetOutlineTextMetrics(dc.GetDC(), sizeof(oMetrics), &oMetrics)) {
212       mMetrics->strikeoutSize = (double)oMetrics.otmsStrikeoutSize;
213       mMetrics->strikeoutOffset = (double)oMetrics.otmsStrikeoutPosition;
214       mMetrics->underlineSize = (double)oMetrics.otmsUnderscoreSize;
215       mMetrics->underlineOffset = (double)oMetrics.otmsUnderscorePosition;
216 
217       const MAT2 kIdentityMatrix = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
218       GLYPHMETRICS gm;
219       DWORD len = GetGlyphOutlineW(dc.GetDC(), char16_t('x'), GGO_METRICS, &gm,
220                                    0, nullptr, &kIdentityMatrix);
221       if (len == GDI_ERROR || gm.gmptGlyphOrigin.y <= 0) {
222         // 56% of ascent, best guess for true type
223         mMetrics->xHeight =
224             ROUND((double)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR);
225       } else {
226         mMetrics->xHeight = gm.gmptGlyphOrigin.y;
227       }
228       len = GetGlyphOutlineW(dc.GetDC(), char16_t('H'), GGO_METRICS, &gm, 0,
229                              nullptr, &kIdentityMatrix);
230       if (len == GDI_ERROR || gm.gmptGlyphOrigin.y <= 0) {
231         mMetrics->capHeight = metrics.tmAscent - metrics.tmInternalLeading;
232       } else {
233         mMetrics->capHeight = gm.gmptGlyphOrigin.y;
234       }
235       mMetrics->emHeight = metrics.tmHeight - metrics.tmInternalLeading;
236       gfxFloat typEmHeight =
237           (double)oMetrics.otmAscent - (double)oMetrics.otmDescent;
238       mMetrics->emAscent =
239           ROUND(mMetrics->emHeight * (double)oMetrics.otmAscent / typEmHeight);
240       mMetrics->emDescent = mMetrics->emHeight - mMetrics->emAscent;
241       if (oMetrics.otmEMSquare > 0) {
242         mFUnitsConvFactor = float(mAdjustedSize / oMetrics.otmEMSquare);
243       }
244     } else {
245       // Make a best-effort guess at extended metrics
246       // this is based on general typographic guidelines
247 
248       // GetTextMetrics can fail if the font file has been removed
249       // or corrupted recently.
250       BOOL result = GetTextMetrics(dc.GetDC(), &metrics);
251       if (!result) {
252         NS_WARNING("Missing or corrupt font data, fasten your seatbelt");
253         mIsValid = false;
254         memset(mMetrics, 0, sizeof(*mMetrics));
255         return;
256       }
257 
258       mMetrics->xHeight =
259           ROUND((float)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR);
260       mMetrics->strikeoutSize = 1;
261       mMetrics->strikeoutOffset =
262           ROUND(mMetrics->xHeight * 0.5f);  // 50% of xHeight
263       mMetrics->underlineSize = 1;
264       mMetrics->underlineOffset =
265           -ROUND((float)metrics.tmDescent * 0.30f);  // 30% of descent
266       mMetrics->emHeight = metrics.tmHeight - metrics.tmInternalLeading;
267       mMetrics->emAscent = metrics.tmAscent - metrics.tmInternalLeading;
268       mMetrics->emDescent = metrics.tmDescent;
269       mMetrics->capHeight = mMetrics->emAscent;
270     }
271 
272     mMetrics->internalLeading = metrics.tmInternalLeading;
273     mMetrics->externalLeading = metrics.tmExternalLeading;
274     mMetrics->maxHeight = metrics.tmHeight;
275     mMetrics->maxAscent = metrics.tmAscent;
276     mMetrics->maxDescent = metrics.tmDescent;
277     mMetrics->maxAdvance = metrics.tmMaxCharWidth;
278     mMetrics->aveCharWidth = std::max<gfxFloat>(1, metrics.tmAveCharWidth);
279     // The font is monospace when TMPF_FIXED_PITCH is *not* set!
280     // See http://msdn2.microsoft.com/en-us/library/ms534202(VS.85).aspx
281     if (!(metrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
282       mMetrics->maxAdvance = mMetrics->aveCharWidth;
283     }
284 
285     mIsBitmap = !(metrics.tmPitchAndFamily & TMPF_VECTOR);
286 
287     // For fonts with USE_TYPO_METRICS set in the fsSelection field,
288     // let the OS/2 sTypo* metrics override the previous values.
289     // (see http://www.microsoft.com/typography/otspec/os2.htm#fss)
290     // Using the equivalent values from oMetrics provides inconsistent
291     // results with CFF fonts, so we instead rely on OS2Table.
292     gfxFontEntry::AutoTable os2Table(mFontEntry,
293                                      TRUETYPE_TAG('O', 'S', '/', '2'));
294     if (os2Table) {
295       uint32_t len;
296       const OS2Table* os2 =
297           reinterpret_cast<const OS2Table*>(hb_blob_get_data(os2Table, &len));
298       if (len >= offsetof(OS2Table, sTypoLineGap) + sizeof(int16_t)) {
299         const uint16_t kUseTypoMetricsMask = 1 << 7;
300         if ((uint16_t(os2->fsSelection) & kUseTypoMetricsMask)) {
301           double ascent = int16_t(os2->sTypoAscender);
302           double descent = int16_t(os2->sTypoDescender);
303           double lineGap = int16_t(os2->sTypoLineGap);
304           mMetrics->maxAscent = ROUND(ascent * mFUnitsConvFactor);
305           mMetrics->maxDescent = -ROUND(descent * mFUnitsConvFactor);
306           mMetrics->maxHeight = mMetrics->maxAscent + mMetrics->maxDescent;
307           mMetrics->internalLeading = mMetrics->maxHeight - mMetrics->emHeight;
308           gfxFloat lineHeight =
309               ROUND((ascent - descent + lineGap) * mFUnitsConvFactor);
310           lineHeight = std::max(lineHeight, mMetrics->maxHeight);
311           mMetrics->externalLeading = lineHeight - mMetrics->maxHeight;
312         }
313       }
314       // although sxHeight and sCapHeight are signed fields, we consider
315       // negative values to be erroneous and just ignore them
316       if (uint16_t(os2->version) >= 2) {
317         // version 2 and later includes the x-height and cap-height fields
318         if (len >= offsetof(OS2Table, sxHeight) + sizeof(int16_t) &&
319             int16_t(os2->sxHeight) > 0) {
320           mMetrics->xHeight = ROUND(int16_t(os2->sxHeight) * mFUnitsConvFactor);
321         }
322         if (len >= offsetof(OS2Table, sCapHeight) + sizeof(int16_t) &&
323             int16_t(os2->sCapHeight) > 0) {
324           mMetrics->capHeight =
325               ROUND(int16_t(os2->sCapHeight) * mFUnitsConvFactor);
326         }
327       }
328     }
329 
330     WORD glyph;
331     SIZE size;
332     DWORD ret = GetGlyphIndicesW(dc.GetDC(), L" ", 1, &glyph,
333                                  GGI_MARK_NONEXISTING_GLYPHS);
334     if (ret != GDI_ERROR && glyph != 0xFFFF) {
335       mSpaceGlyph = glyph;
336       // Cache the width of a single space.
337       GetTextExtentPoint32W(dc.GetDC(), L" ", 1, &size);
338       mMetrics->spaceWidth = ROUND(size.cx);
339     } else {
340       mMetrics->spaceWidth = mMetrics->aveCharWidth;
341     }
342 
343     // Cache the width of digit zero, if available.
344     ret = GetGlyphIndicesW(dc.GetDC(), L"0", 1, &glyph,
345                            GGI_MARK_NONEXISTING_GLYPHS);
346     if (ret != GDI_ERROR && glyph != 0xFFFF) {
347       GetTextExtentPoint32W(dc.GetDC(), L"0", 1, &size);
348       mMetrics->zeroWidth = ROUND(size.cx);
349     } else {
350       mMetrics->zeroWidth = -1.0;  // indicates not found
351     }
352 
353     SanitizeMetrics(mMetrics, GetFontEntry()->mIsBadUnderlineFont);
354   } else {
355     mFUnitsConvFactor = 0.0;  // zero-sized font: all values scale to zero
356   }
357 
358   if (IsSyntheticBold()) {
359     mMetrics->aveCharWidth += GetSyntheticBoldOffset();
360     mMetrics->maxAdvance += GetSyntheticBoldOffset();
361   }
362 
363 #if 0
364     printf("Font: %p (%s) size: %f adjusted size: %f valid: %s\n", this,
365            GetName().get(), mStyle.size, mAdjustedSize, (mIsValid ? "yes" : "no"));
366     printf("    emHeight: %f emAscent: %f emDescent: %f\n", mMetrics->emHeight, mMetrics->emAscent, mMetrics->emDescent);
367     printf("    maxAscent: %f maxDescent: %f maxAdvance: %f\n", mMetrics->maxAscent, mMetrics->maxDescent, mMetrics->maxAdvance);
368     printf("    internalLeading: %f externalLeading: %f\n", mMetrics->internalLeading, mMetrics->externalLeading);
369     printf("    spaceWidth: %f aveCharWidth: %f\n", mMetrics->spaceWidth, mMetrics->aveCharWidth);
370     printf("    xHeight: %f capHeight: %f\n", mMetrics->xHeight, mMetrics->capHeight);
371     printf("    uOff: %f uSize: %f stOff: %f stSize: %f\n",
372            mMetrics->underlineOffset, mMetrics->underlineSize, mMetrics->strikeoutOffset, mMetrics->strikeoutSize);
373 #endif
374 }
375 
FillLogFont(LOGFONTW & aLogFont,gfxFloat aSize)376 void gfxGDIFont::FillLogFont(LOGFONTW& aLogFont, gfxFloat aSize) {
377   GDIFontEntry* fe = static_cast<GDIFontEntry*>(GetFontEntry());
378 
379   // Figure out the lfWeight value to use for GDI font selection,
380   // or zero to use the entry's current LOGFONT value.
381   LONG weight;
382   if (fe->IsUserFont()) {
383     if (fe->IsLocalUserFont()) {
384       // for local user fonts, don't change the original weight
385       // in the entry's logfont, because that could alter the
386       // choice of actual face used (bug 724231)
387       weight = 0;
388     } else {
389       // avoid GDI synthetic bold which occurs when weight
390       // specified is >= font data weight + 200
391       weight = mNeedsSyntheticBold ? 700 : 200;
392     }
393   } else {
394     // GDI doesn't support variation fonts, so for system fonts we know
395     // that the entry has only a single weight, not a range.
396     MOZ_ASSERT(fe->Weight().IsSingle());
397     weight = mNeedsSyntheticBold ? 700 : fe->Weight().Min().ToIntRounded();
398   }
399 
400   fe->FillLogFont(&aLogFont, weight, aSize);
401 }
402 
GetGlyph(uint32_t aUnicode,uint32_t aVarSelector)403 uint32_t gfxGDIFont::GetGlyph(uint32_t aUnicode, uint32_t aVarSelector) {
404   // Callback used only for fonts that lack a 'cmap' table.
405 
406   // We don't support variation selector sequences or non-BMP characters
407   // in the legacy bitmap, vector or postscript fonts that might use
408   // this code path.
409   if (aUnicode > 0xffff || aVarSelector) {
410     return 0;
411   }
412 
413   if (!mGlyphIDs) {
414     mGlyphIDs = MakeUnique<nsTHashMap<nsUint32HashKey, uint32_t>>(64);
415   }
416 
417   uint32_t gid;
418   if (mGlyphIDs->Get(aUnicode, &gid)) {
419     return gid;
420   }
421 
422   wchar_t ch = aUnicode;
423   WORD glyph;
424   DWORD ret = ScriptGetCMap(nullptr, &mScriptCache, &ch, 1, 0, &glyph);
425   if (ret != S_OK) {
426     AutoDC dc;
427     AutoSelectFont fs(dc.GetDC(), GetHFONT());
428     if (ret == E_PENDING) {
429       // Try ScriptGetCMap again now that we've set up the font.
430       ret = ScriptGetCMap(dc.GetDC(), &mScriptCache, &ch, 1, 0, &glyph);
431     }
432     if (ret != S_OK) {
433       // If ScriptGetCMap still failed, fall back to GetGlyphIndicesW
434       // (see bug 1105807).
435       ret = GetGlyphIndicesW(dc.GetDC(), &ch, 1, &glyph,
436                              GGI_MARK_NONEXISTING_GLYPHS);
437       if (ret == GDI_ERROR || glyph == 0xFFFF) {
438         glyph = 0;
439       }
440     }
441   }
442 
443   mGlyphIDs->InsertOrUpdate(aUnicode, glyph);
444   return glyph;
445 }
446 
GetGlyphWidth(uint16_t aGID)447 int32_t gfxGDIFont::GetGlyphWidth(uint16_t aGID) {
448   if (!mGlyphWidths) {
449     mGlyphWidths = MakeUnique<nsTHashMap<nsUint32HashKey, int32_t>>(128);
450   }
451 
452   return mGlyphWidths->WithEntryHandle(aGID, [&](auto&& entry) {
453     if (!entry) {
454       DCForMetrics dc;
455       AutoSelectFont fs(dc, GetHFONT());
456 
457       int devWidth;
458       if (!GetCharWidthI(dc, aGID, 1, nullptr, &devWidth)) {
459         return -1;
460       }
461       // clamp value to range [0..0x7fff], and convert to 16.16 fixed-point
462       devWidth = std::min(std::max(0, devWidth), 0x7fff);
463       entry.Insert(devWidth << 16);
464     }
465     return *entry;
466   });
467 }
468 
GetGlyphBounds(uint16_t aGID,gfxRect * aBounds,bool aTight)469 bool gfxGDIFont::GetGlyphBounds(uint16_t aGID, gfxRect* aBounds, bool aTight) {
470   DCForMetrics dc;
471   AutoSelectFont fs(dc, GetHFONT());
472 
473   if (mIsBitmap) {
474     int devWidth;
475     if (!GetCharWidthI(dc, aGID, 1, nullptr, &devWidth)) {
476       return false;
477     }
478     devWidth = std::min(std::max(0, devWidth), 0x7fff);
479 
480     *aBounds = gfxRect(0, -mMetrics->maxAscent, devWidth,
481                        mMetrics->maxAscent + mMetrics->maxDescent);
482     return true;
483   }
484 
485   const MAT2 kIdentityMatrix = {{0, 1}, {0, 0}, {0, 0}, {0, 1}};
486   GLYPHMETRICS gm;
487   if (GetGlyphOutlineW(dc, aGID, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr,
488                        &kIdentityMatrix) == GDI_ERROR) {
489     return false;
490   }
491 
492   if (gm.gmBlackBoxX == 1 && gm.gmBlackBoxY == 1 &&
493       !GetGlyphOutlineW(dc, aGID, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, 0, nullptr,
494                         &kIdentityMatrix)) {
495     // Workaround for GetGlyphOutline returning 1x1 bounding box
496     // for <space> glyph that is in fact empty.
497     gm.gmBlackBoxX = 0;
498     gm.gmBlackBoxY = 0;
499   } else if (gm.gmBlackBoxX > 0 && !aTight) {
500     // The bounding box reported by Windows supposedly contains the glyph's
501     // "black" area; however, antialiasing (especially with ClearType) means
502     // that the actual image that needs to be rendered may "bleed" into the
503     // adjacent pixels, mainly on the right side.
504     gm.gmptGlyphOrigin.x -= 1;
505     gm.gmBlackBoxX += 3;
506   }
507 
508   *aBounds = gfxRect(gm.gmptGlyphOrigin.x, -gm.gmptGlyphOrigin.y,
509                      gm.gmBlackBoxX, gm.gmBlackBoxY);
510   return true;
511 }
512 
AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,FontCacheSizes * aSizes) const513 void gfxGDIFont::AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf,
514                                         FontCacheSizes* aSizes) const {
515   gfxFont::AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
516   aSizes->mFontInstances += aMallocSizeOf(mMetrics);
517   if (mGlyphWidths) {
518     aSizes->mFontInstances +=
519         mGlyphWidths->ShallowSizeOfIncludingThis(aMallocSizeOf);
520   }
521 }
522 
AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,FontCacheSizes * aSizes) const523 void gfxGDIFont::AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf,
524                                         FontCacheSizes* aSizes) const {
525   aSizes->mFontInstances += aMallocSizeOf(this);
526   AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
527 }
528