From b3d0efd4a74c3f038583e159c27c4fecf4d93570 Mon Sep 17 00:00:00 2001 From: Jonathan Kew Date: Wed, 14 Feb 2018 22:02:05 +1100 Subject: [PATCH] Bug 1435984 - patch 6 - Apply variation settings from the font entry when instantiation fonts for DirectWrite. r=lsalzman --HG-- extra : source : a6b5ddcc7114f50042f783766cabb545f912d8c4 --- gfx/thebes/gfxDWriteFontList.cpp | 61 +++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/gfx/thebes/gfxDWriteFontList.cpp b/gfx/thebes/gfxDWriteFontList.cpp index 87b45b5d9468..52e6822278a6 100644 --- a/gfx/thebes/gfxDWriteFontList.cpp +++ b/gfx/thebes/gfxDWriteFontList.cpp @@ -700,6 +700,15 @@ gfxDWriteFontEntry::CreateFontFace(IDWriteFontFace **aFontFace, const nsTArray* aVariations, DWRITE_FONT_SIMULATIONS aSimulations) { + // Convert an OpenType font tag from our uint32_t representation + // (as constructed by TRUETYPE_TAG(...)) to the order DWrite wants. + auto makeDWriteAxisTag = [](uint32_t aTag) { + return DWRITE_MAKE_FONT_AXIS_TAG((aTag >> 24) & 0xff, + (aTag >> 16) & 0xff, + (aTag >> 8) & 0xff, + aTag & 0xff); + }; + // initialize mFontFace if this hasn't been done before if (!mFontFace) { HRESULT hr; @@ -726,6 +735,26 @@ gfxDWriteFontEntry::CreateFontFace(IDWriteFontFace **aFontFace, if (mFontFace) { mFontFace->QueryInterface(__uuidof(IDWriteFontFace5), (void**)getter_AddRefs(mFontFace5)); + if (!mVariationSettings.IsEmpty()) { + // If the font entry has variations specified, mFontFace5 will + // be a distinct face that has the variations applied. + RefPtr resource; + HRESULT hr = + mFontFace5->GetFontResource(getter_AddRefs(resource)); + MOZ_ASSERT(SUCCEEDED(hr)); + AutoTArray fontAxisValues; + for (const auto& v : mVariationSettings) { + DWRITE_FONT_AXIS_VALUE axisValue = { + makeDWriteAxisTag(v.mTag), + v.mValue + }; + fontAxisValues.AppendElement(axisValue); + } + resource->CreateFontFace(mFontFace->GetSimulations(), + fontAxisValues.Elements(), + fontAxisValues.Length(), + getter_AddRefs(mFontFace5)); + } } } @@ -743,13 +772,24 @@ gfxDWriteFontEntry::CreateFontFace(IDWriteFontFace **aFontFace, MOZ_ASSERT(SUCCEEDED(hr)); AutoTArray fontAxisValues; if (aVariations) { - for (const auto& v : *aVariations) { + // Merge mVariationSettings and *aVariations if both present + const nsTArray* vars; + AutoTArray mergedSettings; + if (!aVariations) { + vars = &mVariationSettings; + } else { + if (mVariationSettings.IsEmpty()) { + vars = aVariations; + } else { + gfxFontUtils::MergeVariations(mVariationSettings, + *aVariations, + &mergedSettings); + vars = &mergedSettings; + } + } + for (const auto& v : *vars) { DWRITE_FONT_AXIS_VALUE axisValue = { - // let dwrite put the tag bytes in the order it wants - DWRITE_MAKE_FONT_AXIS_TAG((v.mTag >> 24) & 0xff, - (v.mTag >> 16) & 0xff, - (v.mTag >> 8) & 0xff, - v.mTag & 0xff), + makeDWriteAxisTag(v.mTag), v.mValue }; fontAxisValues.AppendElement(axisValue); @@ -793,8 +833,13 @@ gfxDWriteFontEntry::CreateFontFace(IDWriteFontFace **aFontFace, return FAILED(hr) ? NS_ERROR_FAILURE : NS_OK; } - // no simulation: we can just add a reference to mFontFace and return that - *aFontFace = mFontFace; + // no simulation: we can just add a reference to mFontFace5 (if present) + // or mFontFace (otherwise) and return that + if (mFontFace5) { + *aFontFace = mFontFace5; + } else { + *aFontFace = mFontFace; + } (*aFontFace)->AddRef(); return NS_OK; }