2010-03-15 02:34:25 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Foundation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005-2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <stuart@mozilla.com>
|
|
|
|
* Masayuki Nakano <masayuki@d-toybox.com>
|
|
|
|
* Mats Palmgren <mats.palmgren@bredband.net>
|
|
|
|
* John Daggett <jdaggett@mozilla.com>
|
|
|
|
* Jonathan Kew <jfkthame@gmail.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "gfxGDIFont.h"
|
|
|
|
#include "gfxGDIShaper.h"
|
|
|
|
#include "gfxUniscribeShaper.h"
|
2009-10-07 10:16:52 -07:00
|
|
|
#include "gfxHarfBuzzShaper.h"
|
2010-03-15 02:34:25 -07:00
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#include "gfxContext.h"
|
2010-06-25 05:43:10 -07:00
|
|
|
#include "gfxUnicodeProperties.h"
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
|
|
|
|
#define ROUND(x) floor((x) + 0.5)
|
|
|
|
|
|
|
|
static inline cairo_antialias_t
|
|
|
|
GetCairoAntialiasOption(gfxFont::AntialiasOption anAntialiasOption)
|
|
|
|
{
|
|
|
|
switch (anAntialiasOption) {
|
|
|
|
default:
|
|
|
|
case gfxFont::kAntialiasDefault:
|
|
|
|
return CAIRO_ANTIALIAS_DEFAULT;
|
|
|
|
case gfxFont::kAntialiasNone:
|
|
|
|
return CAIRO_ANTIALIAS_NONE;
|
|
|
|
case gfxFont::kAntialiasGrayscale:
|
|
|
|
return CAIRO_ANTIALIAS_GRAY;
|
|
|
|
case gfxFont::kAntialiasSubpixel:
|
|
|
|
return CAIRO_ANTIALIAS_SUBPIXEL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxGDIFont::gfxGDIFont(GDIFontEntry *aFontEntry,
|
|
|
|
const gfxFontStyle *aFontStyle,
|
|
|
|
PRBool aNeedsBold,
|
|
|
|
AntialiasOption anAAOption)
|
|
|
|
: gfxFont(aFontEntry, aFontStyle, anAAOption),
|
|
|
|
mFont(NULL),
|
|
|
|
mFontFace(nsnull),
|
|
|
|
mScaledFont(nsnull),
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics(nsnull),
|
|
|
|
mSpaceGlyph(0),
|
|
|
|
mNeedsBold(aNeedsBold)
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
2009-10-07 10:16:52 -07:00
|
|
|
if (FontCanSupportHarfBuzz()) {
|
|
|
|
mHarfBuzzShaper = new gfxHarfBuzzShaper(this);
|
2010-06-09 23:38:13 -07:00
|
|
|
}
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxGDIFont::~gfxGDIFont()
|
|
|
|
{
|
|
|
|
if (mScaledFont) {
|
|
|
|
cairo_scaled_font_destroy(mScaledFont);
|
|
|
|
}
|
|
|
|
if (mFontFace) {
|
|
|
|
cairo_font_face_destroy(mFontFace);
|
|
|
|
}
|
|
|
|
if (mFont) {
|
|
|
|
::DeleteObject(mFont);
|
|
|
|
}
|
2010-05-05 03:10:36 -07:00
|
|
|
delete mMetrics;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
void
|
|
|
|
gfxGDIFont::CreatePlatformShaper()
|
|
|
|
{
|
2010-06-25 05:43:10 -07:00
|
|
|
mPlatformShaper = new gfxGDIShaper(this);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
|
|
|
|
2010-03-15 02:34:25 -07:00
|
|
|
gfxFont*
|
|
|
|
gfxGDIFont::CopyWithAntialiasOption(AntialiasOption anAAOption)
|
|
|
|
{
|
|
|
|
return new gfxGDIFont(static_cast<GDIFontEntry*>(mFontEntry.get()),
|
|
|
|
&mStyle, mNeedsBold, anAAOption);
|
|
|
|
}
|
|
|
|
|
2010-06-25 05:43:10 -07:00
|
|
|
static PRBool
|
|
|
|
UseUniscribe(gfxTextRun *aTextRun,
|
|
|
|
const PRUnichar *aString,
|
|
|
|
PRUint32 aRunStart,
|
|
|
|
PRUint32 aRunLength)
|
|
|
|
{
|
|
|
|
PRUint32 flags = aTextRun->GetFlags();
|
|
|
|
PRBool useGDI;
|
|
|
|
|
|
|
|
PRBool isXP = (gfxWindowsPlatform::WindowsOSVersion()
|
|
|
|
< gfxWindowsPlatform::kWindowsVista);
|
|
|
|
|
|
|
|
// bug 561304 - Uniscribe bug produces bad positioning at certain
|
|
|
|
// font sizes on XP, so default to GDI on XP using logic of 3.6
|
|
|
|
|
|
|
|
useGDI = isXP &&
|
|
|
|
(flags &
|
|
|
|
(gfxTextRunFactory::TEXT_OPTIMIZE_SPEED |
|
|
|
|
gfxTextRunFactory::TEXT_IS_RTL)
|
|
|
|
) == gfxTextRunFactory::TEXT_OPTIMIZE_SPEED;
|
|
|
|
|
|
|
|
return !useGDI ||
|
|
|
|
ScriptIsComplex(aString + aRunStart, aRunLength, SIC_COMPLEX) == S_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
PRBool
|
2010-03-15 02:34:25 -07:00
|
|
|
gfxGDIFont::InitTextRun(gfxContext *aContext,
|
|
|
|
gfxTextRun *aTextRun,
|
|
|
|
const PRUnichar *aString,
|
|
|
|
PRUint32 aRunStart,
|
2010-06-11 12:14:38 -07:00
|
|
|
PRUint32 aRunLength,
|
2010-11-11 03:21:05 -08:00
|
|
|
PRInt32 aRunScript,
|
|
|
|
PRBool aPreferPlatformShaping)
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
2010-05-05 03:10:36 -07:00
|
|
|
if (!mMetrics) {
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
if (!mIsValid) {
|
|
|
|
NS_WARNING("invalid font! expect incorrect text rendering");
|
2009-10-07 10:16:52 -07:00
|
|
|
return PR_FALSE;
|
2010-05-05 03:10:36 -07:00
|
|
|
}
|
2010-06-25 05:43:10 -07:00
|
|
|
|
|
|
|
PRBool ok = PR_FALSE;
|
|
|
|
|
2011-03-24 13:11:37 -07:00
|
|
|
// ensure the cairo font is set up, so there's no risk it'll fall back to
|
|
|
|
// creating a "toy" font internally (see bug 544617)
|
|
|
|
SetupCairoFont(aContext);
|
|
|
|
|
2010-06-25 05:43:10 -07:00
|
|
|
if (mHarfBuzzShaper) {
|
2011-04-11 04:17:31 -07:00
|
|
|
if (gfxPlatform::GetPlatform()->UseHarfBuzzForScript(aRunScript)) {
|
2010-06-25 05:43:10 -07:00
|
|
|
ok = mHarfBuzzShaper->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
aRunStart, aRunLength,
|
|
|
|
aRunScript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok) {
|
2010-06-09 23:38:13 -07:00
|
|
|
GDIFontEntry *fe = static_cast<GDIFontEntry*>(GetFontEntry());
|
2011-01-28 03:14:36 -08:00
|
|
|
PRBool preferUniscribe =
|
|
|
|
(!fe->IsTrueType() || fe->IsSymbolFont()) && !fe->mForceGDI;
|
2010-06-25 05:43:10 -07:00
|
|
|
|
2011-01-28 03:14:36 -08:00
|
|
|
if (preferUniscribe ||
|
|
|
|
UseUniscribe(aTextRun, aString, aRunStart, aRunLength))
|
2010-06-25 05:43:10 -07:00
|
|
|
{
|
|
|
|
// first try Uniscribe
|
|
|
|
if (!mUniscribeShaper) {
|
|
|
|
mUniscribeShaper = new gfxUniscribeShaper(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = mUniscribeShaper->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
aRunStart, aRunLength,
|
|
|
|
aRunScript);
|
|
|
|
if (ok) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to GDI shaping
|
2011-01-28 03:14:36 -08:00
|
|
|
if (!mPlatformShaper) {
|
|
|
|
CreatePlatformShaper();
|
2010-06-25 05:43:10 -07:00
|
|
|
}
|
|
|
|
|
2011-01-28 03:14:36 -08:00
|
|
|
ok = mPlatformShaper->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
aRunStart, aRunLength,
|
|
|
|
aRunScript);
|
2010-06-25 05:43:10 -07:00
|
|
|
} else {
|
|
|
|
// first use GDI
|
|
|
|
if (!mPlatformShaper) {
|
|
|
|
CreatePlatformShaper();
|
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
ok = mPlatformShaper->InitTextRun(aContext, aTextRun, aString,
|
2010-06-25 05:43:10 -07:00
|
|
|
aRunStart, aRunLength,
|
2009-10-07 10:16:52 -07:00
|
|
|
aRunScript);
|
2010-06-25 05:43:10 -07:00
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-01-28 03:14:36 -08:00
|
|
|
// try Uniscribe if GDI failed
|
2010-06-25 05:43:10 -07:00
|
|
|
if (!mUniscribeShaper) {
|
|
|
|
mUniscribeShaper = new gfxUniscribeShaper(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// use Uniscribe shaping
|
|
|
|
ok = mUniscribeShaper->InitTextRun(aContext, aTextRun, aString,
|
|
|
|
aRunStart, aRunLength,
|
|
|
|
aRunScript);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
if (!ok) {
|
|
|
|
NS_ConvertUTF16toUTF8 name(GetName());
|
|
|
|
char msg[256];
|
|
|
|
|
|
|
|
sprintf(msg,
|
|
|
|
"text shaping with both uniscribe and GDI failed for"
|
|
|
|
" font: %s",
|
|
|
|
name.get());
|
|
|
|
NS_WARNING(msg);
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
2010-06-25 05:43:10 -07:00
|
|
|
#endif
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
2009-10-07 10:16:52 -07:00
|
|
|
|
|
|
|
return ok;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const gfxFont::Metrics&
|
|
|
|
gfxGDIFont::GetMetrics()
|
|
|
|
{
|
2010-05-05 03:10:36 -07:00
|
|
|
if (!mMetrics) {
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
return *mMetrics;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32
|
|
|
|
gfxGDIFont::GetSpaceGlyph()
|
|
|
|
{
|
2010-05-05 03:10:36 -07:00
|
|
|
if (!mMetrics) {
|
|
|
|
Initialize();
|
|
|
|
}
|
2010-03-15 02:34:25 -07:00
|
|
|
return mSpaceGlyph;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxGDIFont::SetupCairoFont(gfxContext *aContext)
|
|
|
|
{
|
2010-05-05 03:10:36 -07:00
|
|
|
if (!mMetrics) {
|
|
|
|
Initialize();
|
|
|
|
}
|
2010-08-27 06:16:07 -07:00
|
|
|
if (!mScaledFont ||
|
|
|
|
cairo_scaled_font_status(mScaledFont) != CAIRO_STATUS_SUCCESS) {
|
2010-03-15 02:34:25 -07:00
|
|
|
// Don't cairo_set_scaled_font as that would propagate the error to
|
|
|
|
// the cairo_t, precluding any further drawing.
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
cairo_set_scaled_font(aContext->GetCairo(), mScaledFont);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-05 03:10:36 -07:00
|
|
|
gfxGDIFont::Initialize()
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
2010-05-05 03:10:36 -07:00
|
|
|
NS_ASSERTION(!mMetrics, "re-creating metrics? this will leak");
|
|
|
|
|
|
|
|
LOGFONTW logFont;
|
|
|
|
|
2010-03-15 02:34:25 -07:00
|
|
|
if (mAdjustedSize == 0.0) {
|
|
|
|
mAdjustedSize = mStyle.size;
|
|
|
|
if (mStyle.sizeAdjust != 0.0 && mAdjustedSize > 0.0) {
|
|
|
|
// to implement font-size-adjust, we first create the "unadjusted" font
|
2010-05-05 03:10:36 -07:00
|
|
|
FillLogFont(logFont, mAdjustedSize);
|
|
|
|
mFont = ::CreateFontIndirectW(&logFont);
|
2010-03-15 02:34:25 -07:00
|
|
|
|
2010-05-05 03:10:36 -07:00
|
|
|
// initialize its metrics so we can calculate size adjustment
|
|
|
|
Initialize();
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
// calculate the properly adjusted size, and then proceed
|
2010-05-05 03:10:36 -07:00
|
|
|
// to recreate mFont and recalculate metrics
|
|
|
|
gfxFloat aspect = mMetrics->xHeight / mMetrics->emHeight;
|
2010-03-15 02:34:25 -07:00
|
|
|
mAdjustedSize = mStyle.GetAdjustedSize(aspect);
|
2010-05-05 03:10:36 -07:00
|
|
|
|
|
|
|
// delete the temporary font and metrics
|
|
|
|
::DeleteObject(mFont);
|
|
|
|
mFont = nsnull;
|
|
|
|
delete mMetrics;
|
|
|
|
mMetrics = nsnull;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 03:10:36 -07:00
|
|
|
FillLogFont(logFont, mAdjustedSize);
|
|
|
|
mFont = ::CreateFontIndirectW(&logFont);
|
|
|
|
|
|
|
|
mMetrics = new gfxFont::Metrics;
|
|
|
|
::memset(mMetrics, 0, sizeof(*mMetrics));
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
AutoDC dc;
|
|
|
|
SetGraphicsMode(dc.GetDC(), GM_ADVANCED);
|
|
|
|
AutoSelectFont selectFont(dc.GetDC(), mFont);
|
|
|
|
|
|
|
|
// Get font metrics
|
|
|
|
OUTLINETEXTMETRIC oMetrics;
|
|
|
|
TEXTMETRIC& metrics = oMetrics.otmTextMetrics;
|
|
|
|
|
|
|
|
if (0 < GetOutlineTextMetrics(dc.GetDC(), sizeof(oMetrics), &oMetrics)) {
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->superscriptOffset = (double)oMetrics.otmptSuperscriptOffset.y;
|
2010-03-15 02:34:25 -07:00
|
|
|
// Some fonts have wrong sign on their subscript offset, bug 410917.
|
2010-05-05 03:10:36 -07:00
|
|
|
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;
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
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
|
2010-07-11 12:33:56 -07:00
|
|
|
mMetrics->xHeight =
|
|
|
|
ROUND((double)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR);
|
2010-03-15 02:34:25 -07:00
|
|
|
} else {
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->xHeight = gm.gmptGlyphOrigin.y;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->emHeight = metrics.tmHeight - metrics.tmInternalLeading;
|
2010-03-15 02:34:25 -07:00
|
|
|
gfxFloat typEmHeight = (double)oMetrics.otmAscent - (double)oMetrics.otmDescent;
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->emAscent = ROUND(mMetrics->emHeight * (double)oMetrics.otmAscent / typEmHeight);
|
|
|
|
mMetrics->emDescent = mMetrics->emHeight - mMetrics->emAscent;
|
2009-10-07 10:16:52 -07:00
|
|
|
if (oMetrics.otmEMSquare > 0) {
|
2011-01-21 08:44:32 -08:00
|
|
|
mFUnitsConvFactor = float(mAdjustedSize / oMetrics.otmEMSquare);
|
2009-10-07 10:16:52 -07:00
|
|
|
}
|
2010-03-15 02:34:25 -07:00
|
|
|
} else {
|
|
|
|
// Make a best-effort guess at extended metrics
|
|
|
|
// this is based on general typographic guidelines
|
|
|
|
|
|
|
|
// GetTextMetrics can fail if the font file has been removed
|
|
|
|
// or corrupted recently.
|
|
|
|
BOOL result = GetTextMetrics(dc.GetDC(), &metrics);
|
|
|
|
if (!result) {
|
|
|
|
NS_WARNING("Missing or corrupt font data, fasten your seatbelt");
|
|
|
|
mIsValid = PR_FALSE;
|
2010-05-05 03:10:36 -07:00
|
|
|
memset(mMetrics, 0, sizeof(*mMetrics));
|
2010-03-15 02:34:25 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-11 12:33:56 -07:00
|
|
|
mMetrics->xHeight =
|
|
|
|
ROUND((float)metrics.tmAscent * DEFAULT_XHEIGHT_FACTOR);
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->superscriptOffset = mMetrics->xHeight;
|
|
|
|
mMetrics->subscriptOffset = mMetrics->xHeight;
|
|
|
|
mMetrics->strikeoutSize = 1;
|
|
|
|
mMetrics->strikeoutOffset = ROUND(mMetrics->xHeight * 0.5f); // 50% of xHeight
|
|
|
|
mMetrics->underlineSize = 1;
|
|
|
|
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;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->internalLeading = metrics.tmInternalLeading;
|
|
|
|
mMetrics->externalLeading = metrics.tmExternalLeading;
|
|
|
|
mMetrics->maxHeight = metrics.tmHeight;
|
|
|
|
mMetrics->maxAscent = metrics.tmAscent;
|
|
|
|
mMetrics->maxDescent = metrics.tmDescent;
|
|
|
|
mMetrics->maxAdvance = metrics.tmMaxCharWidth;
|
2011-06-02 05:56:50 -07:00
|
|
|
mMetrics->aveCharWidth = NS_MAX<gfxFloat>(1, metrics.tmAveCharWidth);
|
2010-03-15 02:34:25 -07:00
|
|
|
// 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)) {
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->maxAdvance = mMetrics->aveCharWidth;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the width of a single space.
|
|
|
|
SIZE size;
|
|
|
|
GetTextExtentPoint32W(dc.GetDC(), L" ", 1, &size);
|
2010-05-05 03:10:36 -07:00
|
|
|
mMetrics->spaceWidth = ROUND(size.cx);
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
// 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'?
|
2010-05-05 03:10:36 -07:00
|
|
|
if (GetTextExtentPoint32W(dc.GetDC(), L"0", 1, &size)) {
|
|
|
|
mMetrics->zeroOrAveCharWidth = ROUND(size.cx);
|
|
|
|
} else {
|
|
|
|
mMetrics->zeroOrAveCharWidth = mMetrics->aveCharWidth;
|
|
|
|
}
|
2010-03-15 02:34:25 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 03:10:36 -07:00
|
|
|
SanitizeMetrics(mMetrics, GetFontEntry()->mIsBadUnderlineFont);
|
|
|
|
|
|
|
|
mFontFace = cairo_win32_font_face_create_for_logfontw_hfont(&logFont,
|
|
|
|
mFont);
|
|
|
|
|
|
|
|
cairo_matrix_t sizeMatrix, ctm;
|
|
|
|
cairo_matrix_init_identity(&ctm);
|
|
|
|
cairo_matrix_init_scale(&sizeMatrix, mAdjustedSize, mAdjustedSize);
|
|
|
|
|
|
|
|
cairo_font_options_t *fontOptions = cairo_font_options_create();
|
|
|
|
if (mAntialiasOption != kAntialiasDefault) {
|
|
|
|
cairo_font_options_set_antialias(fontOptions,
|
|
|
|
GetCairoAntialiasOption(mAntialiasOption));
|
|
|
|
}
|
|
|
|
mScaledFont = cairo_scaled_font_create(mFontFace, &sizeMatrix,
|
|
|
|
&ctm, fontOptions);
|
|
|
|
cairo_font_options_destroy(fontOptions);
|
|
|
|
|
2010-08-27 06:16:07 -07:00
|
|
|
if (!mScaledFont ||
|
|
|
|
cairo_scaled_font_status(mScaledFont) != CAIRO_STATUS_SUCCESS) {
|
2010-05-05 03:10:36 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
char warnBuf[1024];
|
|
|
|
sprintf(warnBuf, "Failed to create scaled font: %s status: %d",
|
2010-08-27 06:16:07 -07:00
|
|
|
NS_ConvertUTF16toUTF8(mFontEntry->Name()).get(),
|
|
|
|
mScaledFont ? cairo_scaled_font_status(mScaledFont) : 0);
|
2010-05-05 03:10:36 -07:00
|
|
|
NS_WARNING(warnBuf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-03-15 02:34:25 -07:00
|
|
|
mIsValid = PR_TRUE;
|
2010-03-31 05:46:18 -07:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("Font: %p (%s) size: %f\n", this,
|
|
|
|
NS_ConvertUTF16toUTF8(GetName()).get(), mStyle.size);
|
|
|
|
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(" internalLeading: %f externalLeading: %f\n", mMetrics.internalLeading, mMetrics.externalLeading);
|
|
|
|
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",
|
|
|
|
mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize,
|
|
|
|
mMetrics.superscriptOffset, mMetrics.subscriptOffset);
|
|
|
|
#endif
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-05 03:10:36 -07:00
|
|
|
gfxGDIFont::FillLogFont(LOGFONTW& aLogFont, gfxFloat aSize)
|
2010-03-15 02:34:25 -07:00
|
|
|
{
|
|
|
|
GDIFontEntry *fe = static_cast<GDIFontEntry*>(GetFontEntry());
|
|
|
|
|
|
|
|
PRUint16 weight = mNeedsBold ? 700 : fe->Weight();
|
|
|
|
PRBool italic = (mStyle.style & (FONT_STYLE_ITALIC | FONT_STYLE_OBLIQUE));
|
|
|
|
|
|
|
|
// if user font, disable italics/bold if defined to be italics/bold face
|
|
|
|
// this avoids unwanted synthetic italics/bold
|
|
|
|
if (fe->mIsUserFont) {
|
|
|
|
if (fe->IsItalic())
|
|
|
|
italic = PR_FALSE; // avoid synthetic italic
|
2010-07-27 22:30:21 -07:00
|
|
|
if (fe->IsBold() || !mNeedsBold) {
|
|
|
|
// avoid GDI synthetic bold which occurs when weight
|
|
|
|
// specified is >= font data weight + 200
|
|
|
|
weight = 200;
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-26 22:05:30 -07:00
|
|
|
fe->FillLogFont(&aLogFont, italic, weight, aSize,
|
|
|
|
(mAntialiasOption == kAntialiasSubpixel) ? PR_TRUE : PR_FALSE);
|
2010-03-15 02:34:25 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
PRInt32
|
2011-01-21 02:35:21 -08:00
|
|
|
gfxGDIFont::GetGlyphWidth(gfxContext *aCtx, PRUint16 aGID)
|
2009-10-07 10:16:52 -07:00
|
|
|
{
|
|
|
|
if (!mGlyphWidths.IsInitialized()) {
|
|
|
|
mGlyphWidths.Init(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 width;
|
|
|
|
if (mGlyphWidths.Get(aGID, &width)) {
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2011-02-23 01:39:21 -08:00
|
|
|
DCFromContext dc(aCtx);
|
2011-03-24 13:11:37 -07:00
|
|
|
AutoSelectFont fs(dc, GetHFONT());
|
2011-02-23 01:39:21 -08:00
|
|
|
|
2009-10-07 10:16:52 -07:00
|
|
|
int devWidth;
|
2011-02-23 01:39:21 -08:00
|
|
|
if (GetCharWidthI(dc, aGID, 1, NULL, &devWidth)) {
|
2009-10-07 10:16:52 -07:00
|
|
|
// ensure width is positive, 16.16 fixed-point value
|
|
|
|
width = (devWidth & 0x7fff) << 16;
|
|
|
|
mGlyphWidths.Put(aGID, width);
|
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|