2018-11-30 22:52:05 +03: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/. */
|
2010-02-26 09:36:07 +03:00
|
|
|
|
|
|
|
#include "gfxDWriteFonts.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2010-02-26 09:36:07 +03:00
|
|
|
#include "gfxDWriteFontList.h"
|
|
|
|
#include "gfxContext.h"
|
2022-04-02 00:20:26 +03:00
|
|
|
#include "gfxHarfBuzzShaper.h"
|
2014-09-16 13:58:12 +04:00
|
|
|
#include "gfxTextRun.h"
|
2021-08-05 12:13:09 +03:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2021-08-05 12:13:10 +03:00
|
|
|
#include "mozilla/gfx/DWriteSettings.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2018-09-13 21:59:27 +03:00
|
|
|
#include "mozilla/gfx/gfxVars.h"
|
2021-08-05 12:13:09 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-02-26 09:36:07 +03:00
|
|
|
|
2012-04-22 01:24:39 +04:00
|
|
|
#include "harfbuzz/hb.h"
|
2018-04-13 22:34:37 +03:00
|
|
|
#include "mozilla/FontPropertyTypes.h"
|
2010-06-11 23:14:38 +04:00
|
|
|
|
2013-05-16 20:29:20 +04:00
|
|
|
using namespace mozilla;
|
2012-03-19 23:20:18 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2011-01-04 19:58:31 +03: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
|
|
|
|
|
2018-09-13 21:59:27 +03:00
|
|
|
// Cleartype can be dynamically enabled/disabled, so we have to allow for
|
|
|
|
// dynamically updating it.
|
|
|
|
static BYTE GetSystemTextQuality() {
|
|
|
|
BOOL font_smoothing;
|
|
|
|
UINT smoothing_type;
|
2011-01-04 19:58:31 +03:00
|
|
|
|
2018-09-13 21:59:27 +03:00
|
|
|
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;
|
2011-01-04 19:58:31 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 21:51:28 +03:00
|
|
|
#ifndef SPI_GETFONTSMOOTHINGCONTRAST
|
|
|
|
# define SPI_GETFONTSMOOTHINGCONTRAST 0x200c
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// "Retrieves a contrast value that is used in ClearType smoothing. Valid
|
|
|
|
// contrast values are from 1000 to 2200. The default value is 1400."
|
|
|
|
static FLOAT GetSystemGDIGamma() {
|
2021-08-05 12:13:09 +03:00
|
|
|
UINT value = 0;
|
|
|
|
if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &value, 0) ||
|
|
|
|
value < 1000 || value > 2200) {
|
|
|
|
value = 1400;
|
2019-07-24 21:51:28 +03:00
|
|
|
}
|
2021-08-05 12:13:09 +03:00
|
|
|
return value / 1000.0f;
|
2019-07-24 21:51:28 +03:00
|
|
|
}
|
|
|
|
|
2010-02-26 09:36:07 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// gfxDWriteFont
|
2017-04-07 00:41:02 +03:00
|
|
|
gfxDWriteFont::gfxDWriteFont(const RefPtr<UnscaledFontDWrite>& aUnscaledFont,
|
|
|
|
gfxFontEntry* aFontEntry,
|
2010-03-12 07:25:35 +03:00
|
|
|
const gfxFontStyle* aFontStyle,
|
2018-10-11 18:19:25 +03:00
|
|
|
RefPtr<IDWriteFontFace> aFontFace,
|
2010-06-10 22:19:51 +04:00
|
|
|
AntialiasOption anAAOption)
|
2017-04-07 00:41:02 +03:00
|
|
|
: gfxFont(aUnscaledFont, aFontEntry, aFontStyle, anAAOption),
|
2018-10-11 18:19:25 +03:00
|
|
|
mFontFace(aFontFace ? aFontFace : aUnscaledFont->GetFontFace()),
|
2011-10-17 18:59:28 +04:00
|
|
|
mUseSubpixelPositions(false),
|
|
|
|
mAllowManualShowGlyphs(true),
|
2022-01-13 19:18:47 +03:00
|
|
|
mAzureScaledFontUsedClearType(false) {
|
2017-05-20 00:25:28 +03:00
|
|
|
// If the IDWriteFontFace1 interface is available, we can use that for
|
|
|
|
// faster glyph width retrieval.
|
|
|
|
mFontFace->QueryInterface(__uuidof(IDWriteFontFace1),
|
|
|
|
(void**)getter_AddRefs(mFontFace1));
|
2022-02-09 15:07:05 +03:00
|
|
|
// If a fake-bold effect is needed, determine whether we're using DWrite's
|
|
|
|
// "simulation" or applying our multi-strike "synthetic bold".
|
|
|
|
if (aFontStyle->NeedsSyntheticBold(aFontEntry)) {
|
|
|
|
switch (StaticPrefs::gfx_font_rendering_directwrite_bold_simulation()) {
|
|
|
|
case 0: // never use the DWrite simulation
|
|
|
|
mApplySyntheticBold = true;
|
|
|
|
break;
|
|
|
|
case 1: // use DWrite simulation for installed fonts but not webfonts
|
|
|
|
mApplySyntheticBold = aFontEntry->mIsDataUserFont;
|
|
|
|
break;
|
|
|
|
default: // always use DWrite bold simulation
|
|
|
|
// the flag is initialized to false in gfxFont
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-06-03 08:31:08 +04:00
|
|
|
ComputeMetrics(anAAOption);
|
2010-02-26 09:36:07 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 21:58:00 +03:00
|
|
|
gfxDWriteFont::~gfxDWriteFont() {
|
|
|
|
if (auto* scaledFont = mAzureScaledFontGDI.exchange(nullptr)) {
|
|
|
|
scaledFont->Release();
|
|
|
|
}
|
|
|
|
}
|
2010-02-26 09:36:07 +03:00
|
|
|
|
2021-08-05 12:13:09 +03:00
|
|
|
/* static */
|
|
|
|
bool gfxDWriteFont::InitDWriteSupport() {
|
|
|
|
if (!Factory::EnsureDWriteFactory()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
UpdateSystemTextVars();
|
2022-01-10 21:15:59 +03:00
|
|
|
} else {
|
|
|
|
// UpdateClearTypeVars doesn't update the vars in non parent processes, but
|
|
|
|
// it does set sForceGDIClassicEnabled so we still need to call it.
|
|
|
|
UpdateClearTypeVars();
|
2021-08-05 12:13:09 +03:00
|
|
|
}
|
2021-08-05 12:13:10 +03:00
|
|
|
DWriteSettings::Initialize();
|
2021-08-05 12:13:09 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void gfxDWriteFont::UpdateSystemTextVars() {
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
|
2018-09-13 21:59:27 +03:00
|
|
|
BYTE newQuality = GetSystemTextQuality();
|
|
|
|
if (gfxVars::SystemTextQuality() != newQuality) {
|
|
|
|
gfxVars::SetSystemTextQuality(newQuality);
|
|
|
|
}
|
2021-08-05 12:13:09 +03:00
|
|
|
|
|
|
|
FLOAT newGDIGamma = GetSystemGDIGamma();
|
|
|
|
if (gfxVars::SystemGDIGamma() != newGDIGamma) {
|
|
|
|
gfxVars::SetSystemGDIGamma(newGDIGamma);
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateClearTypeVars();
|
2018-09-13 21:59:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void gfxDWriteFont::SystemTextQualityChanged() {
|
|
|
|
// If ClearType status has changed, update our value,
|
2020-05-15 16:55:11 +03:00
|
|
|
Factory::SetSystemTextQuality(gfxVars::SystemTextQuality());
|
2018-09-13 21:59:27 +03:00
|
|
|
// flush cached stuff that depended on the old setting, and force
|
|
|
|
// reflow everywhere to ensure we are using correct glyph metrics.
|
|
|
|
gfxPlatform::FlushFontAndWordCaches();
|
2021-08-27 02:17:54 +03:00
|
|
|
gfxPlatform::ForceGlobalReflow(gfxPlatform::NeedsReframe::No);
|
2016-12-22 14:06:15 +03:00
|
|
|
}
|
|
|
|
|
2021-08-26 14:55:17 +03:00
|
|
|
mozilla::Atomic<bool> gfxDWriteFont::sForceGDIClassicEnabled{true};
|
|
|
|
|
2021-08-05 12:13:09 +03:00
|
|
|
/* static */
|
|
|
|
void gfxDWriteFont::UpdateClearTypeVars() {
|
2021-08-26 14:55:17 +03:00
|
|
|
// We don't force GDI classic if the cleartype rendering mode pref is set to
|
|
|
|
// something valid.
|
|
|
|
int32_t renderingModePref =
|
|
|
|
Preferences::GetInt(GFX_CLEARTYPE_PARAMS_MODE, -1);
|
|
|
|
if (renderingModePref < 0 || renderingModePref > 5) {
|
|
|
|
renderingModePref = -1;
|
|
|
|
}
|
|
|
|
sForceGDIClassicEnabled = (renderingModePref == -1);
|
|
|
|
|
|
|
|
if (!XRE_IsParentProcess()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-05 12:13:09 +03:00
|
|
|
|
|
|
|
if (!Factory::GetDWriteFactory()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First set sensible hard coded defaults.
|
|
|
|
float clearTypeLevel = 1.0f;
|
|
|
|
float enhancedContrast = 1.0f;
|
|
|
|
float gamma = 2.2f;
|
|
|
|
int pixelGeometry = DWRITE_PIXEL_GEOMETRY_RGB;
|
|
|
|
int renderingMode = DWRITE_RENDERING_MODE_DEFAULT;
|
|
|
|
|
|
|
|
// Override these from DWrite function if available.
|
|
|
|
RefPtr<IDWriteRenderingParams> defaultRenderingParams;
|
|
|
|
HRESULT hr = Factory::GetDWriteFactory()->CreateRenderingParams(
|
|
|
|
getter_AddRefs(defaultRenderingParams));
|
|
|
|
if (SUCCEEDED(hr) && defaultRenderingParams) {
|
|
|
|
clearTypeLevel = defaultRenderingParams->GetClearTypeLevel();
|
|
|
|
|
|
|
|
// For enhanced contrast, we only use the default if the user has set it
|
|
|
|
// in the registry (by using the ClearType Tuner).
|
|
|
|
// XXXbobowen it seems slightly odd that we do this and only for enhanced
|
|
|
|
// contrast, but this reproduces previous functionality from
|
|
|
|
// gfxWindowsPlatform::SetupClearTypeParams.
|
|
|
|
HKEY hKey;
|
|
|
|
LONG res = RegOpenKeyExW(DISPLAY1_REGISTRY_KEY, 0, KEY_READ, &hKey);
|
|
|
|
if (res == ERROR_SUCCESS) {
|
|
|
|
res = RegQueryValueExW(hKey, ENHANCED_CONTRAST_VALUE_NAME, nullptr,
|
|
|
|
nullptr, nullptr, nullptr);
|
|
|
|
if (res == ERROR_SUCCESS) {
|
|
|
|
enhancedContrast = defaultRenderingParams->GetEnhancedContrast();
|
|
|
|
}
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
gamma = defaultRenderingParams->GetGamma();
|
|
|
|
pixelGeometry = defaultRenderingParams->GetPixelGeometry();
|
|
|
|
renderingMode = defaultRenderingParams->GetRenderingMode();
|
|
|
|
} else {
|
|
|
|
gfxWarning() << "Failed to create default rendering params";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally override from prefs if valid values are set. If ClearType is
|
|
|
|
// turned off we just use the default params, this reproduces the previous
|
|
|
|
// functionality that was spread across gfxDWriteFont::GetScaledFont and
|
|
|
|
// gfxWindowsPlatform::SetupClearTypeParams, but it seems odd because the
|
|
|
|
// default params will still be the ClearType ones although we won't use the
|
|
|
|
// anti-alias for ClearType because of GetSystemDefaultAAMode.
|
|
|
|
if (gfxVars::SystemTextQuality() == CLEARTYPE_QUALITY) {
|
|
|
|
int32_t prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_LEVEL, -1);
|
|
|
|
if (prefInt >= 0 && prefInt <= 100) {
|
|
|
|
clearTypeLevel = float(prefInt / 100.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_CONTRAST, -1);
|
|
|
|
if (prefInt >= 0 && prefInt <= 1000) {
|
|
|
|
enhancedContrast = float(prefInt / 100.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_GAMMA, -1);
|
|
|
|
if (prefInt >= 1000 && prefInt <= 2200) {
|
|
|
|
gamma = float(prefInt / 1000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
prefInt = Preferences::GetInt(GFX_CLEARTYPE_PARAMS_STRUCTURE, -1);
|
|
|
|
if (prefInt >= 0 && prefInt <= 2) {
|
|
|
|
pixelGeometry = prefInt;
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:55:17 +03:00
|
|
|
// renderingModePref is retrieved and validated above.
|
|
|
|
if (renderingModePref != -1) {
|
|
|
|
renderingMode = renderingModePref;
|
2021-08-05 12:13:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxVars::SystemTextClearTypeLevel() != clearTypeLevel) {
|
|
|
|
gfxVars::SetSystemTextClearTypeLevel(clearTypeLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxVars::SystemTextEnhancedContrast() != enhancedContrast) {
|
|
|
|
gfxVars::SetSystemTextEnhancedContrast(enhancedContrast);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxVars::SystemTextGamma() != gamma) {
|
|
|
|
gfxVars::SetSystemTextGamma(gamma);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxVars::SystemTextPixelGeometry() != pixelGeometry) {
|
|
|
|
gfxVars::SetSystemTextPixelGeometry(pixelGeometry);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfxVars::SystemTextRenderingMode() != renderingMode) {
|
|
|
|
gfxVars::SetSystemTextRenderingMode(renderingMode);
|
|
|
|
}
|
2021-08-26 14:55:17 +03:00
|
|
|
|
|
|
|
// Set cairo dwrite params in the parent process where it might still be
|
|
|
|
// needed for printing. We use the validated pref int directly for rendering
|
|
|
|
// mode, because a negative (i.e. not set) rendering mode is also used for
|
|
|
|
// deciding on forcing GDI in cairo.
|
|
|
|
cairo_dwrite_set_cleartype_params(gamma, enhancedContrast, clearTypeLevel,
|
|
|
|
pixelGeometry, renderingModePref);
|
2021-08-05 12:13:09 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 21:58:00 +03:00
|
|
|
gfxFont* gfxDWriteFont::CopyWithAntialiasOption(
|
|
|
|
AntialiasOption anAAOption) const {
|
2017-01-09 20:41:35 +03:00
|
|
|
auto entry = static_cast<gfxDWriteFontEntry*>(mFontEntry.get());
|
2017-04-07 00:41:02 +03:00
|
|
|
RefPtr<UnscaledFontDWrite> unscaledFont =
|
|
|
|
static_cast<UnscaledFontDWrite*>(mUnscaledFont.get());
|
2022-03-24 21:58:00 +03:00
|
|
|
return new gfxDWriteFont(unscaledFont, entry, &mStyle, mFontFace, anAAOption);
|
2010-02-26 09:36:07 +03:00
|
|
|
}
|
|
|
|
|
2011-03-24 06:01:14 +03:00
|
|
|
bool gfxDWriteFont::GetFakeMetricsForArialBlack(
|
|
|
|
DWRITE_FONT_METRICS* aFontMetrics) {
|
|
|
|
gfxFontStyle style(mStyle);
|
2022-06-13 03:59:23 +03:00
|
|
|
style.weight = FontWeight::FromInt(700);
|
2011-03-24 06:01:14 +03:00
|
|
|
|
Bug 1449605 - part 1 - Rearrange thebes font code so that the decision whether to apply synthetic-bold is deferred until actually instantiating a font, not made during the font-matching process. r=jwatt
This rearranges how synthetic-bold use is determined in the font selection
& rendering code. Previously, we would decide during the font-selection
algorithm whether we need to apply synthetic-bold to the chosen face, and
then pass that decision through the fontgroup (storing it in the FamilyFace
entries of the mFonts array there) down to the actual rendering code that
instantiates fonts from the faces (font entries) we've selected.
That became a problem for variation fonts because in the case of a user
font, we may not have downloaded the resource yet, so we just have a "user
font container" entry, which carries the descriptors from the @font-face
rule and will fetch the actual resource when needed. But in the case of a
@font-face rule without a weight descriptor, we don't actually know at
font-selection time whether the face will support "true" bold (via a
variation axis) or not, so we can't reliably make the right decision about
applying synthetic bold.
So we now defer that decision until we actually instantiate a platform font
object to shape/measure/draw text. At that point, we have the requested
style and we also have the real font resource, so we can easily determine
whether fake-bold is required.
(This patch should not result in any visible behavior change; that will
come in a second patch now that the architecture supports it.)
2018-05-01 12:30:50 +03:00
|
|
|
gfxFontEntry* fe = gfxPlatformFontList::PlatformFontList()->FindFontForFamily(
|
2021-09-08 15:18:17 +03:00
|
|
|
nullptr, "Arial"_ns, &style);
|
2011-03-24 06:01:14 +03:00
|
|
|
if (!fe || fe == mFontEntry) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-03-24 06:01:14 +03:00
|
|
|
|
Bug 1449605 - part 1 - Rearrange thebes font code so that the decision whether to apply synthetic-bold is deferred until actually instantiating a font, not made during the font-matching process. r=jwatt
This rearranges how synthetic-bold use is determined in the font selection
& rendering code. Previously, we would decide during the font-selection
algorithm whether we need to apply synthetic-bold to the chosen face, and
then pass that decision through the fontgroup (storing it in the FamilyFace
entries of the mFonts array there) down to the actual rendering code that
instantiates fonts from the faces (font entries) we've selected.
That became a problem for variation fonts because in the case of a user
font, we may not have downloaded the resource yet, so we just have a "user
font container" entry, which carries the descriptors from the @font-face
rule and will fetch the actual resource when needed. But in the case of a
@font-face rule without a weight descriptor, we don't actually know at
font-selection time whether the face will support "true" bold (via a
variation axis) or not, so we can't reliably make the right decision about
applying synthetic bold.
So we now defer that decision until we actually instantiate a platform font
object to shape/measure/draw text. At that point, we have the requested
style and we also have the real font resource, so we can easily determine
whether fake-bold is required.
(This patch should not result in any visible behavior change; that will
come in a second patch now that the architecture supports it.)
2018-05-01 12:30:50 +03:00
|
|
|
RefPtr<gfxFont> font = fe->FindOrMakeFont(&style);
|
2011-03-24 06:01:14 +03:00
|
|
|
gfxDWriteFont* dwFont = static_cast<gfxDWriteFont*>(font.get());
|
|
|
|
dwFont->mFontFace->GetMetrics(aFontMetrics);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-03-24 06:01:14 +03:00
|
|
|
}
|
|
|
|
|
2011-06-03 08:31:08 +04:00
|
|
|
void gfxDWriteFont::ComputeMetrics(AntialiasOption anAAOption) {
|
2022-06-16 15:38:35 +03:00
|
|
|
::memset(&mMetrics, 0, sizeof(mMetrics));
|
|
|
|
|
2010-02-26 09:36:07 +03:00
|
|
|
DWRITE_FONT_METRICS fontMetrics;
|
2022-06-13 03:59:23 +03:00
|
|
|
if (!(mFontEntry->Weight().Min() == FontWeight::FromInt(900) &&
|
|
|
|
mFontEntry->Weight().Max() == FontWeight::FromInt(900) &&
|
2011-03-24 06:01:14 +03:00
|
|
|
!mFontEntry->IsUserFont() &&
|
2012-12-19 13:42:25 +04:00
|
|
|
mFontEntry->Name().EqualsLiteral("Arial Black") &&
|
2011-03-24 06:01:14 +03:00
|
|
|
GetFakeMetricsForArialBlack(&fontMetrics))) {
|
|
|
|
mFontFace->GetMetrics(&fontMetrics);
|
|
|
|
}
|
2010-02-26 09:36:07 +03:00
|
|
|
|
2021-06-14 15:32:37 +03:00
|
|
|
if (GetAdjustedSize() > 0.0 && mStyle.sizeAdjust >= 0.0 &&
|
|
|
|
FontSizeAdjust::Tag(mStyle.sizeAdjustBasis) !=
|
|
|
|
FontSizeAdjust::Tag::None) {
|
|
|
|
// For accurate measurement during the font-size-adjust computations;
|
|
|
|
// these may be reset later according to the adjusted size.
|
|
|
|
mUseSubpixelPositions = true;
|
|
|
|
mFUnitsConvFactor = float(mAdjustedSize / fontMetrics.designUnitsPerEm);
|
|
|
|
gfxFloat aspect;
|
|
|
|
switch (FontSizeAdjust::Tag(mStyle.sizeAdjustBasis)) {
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unhandled sizeAdjustBasis?");
|
|
|
|
aspect = 0.0;
|
|
|
|
break;
|
2021-07-07 14:58:40 +03:00
|
|
|
case FontSizeAdjust::Tag::ExHeight:
|
2021-06-14 15:32:37 +03:00
|
|
|
aspect = (gfxFloat)fontMetrics.xHeight / fontMetrics.designUnitsPerEm;
|
|
|
|
break;
|
2021-07-07 14:58:40 +03:00
|
|
|
case FontSizeAdjust::Tag::CapHeight:
|
2021-06-14 15:32:37 +03:00
|
|
|
aspect = (gfxFloat)fontMetrics.capHeight / fontMetrics.designUnitsPerEm;
|
|
|
|
break;
|
2021-07-07 14:58:40 +03:00
|
|
|
case FontSizeAdjust::Tag::ChWidth: {
|
|
|
|
gfxFloat advance = GetCharAdvance('0');
|
|
|
|
aspect = advance > 0.0 ? advance / mAdjustedSize : 0.5;
|
2021-06-14 15:32:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-07-07 14:58:40 +03:00
|
|
|
case FontSizeAdjust::Tag::IcWidth:
|
|
|
|
case FontSizeAdjust::Tag::IcHeight: {
|
|
|
|
bool vertical = FontSizeAdjust::Tag(mStyle.sizeAdjustBasis) ==
|
|
|
|
FontSizeAdjust::Tag::IcHeight;
|
2021-12-08 20:07:05 +03:00
|
|
|
gfxFloat advance = GetCharAdvance(kWaterIdeograph, vertical);
|
2021-07-07 14:58:40 +03:00
|
|
|
aspect = advance > 0.0 ? advance / mAdjustedSize : 1.0;
|
2021-06-14 15:32:37 +03:00
|
|
|
break;
|
2021-07-07 14:58:40 +03:00
|
|
|
}
|
2021-06-14 15:32:37 +03:00
|
|
|
}
|
|
|
|
if (aspect > 0.0) {
|
2021-07-07 14:58:40 +03:00
|
|
|
// If we created a shaper above (to measure glyphs), discard it so we
|
|
|
|
// get a new one for the adjusted scaling.
|
2022-04-02 00:20:26 +03:00
|
|
|
delete mHarfBuzzShaper.exchange(nullptr);
|
2021-06-14 15:32:37 +03:00
|
|
|
mAdjustedSize = mStyle.GetAdjustedSize(aspect);
|
|
|
|
}
|
2010-03-12 14:57:49 +03:00
|
|
|
}
|
|
|
|
|
2021-06-14 15:32:37 +03:00
|
|
|
// Update now that we've adjusted the size if necessary.
|
|
|
|
mFUnitsConvFactor = float(mAdjustedSize / fontMetrics.designUnitsPerEm);
|
|
|
|
|
2011-06-03 08:31:08 +04:00
|
|
|
// Note that GetMeasuringMode depends on mAdjustedSize
|
|
|
|
if ((anAAOption == gfxFont::kAntialiasDefault && UsingClearType() &&
|
|
|
|
GetMeasuringMode() == DWRITE_MEASURING_MODE_NATURAL) ||
|
|
|
|
anAAOption == gfxFont::kAntialiasSubpixel) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mUseSubpixelPositions = true;
|
2011-06-03 08:31:08 +04:00
|
|
|
// note that this may be reset to FALSE if we determine that a bitmap
|
|
|
|
// strike is going to be used
|
2021-06-14 15:32:37 +03:00
|
|
|
} else {
|
|
|
|
mUseSubpixelPositions = false;
|
2011-06-03 08:31:08 +04:00
|
|
|
}
|
|
|
|
|
2011-01-25 12:17:18 +03:00
|
|
|
gfxDWriteFontEntry* fe = static_cast<gfxDWriteFontEntry*>(mFontEntry.get());
|
|
|
|
if (fe->IsCJKFont() && HasBitmapStrikeForSize(NS_lround(mAdjustedSize))) {
|
2011-01-21 13:36:28 +03:00
|
|
|
mAdjustedSize = NS_lround(mAdjustedSize);
|
2011-10-17 18:59:28 +04:00
|
|
|
mUseSubpixelPositions = false;
|
2011-01-21 22:26:30 +03: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 23:55:01 +03: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 18:59:28 +04:00
|
|
|
mAllowManualShowGlyphs = false;
|
2011-01-21 13:36:28 +03:00
|
|
|
}
|
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.xHeight = fontMetrics.xHeight * mFUnitsConvFactor;
|
|
|
|
mMetrics.capHeight = fontMetrics.capHeight * mFUnitsConvFactor;
|
2010-08-05 13:18:42 +04:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.maxAscent = round(fontMetrics.ascent * mFUnitsConvFactor);
|
|
|
|
mMetrics.maxDescent = round(fontMetrics.descent * mFUnitsConvFactor);
|
|
|
|
mMetrics.maxHeight = mMetrics.maxAscent + mMetrics.maxDescent;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.emHeight = mAdjustedSize;
|
|
|
|
mMetrics.emAscent =
|
|
|
|
mMetrics.emHeight * mMetrics.maxAscent / mMetrics.maxHeight;
|
|
|
|
mMetrics.emDescent = mMetrics.emHeight - mMetrics.emAscent;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.maxAdvance = mAdjustedSize;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-05 13:18:42 +04:00
|
|
|
// try to get the true maxAdvance value from 'hhea'
|
2013-05-16 20:29:20 +04:00
|
|
|
gfxFontEntry::AutoTable hheaTable(GetFontEntry(),
|
|
|
|
TRUETYPE_TAG('h', 'h', 'e', 'a'));
|
|
|
|
if (hheaTable) {
|
|
|
|
uint32_t len;
|
|
|
|
const MetricsHeader* hhea = reinterpret_cast<const MetricsHeader*>(
|
|
|
|
hb_blob_get_data(hheaTable, &len));
|
|
|
|
if (len >= sizeof(MetricsHeader)) {
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.maxAdvance = uint16_t(hhea->advanceWidthMax) * mFUnitsConvFactor;
|
2010-08-05 13:18:42 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-08-05 13:18:42 +04:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.internalLeading =
|
|
|
|
std::max(mMetrics.maxHeight - mMetrics.emHeight, 0.0);
|
|
|
|
mMetrics.externalLeading = ceil(fontMetrics.lineGap * mFUnitsConvFactor);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-11-18 13:19:54 +03:00
|
|
|
UINT32 ucs = L' ';
|
|
|
|
UINT16 glyph;
|
2019-03-21 07:50:41 +03:00
|
|
|
if (SUCCEEDED(mFontFace->GetGlyphIndices(&ucs, 1, &glyph)) && glyph != 0) {
|
2014-11-18 13:19:54 +03:00
|
|
|
mSpaceGlyph = glyph;
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.spaceWidth = MeasureGlyphWidth(glyph);
|
2019-03-21 07:50:41 +03:00
|
|
|
} else {
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.spaceWidth = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-04-21 10:29:50 +04:00
|
|
|
// try to get aveCharWidth from the OS/2 table, fall back to measuring 'x'
|
|
|
|
// if the table is not available or if using hinted/pixel-snapped widths
|
|
|
|
if (mUseSubpixelPositions) {
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.aveCharWidth = 0;
|
2013-05-16 20:29:20 +04:00
|
|
|
gfxFontEntry::AutoTable os2Table(GetFontEntry(),
|
2011-01-21 13:36:28 +03:00
|
|
|
TRUETYPE_TAG('O', 'S', '/', '2'));
|
2013-05-16 20:29:20 +04:00
|
|
|
if (os2Table) {
|
|
|
|
uint32_t len;
|
|
|
|
const OS2Table* os2 =
|
2011-01-21 13:36:28 +03:00
|
|
|
reinterpret_cast<const OS2Table*>(hb_blob_get_data(os2Table, &len));
|
2014-10-01 23:25:48 +04:00
|
|
|
if (len >= 4) {
|
2011-04-21 10:29:50 +04: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.
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.aveCharWidth = int16_t(os2->xAvgCharWidth) * mFUnitsConvFactor;
|
2010-08-05 13:18:42 +04:00
|
|
|
}
|
2010-02-26 09:36:07 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-08-05 13:18:42 +04:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
if (mMetrics.aveCharWidth < 1) {
|
|
|
|
mMetrics.aveCharWidth = GetCharAdvance('x');
|
|
|
|
if (mMetrics.aveCharWidth < 1) {
|
2011-01-21 19:44:32 +03:00
|
|
|
// Let's just assume the X is square.
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.aveCharWidth = fontMetrics.xHeight * mFUnitsConvFactor;
|
2010-02-26 09:36:07 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.zeroWidth = GetCharAdvance('0');
|
2010-08-05 13:18:42 +04:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.ideographicWidth = GetCharAdvance(kWaterIdeograph);
|
2021-12-08 20:07:05 +03:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.underlineOffset = fontMetrics.underlinePosition * mFUnitsConvFactor;
|
|
|
|
mMetrics.underlineSize = fontMetrics.underlineThickness * mFUnitsConvFactor;
|
|
|
|
mMetrics.strikeoutOffset =
|
2011-04-21 10:29:50 +04:00
|
|
|
fontMetrics.strikethroughPosition * mFUnitsConvFactor;
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.strikeoutSize =
|
2011-04-21 10:29:50 +04:00
|
|
|
fontMetrics.strikethroughThickness * mFUnitsConvFactor;
|
2010-02-26 09:36:07 +03:00
|
|
|
|
2022-06-16 15:38:35 +03:00
|
|
|
SanitizeMetrics(&mMetrics, GetFontEntry()->mIsBadUnderlineFont);
|
2010-03-31 16:46:18 +04:00
|
|
|
|
2022-02-09 15:07:05 +03:00
|
|
|
if (ApplySyntheticBold()) {
|
|
|
|
auto delta = GetSyntheticBoldOffset();
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.spaceWidth += delta;
|
|
|
|
mMetrics.aveCharWidth += delta;
|
|
|
|
mMetrics.maxAdvance += delta;
|
|
|
|
if (mMetrics.zeroWidth > 0) {
|
|
|
|
mMetrics.zeroWidth += delta;
|
2022-02-09 15:07:05 +03:00
|
|
|
}
|
2022-06-16 15:38:35 +03:00
|
|
|
if (mMetrics.ideographicWidth > 0) {
|
|
|
|
mMetrics.ideographicWidth += delta;
|
2022-02-09 15:07:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 16:46:18 +04:00
|
|
|
#if 0
|
|
|
|
printf("Font: %p (%s) size: %f\n", this,
|
|
|
|
NS_ConvertUTF16toUTF8(GetName()).get(), mStyle.size);
|
2022-06-16 15:38:35 +03: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);
|
2019-03-21 07:50:41 +03:00
|
|
|
printf(" spaceWidth: %f aveCharWidth: %f zeroWidth: %f\n",
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.zeroWidth);
|
|
|
|
printf(" xHeight: %f capHeight: %f\n", mMetrics.xHeight, mMetrics.capHeight);
|
2014-06-28 10:40:36 +04:00
|
|
|
printf(" uOff: %f uSize: %f stOff: %f stSize: %f\n",
|
2022-06-16 15:38:35 +03:00
|
|
|
mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize);
|
2010-03-31 16:46:18 +04:00
|
|
|
#endif
|
|
|
|
}
|
2010-02-26 09:36:07 +03:00
|
|
|
|
2011-01-21 13:36:28 +03:00
|
|
|
using namespace mozilla; // for AutoSwap_* types
|
|
|
|
|
|
|
|
struct EBLCHeader {
|
|
|
|
AutoSwap_PRUint32 version;
|
|
|
|
AutoSwap_PRUint32 numSizes;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SbitLineMetrics {
|
2012-08-22 19:56:38 +04: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 13:36:28 +03: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 19:56:38 +04:00
|
|
|
uint8_t ppemX;
|
|
|
|
uint8_t ppemY;
|
|
|
|
uint8_t bitDepth;
|
|
|
|
uint8_t flags;
|
2011-01-21 13:36:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef EBLCHeader EBSCHeader;
|
|
|
|
|
|
|
|
struct BitmapScaleTable {
|
|
|
|
SbitLineMetrics hori;
|
|
|
|
SbitLineMetrics vert;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t ppemX;
|
|
|
|
uint8_t ppemY;
|
|
|
|
uint8_t substitutePpemX;
|
|
|
|
uint8_t substitutePpemY;
|
2011-01-21 13:36:28 +03:00
|
|
|
};
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool gfxDWriteFont::HasBitmapStrikeForSize(uint32_t aSize) {
|
|
|
|
uint8_t* tableData;
|
|
|
|
uint32_t len;
|
2011-01-21 13:36:28 +03: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 18:59:28 +04:00
|
|
|
return false;
|
2011-01-21 13:36:28 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasStrike = false;
|
2011-01-21 13:36:28 +03: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 19:56:38 +04:00
|
|
|
uint32_t numSizes = hdr->numSizes;
|
2011-01-21 13:36:28 +03: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 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < numSizes; ++i, ++sizeTable) {
|
2011-01-21 13:36:28 +03: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 19:56:38 +04:00
|
|
|
hasStrike = (uint16_t(sizeTable->endGlyphIndex) >=
|
|
|
|
uint16_t(sizeTable->startGlyphIndex) + 3);
|
2011-01-21 13:36:28 +03:00
|
|
|
break;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-01-21 13:36:28 +03:00
|
|
|
}
|
|
|
|
// if we reach here, we didn't find a strike; unconditionally break
|
|
|
|
// out of the while-loop block
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-01-21 13:36:28 +03:00
|
|
|
mFontFace->ReleaseFontTable(tableContext);
|
|
|
|
|
|
|
|
if (hasStrike) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2011-01-21 13:36:28 +03: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 18:59:28 +04:00
|
|
|
return false;
|
2011-01-21 13:36:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (exists) {
|
|
|
|
if (len < sizeof(EBSCHeader)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const EBSCHeader* hdr = reinterpret_cast<const EBSCHeader*>(tableData);
|
|
|
|
if (hdr->version != 0x00020000) {
|
|
|
|
break;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numSizes = hdr->numSizes;
|
2011-01-21 13:36:28 +03: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 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < numSizes; ++i, ++scaleTable) {
|
2011-01-21 13:36:28 +03:00
|
|
|
if (scaleTable->ppemX == aSize && scaleTable->ppemY == aSize) {
|
2011-10-17 18:59:28 +04:00
|
|
|
hasStrike = true;
|
2011-01-21 13:36:28 +03:00
|
|
|
break;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-01-21 13:36:28 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-01-21 13:36:28 +03:00
|
|
|
mFontFace->ReleaseFontTable(tableContext);
|
|
|
|
|
|
|
|
return hasStrike;
|
|
|
|
}
|
|
|
|
|
2014-11-17 12:59:50 +03:00
|
|
|
bool gfxDWriteFont::IsValid() const { return mFontFace != nullptr; }
|
2011-01-21 19:44:32 +03:00
|
|
|
|
|
|
|
IDWriteFontFace* gfxDWriteFont::GetFontFace() { return mFontFace.get(); }
|
|
|
|
|
2016-06-27 19:41:55 +03:00
|
|
|
gfxFont::RunMetrics gfxDWriteFont::Measure(const gfxTextRun* aTextRun,
|
2015-12-16 00:56:41 +03:00
|
|
|
uint32_t aStart, uint32_t aEnd,
|
|
|
|
BoundingBoxType aBoundingBoxType,
|
|
|
|
DrawTarget* aRefDrawTarget,
|
|
|
|
Spacing* aSpacing,
|
2017-05-05 00:27:05 +03:00
|
|
|
gfx::ShapedTextFlags aOrientation) {
|
2011-07-01 10:38:17 +04:00
|
|
|
gfxFont::RunMetrics metrics =
|
2015-12-16 00:56:41 +03:00
|
|
|
gfxFont::Measure(aTextRun, aStart, aEnd, aBoundingBoxType, aRefDrawTarget,
|
|
|
|
aSpacing, aOrientation);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-07-01 10:38:17 +04:00
|
|
|
// 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 &&
|
2017-12-19 23:48:39 +03:00
|
|
|
metrics.mBoundingBox.Width() > 0) {
|
|
|
|
metrics.mBoundingBox.MoveByX(-aTextRun->GetAppUnitsPerDevUnit());
|
|
|
|
metrics.mBoundingBox.SetWidth(metrics.mBoundingBox.Width() +
|
|
|
|
aTextRun->GetAppUnitsPerDevUnit() * 3);
|
2011-07-01 10:38:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return metrics;
|
|
|
|
}
|
|
|
|
|
2014-06-09 18:47:31 +04:00
|
|
|
bool gfxDWriteFont::ProvidesGlyphWidths() const {
|
2011-02-15 23:55:01 +03:00
|
|
|
return !mUseSubpixelPositions ||
|
2018-01-18 20:45:24 +03:00
|
|
|
(mFontFace->GetSimulations() & DWRITE_FONT_SIMULATIONS_BOLD) ||
|
2021-06-23 18:54:55 +03:00
|
|
|
((gfxDWriteFontEntry*)(GetFontEntry()))->HasVariations();
|
2011-02-15 23:55:01 +03:00
|
|
|
}
|
2011-01-21 19:44:33 +03:00
|
|
|
|
2018-12-31 14:43:27 +03:00
|
|
|
int32_t gfxDWriteFont::GetGlyphWidth(uint16_t aGID) {
|
2013-09-02 12:41:57 +04:00
|
|
|
if (!mGlyphWidths) {
|
2021-03-10 13:47:47 +03:00
|
|
|
mGlyphWidths = MakeUnique<nsTHashMap<nsUint32HashKey, int32_t>>(128);
|
2011-01-04 19:58:31 +03:00
|
|
|
}
|
|
|
|
|
2021-03-01 12:59:29 +03:00
|
|
|
return mGlyphWidths->LookupOrInsertWith(
|
|
|
|
aGID, [&] { return NS_lround(MeasureGlyphWidth(aGID) * 65536.0); });
|
2011-04-21 10:29:50 +04:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:15:10 +03:00
|
|
|
bool gfxDWriteFont::GetForceGDIClassic() const {
|
2021-08-26 14:55:17 +03:00
|
|
|
return sForceGDIClassicEnabled &&
|
|
|
|
static_cast<gfxDWriteFontEntry*>(mFontEntry.get())
|
2011-06-03 08:31:08 +04:00
|
|
|
->GetForceGDIClassic() &&
|
|
|
|
GetAdjustedSize() <= gfxDWriteFontList::PlatformFontList()
|
|
|
|
->GetForceGDIClassicMaxFontSize();
|
|
|
|
}
|
|
|
|
|
2011-06-03 08:31:07 +04:00
|
|
|
DWRITE_MEASURING_MODE
|
2019-09-16 20:15:10 +03:00
|
|
|
gfxDWriteFont::GetMeasuringMode() const {
|
2021-08-05 12:13:11 +03:00
|
|
|
return DWriteSettings::Get(GetForceGDIClassic()).MeasuringMode();
|
2011-06-03 08:31:07 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxFloat gfxDWriteFont::MeasureGlyphWidth(uint16_t aGlyph) {
|
2020-03-16 16:57:32 +03:00
|
|
|
MOZ_SEH_TRY {
|
|
|
|
HRESULT hr;
|
|
|
|
if (mFontFace1) {
|
|
|
|
int32_t advance;
|
|
|
|
if (mUseSubpixelPositions) {
|
|
|
|
hr = mFontFace1->GetDesignGlyphAdvances(1, &aGlyph, &advance, FALSE);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
return advance * mFUnitsConvFactor;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hr = mFontFace1->GetGdiCompatibleGlyphAdvances(
|
|
|
|
FLOAT(mAdjustedSize), 1.0f, nullptr,
|
|
|
|
GetMeasuringMode() == DWRITE_MEASURING_MODE_GDI_NATURAL, FALSE, 1,
|
|
|
|
&aGlyph, &advance);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
return NS_lround(advance * mFUnitsConvFactor);
|
|
|
|
}
|
2017-05-20 00:25:28 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-03-16 16:57:32 +03:00
|
|
|
DWRITE_GLYPH_METRICS metrics;
|
|
|
|
if (mUseSubpixelPositions) {
|
|
|
|
hr = mFontFace->GetDesignGlyphMetrics(&aGlyph, 1, &metrics, FALSE);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
return metrics.advanceWidth * mFUnitsConvFactor;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hr = mFontFace->GetGdiCompatibleGlyphMetrics(
|
|
|
|
FLOAT(mAdjustedSize), 1.0f, nullptr,
|
|
|
|
GetMeasuringMode() == DWRITE_MEASURING_MODE_GDI_NATURAL, &aGlyph, 1,
|
|
|
|
&metrics, FALSE);
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
return NS_lround(metrics.advanceWidth * mFUnitsConvFactor);
|
|
|
|
}
|
2017-05-20 00:25:28 +03:00
|
|
|
}
|
2011-01-04 19:58:31 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2020-03-16 16:57:32 +03:00
|
|
|
MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
|
|
|
|
// Exception (e.g. disk i/o error) occurred when DirectWrite tried to use
|
|
|
|
// the font resource; possibly a failing drive or similar hardware issue.
|
|
|
|
// Mark the font as invalid, and wipe the fontEntry's charmap so that font
|
|
|
|
// selection will skip it; we'll use a fallback font instead.
|
|
|
|
mIsValid = false;
|
|
|
|
GetFontEntry()->mCharacterMap = new gfxCharacterMap();
|
|
|
|
GetFontEntry()->mShmemCharacterMap = nullptr;
|
|
|
|
gfxCriticalError() << "Exception occurred measuring glyph width for "
|
|
|
|
<< GetFontEntry()->Name().get();
|
|
|
|
}
|
2019-03-21 07:50:41 +03:00
|
|
|
return 0.0;
|
2011-01-04 19:58:31 +03:00
|
|
|
}
|
2012-03-23 16:14:16 +04:00
|
|
|
|
2019-09-20 19:30:21 +03:00
|
|
|
bool gfxDWriteFont::GetGlyphBounds(uint16_t aGID, gfxRect* aBounds,
|
2022-12-20 12:32:02 +03:00
|
|
|
bool aTight) {
|
|
|
|
MOZ_SEH_TRY {
|
|
|
|
DWRITE_GLYPH_METRICS m;
|
|
|
|
HRESULT hr = mFontFace->GetDesignGlyphMetrics(&aGID, 1, &m, FALSE);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gfxRect bounds(m.leftSideBearing, m.topSideBearing - m.verticalOriginY,
|
|
|
|
m.advanceWidth - m.leftSideBearing - m.rightSideBearing,
|
|
|
|
m.advanceHeight - m.topSideBearing - m.bottomSideBearing);
|
|
|
|
bounds.Scale(mFUnitsConvFactor);
|
|
|
|
// GetDesignGlyphMetrics returns 'ideal' glyph metrics, we need to pad to
|
|
|
|
// account for antialiasing.
|
|
|
|
if (!aTight && !aBounds->IsEmpty()) {
|
|
|
|
bounds.Inflate(1.0, 0.0);
|
|
|
|
}
|
|
|
|
*aBounds = bounds;
|
|
|
|
return true;
|
2019-09-20 19:30:21 +03:00
|
|
|
}
|
2022-12-20 12:32:02 +03:00
|
|
|
MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
|
|
|
|
// Exception (e.g. disk i/o error) occurred when DirectWrite tried to use
|
|
|
|
// the font resource; possibly a failing drive or similar hardware issue.
|
|
|
|
// Mark the font as invalid, and wipe the fontEntry's charmap so that font
|
|
|
|
// selection will skip it; we'll use a fallback font instead.
|
|
|
|
mIsValid = false;
|
|
|
|
GetFontEntry()->mCharacterMap = new gfxCharacterMap();
|
|
|
|
GetFontEntry()->mShmemCharacterMap = nullptr;
|
|
|
|
gfxCriticalError() << "Exception occurred measuring glyph bounds for "
|
|
|
|
<< GetFontEntry()->Name().get();
|
2019-09-20 19:30:21 +03:00
|
|
|
}
|
2022-12-20 12:32:02 +03:00
|
|
|
return false;
|
2019-09-20 19:30:21 +03:00
|
|
|
}
|
|
|
|
|
2013-10-15 06:19:47 +04:00
|
|
|
void gfxDWriteFont::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
FontCacheSizes* aSizes) const {
|
|
|
|
gfxFont::AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2013-09-02 12:41:57 +04:00
|
|
|
if (mGlyphWidths) {
|
|
|
|
aSizes->mFontInstances +=
|
2015-07-31 07:19:57 +03:00
|
|
|
mGlyphWidths->ShallowSizeOfIncludingThis(aMallocSizeOf);
|
2013-09-02 12:41:57 +04:00
|
|
|
}
|
2012-03-23 16:14:16 +04:00
|
|
|
}
|
|
|
|
|
2013-10-15 06:19:47 +04:00
|
|
|
void gfxDWriteFont::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
FontCacheSizes* aSizes) const {
|
2012-03-23 16:14:16 +04:00
|
|
|
aSizes->mFontInstances += aMallocSizeOf(this);
|
2013-10-15 06:19:47 +04:00
|
|
|
AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2012-03-23 16:14:16 +04:00
|
|
|
}
|
2012-09-24 19:02:49 +04:00
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<ScaledFont> gfxDWriteFont::GetScaledFont(
|
2021-12-10 01:16:17 +03:00
|
|
|
const TextRunDrawParams& aRunParams) {
|
2022-03-24 21:58:00 +03:00
|
|
|
bool useClearType = UsingClearType();
|
|
|
|
if (mAzureScaledFontUsedClearType != useClearType) {
|
|
|
|
if (auto* oldScaledFont = mAzureScaledFont.exchange(nullptr)) {
|
|
|
|
oldScaledFont->Release();
|
|
|
|
}
|
|
|
|
if (auto* oldScaledFont = mAzureScaledFontGDI.exchange(nullptr)) {
|
|
|
|
oldScaledFont->Release();
|
|
|
|
}
|
2018-03-20 16:46:40 +03:00
|
|
|
}
|
2022-01-13 19:18:47 +03:00
|
|
|
bool forceGDI = aRunParams.allowGDI && GetForceGDIClassic();
|
2022-03-24 21:58:00 +03:00
|
|
|
ScaledFont* scaledFont = forceGDI ? mAzureScaledFontGDI : mAzureScaledFont;
|
|
|
|
if (scaledFont) {
|
|
|
|
return do_AddRef(scaledFont);
|
2022-03-24 02:17:34 +03:00
|
|
|
}
|
2022-03-24 04:28:12 +03:00
|
|
|
|
2022-03-24 21:58:00 +03:00
|
|
|
gfxDWriteFontEntry* fe = static_cast<gfxDWriteFontEntry*>(mFontEntry.get());
|
|
|
|
bool useEmbeddedBitmap =
|
|
|
|
(gfxVars::SystemTextRenderingMode() == DWRITE_RENDERING_MODE_DEFAULT ||
|
|
|
|
forceGDI) &&
|
|
|
|
fe->IsCJKFont() && HasBitmapStrikeForSize(NS_lround(mAdjustedSize));
|
|
|
|
|
|
|
|
const gfxFontStyle* fontStyle = GetStyle();
|
|
|
|
RefPtr<ScaledFont> newScaledFont = Factory::CreateScaledFontForDWriteFont(
|
|
|
|
mFontFace, fontStyle, GetUnscaledFont(), GetAdjustedSize(),
|
|
|
|
useEmbeddedBitmap, ApplySyntheticBold(), forceGDI);
|
|
|
|
if (!newScaledFont) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
InitializeScaledFont(newScaledFont);
|
|
|
|
|
|
|
|
if (forceGDI) {
|
|
|
|
if (mAzureScaledFontGDI.compareExchange(nullptr, newScaledFont.get())) {
|
|
|
|
Unused << newScaledFont.forget();
|
|
|
|
mAzureScaledFontUsedClearType = useClearType;
|
|
|
|
}
|
|
|
|
scaledFont = mAzureScaledFontGDI;
|
|
|
|
} else {
|
|
|
|
if (mAzureScaledFont.compareExchange(nullptr, newScaledFont.get())) {
|
|
|
|
Unused << newScaledFont.forget();
|
|
|
|
mAzureScaledFontUsedClearType = useClearType;
|
|
|
|
}
|
|
|
|
scaledFont = mAzureScaledFont;
|
|
|
|
}
|
|
|
|
return do_AddRef(scaledFont);
|
2012-09-24 19:02:49 +04:00
|
|
|
}
|
2019-09-16 20:15:10 +03:00
|
|
|
|
|
|
|
bool gfxDWriteFont::ShouldRoundXOffset(cairo_t* aCairo) const {
|
|
|
|
// show_glyphs is implemented on the font and so is used for all Cairo
|
|
|
|
// surface types; however, it may pixel-snap depending on the dwrite
|
|
|
|
// rendering mode
|
2021-08-05 12:13:11 +03:00
|
|
|
return GetMeasuringMode() != DWRITE_MEASURING_MODE_NATURAL;
|
2019-09-16 20:15:10 +03:00
|
|
|
}
|