2011-06-24 21:41:16 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04: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/. */
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#include "ScaledFontDWrite.h"
|
|
|
|
#include "PathD2D.h"
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
#include <vector>
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2016-01-05 13:08:56 +03:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
|
|
|
#include "cairo-win32.h"
|
|
|
|
#endif
|
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2012-11-23 05:53:12 +04:00
|
|
|
static BYTE
|
|
|
|
GetSystemTextQuality()
|
|
|
|
{
|
|
|
|
BOOL font_smoothing;
|
|
|
|
UINT smoothing_type;
|
|
|
|
|
|
|
|
if (!SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &font_smoothing, 0)) {
|
|
|
|
return DEFAULT_QUALITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (font_smoothing) {
|
|
|
|
if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE,
|
|
|
|
0, &smoothing_type, 0)) {
|
|
|
|
return DEFAULT_QUALITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smoothing_type == FE_FONTSMOOTHINGCLEARTYPE) {
|
|
|
|
return CLEARTYPE_QUALITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ANTIALIASED_QUALITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DEFAULT_QUALITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GASP_TAG 0x70736167
|
|
|
|
#define GASP_DOGRAY 0x2
|
|
|
|
|
|
|
|
static inline unsigned short
|
|
|
|
readShort(const char *aBuf)
|
|
|
|
{
|
|
|
|
return (*aBuf << 8) | *(aBuf + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2013-01-15 16:21:00 +04:00
|
|
|
DoGrayscale(IDWriteFontFace *aDWFace, Float ppem)
|
2012-11-23 05:53:12 +04:00
|
|
|
{
|
|
|
|
void *tableContext;
|
|
|
|
char *tableData;
|
|
|
|
UINT32 tableSize;
|
|
|
|
BOOL exists;
|
|
|
|
aDWFace->TryGetFontTable(GASP_TAG, (const void**)&tableData, &tableSize, &tableContext, &exists);
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
if (tableSize < 4) {
|
|
|
|
aDWFace->ReleaseFontTable(tableContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
struct gaspRange {
|
|
|
|
unsigned short maxPPEM; // Stored big-endian
|
|
|
|
unsigned short behavior; // Stored big-endian
|
|
|
|
};
|
|
|
|
unsigned short numRanges = readShort(tableData + 2);
|
|
|
|
if (tableSize < (UINT)4 + numRanges * 4) {
|
|
|
|
aDWFace->ReleaseFontTable(tableContext);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
gaspRange *ranges = (gaspRange *)(tableData + 4);
|
|
|
|
for (int i = 0; i < numRanges; i++) {
|
|
|
|
if (readShort((char*)&ranges[i].maxPPEM) > ppem) {
|
|
|
|
if (!(readShort((char*)&ranges[i].behavior) & GASP_DOGRAY)) {
|
|
|
|
aDWFace->ReleaseFontTable(tableContext);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aDWFace->ReleaseFontTable(tableContext);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-24 19:02:49 +04:00
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<Path>
|
2011-06-24 21:41:16 +04:00
|
|
|
ScaledFontDWrite::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget)
|
|
|
|
{
|
2014-09-15 01:52:46 +04:00
|
|
|
if (aTarget->GetBackendType() != BackendType::DIRECT2D && aTarget->GetBackendType() != BackendType::DIRECT2D1_1) {
|
2012-12-05 04:11:02 +04:00
|
|
|
return ScaledFontBase::GetPathForGlyphs(aBuffer, aTarget);
|
2011-06-24 21:41:16 +04:00
|
|
|
}
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> pathBuilder = aTarget->CreatePathBuilder();
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
PathBuilderD2D *pathBuilderD2D =
|
|
|
|
static_cast<PathBuilderD2D*>(pathBuilder.get());
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2012-03-29 22:53:44 +04:00
|
|
|
CopyGlyphsToSink(aBuffer, pathBuilderD2D->GetSink());
|
|
|
|
|
|
|
|
return pathBuilder->Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-12-13 02:37:00 +04:00
|
|
|
ScaledFontDWrite::CopyGlyphsToBuilder(const GlyphBuffer &aBuffer, PathBuilder *aBuilder, BackendType aBackendType, const Matrix *aTransformHint)
|
2012-03-29 22:53:44 +04:00
|
|
|
{
|
2014-10-01 21:50:24 +04:00
|
|
|
if (aBackendType != BackendType::DIRECT2D && aBackendType != BackendType::DIRECT2D1_1) {
|
2013-12-13 02:37:00 +04:00
|
|
|
ScaledFontBase::CopyGlyphsToBuilder(aBuffer, aBuilder, aBackendType, aTransformHint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-29 22:53:44 +04:00
|
|
|
PathBuilderD2D *pathBuilderD2D =
|
|
|
|
static_cast<PathBuilderD2D*>(aBuilder);
|
|
|
|
|
|
|
|
CopyGlyphsToSink(aBuffer, pathBuilderD2D->GetSink());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ScaledFontDWrite::CopyGlyphsToSink(const GlyphBuffer &aBuffer, ID2D1GeometrySink *aSink)
|
|
|
|
{
|
2011-06-24 21:41:16 +04:00
|
|
|
std::vector<UINT16> indices;
|
|
|
|
std::vector<FLOAT> advances;
|
|
|
|
std::vector<DWRITE_GLYPH_OFFSET> offsets;
|
|
|
|
indices.resize(aBuffer.mNumGlyphs);
|
|
|
|
advances.resize(aBuffer.mNumGlyphs);
|
|
|
|
offsets.resize(aBuffer.mNumGlyphs);
|
2011-05-26 23:41:33 +04:00
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
memset(&advances.front(), 0, sizeof(FLOAT) * aBuffer.mNumGlyphs);
|
|
|
|
for (unsigned int i = 0; i < aBuffer.mNumGlyphs; i++) {
|
|
|
|
indices[i] = aBuffer.mGlyphs[i].mIndex;
|
|
|
|
offsets[i].advanceOffset = aBuffer.mGlyphs[i].mPosition.x;
|
|
|
|
offsets[i].ascenderOffset = -aBuffer.mGlyphs[i].mPosition.y;
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
mFontFace->GetGlyphRunOutline(mSize, &indices.front(), &advances.front(),
|
|
|
|
&offsets.front(), aBuffer.mNumGlyphs,
|
2012-03-29 22:53:44 +04:00
|
|
|
FALSE, FALSE, aSink);
|
2011-05-26 23:41:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-24 19:02:49 +04:00
|
|
|
bool
|
|
|
|
ScaledFontDWrite::GetFontFileData(FontFileDataOutput aDataCallback, void *aBaton)
|
|
|
|
{
|
|
|
|
UINT32 fileCount = 0;
|
|
|
|
mFontFace->GetFiles(&fileCount, nullptr);
|
|
|
|
|
|
|
|
if (fileCount > 1) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDWriteFontFile> file;
|
2015-10-18 07:40:10 +03:00
|
|
|
mFontFace->GetFiles(&fileCount, getter_AddRefs(file));
|
2012-09-24 19:02:49 +04:00
|
|
|
|
|
|
|
const void *referenceKey;
|
|
|
|
UINT32 refKeySize;
|
|
|
|
// XXX - This can currently crash for webfonts, as when we get the reference
|
|
|
|
// key out of the file, that can be an invalid reference key for the loader
|
|
|
|
// we use it with. The fix to this is not obvious but it will probably
|
|
|
|
// have to happen inside thebes.
|
|
|
|
file->GetReferenceKey(&referenceKey, &refKeySize);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDWriteFontFileLoader> loader;
|
2015-10-18 07:40:10 +03:00
|
|
|
file->GetLoader(getter_AddRefs(loader));
|
2012-09-24 19:02:49 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDWriteFontFileStream> stream;
|
2015-10-18 07:40:10 +03:00
|
|
|
loader->CreateStreamFromKey(referenceKey, refKeySize, getter_AddRefs(stream));
|
2012-09-24 19:02:49 +04:00
|
|
|
|
2013-01-15 16:21:00 +04:00
|
|
|
UINT64 fileSize64;
|
|
|
|
stream->GetFileSize(&fileSize64);
|
|
|
|
if (fileSize64 > UINT32_MAX) {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t fileSize = static_cast<uint32_t>(fileSize64);
|
2012-09-24 19:02:49 +04:00
|
|
|
const void *fragmentStart;
|
|
|
|
void *context;
|
|
|
|
stream->ReadFileFragment(&fragmentStart, 0, fileSize, &context);
|
|
|
|
|
|
|
|
aDataCallback((uint8_t*)fragmentStart, fileSize, mFontFace->GetIndex(), mSize, aBaton);
|
|
|
|
|
|
|
|
stream->ReleaseFileFragment(context);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-23 05:53:12 +04:00
|
|
|
AntialiasMode
|
|
|
|
ScaledFontDWrite::GetDefaultAAMode()
|
|
|
|
{
|
2014-01-10 23:06:17 +04:00
|
|
|
AntialiasMode defaultMode = AntialiasMode::SUBPIXEL;
|
2012-11-23 05:53:12 +04:00
|
|
|
|
|
|
|
switch (GetSystemTextQuality()) {
|
|
|
|
case CLEARTYPE_QUALITY:
|
2014-01-10 23:06:17 +04:00
|
|
|
defaultMode = AntialiasMode::SUBPIXEL;
|
2012-11-23 05:53:12 +04:00
|
|
|
break;
|
|
|
|
case ANTIALIASED_QUALITY:
|
2014-01-10 23:06:17 +04:00
|
|
|
defaultMode = AntialiasMode::GRAY;
|
2012-11-23 05:53:12 +04:00
|
|
|
break;
|
|
|
|
case DEFAULT_QUALITY:
|
2014-01-10 23:06:17 +04:00
|
|
|
defaultMode = AntialiasMode::NONE;
|
2012-11-23 05:53:12 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-01-10 23:06:17 +04:00
|
|
|
if (defaultMode == AntialiasMode::GRAY) {
|
2012-11-23 05:53:12 +04:00
|
|
|
if (!DoGrayscale(mFontFace, mSize)) {
|
2014-01-10 23:06:17 +04:00
|
|
|
defaultMode = AntialiasMode::NONE;
|
2012-11-23 05:53:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultMode;
|
|
|
|
}
|
|
|
|
|
2016-01-05 13:08:56 +03:00
|
|
|
#ifdef USE_CAIRO_SCALED_FONT
|
|
|
|
cairo_font_face_t*
|
|
|
|
ScaledFontDWrite::GetCairoFontFace()
|
|
|
|
{
|
|
|
|
if (!mFontFace) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cairo_dwrite_font_face_create_for_dwrite_fontface(nullptr, mFontFace);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-24 21:41:16 +04:00
|
|
|
}
|
|
|
|
}
|