Bug 1611467 - unify UnscaledFont::GetFontDescriptor and GetWRFontDescriptor implementations. r=jrmuizel

Differential Revision: https://phabricator.services.mozilla.com/D68284

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Lee Salzman 2020-03-26 16:11:00 +00:00
Родитель b94670173c
Коммит e7163f076c
10 изменённых файлов: 102 добавлений и 28 удалений

Просмотреть файл

@ -902,8 +902,6 @@ class UnscaledFont : public SupportsThreadSafeWeakPtr<UnscaledFont> {
typedef void (*FontFileDataOutput)(const uint8_t* aData, uint32_t aLength,
uint32_t aIndex, void* aBaton);
typedef void (*WRFontDescriptorOutput)(const uint8_t* aData, uint32_t aLength,
uint32_t aIndex, void* aBaton);
typedef void (*FontInstanceDataOutput)(const uint8_t* aData, uint32_t aLength,
void* aBaton);
typedef void (*FontDescriptorOutput)(const uint8_t* aData, uint32_t aLength,
@ -911,10 +909,6 @@ class UnscaledFont : public SupportsThreadSafeWeakPtr<UnscaledFont> {
virtual bool GetFontFileData(FontFileDataOutput, void*) { return false; }
virtual bool GetWRFontDescriptor(WRFontDescriptorOutput, void*) {
return false;
}
virtual bool GetFontInstanceData(FontInstanceDataOutput, void*) {
return false;
}

Просмотреть файл

@ -26,6 +26,7 @@
#ifdef XP_DARWIN
# include "ScaledFontMac.h"
# include "NativeFontResourceMac.h"
# include "UnscaledFontMac.h"
#endif
#ifdef MOZ_WIDGET_GTK
@ -37,12 +38,14 @@
#ifdef MOZ_WIDGET_ANDROID
# include "ScaledFontFreeType.h"
# include "NativeFontResourceFreeType.h"
# include "UnscaledFontFreeType.h"
#endif
#ifdef WIN32
# include "DrawTargetD2D1.h"
# include "ScaledFontDWrite.h"
# include "NativeFontResourceDWrite.h"
# include "UnscaledFontDWrite.h"
# include <d3d10_1.h>
# include <stdlib.h>
# include "HelpersD2D.h"
@ -595,14 +598,24 @@ already_AddRefed<UnscaledFont> Factory::CreateUnscaledFontFromFontDescriptor(
uint32_t aIndex) {
switch (aType) {
#ifdef WIN32
case FontType::DWRITE:
return UnscaledFontDWrite::CreateFromFontDescriptor(aData, aDataLength,
aIndex);
case FontType::GDI:
return UnscaledFontGDI::CreateFromFontDescriptor(aData, aDataLength,
aIndex);
#endif
#ifdef MOZ_WIDGET_GTK
#elif defined(XP_DARWIN)
case FontType::MAC:
return UnscaledFontMac::CreateFromFontDescriptor(aData, aDataLength,
aIndex);
#elif defined(MOZ_WIDGET_GTK)
case FontType::FONTCONFIG:
return UnscaledFontFontconfig::CreateFromFontDescriptor(
aData, aDataLength, aIndex);
#elif defined(MOZ_WIDGET_ANDROID)
case FontType::FREETYPE:
return UnscaledFontFreeType::CreateFromFontDescriptor(aData, aDataLength,
aIndex);
#endif
default:
gfxWarning() << "Invalid type specified for UnscaledFont font descriptor";

Просмотреть файл

@ -381,8 +381,8 @@ static bool GetFontFileName(RefPtr<IDWriteFontFace> aFontFace,
return true;
}
bool UnscaledFontDWrite::GetWRFontDescriptor(WRFontDescriptorOutput aCb,
void* aBaton) {
bool UnscaledFontDWrite::GetFontDescriptor(FontDescriptorOutput aCb,
void* aBaton) {
if (!mFont) {
return false;
}
@ -710,5 +710,42 @@ void ScaledFontDWrite::PrepareCairoScaledFont(cairo_scaled_font_t* aFont) {
}
#endif
already_AddRefed<UnscaledFont> UnscaledFontDWrite::CreateFromFontDescriptor(
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex) {
if (aDataLength == 0) {
gfxWarning() << "DWrite font descriptor is truncated.";
return nullptr;
}
RefPtr<IDWriteFactory> factory = Factory::GetDWriteFactory();
if (!factory) {
return nullptr;
}
RefPtr<IDWriteFontFile> fontFile;
HRESULT hr = factory->CreateFontFileReference((const WCHAR*)aData, nullptr,
getter_AddRefs(fontFile));
if (FAILED(hr)) {
return nullptr;
}
BOOL isSupported;
DWRITE_FONT_FILE_TYPE fileType;
DWRITE_FONT_FACE_TYPE faceType;
UINT32 numFaces;
hr = fontFile->Analyze(&isSupported, &fileType, &faceType, &numFaces);
if (FAILED(hr) || !isSupported || aIndex >= numFaces) {
return nullptr;
}
IDWriteFontFile* fontFiles[1] = {fontFile.get()};
RefPtr<IDWriteFontFace> fontFace;
hr = factory->CreateFontFace(faceType, 1, fontFiles, aIndex,
DWRITE_FONT_SIMULATIONS_NONE,
getter_AddRefs(fontFace));
if (FAILED(hr)) {
return nullptr;
}
RefPtr<UnscaledFont> unscaledFont = new UnscaledFontDWrite(fontFace, nullptr);
return unscaledFont.forget();
}
} // namespace gfx
} // namespace mozilla

Просмотреть файл

@ -181,5 +181,17 @@ bool ScaledFontFreeType::HasVariationSettings() {
static_cast<UnscaledFontFreeType*>(mUnscaledFont.get())->GetFace();
}
already_AddRefed<UnscaledFont> UnscaledFontFreeType::CreateFromFontDescriptor(
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex) {
if (aDataLength == 0) {
gfxWarning() << "FreeType font descriptor is truncated.";
return nullptr;
}
const char* path = reinterpret_cast<const char*>(aData);
RefPtr<UnscaledFont> unscaledFont =
new UnscaledFontFreeType(std::string(path, aDataLength), aIndex);
return unscaledFont.forget();
}
} // namespace gfx
} // namespace mozilla

Просмотреть файл

@ -374,8 +374,8 @@ bool UnscaledFontMac::GetFontFileData(FontFileDataOutput aDataCallback,
return true;
}
bool UnscaledFontMac::GetWRFontDescriptor(WRFontDescriptorOutput aCb,
void* aBaton) {
bool UnscaledFontMac::GetFontDescriptor(FontDescriptorOutput aCb,
void* aBaton) {
if (mIsDataFont) {
return false;
}
@ -654,5 +654,27 @@ cairo_font_face_t* ScaledFontMac::CreateCairoFontFace(
}
#endif
already_AddRefed<UnscaledFont> UnscaledFontMac::CreateFromFontDescriptor(
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex) {
if (aDataLength == 0) {
gfxWarning() << "Mac font descriptor is truncated.";
return nullptr;
}
CFStringRef name =
CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8*)aData,
aDataLength, kCFStringEncodingUTF8, false);
if (!name) {
return nullptr;
}
CGFontRef font = CGFontCreateWithFontName(name);
CFRelease(name);
if (!font) {
return nullptr;
}
RefPtr<UnscaledFont> unscaledFont = new UnscaledFontMac(font);
CFRelease(font);
return unscaledFont.forget();
}
} // namespace gfx
} // namespace mozilla

Просмотреть файл

@ -40,7 +40,10 @@ class UnscaledFontDWrite final : public UnscaledFont {
const wr::FontInstancePlatformOptions* aPlatformOptions,
const FontVariation* aVariations, uint32_t aNumVariations) override;
bool GetWRFontDescriptor(WRFontDescriptorOutput aCb, void* aBaton) override;
bool GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton) override;
static already_AddRefed<UnscaledFont> CreateFromFontDescriptor(
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex);
private:
bool InitBold();

Просмотреть файл

@ -77,17 +77,6 @@ bool UnscaledFontFreeType::GetFontDescriptor(FontDescriptorOutput aCb,
return true;
}
bool UnscaledFontFreeType::GetWRFontDescriptor(WRFontDescriptorOutput aCb,
void* aBaton) {
if (mFile.empty()) {
return false;
}
aCb(reinterpret_cast<const uint8_t*>(mFile.data()), mFile.size(), mIndex,
aBaton);
return true;
}
RefPtr<SharedFTFace> UnscaledFontFreeType::InitFace() {
if (mFace) {
return mFace;

Просмотреть файл

@ -39,11 +39,12 @@ class UnscaledFontFreeType : public UnscaledFont {
bool GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton) override;
bool GetWRFontDescriptor(WRFontDescriptorOutput aCb, void* aBaton) override;
RefPtr<SharedFTFace> InitFace();
#ifdef MOZ_WIDGET_ANDROID
static already_AddRefed<UnscaledFont> CreateFromFontDescriptor(
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex);
already_AddRefed<ScaledFont> CreateScaledFont(
Float aGlyphSize, const uint8_t* aInstanceData,
uint32_t aInstanceDataLength, const FontVariation* aVariations,

Просмотреть файл

@ -50,7 +50,10 @@ class UnscaledFontMac final : public UnscaledFont {
uint32_t aVariationCount,
const FontVariation* aVariations);
bool GetWRFontDescriptor(WRFontDescriptorOutput aCb, void* aBaton) override;
bool GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton) override;
static already_AddRefed<UnscaledFont> CreateFromFontDescriptor(
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex);
private:
CGFontRef mFont;

Просмотреть файл

@ -329,7 +329,7 @@ Maybe<wr::FontKey> WebRenderBridgeChild::GetFontKeyForUnscaledFont(
// then and only then fall back to getting the raw font file data. If that
// fails, then the only thing left to do is signal failure by returning a
// null font key.
if (!aUnscaled->GetWRFontDescriptor(WriteFontDescriptor, &sink) &&
if (!aUnscaled->GetFontDescriptor(WriteFontDescriptor, &sink) &&
!aUnscaled->GetFontFileData(WriteFontFileData, &sink)) {
return Nothing();
}