2010-02-25 22:36:07 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-02-25 22:36:07 -08:00
|
|
|
|
|
|
|
#include "gfxDWriteFonts.h"
|
2013-06-30 09:26:39 -07:00
|
|
|
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2010-02-25 22:36:07 -08:00
|
|
|
#include "gfxDWriteFontList.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include <dwrite.h>
|
|
|
|
|
2012-04-21 14:24:39 -07:00
|
|
|
#include "harfbuzz/hb.h"
|
2010-06-11 12:14:38 -07:00
|
|
|
|
2010-02-25 22:36:07 -08:00
|
|
|
// Chosen this as to resemble DWrite's own oblique face style.
|
|
|
|
#define OBLIQUE_SKEW_FACTOR 0.3
|
|
|
|
|
2013-05-16 09:29:20 -07:00
|
|
|
using namespace mozilla;
|
2012-03-19 12:20:18 -07:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-06-10 11:19:51 -07:00
|
|
|
// This is also in gfxGDIFont.cpp. Would be nice to put it somewhere common,
|
|
|
|
// but we can't declare it in the gfxFont.h or gfxFontUtils.h headers
|
|
|
|
// because those are exported, and the cairo headers aren't.
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-04 08:58:31 -08:00
|
|
|
// Code to determine whether Windows is set to use ClearType font smoothing;
|
|
|
|
// based on private functions in cairo-win32-font.c
|
|
|
|
|
|
|
|
#ifndef SPI_GETFONTSMOOTHINGTYPE
|
|
|
|
#define SPI_GETFONTSMOOTHINGTYPE 0x200a
|
|
|
|
#endif
|
|
|
|
#ifndef FE_FONTSMOOTHINGCLEARTYPE
|
|
|
|
#define FE_FONTSMOOTHINGCLEARTYPE 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool
|
|
|
|
UsingClearType()
|
|
|
|
{
|
|
|
|
BOOL fontSmoothing;
|
|
|
|
if (!SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &fontSmoothing, 0) ||
|
|
|
|
!fontSmoothing)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT type;
|
|
|
|
if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &type, 0) &&
|
|
|
|
type == FE_FONTSMOOTHINGCLEARTYPE)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-25 22:36:07 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// gfxDWriteFont
|
|
|
|
gfxDWriteFont::gfxDWriteFont(gfxFontEntry *aFontEntry,
|
2010-03-11 20:25:35 -08:00
|
|
|
const gfxFontStyle *aFontStyle,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNeedsBold,
|
2010-06-10 11:19:51 -07:00
|
|
|
AntialiasOption anAAOption)
|
|
|
|
: gfxFont(aFontEntry, aFontStyle, anAAOption)
|
2012-07-30 07:20:58 -07:00
|
|
|
, mCairoFontFace(nullptr)
|
|
|
|
, mMetrics(nullptr)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mNeedsOblique(false)
|
2010-06-10 11:19:51 -07:00
|
|
|
, mNeedsBold(aNeedsBold)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mUseSubpixelPositions(false)
|
|
|
|
, mAllowManualShowGlyphs(true)
|
2010-02-25 22:36:07 -08:00
|
|
|
{
|
2011-01-27 16:40:06 -08:00
|
|
|
gfxDWriteFontEntry *fe =
|
|
|
|
static_cast<gfxDWriteFontEntry*>(aFontEntry);
|
|
|
|
nsresult rv;
|
|
|
|
DWRITE_FONT_SIMULATIONS sims = DWRITE_FONT_SIMULATIONS_NONE;
|
2012-03-15 02:04:46 -07:00
|
|
|
if ((GetStyle()->style & (NS_FONT_STYLE_ITALIC | NS_FONT_STYLE_OBLIQUE)) &&
|
2014-06-19 00:08:58 -07:00
|
|
|
!fe->IsItalic() && GetStyle()->allowSyntheticStyle) {
|
2011-01-27 16:40:06 -08:00
|
|
|
// For this we always use the font_matrix for uniformity. Not the
|
|
|
|
// DWrite simulation.
|
2011-10-17 07:59:28 -07:00
|
|
|
mNeedsOblique = true;
|
2011-01-27 16:40:06 -08:00
|
|
|
}
|
|
|
|
if (aNeedsBold) {
|
|
|
|
sims |= DWRITE_FONT_SIMULATIONS_BOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = fe->CreateFontFace(getter_AddRefs(mFontFace), sims);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsValid = false;
|
2011-01-27 16:40:06 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-02 21:31:08 -07:00
|
|
|
ComputeMetrics(anAAOption);
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxDWriteFont::~gfxDWriteFont()
|
|
|
|
{
|
|
|
|
if (mCairoFontFace) {
|
|
|
|
cairo_font_face_destroy(mCairoFontFace);
|
|
|
|
}
|
2011-12-15 08:12:40 -08:00
|
|
|
if (mScaledFont) {
|
|
|
|
cairo_scaled_font_destroy(mScaledFont);
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
2011-01-21 08:44:32 -08:00
|
|
|
delete mMetrics;
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
|
|
|
|
2010-06-10 11:19:51 -07:00
|
|
|
gfxFont*
|
|
|
|
gfxDWriteFont::CopyWithAntialiasOption(AntialiasOption anAAOption)
|
|
|
|
{
|
|
|
|
return new gfxDWriteFont(static_cast<gfxDWriteFontEntry*>(mFontEntry.get()),
|
|
|
|
&mStyle, mNeedsBold, anAAOption);
|
|
|
|
}
|
|
|
|
|
2010-02-25 22:36:07 -08:00
|
|
|
const gfxFont::Metrics&
|
|
|
|
gfxDWriteFont::GetMetrics()
|
|
|
|
{
|
2011-01-21 08:44:32 -08:00
|
|
|
return *mMetrics;
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-03-23 20:01:14 -07:00
|
|
|
gfxDWriteFont::GetFakeMetricsForArialBlack(DWRITE_FONT_METRICS *aFontMetrics)
|
|
|
|
{
|
|
|
|
gfxFontStyle style(mStyle);
|
|
|
|
style.weight = 700;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool needsBold;
|
2012-12-19 01:42:25 -08:00
|
|
|
|
|
|
|
gfxFontEntry* fe =
|
|
|
|
gfxPlatformFontList::PlatformFontList()->
|
|
|
|
FindFontForFamily(NS_LITERAL_STRING("Arial"), &style, needsBold);
|
2011-03-23 20:01:14 -07:00
|
|
|
if (!fe || fe == mFontEntry) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-03-23 20:01:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxFont> font = fe->FindOrMakeFont(&style, needsBold);
|
|
|
|
gfxDWriteFont *dwFont = static_cast<gfxDWriteFont*>(font.get());
|
|
|
|
dwFont->mFontFace->GetMetrics(aFontMetrics);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-03-23 20:01:14 -07:00
|
|
|
}
|
|
|
|
|
2010-02-25 22:36:07 -08:00
|
|
|
void
|
2011-06-02 21:31:08 -07:00
|
|
|
gfxDWriteFont::ComputeMetrics(AntialiasOption anAAOption)
|
2010-02-25 22:36:07 -08:00
|
|
|
{
|
|
|
|
DWRITE_FONT_METRICS fontMetrics;
|
2011-03-23 20:01:14 -07:00
|
|
|
if (!(mFontEntry->Weight() == 900 &&
|
|
|
|
!mFontEntry->IsUserFont() &&
|
2012-12-19 01:42:25 -08:00
|
|
|
mFontEntry->Name().EqualsLiteral("Arial Black") &&
|
2011-03-23 20:01:14 -07:00
|
|
|
GetFakeMetricsForArialBlack(&fontMetrics)))
|
|
|
|
{
|
|
|
|
mFontFace->GetMetrics(&fontMetrics);
|
|
|
|
}
|
2010-02-25 22:36:07 -08:00
|
|
|
|
2010-03-12 03:57:49 -08:00
|
|
|
if (mStyle.sizeAdjust != 0.0) {
|
|
|
|
gfxFloat aspect = (gfxFloat)fontMetrics.xHeight /
|
|
|
|
fontMetrics.designUnitsPerEm;
|
|
|
|
mAdjustedSize = mStyle.GetAdjustedSize(aspect);
|
|
|
|
} else {
|
|
|
|
mAdjustedSize = mStyle.size;
|
|
|
|
}
|
|
|
|
|
2011-06-02 21:31:08 -07:00
|
|
|
// Note that GetMeasuringMode depends on mAdjustedSize
|
|
|
|
if ((anAAOption == gfxFont::kAntialiasDefault &&
|
|
|
|
UsingClearType() &&
|
|
|
|
GetMeasuringMode() == DWRITE_MEASURING_MODE_NATURAL) ||
|
|
|
|
anAAOption == gfxFont::kAntialiasSubpixel)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mUseSubpixelPositions = true;
|
2011-06-02 21:31:08 -07:00
|
|
|
// note that this may be reset to FALSE if we determine that a bitmap
|
|
|
|
// strike is going to be used
|
|
|
|
}
|
|
|
|
|
2011-01-25 01:17:18 -08:00
|
|
|
gfxDWriteFontEntry *fe =
|
|
|
|
static_cast<gfxDWriteFontEntry*>(mFontEntry.get());
|
|
|
|
if (fe->IsCJKFont() && HasBitmapStrikeForSize(NS_lround(mAdjustedSize))) {
|
2011-01-21 02:36:28 -08:00
|
|
|
mAdjustedSize = NS_lround(mAdjustedSize);
|
2011-10-17 07:59:28 -07:00
|
|
|
mUseSubpixelPositions = false;
|
2011-01-21 11:26:30 -08:00
|
|
|
// if we have bitmaps, we need to tell Cairo NOT to use subpixel AA,
|
|
|
|
// to avoid the manual-subpixel codepath in cairo-d2d-surface.cpp
|
2011-02-15 12:55:01 -08:00
|
|
|
// which fails to render bitmap glyphs (see bug 626299).
|
|
|
|
// This option will be passed to the cairo_dwrite_scaled_font_t
|
|
|
|
// after creation.
|
2011-10-17 07:59:28 -07:00
|
|
|
mAllowManualShowGlyphs = false;
|
2011-01-21 02:36:28 -08:00
|
|
|
}
|
|
|
|
|
2011-01-21 08:44:32 -08:00
|
|
|
mMetrics = new gfxFont::Metrics;
|
|
|
|
::memset(mMetrics, 0, sizeof(*mMetrics));
|
|
|
|
|
2011-04-20 23:29:50 -07:00
|
|
|
mFUnitsConvFactor = float(mAdjustedSize / fontMetrics.designUnitsPerEm);
|
|
|
|
|
|
|
|
mMetrics->xHeight = fontMetrics.xHeight * mFUnitsConvFactor;
|
2010-04-06 13:19:41 -07:00
|
|
|
|
2011-04-20 23:29:50 -07:00
|
|
|
mMetrics->maxAscent = ceil(fontMetrics.ascent * mFUnitsConvFactor);
|
|
|
|
mMetrics->maxDescent = ceil(fontMetrics.descent * mFUnitsConvFactor);
|
2011-01-21 08:44:32 -08:00
|
|
|
mMetrics->maxHeight = mMetrics->maxAscent + mMetrics->maxDescent;
|
2010-04-06 13:19:41 -07:00
|
|
|
|
2011-01-21 08:44:32 -08:00
|
|
|
mMetrics->emHeight = mAdjustedSize;
|
|
|
|
mMetrics->emAscent = mMetrics->emHeight *
|
|
|
|
mMetrics->maxAscent / mMetrics->maxHeight;
|
|
|
|
mMetrics->emDescent = mMetrics->emHeight - mMetrics->emAscent;
|
2010-04-06 13:19:41 -07:00
|
|
|
|
2011-01-21 08:44:32 -08:00
|
|
|
mMetrics->maxAdvance = mAdjustedSize;
|
2010-04-06 13:19:41 -07:00
|
|
|
|
2010-08-05 02:18:42 -07:00
|
|
|
// try to get the true maxAdvance value from 'hhea'
|
2013-05-16 09:29:20 -07:00
|
|
|
gfxFontEntry::AutoTable hheaTable(GetFontEntry(),
|
|
|
|
TRUETYPE_TAG('h','h','e','a'));
|
|
|
|
if (hheaTable) {
|
|
|
|
uint32_t len;
|
|
|
|
const HheaTable* hhea =
|
|
|
|
reinterpret_cast<const HheaTable*>(hb_blob_get_data(hheaTable, &len));
|
|
|
|
if (len >= sizeof(HheaTable)) {
|
2011-04-20 23:29:50 -07:00
|
|
|
mMetrics->maxAdvance =
|
2012-08-22 08:56:38 -07:00
|
|
|
uint16_t(hhea->advanceWidthMax) * mFUnitsConvFactor;
|
2010-08-05 02:18:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
mMetrics->internalLeading = std::max(mMetrics->maxHeight - mMetrics->emHeight, 0.0);
|
2011-04-20 23:29:50 -07:00
|
|
|
mMetrics->externalLeading = ceil(fontMetrics.lineGap * mFUnitsConvFactor);
|
2010-02-25 22:36:07 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
UINT16 glyph = (uint16_t)GetSpaceGlyph();
|
2011-04-20 23:29:50 -07:00
|
|
|
mMetrics->spaceWidth = MeasureGlyphWidth(glyph);
|
2010-08-05 02:18:42 -07:00
|
|
|
|
|
|
|
// try to get aveCharWidth from the OS/2 table, fall back to measuring 'x'
|
2011-04-20 23:29:50 -07:00
|
|
|
// if the table is not available or if using hinted/pixel-snapped widths
|
|
|
|
if (mUseSubpixelPositions) {
|
|
|
|
mMetrics->aveCharWidth = 0;
|
2013-05-16 09:29:20 -07:00
|
|
|
gfxFontEntry::AutoTable os2Table(GetFontEntry(),
|
|
|
|
TRUETYPE_TAG('O','S','/','2'));
|
|
|
|
if (os2Table) {
|
|
|
|
uint32_t len;
|
|
|
|
const OS2Table* os2 =
|
|
|
|
reinterpret_cast<const OS2Table*>(hb_blob_get_data(os2Table, &len));
|
|
|
|
if (len >= 4) {
|
2011-04-20 23:29:50 -07:00
|
|
|
// Not checking against sizeof(mozilla::OS2Table) here because older
|
|
|
|
// versions of the table have different sizes; we only need the first
|
|
|
|
// two 16-bit fields here.
|
|
|
|
mMetrics->aveCharWidth =
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t(os2->xAvgCharWidth) * mFUnitsConvFactor;
|
2011-04-20 23:29:50 -07:00
|
|
|
}
|
2010-08-05 02:18:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 ucs;
|
2011-01-21 08:44:32 -08:00
|
|
|
if (mMetrics->aveCharWidth < 1) {
|
2010-08-05 02:18:42 -07:00
|
|
|
ucs = L'x';
|
2011-04-20 23:29:50 -07:00
|
|
|
if (SUCCEEDED(mFontFace->GetGlyphIndicesA(&ucs, 1, &glyph))) {
|
|
|
|
mMetrics->aveCharWidth = MeasureGlyphWidth(glyph);
|
|
|
|
}
|
|
|
|
if (mMetrics->aveCharWidth < 1) {
|
2010-08-05 02:18:42 -07:00
|
|
|
// Let's just assume the X is square.
|
2011-04-20 23:29:50 -07:00
|
|
|
mMetrics->aveCharWidth = fontMetrics.xHeight * mFUnitsConvFactor;
|
2010-08-05 02:18:42 -07:00
|
|
|
}
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
2010-08-05 02:18:42 -07:00
|
|
|
|
2010-02-25 22:36:07 -08:00
|
|
|
ucs = L'0';
|
2011-04-20 23:29:50 -07:00
|
|
|
if (SUCCEEDED(mFontFace->GetGlyphIndicesA(&ucs, 1, &glyph))) {
|
|
|
|
mMetrics->zeroOrAveCharWidth = MeasureGlyphWidth(glyph);
|
|
|
|
}
|
|
|
|
if (mMetrics->zeroOrAveCharWidth < 1) {
|
2011-01-21 08:44:32 -08:00
|
|
|
mMetrics->zeroOrAveCharWidth = mMetrics->aveCharWidth;
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
2010-08-05 02:18:42 -07:00
|
|
|
|
2011-04-20 23:29:50 -07:00
|
|
|
mMetrics->underlineOffset =
|
|
|
|
fontMetrics.underlinePosition * mFUnitsConvFactor;
|
2011-01-21 08:44:32 -08:00
|
|
|
mMetrics->underlineSize =
|
2011-04-20 23:29:50 -07:00
|
|
|
fontMetrics.underlineThickness * mFUnitsConvFactor;
|
|
|
|
mMetrics->strikeoutOffset =
|
|
|
|
fontMetrics.strikethroughPosition * mFUnitsConvFactor;
|
|
|
|
mMetrics->strikeoutSize =
|
|
|
|
fontMetrics.strikethroughThickness * mFUnitsConvFactor;
|
2010-02-25 22:36:07 -08:00
|
|
|
|
2011-01-21 08:44:32 -08:00
|
|
|
SanitizeMetrics(mMetrics, GetFontEntry()->mIsBadUnderlineFont);
|
2010-03-31 05:46:18 -07:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("Font: %p (%s) size: %f\n", this,
|
|
|
|
NS_ConvertUTF16toUTF8(GetName()).get(), mStyle.size);
|
2011-01-21 08:44:32 -08:00
|
|
|
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);
|
2010-08-05 02:18:42 -07:00
|
|
|
printf(" spaceWidth: %f aveCharWidth: %f zeroOrAve: %f xHeight: %f\n",
|
2011-01-21 08:44:32 -08:00
|
|
|
mMetrics->spaceWidth, mMetrics->aveCharWidth, mMetrics->zeroOrAveCharWidth, mMetrics->xHeight);
|
2014-06-27 23:40:36 -07:00
|
|
|
printf(" uOff: %f uSize: %f stOff: %f stSize: %f\n",
|
|
|
|
mMetrics->underlineOffset, mMetrics->underlineSize, mMetrics->strikeoutOffset, mMetrics->strikeoutSize);
|
2010-03-31 05:46:18 -07:00
|
|
|
#endif
|
|
|
|
}
|
2010-02-25 22:36:07 -08:00
|
|
|
|
2011-01-21 02:36:28 -08:00
|
|
|
using namespace mozilla; // for AutoSwap_* types
|
|
|
|
|
|
|
|
struct EBLCHeader {
|
|
|
|
AutoSwap_PRUint32 version;
|
|
|
|
AutoSwap_PRUint32 numSizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SbitLineMetrics {
|
2012-08-22 08:56:38 -07:00
|
|
|
int8_t ascender;
|
|
|
|
int8_t descender;
|
|
|
|
uint8_t widthMax;
|
|
|
|
int8_t caretSlopeNumerator;
|
|
|
|
int8_t caretSlopeDenominator;
|
|
|
|
int8_t caretOffset;
|
|
|
|
int8_t minOriginSB;
|
|
|
|
int8_t minAdvanceSB;
|
|
|
|
int8_t maxBeforeBL;
|
|
|
|
int8_t minAfterBL;
|
|
|
|
int8_t pad1;
|
|
|
|
int8_t pad2;
|
2011-01-21 02:36:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BitmapSizeTable {
|
|
|
|
AutoSwap_PRUint32 indexSubTableArrayOffset;
|
|
|
|
AutoSwap_PRUint32 indexTablesSize;
|
|
|
|
AutoSwap_PRUint32 numberOfIndexSubTables;
|
|
|
|
AutoSwap_PRUint32 colorRef;
|
|
|
|
SbitLineMetrics hori;
|
|
|
|
SbitLineMetrics vert;
|
|
|
|
AutoSwap_PRUint16 startGlyphIndex;
|
|
|
|
AutoSwap_PRUint16 endGlyphIndex;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t ppemX;
|
|
|
|
uint8_t ppemY;
|
|
|
|
uint8_t bitDepth;
|
|
|
|
uint8_t flags;
|
2011-01-21 02:36:28 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef EBLCHeader EBSCHeader;
|
|
|
|
|
|
|
|
struct BitmapScaleTable {
|
|
|
|
SbitLineMetrics hori;
|
|
|
|
SbitLineMetrics vert;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t ppemX;
|
|
|
|
uint8_t ppemY;
|
|
|
|
uint8_t substitutePpemX;
|
|
|
|
uint8_t substitutePpemY;
|
2011-01-21 02:36:28 -08:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxDWriteFont::HasBitmapStrikeForSize(uint32_t aSize)
|
2011-01-21 02:36:28 -08:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t *tableData;
|
|
|
|
uint32_t len;
|
2011-01-21 02:36:28 -08:00
|
|
|
void *tableContext;
|
|
|
|
BOOL exists;
|
|
|
|
HRESULT hr =
|
|
|
|
mFontFace->TryGetFontTable(DWRITE_MAKE_OPENTYPE_TAG('E', 'B', 'L', 'C'),
|
|
|
|
(const void**)&tableData, &len,
|
|
|
|
&tableContext, &exists);
|
|
|
|
if (FAILED(hr)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-01-21 02:36:28 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasStrike = false;
|
2011-01-21 02:36:28 -08:00
|
|
|
// not really a loop, but this lets us use 'break' to skip out of the block
|
|
|
|
// as soon as we know the answer, and skips it altogether if the table is
|
|
|
|
// not present
|
|
|
|
while (exists) {
|
|
|
|
if (len < sizeof(EBLCHeader)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const EBLCHeader *hdr = reinterpret_cast<const EBLCHeader*>(tableData);
|
|
|
|
if (hdr->version != 0x00020000) {
|
|
|
|
break;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numSizes = hdr->numSizes;
|
2011-01-21 02:36:28 -08:00
|
|
|
if (numSizes > 0xffff) { // sanity-check, prevent overflow below
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len < sizeof(EBLCHeader) + numSizes * sizeof(BitmapSizeTable)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const BitmapSizeTable *sizeTable =
|
|
|
|
reinterpret_cast<const BitmapSizeTable*>(hdr + 1);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < numSizes; ++i, ++sizeTable) {
|
2011-01-21 02:36:28 -08:00
|
|
|
if (sizeTable->ppemX == aSize && sizeTable->ppemY == aSize) {
|
|
|
|
// we ignore a strike that contains fewer than 4 glyphs,
|
|
|
|
// as that probably indicates a font such as Courier New
|
|
|
|
// that provides bitmaps ONLY for the "shading" characters
|
|
|
|
// U+2591..2593
|
2012-08-22 08:56:38 -07:00
|
|
|
hasStrike = (uint16_t(sizeTable->endGlyphIndex) >=
|
|
|
|
uint16_t(sizeTable->startGlyphIndex) + 3);
|
2011-01-21 02:36:28 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we reach here, we didn't find a strike; unconditionally break
|
|
|
|
// out of the while-loop block
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mFontFace->ReleaseFontTable(tableContext);
|
|
|
|
|
|
|
|
if (hasStrike) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-01-21 02:36:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// if we didn't find a real strike, check if the font calls for scaling
|
|
|
|
// another bitmap to this size
|
|
|
|
hr = mFontFace->TryGetFontTable(DWRITE_MAKE_OPENTYPE_TAG('E', 'B', 'S', 'C'),
|
|
|
|
(const void**)&tableData, &len,
|
|
|
|
&tableContext, &exists);
|
|
|
|
if (FAILED(hr)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-01-21 02:36:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (exists) {
|
|
|
|
if (len < sizeof(EBSCHeader)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const EBSCHeader *hdr = reinterpret_cast<const EBSCHeader*>(tableData);
|
|
|
|
if (hdr->version != 0x00020000) {
|
|
|
|
break;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numSizes = hdr->numSizes;
|
2011-01-21 02:36:28 -08:00
|
|
|
if (numSizes > 0xffff) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (len < sizeof(EBSCHeader) + numSizes * sizeof(BitmapScaleTable)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const BitmapScaleTable *scaleTable =
|
|
|
|
reinterpret_cast<const BitmapScaleTable*>(hdr + 1);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < numSizes; ++i, ++scaleTable) {
|
2011-01-21 02:36:28 -08:00
|
|
|
if (scaleTable->ppemX == aSize && scaleTable->ppemY == aSize) {
|
2011-10-17 07:59:28 -07:00
|
|
|
hasStrike = true;
|
2011-01-21 02:36:28 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mFontFace->ReleaseFontTable(tableContext);
|
|
|
|
|
|
|
|
return hasStrike;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2010-02-25 22:36:07 -08:00
|
|
|
gfxDWriteFont::GetSpaceGlyph()
|
|
|
|
{
|
|
|
|
UINT32 ucs = L' ';
|
|
|
|
UINT16 glyph;
|
|
|
|
HRESULT hr;
|
|
|
|
hr = mFontFace->GetGlyphIndicesA(&ucs, 1, &glyph);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return glyph;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-02-25 22:36:07 -08:00
|
|
|
gfxDWriteFont::SetupCairoFont(gfxContext *aContext)
|
|
|
|
{
|
2012-11-12 11:35:49 -08:00
|
|
|
cairo_scaled_font_t *scaledFont = GetCairoScaledFont();
|
2010-02-25 22:36:07 -08:00
|
|
|
if (cairo_scaled_font_status(scaledFont) != CAIRO_STATUS_SUCCESS) {
|
|
|
|
// Don't cairo_set_scaled_font as that would propagate the error to
|
|
|
|
// the cairo_t, precluding any further drawing.
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
|
|
|
cairo_set_scaled_font(aContext->GetCairo(), scaledFont);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-01-21 08:44:32 -08:00
|
|
|
gfxDWriteFont::IsValid()
|
|
|
|
{
|
2013-07-31 08:44:31 -07:00
|
|
|
return mFontFace != nullptr;
|
2011-01-21 08:44:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
IDWriteFontFace*
|
|
|
|
gfxDWriteFont::GetFontFace()
|
|
|
|
{
|
|
|
|
return mFontFace.get();
|
|
|
|
}
|
|
|
|
|
2010-02-25 22:36:07 -08:00
|
|
|
cairo_font_face_t *
|
|
|
|
gfxDWriteFont::CairoFontFace()
|
|
|
|
{
|
|
|
|
if (!mCairoFontFace) {
|
|
|
|
#ifdef CAIRO_HAS_DWRITE_FONT
|
|
|
|
mCairoFontFace =
|
|
|
|
cairo_dwrite_font_face_create_for_dwrite_fontface(
|
|
|
|
((gfxDWriteFontEntry*)mFontEntry.get())->mFont, mFontFace);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return mCairoFontFace;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cairo_scaled_font_t *
|
2012-11-12 11:35:49 -08:00
|
|
|
gfxDWriteFont::GetCairoScaledFont()
|
2010-02-25 22:36:07 -08:00
|
|
|
{
|
2011-12-15 08:12:40 -08:00
|
|
|
if (!mScaledFont) {
|
2010-02-25 22:36:07 -08:00
|
|
|
cairo_matrix_t sizeMatrix;
|
|
|
|
cairo_matrix_t identityMatrix;
|
|
|
|
|
2010-03-12 03:57:49 -08:00
|
|
|
cairo_matrix_init_scale(&sizeMatrix, mAdjustedSize, mAdjustedSize);
|
2010-02-25 22:36:07 -08:00
|
|
|
cairo_matrix_init_identity(&identityMatrix);
|
|
|
|
|
|
|
|
cairo_font_options_t *fontOptions = cairo_font_options_create();
|
|
|
|
if (mNeedsOblique) {
|
|
|
|
double skewfactor = OBLIQUE_SKEW_FACTOR;
|
|
|
|
|
|
|
|
cairo_matrix_t style;
|
|
|
|
cairo_matrix_init(&style,
|
|
|
|
1, //xx
|
|
|
|
0, //yx
|
|
|
|
-1 * skewfactor, //xy
|
|
|
|
1, //yy
|
|
|
|
0, //x0
|
|
|
|
0); //y0
|
|
|
|
cairo_matrix_multiply(&sizeMatrix, &sizeMatrix, &style);
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:19:51 -07:00
|
|
|
if (mAntialiasOption != kAntialiasDefault) {
|
|
|
|
cairo_font_options_set_antialias(fontOptions,
|
|
|
|
GetCairoAntialiasOption(mAntialiasOption));
|
|
|
|
}
|
|
|
|
|
2011-12-15 08:12:40 -08:00
|
|
|
mScaledFont = cairo_scaled_font_create(CairoFontFace(),
|
2010-02-25 22:36:07 -08:00
|
|
|
&sizeMatrix,
|
|
|
|
&identityMatrix,
|
|
|
|
fontOptions);
|
|
|
|
cairo_font_options_destroy(fontOptions);
|
2011-02-15 12:55:01 -08:00
|
|
|
|
2011-12-15 08:12:40 -08:00
|
|
|
cairo_dwrite_scaled_font_allow_manual_show_glyphs(mScaledFont,
|
2011-02-15 12:55:01 -08:00
|
|
|
mAllowManualShowGlyphs);
|
2011-06-02 21:31:07 -07:00
|
|
|
|
|
|
|
gfxDWriteFontEntry *fe =
|
|
|
|
static_cast<gfxDWriteFontEntry*>(mFontEntry.get());
|
2011-12-15 08:12:40 -08:00
|
|
|
cairo_dwrite_scaled_font_set_force_GDI_classic(mScaledFont,
|
2011-06-02 21:31:08 -07:00
|
|
|
GetForceGDIClassic());
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
|
|
|
|
2010-03-12 03:57:49 -08:00
|
|
|
NS_ASSERTION(mAdjustedSize == 0.0 ||
|
2011-12-15 08:12:40 -08:00
|
|
|
cairo_scaled_font_status(mScaledFont)
|
2010-02-25 22:36:07 -08:00
|
|
|
== CAIRO_STATUS_SUCCESS,
|
|
|
|
"Failed to make scaled font");
|
|
|
|
|
2011-12-15 08:12:40 -08:00
|
|
|
return mScaledFont;
|
2010-02-25 22:36:07 -08:00
|
|
|
}
|
2010-06-11 12:14:38 -07:00
|
|
|
|
2011-06-30 23:38:17 -07:00
|
|
|
gfxFont::RunMetrics
|
|
|
|
gfxDWriteFont::Measure(gfxTextRun *aTextRun,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aStart, uint32_t aEnd,
|
2011-06-30 23:38:17 -07:00
|
|
|
BoundingBoxType aBoundingBoxType,
|
|
|
|
gfxContext *aRefContext,
|
|
|
|
Spacing *aSpacing)
|
|
|
|
{
|
|
|
|
gfxFont::RunMetrics metrics =
|
|
|
|
gfxFont::Measure(aTextRun, aStart, aEnd,
|
|
|
|
aBoundingBoxType, aRefContext, aSpacing);
|
|
|
|
|
|
|
|
// if aBoundingBoxType is LOOSE_INK_EXTENTS
|
|
|
|
// and the underlying cairo font may be antialiased,
|
|
|
|
// we can't trust Windows to have considered all the pixels
|
|
|
|
// so we need to add "padding" to the bounds.
|
|
|
|
// (see bugs 475968, 439831, compare also bug 445087)
|
|
|
|
if (aBoundingBoxType == LOOSE_INK_EXTENTS &&
|
|
|
|
mAntialiasOption != kAntialiasNone &&
|
|
|
|
GetMeasuringMode() == DWRITE_MEASURING_MODE_GDI_CLASSIC &&
|
|
|
|
metrics.mBoundingBox.width > 0) {
|
|
|
|
metrics.mBoundingBox.x -= aTextRun->GetAppUnitsPerDevUnit();
|
|
|
|
metrics.mBoundingBox.width += aTextRun->GetAppUnitsPerDevUnit() * 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return metrics;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-06-09 07:47:31 -07:00
|
|
|
gfxDWriteFont::ProvidesGlyphWidths() const
|
2011-01-21 08:44:33 -08:00
|
|
|
{
|
2011-02-15 12:55:01 -08:00
|
|
|
return !mUseSubpixelPositions ||
|
|
|
|
(mFontFace->GetSimulations() & DWRITE_FONT_SIMULATIONS_BOLD);
|
|
|
|
}
|
2011-01-21 08:44:33 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
|
|
|
gfxDWriteFont::GetGlyphWidth(gfxContext *aCtx, uint16_t aGID)
|
2011-01-04 08:58:31 -08:00
|
|
|
{
|
2013-09-02 01:41:57 -07:00
|
|
|
if (!mGlyphWidths) {
|
|
|
|
mGlyphWidths = new nsDataHashtable<nsUint32HashKey,int32_t>(200);
|
2011-01-04 08:58:31 -08:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t width = -1;
|
2013-09-02 01:41:57 -07:00
|
|
|
if (mGlyphWidths->Get(aGID, &width)) {
|
2011-01-04 08:58:31 -08:00
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2011-04-20 23:29:50 -07:00
|
|
|
width = NS_lround(MeasureGlyphWidth(aGID) * 65536.0);
|
2013-09-02 01:41:57 -07:00
|
|
|
mGlyphWidths->Put(aGID, width);
|
2011-04-20 23:29:50 -07:00
|
|
|
return width;
|
|
|
|
}
|
|
|
|
|
2012-03-19 12:20:18 -07:00
|
|
|
TemporaryRef<GlyphRenderingOptions>
|
|
|
|
gfxDWriteFont::GetGlyphRenderingOptions()
|
|
|
|
{
|
|
|
|
if (UsingClearType()) {
|
|
|
|
return Factory::CreateDWriteGlyphRenderingOptions(
|
|
|
|
gfxWindowsPlatform::GetPlatform()->GetRenderingParams(GetForceGDIClassic() ?
|
|
|
|
gfxWindowsPlatform::TEXT_RENDERING_GDI_CLASSIC : gfxWindowsPlatform::TEXT_RENDERING_NORMAL));
|
|
|
|
} else {
|
|
|
|
return Factory::CreateDWriteGlyphRenderingOptions(gfxWindowsPlatform::GetPlatform()->
|
|
|
|
GetRenderingParams(gfxWindowsPlatform::TEXT_RENDERING_NO_CLEARTYPE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-02 21:31:08 -07:00
|
|
|
bool
|
|
|
|
gfxDWriteFont::GetForceGDIClassic()
|
|
|
|
{
|
|
|
|
return static_cast<gfxDWriteFontEntry*>(mFontEntry.get())->GetForceGDIClassic() &&
|
2011-06-09 00:17:20 -07:00
|
|
|
cairo_dwrite_get_cleartype_rendering_mode() < 0 &&
|
|
|
|
GetAdjustedSize() <=
|
2011-06-02 21:31:08 -07:00
|
|
|
gfxDWriteFontList::PlatformFontList()->GetForceGDIClassicMaxFontSize();
|
|
|
|
}
|
|
|
|
|
2011-06-02 21:31:07 -07:00
|
|
|
DWRITE_MEASURING_MODE
|
|
|
|
gfxDWriteFont::GetMeasuringMode()
|
|
|
|
{
|
2011-06-02 21:31:08 -07:00
|
|
|
return GetForceGDIClassic()
|
2011-06-02 21:31:07 -07:00
|
|
|
? DWRITE_MEASURING_MODE_GDI_CLASSIC
|
|
|
|
: gfxWindowsPlatform::GetPlatform()->DWriteMeasuringMode();
|
|
|
|
}
|
|
|
|
|
2011-04-20 23:29:50 -07:00
|
|
|
gfxFloat
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxDWriteFont::MeasureGlyphWidth(uint16_t aGlyph)
|
2011-04-20 23:29:50 -07:00
|
|
|
{
|
|
|
|
DWRITE_GLYPH_METRICS metrics;
|
2011-01-21 02:35:21 -08:00
|
|
|
HRESULT hr;
|
2011-01-21 02:36:28 -08:00
|
|
|
if (mUseSubpixelPositions) {
|
2011-04-20 23:29:50 -07:00
|
|
|
hr = mFontFace->GetDesignGlyphMetrics(&aGlyph, 1, &metrics, FALSE);
|
2011-01-21 02:35:21 -08:00
|
|
|
if (SUCCEEDED(hr)) {
|
2011-04-20 23:29:50 -07:00
|
|
|
return metrics.advanceWidth * mFUnitsConvFactor;
|
2011-01-21 02:35:21 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hr = mFontFace->GetGdiCompatibleGlyphMetrics(
|
2012-07-30 07:20:58 -07:00
|
|
|
FLOAT(mAdjustedSize), 1.0f, nullptr,
|
2011-06-02 21:31:07 -07:00
|
|
|
GetMeasuringMode() == DWRITE_MEASURING_MODE_GDI_NATURAL,
|
2011-04-20 23:29:50 -07:00
|
|
|
&aGlyph, 1, &metrics, FALSE);
|
2011-01-21 02:35:21 -08:00
|
|
|
if (SUCCEEDED(hr)) {
|
2011-04-20 23:29:50 -07:00
|
|
|
return NS_lround(metrics.advanceWidth * mFUnitsConvFactor);
|
2011-01-21 02:35:21 -08:00
|
|
|
}
|
2011-01-04 08:58:31 -08:00
|
|
|
}
|
2011-04-20 23:29:50 -07:00
|
|
|
return 0;
|
2011-01-04 08:58:31 -08:00
|
|
|
}
|
2012-03-23 05:14:16 -07:00
|
|
|
|
|
|
|
void
|
2013-10-14 19:19:47 -07:00
|
|
|
gfxDWriteFont::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
FontCacheSizes* aSizes) const
|
2012-03-23 05:14:16 -07:00
|
|
|
{
|
2013-10-14 19:19:47 -07:00
|
|
|
gfxFont::AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2013-09-02 01:41:57 -07:00
|
|
|
aSizes->mFontInstances += aMallocSizeOf(mMetrics);
|
|
|
|
if (mGlyphWidths) {
|
|
|
|
aSizes->mFontInstances +=
|
2014-05-01 10:37:54 -07:00
|
|
|
mGlyphWidths->SizeOfIncludingThis(nullptr, aMallocSizeOf);
|
2013-09-02 01:41:57 -07:00
|
|
|
}
|
2012-03-23 05:14:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-10-14 19:19:47 -07:00
|
|
|
gfxDWriteFont::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
FontCacheSizes* aSizes) const
|
2012-03-23 05:14:16 -07:00
|
|
|
{
|
|
|
|
aSizes->mFontInstances += aMallocSizeOf(this);
|
2013-10-14 19:19:47 -07:00
|
|
|
AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2012-03-23 05:14:16 -07:00
|
|
|
}
|
2012-09-24 08:02:49 -07:00
|
|
|
|
|
|
|
TemporaryRef<ScaledFont>
|
|
|
|
gfxDWriteFont::GetScaledFont(mozilla::gfx::DrawTarget *aTarget)
|
|
|
|
{
|
2014-06-19 13:35:33 -07:00
|
|
|
bool wantCairo = aTarget->GetBackendType() == BackendType::CAIRO;
|
2012-09-24 08:02:49 -07:00
|
|
|
if (mAzureScaledFont && mAzureScaledFontIsCairo == wantCairo) {
|
|
|
|
return mAzureScaledFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
NativeFont nativeFont;
|
2014-01-10 11:06:16 -08:00
|
|
|
nativeFont.mType = NativeFontType::DWRITE_FONT_FACE;
|
2012-09-24 08:02:49 -07:00
|
|
|
nativeFont.mFont = GetFontFace();
|
|
|
|
|
|
|
|
if (wantCairo) {
|
|
|
|
mAzureScaledFont = Factory::CreateScaledFontWithCairo(nativeFont,
|
|
|
|
GetAdjustedSize(),
|
2012-11-12 11:35:49 -08:00
|
|
|
GetCairoScaledFont());
|
2012-09-24 08:02:49 -07:00
|
|
|
} else {
|
|
|
|
mAzureScaledFont = Factory::CreateScaledFontForNativeFont(nativeFont,
|
|
|
|
GetAdjustedSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
mAzureScaledFontIsCairo = wantCairo;
|
|
|
|
|
|
|
|
return mAzureScaledFont;
|
|
|
|
}
|