Bug 693143. Fix top crasher related to small font sizes and bitmap fonts. r=roc

This commit is contained in:
John Daggett 2011-12-07 12:02:53 +09:00
parent b86796df77
commit 7fef4d4b86
2 changed files with 105 additions and 100 deletions

View File

@ -1832,7 +1832,7 @@ gfxFont::SanitizeMetrics(gfxFont::Metrics *aMetrics, bool aIsBadUnderlineFont)
{ {
// Even if this font size is zero, this font is created with non-zero size. // Even if this font size is zero, this font is created with non-zero size.
// However, for layout and others, we should return the metrics of zero size font. // However, for layout and others, we should return the metrics of zero size font.
if (mStyle.size == 0) { if (mStyle.size == 0.0) {
memset(aMetrics, 0, sizeof(gfxFont::Metrics)); memset(aMetrics, 0, sizeof(gfxFont::Metrics));
return; return;
} }

View File

@ -327,6 +327,7 @@ gfxGDIFont::Initialize()
} }
} }
// this may end up being zero
mAdjustedSize = ROUND(mAdjustedSize); mAdjustedSize = ROUND(mAdjustedSize);
FillLogFont(logFont, mAdjustedSize); FillLogFont(logFont, mAdjustedSize);
mFont = ::CreateFontIndirectW(&logFont); mFont = ::CreateFontIndirectW(&logFont);
@ -338,102 +339,105 @@ gfxGDIFont::Initialize()
SetGraphicsMode(dc.GetDC(), GM_ADVANCED); SetGraphicsMode(dc.GetDC(), GM_ADVANCED);
AutoSelectFont selectFont(dc.GetDC(), mFont); AutoSelectFont selectFont(dc.GetDC(), mFont);
// Get font metrics // Get font metrics if size > 0
OUTLINETEXTMETRIC oMetrics; if (mAdjustedSize > 0.0) {
TEXTMETRIC& metrics = oMetrics.otmTextMetrics;
if (0 < GetOutlineTextMetrics(dc.GetDC(), sizeof(oMetrics), &oMetrics)) { OUTLINETEXTMETRIC oMetrics;
mMetrics->superscriptOffset = (double)oMetrics.otmptSuperscriptOffset.y; TEXTMETRIC& metrics = oMetrics.otmTextMetrics;
// Some fonts have wrong sign on their subscript offset, bug 410917.
mMetrics->subscriptOffset = fabs((double)oMetrics.otmptSubscriptOffset.y);
mMetrics->strikeoutSize = (double)oMetrics.otmsStrikeoutSize;
mMetrics->strikeoutOffset = (double)oMetrics.otmsStrikeoutPosition;
mMetrics->underlineSize = (double)oMetrics.otmsUnderscoreSize;
mMetrics->underlineOffset = (double)oMetrics.otmsUnderscorePosition;
const MAT2 kIdentityMatrix = { {0, 1}, {0, 0}, {0, 0}, {0, 1} }; if (0 < GetOutlineTextMetrics(dc.GetDC(), sizeof(oMetrics), &oMetrics)) {
GLYPHMETRICS gm; mMetrics->superscriptOffset = (double)oMetrics.otmptSuperscriptOffset.y;
DWORD len = GetGlyphOutlineW(dc.GetDC(), PRUnichar('x'), GGO_METRICS, &gm, 0, nsnull, &kIdentityMatrix); // Some fonts have wrong sign on their subscript offset, bug 410917.
if (len == GDI_ERROR || gm.gmptGlyphOrigin.y <= 0) { mMetrics->subscriptOffset = fabs((double)oMetrics.otmptSubscriptOffset.y);
// 56% of ascent, best guess for true type mMetrics->strikeoutSize = (double)oMetrics.otmsStrikeoutSize;
mMetrics->xHeight = mMetrics->strikeoutOffset = (double)oMetrics.otmsStrikeoutPosition;
ROUND((double)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR); mMetrics->underlineSize = (double)oMetrics.otmsUnderscoreSize;
mMetrics->underlineOffset = (double)oMetrics.otmsUnderscorePosition;
const MAT2 kIdentityMatrix = { {0, 1}, {0, 0}, {0, 0}, {0, 1} };
GLYPHMETRICS gm;
DWORD len = GetGlyphOutlineW(dc.GetDC(), PRUnichar('x'), GGO_METRICS, &gm, 0, nsnull, &kIdentityMatrix);
if (len == GDI_ERROR || gm.gmptGlyphOrigin.y <= 0) {
// 56% of ascent, best guess for true type
mMetrics->xHeight =
ROUND((double)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR);
} else {
mMetrics->xHeight = gm.gmptGlyphOrigin.y;
}
mMetrics->emHeight = metrics.tmHeight - metrics.tmInternalLeading;
gfxFloat typEmHeight = (double)oMetrics.otmAscent - (double)oMetrics.otmDescent;
mMetrics->emAscent = ROUND(mMetrics->emHeight * (double)oMetrics.otmAscent / typEmHeight);
mMetrics->emDescent = mMetrics->emHeight - mMetrics->emAscent;
if (oMetrics.otmEMSquare > 0) {
mFUnitsConvFactor = float(mAdjustedSize / oMetrics.otmEMSquare);
}
} else { } else {
mMetrics->xHeight = gm.gmptGlyphOrigin.y; // Make a best-effort guess at extended metrics
} // this is based on general typographic guidelines
mMetrics->emHeight = metrics.tmHeight - metrics.tmInternalLeading;
gfxFloat typEmHeight = (double)oMetrics.otmAscent - (double)oMetrics.otmDescent; // GetTextMetrics can fail if the font file has been removed
mMetrics->emAscent = ROUND(mMetrics->emHeight * (double)oMetrics.otmAscent / typEmHeight); // or corrupted recently.
mMetrics->emDescent = mMetrics->emHeight - mMetrics->emAscent; BOOL result = GetTextMetrics(dc.GetDC(), &metrics);
if (oMetrics.otmEMSquare > 0) { if (!result) {
mFUnitsConvFactor = float(mAdjustedSize / oMetrics.otmEMSquare); NS_WARNING("Missing or corrupt font data, fasten your seatbelt");
} mIsValid = false;
} else { memset(mMetrics, 0, sizeof(*mMetrics));
// Make a best-effort guess at extended metrics return;
// this is based on general typographic guidelines }
// GetTextMetrics can fail if the font file has been removed mMetrics->xHeight =
// or corrupted recently. ROUND((float)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR);
BOOL result = GetTextMetrics(dc.GetDC(), &metrics); mMetrics->superscriptOffset = mMetrics->xHeight;
if (!result) { mMetrics->subscriptOffset = mMetrics->xHeight;
NS_WARNING("Missing or corrupt font data, fasten your seatbelt"); mMetrics->strikeoutSize = 1;
mIsValid = false; mMetrics->strikeoutOffset = ROUND(mMetrics->xHeight * 0.5f); // 50% of xHeight
memset(mMetrics, 0, sizeof(*mMetrics)); mMetrics->underlineSize = 1;
return; mMetrics->underlineOffset = -ROUND((float)metrics.tmDescent * 0.30f); // 30% of descent
mMetrics->emHeight = metrics.tmHeight - metrics.tmInternalLeading;
mMetrics->emAscent = metrics.tmAscent - metrics.tmInternalLeading;
mMetrics->emDescent = metrics.tmDescent;
} }
mMetrics->xHeight = mMetrics->internalLeading = metrics.tmInternalLeading;
ROUND((float)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR); mMetrics->externalLeading = metrics.tmExternalLeading;
mMetrics->superscriptOffset = mMetrics->xHeight; mMetrics->maxHeight = metrics.tmHeight;
mMetrics->subscriptOffset = mMetrics->xHeight; mMetrics->maxAscent = metrics.tmAscent;
mMetrics->strikeoutSize = 1; mMetrics->maxDescent = metrics.tmDescent;
mMetrics->strikeoutOffset = ROUND(mMetrics->xHeight * 0.5f); // 50% of xHeight mMetrics->maxAdvance = metrics.tmMaxCharWidth;
mMetrics->underlineSize = 1; mMetrics->aveCharWidth = NS_MAX<gfxFloat>(1, metrics.tmAveCharWidth);
mMetrics->underlineOffset = -ROUND((float)metrics.tmDescent * 0.30f); // 30% of descent // The font is monospace when TMPF_FIXED_PITCH is *not* set!
mMetrics->emHeight = metrics.tmHeight - metrics.tmInternalLeading; // See http://msdn2.microsoft.com/en-us/library/ms534202(VS.85).aspx
mMetrics->emAscent = metrics.tmAscent - metrics.tmInternalLeading; if (!(metrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
mMetrics->emDescent = metrics.tmDescent; mMetrics->maxAdvance = mMetrics->aveCharWidth;
}
mMetrics->internalLeading = metrics.tmInternalLeading;
mMetrics->externalLeading = metrics.tmExternalLeading;
mMetrics->maxHeight = metrics.tmHeight;
mMetrics->maxAscent = metrics.tmAscent;
mMetrics->maxDescent = metrics.tmDescent;
mMetrics->maxAdvance = metrics.tmMaxCharWidth;
mMetrics->aveCharWidth = NS_MAX<gfxFloat>(1, metrics.tmAveCharWidth);
// The font is monospace when TMPF_FIXED_PITCH is *not* set!
// See http://msdn2.microsoft.com/en-us/library/ms534202(VS.85).aspx
if (!(metrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
mMetrics->maxAdvance = mMetrics->aveCharWidth;
}
// Cache the width of a single space.
SIZE size;
GetTextExtentPoint32W(dc.GetDC(), L" ", 1, &size);
mMetrics->spaceWidth = ROUND(size.cx);
// Cache the width of digit zero.
// XXX MSDN (http://msdn.microsoft.com/en-us/library/ms534223.aspx)
// does not say what the failure modes for GetTextExtentPoint32 are -
// is it safe to assume it will fail iff the font has no '0'?
if (GetTextExtentPoint32W(dc.GetDC(), L"0", 1, &size)) {
mMetrics->zeroOrAveCharWidth = ROUND(size.cx);
} else {
mMetrics->zeroOrAveCharWidth = mMetrics->aveCharWidth;
}
mSpaceGlyph = 0;
if (metrics.tmPitchAndFamily & TMPF_TRUETYPE) {
WORD glyph;
DWORD ret = GetGlyphIndicesW(dc.GetDC(), L" ", 1, &glyph,
GGI_MARK_NONEXISTING_GLYPHS);
if (ret != GDI_ERROR && glyph != 0xFFFF) {
mSpaceGlyph = glyph;
} }
}
SanitizeMetrics(mMetrics, GetFontEntry()->mIsBadUnderlineFont); // Cache the width of a single space.
SIZE size;
GetTextExtentPoint32W(dc.GetDC(), L" ", 1, &size);
mMetrics->spaceWidth = ROUND(size.cx);
// Cache the width of digit zero.
// XXX MSDN (http://msdn.microsoft.com/en-us/library/ms534223.aspx)
// does not say what the failure modes for GetTextExtentPoint32 are -
// is it safe to assume it will fail iff the font has no '0'?
if (GetTextExtentPoint32W(dc.GetDC(), L"0", 1, &size)) {
mMetrics->zeroOrAveCharWidth = ROUND(size.cx);
} else {
mMetrics->zeroOrAveCharWidth = mMetrics->aveCharWidth;
}
mSpaceGlyph = 0;
if (metrics.tmPitchAndFamily & TMPF_TRUETYPE) {
WORD glyph;
DWORD ret = GetGlyphIndicesW(dc.GetDC(), L" ", 1, &glyph,
GGI_MARK_NONEXISTING_GLYPHS);
if (ret != GDI_ERROR && glyph != 0xFFFF) {
mSpaceGlyph = glyph;
}
}
SanitizeMetrics(mMetrics, GetFontEntry()->mIsBadUnderlineFont);
}
mFontFace = cairo_win32_font_face_create_for_logfontw_hfont(&logFont, mFontFace = cairo_win32_font_face_create_for_logfontw_hfont(&logFont,
mFont); mFont);
@ -460,20 +464,21 @@ gfxGDIFont::Initialize()
mScaledFont ? cairo_scaled_font_status(mScaledFont) : 0); mScaledFont ? cairo_scaled_font_status(mScaledFont) : 0);
NS_WARNING(warnBuf); NS_WARNING(warnBuf);
#endif #endif
mIsValid = false;
} else {
mIsValid = true;
} }
mIsValid = true;
#if 0 #if 0
printf("Font: %p (%s) size: %f\n", this, printf("Font: %p (%s) size: %f adjusted size: %f valid: %s\n", this,
NS_ConvertUTF16toUTF8(GetName()).get(), mStyle.size); NS_ConvertUTF16toUTF8(GetName()).get(), mStyle.size, mAdjustedSize, (mIsValid ? "yes" : "no"));
printf(" emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent); printf(" emHeight: %f emAscent: %f emDescent: %f\n", mMetrics->emHeight, mMetrics->emAscent, mMetrics->emDescent);
printf(" maxAscent: %f maxDescent: %f maxAdvance: %f\n", mMetrics.maxAscent, mMetrics.maxDescent, mMetrics.maxAdvance); printf(" maxAscent: %f maxDescent: %f maxAdvance: %f\n", mMetrics->maxAscent, mMetrics->maxDescent, mMetrics->maxAdvance);
printf(" internalLeading: %f externalLeading: %f\n", mMetrics.internalLeading, mMetrics.externalLeading); printf(" internalLeading: %f externalLeading: %f\n", mMetrics->internalLeading, mMetrics->externalLeading);
printf(" spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight); printf(" spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics->spaceWidth, mMetrics->aveCharWidth, mMetrics->xHeight);
printf(" uOff: %f uSize: %f stOff: %f stSize: %f supOff: %f subOff: %f\n", printf(" uOff: %f uSize: %f stOff: %f stSize: %f supOff: %f subOff: %f\n",
mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize, mMetrics->underlineOffset, mMetrics->underlineSize, mMetrics->strikeoutOffset, mMetrics->strikeoutSize,
mMetrics.superscriptOffset, mMetrics.subscriptOffset); mMetrics->superscriptOffset, mMetrics->subscriptOffset);
#endif #endif
} }