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-03-10 15:46:41 +03:00
|
|
|
|
|
|
|
#include "gfxMacFont.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
2016-08-17 01:41:12 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_gfx.h"
|
2013-06-30 20:26:39 +04:00
|
|
|
|
2010-03-10 15:46:41 +03:00
|
|
|
#include "gfxCoreTextShaper.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2010-03-10 15:46:41 +03:00
|
|
|
#include "gfxPlatformMac.h"
|
|
|
|
#include "gfxContext.h"
|
2010-08-05 13:18:44 +04:00
|
|
|
#include "gfxFontUtils.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "gfxMacPlatformFontList.h"
|
|
|
|
#include "gfxFontConstants.h"
|
2014-09-16 13:58:12 +04:00
|
|
|
#include "gfxTextRun.h"
|
2020-07-15 15:47:28 +03:00
|
|
|
#include "gfxUtils.h"
|
2017-02-10 00:37:24 +03:00
|
|
|
#include "nsCocoaFeatures.h"
|
2010-03-10 15:46:41 +03:00
|
|
|
#include "cairo-quartz.h"
|
|
|
|
|
2010-08-05 13:18:44 +04:00
|
|
|
using namespace mozilla;
|
2011-11-18 08:00:38 +04:00
|
|
|
using namespace mozilla::gfx;
|
2010-08-05 13:18:44 +04:00
|
|
|
|
2018-05-15 16:59:26 +03:00
|
|
|
template <class T>
|
|
|
|
struct TagEquals {
|
|
|
|
bool Equals(const T& aIter, uint32_t aTag) const {
|
|
|
|
return aIter.mTag == aTag;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-07 00:41:02 +03:00
|
|
|
gfxMacFont::gfxMacFont(const RefPtr<UnscaledFontMac>& aUnscaledFont,
|
|
|
|
MacOSFontEntry* aFontEntry,
|
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
|
|
|
const gfxFontStyle* aFontStyle)
|
2017-04-07 00:41:02 +03:00
|
|
|
: gfxFont(aUnscaledFont, aFontEntry, aFontStyle),
|
2012-07-30 18:20:58 +04:00
|
|
|
mCGFont(nullptr),
|
2014-11-17 12:59:49 +03:00
|
|
|
mCTFont(nullptr),
|
2017-11-03 04:02:30 +03:00
|
|
|
mFontSmoothingBackgroundColor(aFontStyle->fontSmoothingBackgroundColor),
|
2017-04-03 17:45:52 +03:00
|
|
|
mVariationFont(aFontEntry->HasVariations()) {
|
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
|
|
|
mApplySyntheticBold = aFontStyle->NeedsSyntheticBold(aFontEntry);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-25 09:18:23 +03:00
|
|
|
if (mVariationFont) {
|
2017-04-07 00:41:02 +03:00
|
|
|
CGFontRef baseFont = aUnscaledFont->GetFont();
|
2017-01-06 19:35:11 +03:00
|
|
|
if (!baseFont) {
|
|
|
|
mIsValid = false;
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-25 09:18:23 +03:00
|
|
|
// Get the variation settings needed to instantiate the fontEntry
|
|
|
|
// for a particular fontStyle.
|
|
|
|
AutoTArray<gfxFontVariation, 4> vars;
|
|
|
|
aFontEntry->GetVariationsForStyle(vars, *aFontStyle);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-15 16:59:26 +03:00
|
|
|
// Because of a Core Text bug, we need to ensure that if the font has
|
|
|
|
// an 'opsz' axis, it is always explicitly set, and NOT to the font's
|
2018-07-30 19:53:09 +03:00
|
|
|
// default value. (See bug 1457417, bug 1478720.)
|
2018-05-15 16:59:26 +03:00
|
|
|
// We record the result of searching the font's axes in the font entry,
|
|
|
|
// so that this only has to be done by the first instance created for
|
|
|
|
// a given font resource.
|
|
|
|
const uint32_t kOpszTag = HB_TAG('o', 'p', 's', 'z');
|
2018-07-30 19:53:09 +03:00
|
|
|
const float kOpszFudgeAmount = 0.01f;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-15 16:59:26 +03:00
|
|
|
if (!aFontEntry->mCheckedForOpszAxis) {
|
|
|
|
aFontEntry->mCheckedForOpszAxis = true;
|
|
|
|
AutoTArray<gfxFontVariationAxis, 4> axes;
|
|
|
|
aFontEntry->GetVariationAxes(axes);
|
|
|
|
auto index = axes.IndexOf(kOpszTag, 0, TagEquals<gfxFontVariationAxis>());
|
|
|
|
if (index == axes.NoIndex) {
|
|
|
|
aFontEntry->mHasOpszAxis = false;
|
|
|
|
} else {
|
|
|
|
const auto& axis = axes[index];
|
|
|
|
aFontEntry->mHasOpszAxis = true;
|
|
|
|
aFontEntry->mOpszAxis = axis;
|
|
|
|
// Pick a slightly-adjusted version of the default that we'll
|
|
|
|
// use to work around Core Text's habit of ignoring any attempt
|
|
|
|
// to explicitly set the default value.
|
|
|
|
aFontEntry->mAdjustedDefaultOpsz =
|
|
|
|
axis.mDefaultValue == axis.mMinValue
|
2018-07-30 19:53:09 +03:00
|
|
|
? axis.mDefaultValue + kOpszFudgeAmount
|
|
|
|
: axis.mDefaultValue - kOpszFudgeAmount;
|
2018-05-15 16:59:26 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-15 16:59:26 +03:00
|
|
|
// Add 'opsz' if not present, or tweak its value if it looks too close
|
|
|
|
// to the default (after clamping to the font's available range).
|
|
|
|
if (aFontEntry->mHasOpszAxis) {
|
|
|
|
auto index = vars.IndexOf(kOpszTag, 0, TagEquals<gfxFontVariation>());
|
|
|
|
if (index == vars.NoIndex) {
|
|
|
|
gfxFontVariation opsz{kOpszTag, aFontEntry->mAdjustedDefaultOpsz};
|
|
|
|
vars.AppendElement(opsz);
|
|
|
|
} else {
|
|
|
|
// Figure out a "safe" value that Core Text won't ignore.
|
|
|
|
auto& value = vars[index].mValue;
|
|
|
|
auto& axis = aFontEntry->mOpszAxis;
|
|
|
|
value = fmin(fmax(value, axis.mMinValue), axis.mMaxValue);
|
2018-07-30 19:53:09 +03:00
|
|
|
if (std::abs(value - axis.mDefaultValue) < kOpszFudgeAmount) {
|
2018-05-15 16:59:26 +03:00
|
|
|
value = aFontEntry->mAdjustedDefaultOpsz;
|
|
|
|
}
|
2017-01-06 19:35:11 +03:00
|
|
|
}
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
|
|
|
|
2010-03-10 15:46:41 +03:00
|
|
|
mCGFont = UnscaledFontMac::CreateCGFontWithVariations(
|
2020-08-09 10:23:42 +03:00
|
|
|
baseFont, aUnscaledFont->AxesCache(), vars.Length(), vars.Elements());
|
2010-06-11 23:14:38 +04:00
|
|
|
if (!mCGFont) {
|
2010-03-10 15:46:41 +03:00
|
|
|
::CFRetain(baseFont);
|
2017-04-14 21:11:00 +03:00
|
|
|
mCGFont = baseFont;
|
2010-06-11 23:14:38 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mCGFont = aUnscaledFont->GetFont();
|
2010-03-10 15:46:41 +03:00
|
|
|
if (!mCGFont) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsValid = false;
|
2010-03-10 15:46:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-01-06 19:35:11 +03:00
|
|
|
::CFRetain(mCGFont);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-03-10 15:46:41 +03:00
|
|
|
|
|
|
|
// InitMetrics will handle the sizeAdjust factor and set mAdjustedSize
|
|
|
|
InitMetrics();
|
2013-07-30 00:00:41 +04:00
|
|
|
if (!mIsValid) {
|
|
|
|
return;
|
2010-03-10 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// turn off font anti-aliasing based on user pref setting
|
2019-10-02 00:56:30 +03:00
|
|
|
if (mAdjustedSize <=
|
|
|
|
(gfxFloat)gfxPlatformMac::GetPlatform()->GetAntiAliasingThreshold()) {
|
2013-07-30 00:00:53 +04:00
|
|
|
mAntialiasOption = kAntialiasNone;
|
2013-07-30 00:00:41 +04:00
|
|
|
} else if (mStyle.useGrayscaleAntialiasing) {
|
2013-07-30 00:00:53 +04:00
|
|
|
mAntialiasOption = kAntialiasGrayscale;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-03-10 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxMacFont::~gfxMacFont() {
|
2017-01-06 19:35:11 +03:00
|
|
|
if (mCGFont) {
|
|
|
|
::CFRelease(mCGFont);
|
|
|
|
}
|
2014-11-17 12:59:49 +03:00
|
|
|
if (mCTFont) {
|
|
|
|
::CFRelease(mCTFont);
|
|
|
|
}
|
2010-03-10 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
2015-12-16 00:56:41 +03:00
|
|
|
bool gfxMacFont::ShapeText(DrawTarget* aDrawTarget, const char16_t* aText,
|
2014-05-31 11:12:40 +04:00
|
|
|
uint32_t aOffset, uint32_t aLength, Script aScript,
|
2020-11-13 16:15:39 +03:00
|
|
|
nsAtom* aLanguage, bool aVertical,
|
|
|
|
RoundingFlags aRounding,
|
2014-05-31 11:12:40 +04:00
|
|
|
gfxShapedText* aShapedText) {
|
2010-07-22 13:25:21 +04:00
|
|
|
if (!mIsValid) {
|
|
|
|
NS_WARNING("invalid font! expect incorrect text rendering");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-22 13:25:21 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-10-01 23:25:48 +04:00
|
|
|
// Currently, we don't support vertical shaping via CoreText,
|
|
|
|
// so we ignore RequiresAATLayout if vertical is requested.
|
2017-09-27 13:16:35 +03:00
|
|
|
auto macFontEntry = static_cast<MacOSFontEntry*>(GetFontEntry());
|
2019-05-20 17:29:15 +03:00
|
|
|
if (macFontEntry->RequiresAATLayout() && !aVertical &&
|
2019-06-26 03:38:09 +03:00
|
|
|
StaticPrefs::gfx_font_rendering_coretext_enabled()) {
|
2014-05-31 11:12:40 +04:00
|
|
|
if (!mCoreTextShaper) {
|
2016-04-15 22:45:37 +03:00
|
|
|
mCoreTextShaper = MakeUnique<gfxCoreTextShaper>(this);
|
2014-05-31 11:12:40 +04:00
|
|
|
}
|
2019-05-30 13:28:44 +03:00
|
|
|
if (mCoreTextShaper->ShapeText(aDrawTarget, aText, aOffset, aLength,
|
2020-11-13 16:15:39 +03:00
|
|
|
aScript, aLanguage, aVertical, aRounding,
|
2019-05-30 13:28:44 +03:00
|
|
|
aShapedText)) {
|
2015-12-16 00:56:41 +03:00
|
|
|
PostShapingFixup(aDrawTarget, aText, aOffset, aLength, aVertical,
|
2015-12-16 00:56:40 +03:00
|
|
|
aShapedText);
|
2019-05-30 13:28:44 +03:00
|
|
|
if (GetFontEntry()->HasTrackingTable()) {
|
|
|
|
// Convert font size from device pixels back to CSS px
|
|
|
|
// to use in selecting tracking value
|
|
|
|
float trackSize = GetAdjustedSize() *
|
|
|
|
aShapedText->GetAppUnitsPerDevUnit() /
|
|
|
|
AppUnitsPerCSSPixel();
|
|
|
|
float tracking =
|
|
|
|
GetFontEntry()->TrackingForCSSPx(trackSize) * mFUnitsConvFactor;
|
|
|
|
// Applying tracking is a lot like the adjustment we do for
|
|
|
|
// synthetic bold: we want to apply between clusters, not to
|
|
|
|
// non-spacing glyphs within a cluster. So we can reuse that
|
|
|
|
// helper here.
|
|
|
|
aShapedText->AdjustAdvancesForSyntheticBold(tracking, aOffset, aLength);
|
|
|
|
}
|
|
|
|
return true;
|
2019-05-28 14:52:42 +03:00
|
|
|
}
|
2019-05-28 12:53:11 +03:00
|
|
|
}
|
|
|
|
|
2019-05-30 13:28:44 +03:00
|
|
|
return gfxFont::ShapeText(aDrawTarget, aText, aOffset, aLength, aScript,
|
2020-11-13 16:15:39 +03:00
|
|
|
aLanguage, aVertical, aRounding, aShapedText);
|
2009-10-07 21:16:52 +04:00
|
|
|
}
|
|
|
|
|
2016-06-27 19:41:55 +03:00
|
|
|
gfxFont::RunMetrics gfxMacFont::Measure(const gfxTextRun* aTextRun,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aStart, uint32_t aEnd,
|
2011-06-22 12:49:57 +04:00
|
|
|
BoundingBoxType aBoundingBoxType,
|
2015-12-16 00:56:41 +03:00
|
|
|
DrawTarget* aRefDrawTarget,
|
2014-10-16 12:40:19 +04:00
|
|
|
Spacing* aSpacing,
|
2017-05-05 00:27:05 +03:00
|
|
|
gfx::ShapedTextFlags aOrientation) {
|
2011-06-22 12:49:57 +04:00
|
|
|
gfxFont::RunMetrics metrics =
|
|
|
|
gfxFont::Measure(aTextRun, aStart, aEnd, aBoundingBoxType, aRefDrawTarget,
|
2014-10-16 12:40:19 +04:00
|
|
|
aSpacing, aOrientation);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-06-22 12:49:57 +04:00
|
|
|
// if aBoundingBoxType is not TIGHT_HINTED_OUTLINE_EXTENTS then we need to add
|
|
|
|
// a pixel column each side of the bounding box in case of antialiasing
|
|
|
|
// "bleed"
|
|
|
|
if (aBoundingBoxType != TIGHT_HINTED_OUTLINE_EXTENTS &&
|
|
|
|
metrics.mBoundingBox.width > 0) {
|
|
|
|
metrics.mBoundingBox.x -= aTextRun->GetAppUnitsPerDevUnit();
|
|
|
|
metrics.mBoundingBox.width += aTextRun->GetAppUnitsPerDevUnit() * 2;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-06-22 12:49:57 +04:00
|
|
|
return metrics;
|
|
|
|
}
|
|
|
|
|
2010-03-10 15:46:41 +03:00
|
|
|
void gfxMacFont::InitMetrics() {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsValid = false;
|
2010-07-11 23:33:56 +04:00
|
|
|
::memset(&mMetrics, 0, sizeof(mMetrics));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t upem = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-05 13:18:44 +04:00
|
|
|
// try to get unitsPerEm from sfnt head table, to avoid calling CGFont
|
|
|
|
// if possible (bug 574368) and because CGFontGetUnitsPerEm does not
|
|
|
|
// return the true value for OpenType/CFF fonts (it normalizes to 1000,
|
|
|
|
// which then leads to metrics errors when we read the 'hmtx' table to
|
|
|
|
// get glyph advances for HarfBuzz, see bug 580863)
|
2013-05-16 20:29:20 +04:00
|
|
|
CFDataRef headData =
|
|
|
|
::CGFontCopyTableForTag(mCGFont, TRUETYPE_TAG('h', 'e', 'a', 'd'));
|
|
|
|
if (headData) {
|
|
|
|
if (size_t(::CFDataGetLength(headData)) >= sizeof(HeadTable)) {
|
|
|
|
const HeadTable* head =
|
|
|
|
reinterpret_cast<const HeadTable*>(::CFDataGetBytePtr(headData));
|
|
|
|
upem = head->unitsPerEm;
|
|
|
|
}
|
|
|
|
::CFRelease(headData);
|
|
|
|
}
|
|
|
|
if (!upem) {
|
2010-08-05 13:18:44 +04:00
|
|
|
upem = ::CGFontGetUnitsPerEm(mCGFont);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-08-05 13:18:44 +04:00
|
|
|
if (upem < 16 || upem > 16384) {
|
|
|
|
// See http://www.microsoft.com/typography/otspec/head.htm
|
2010-06-11 23:14:38 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
char warnBuf[1024];
|
2016-08-17 01:41:12 +03:00
|
|
|
SprintfLiteral(warnBuf,
|
|
|
|
"Bad font metrics for: %s (invalid unitsPerEm value)",
|
2018-09-12 22:34:57 +03:00
|
|
|
mFontEntry->Name().get());
|
2010-06-11 23:14:38 +04:00
|
|
|
NS_WARNING(warnBuf);
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
mAdjustedSize = std::max(mStyle.size, 1.0);
|
2010-07-11 23:33:56 +04:00
|
|
|
mFUnitsConvFactor = mAdjustedSize / upem;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-10-07 11:59:16 +04:00
|
|
|
// For CFF fonts, when scaling values read from CGFont* APIs, we need to
|
|
|
|
// use CG's idea of unitsPerEm, which may differ from the "true" value in
|
|
|
|
// the head table of the font (see bug 580863)
|
|
|
|
gfxFloat cgConvFactor;
|
|
|
|
if (static_cast<MacOSFontEntry*>(mFontEntry.get())->IsCFF()) {
|
|
|
|
cgConvFactor = mAdjustedSize / ::CGFontGetUnitsPerEm(mCGFont);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2010-10-07 11:59:16 +04:00
|
|
|
cgConvFactor = mFUnitsConvFactor;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-07-11 23:33:56 +04:00
|
|
|
// Try to read 'sfnt' metrics; for local, non-sfnt fonts ONLY, fall back to
|
|
|
|
// platform APIs. The InitMetrics...() functions will set mIsValid on success.
|
|
|
|
if (!InitMetricsFromSfntTables(mMetrics) &&
|
|
|
|
(!mFontEntry->IsUserFont() || mFontEntry->IsLocalUserFont())) {
|
2011-06-24 21:55:27 +04:00
|
|
|
InitMetricsFromPlatform();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-07-11 23:33:56 +04:00
|
|
|
if (!mIsValid) {
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-11 23:33:56 +04:00
|
|
|
if (mMetrics.xHeight == 0.0) {
|
2010-10-07 11:59:16 +04:00
|
|
|
mMetrics.xHeight = ::CGFontGetXHeight(mCGFont) * cgConvFactor;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 12:43:54 +03:00
|
|
|
if (mMetrics.capHeight == 0.0) {
|
|
|
|
mMetrics.capHeight = ::CGFontGetCapHeight(mCGFont) * cgConvFactor;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 09:10:40 +03:00
|
|
|
if (mStyle.sizeAdjust > 0.0 && mStyle.size > 0.0 && mMetrics.xHeight > 0.0) {
|
2010-07-11 23:33:56 +04:00
|
|
|
// apply font-size-adjust, and recalculate metrics
|
|
|
|
gfxFloat aspect = mMetrics.xHeight / mStyle.size;
|
|
|
|
mAdjustedSize = mStyle.GetAdjustedSize(aspect);
|
|
|
|
mFUnitsConvFactor = mAdjustedSize / upem;
|
2010-10-07 11:59:16 +04:00
|
|
|
if (static_cast<MacOSFontEntry*>(mFontEntry.get())->IsCFF()) {
|
|
|
|
cgConvFactor = mAdjustedSize / ::CGFontGetUnitsPerEm(mCGFont);
|
|
|
|
} else {
|
|
|
|
cgConvFactor = mFUnitsConvFactor;
|
|
|
|
}
|
2010-07-11 23:33:56 +04:00
|
|
|
mMetrics.xHeight = 0.0;
|
|
|
|
if (!InitMetricsFromSfntTables(mMetrics) &&
|
|
|
|
(!mFontEntry->IsUserFont() || mFontEntry->IsLocalUserFont())) {
|
2011-06-24 21:55:27 +04:00
|
|
|
InitMetricsFromPlatform();
|
2010-07-11 23:33:56 +04:00
|
|
|
}
|
|
|
|
if (!mIsValid) {
|
|
|
|
// this shouldn't happen, as we succeeded earlier before applying
|
|
|
|
// the size-adjust factor! But check anyway, for paranoia's sake.
|
2010-03-10 15:46:41 +03:00
|
|
|
return;
|
|
|
|
}
|
2010-07-11 23:33:56 +04:00
|
|
|
if (mMetrics.xHeight == 0.0) {
|
2010-10-07 11:59:16 +04:00
|
|
|
mMetrics.xHeight = ::CGFontGetXHeight(mCGFont) * cgConvFactor;
|
2016-08-18 12:43:54 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-08-18 12:43:54 +03:00
|
|
|
|
2010-07-11 23:33:56 +04:00
|
|
|
// Once we reach here, we've got basic metrics and set mIsValid = TRUE;
|
2010-10-07 11:59:16 +04:00
|
|
|
// there should be no further points of actual failure in InitMetrics().
|
2010-07-11 23:33:56 +04:00
|
|
|
// (If one is introduced, be sure to reset mIsValid to FALSE!)
|
2010-03-10 15:46:41 +03:00
|
|
|
|
2010-07-11 23:33:56 +04:00
|
|
|
mMetrics.emHeight = mAdjustedSize;
|
2010-03-10 15:46:41 +03:00
|
|
|
|
2010-07-11 23:33:56 +04:00
|
|
|
// Measure/calculate additional metrics, independent of whether we used
|
|
|
|
// the tables directly or ATS metrics APIs
|
2010-03-10 15:46:41 +03:00
|
|
|
|
2010-06-11 23:14:38 +04:00
|
|
|
CFDataRef cmap =
|
|
|
|
::CGFontCopyTableForTag(mCGFont, TRUETYPE_TAG('c', 'm', 'a', 'p'));
|
2010-03-10 15:46:41 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t glyphID;
|
2010-07-11 23:33:56 +04:00
|
|
|
if (mMetrics.aveCharWidth <= 0) {
|
2010-10-07 11:59:16 +04:00
|
|
|
mMetrics.aveCharWidth = GetCharWidth(cmap, 'x', &glyphID, cgConvFactor);
|
2010-07-11 23:33:56 +04:00
|
|
|
if (glyphID == 0) {
|
|
|
|
// we didn't find 'x', so use maxAdvance rather than zero
|
|
|
|
mMetrics.aveCharWidth = mMetrics.maxAdvance;
|
2010-03-10 15:46:41 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-09-03 00:18:25 +04:00
|
|
|
if (IsSyntheticBold()) {
|
2010-03-10 15:46:41 +03:00
|
|
|
mMetrics.aveCharWidth += GetSyntheticBoldOffset();
|
|
|
|
mMetrics.maxAdvance += GetSyntheticBoldOffset();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-03-10 15:46:41 +03:00
|
|
|
|
2010-10-07 11:59:16 +04:00
|
|
|
mMetrics.spaceWidth = GetCharWidth(cmap, ' ', &glyphID, cgConvFactor);
|
2010-07-11 23:33:56 +04:00
|
|
|
if (glyphID == 0) {
|
|
|
|
// no space glyph?!
|
2010-03-10 15:46:41 +03:00
|
|
|
mMetrics.spaceWidth = mMetrics.aveCharWidth;
|
2010-07-11 23:33:56 +04:00
|
|
|
}
|
2010-03-10 15:46:41 +03:00
|
|
|
mSpaceGlyph = glyphID;
|
|
|
|
|
2019-03-21 07:50:41 +03:00
|
|
|
mMetrics.zeroWidth = GetCharWidth(cmap, '0', &glyphID, cgConvFactor);
|
2010-07-11 23:33:56 +04:00
|
|
|
if (glyphID == 0) {
|
2019-03-21 07:50:41 +03:00
|
|
|
mMetrics.zeroWidth = -1.0; // indicates not found
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-06-11 23:14:38 +04:00
|
|
|
if (cmap) {
|
|
|
|
::CFRelease(cmap);
|
|
|
|
}
|
2010-03-10 15:46:41 +03:00
|
|
|
|
2010-07-11 23:33:56 +04:00
|
|
|
CalculateDerivedMetrics(mMetrics);
|
2009-10-07 21:16:52 +04:00
|
|
|
|
2010-03-10 15:46:41 +03:00
|
|
|
SanitizeMetrics(&mMetrics, mFontEntry->mIsBadUnderlineFont);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
fprintf (stderr, "Font: %p (%s) size: %f\n", this,
|
|
|
|
NS_ConvertUTF16toUTF8(GetName()).get(), mStyle.size);
|
|
|
|
// fprintf (stderr, " fbounds.origin.x %f y %f size.width %f height %f\n", fbounds.origin.x, fbounds.origin.y, fbounds.size.width, fbounds.size.height);
|
|
|
|
fprintf (stderr, " emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent);
|
|
|
|
fprintf (stderr, " maxAscent: %f maxDescent: %f maxAdvance: %f\n", mMetrics.maxAscent, mMetrics.maxDescent, mMetrics.maxAdvance);
|
|
|
|
fprintf (stderr, " internalLeading: %f externalLeading: %f\n", mMetrics.internalLeading, mMetrics.externalLeading);
|
2016-08-18 12:43:54 +03:00
|
|
|
fprintf (stderr, " spaceWidth: %f aveCharWidth: %f xHeight: %f capHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight, mMetrics.capHeight);
|
2014-06-28 10:40:36 +04:00
|
|
|
fprintf (stderr, " uOff: %f uSize: %f stOff: %f stSize: %f\n", mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize);
|
2010-03-10 15:46:41 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
gfxFloat gfxMacFont::GetCharWidth(CFDataRef aCmap, char16_t aUniChar,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t* aGlyphID, gfxFloat aConvFactor) {
|
2010-06-11 23:14:38 +04:00
|
|
|
CGGlyph glyph = 0;
|
|
|
|
|
|
|
|
if (aCmap) {
|
|
|
|
glyph = gfxFontUtils::MapCharToGlyph(::CFDataGetBytePtr(aCmap),
|
|
|
|
::CFDataGetLength(aCmap), aUniChar);
|
|
|
|
}
|
|
|
|
|
2010-07-11 23:33:56 +04:00
|
|
|
if (aGlyphID) {
|
2010-06-11 23:14:38 +04:00
|
|
|
*aGlyphID = glyph;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2010-06-11 23:14:38 +04:00
|
|
|
if (glyph) {
|
|
|
|
int advance;
|
|
|
|
if (::CGFontGetGlyphAdvances(mCGFont, &glyph, 1, &advance)) {
|
2010-10-07 11:59:16 +04:00
|
|
|
return advance * aConvFactor;
|
2010-03-10 15:46:41 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-03-10 15:46:41 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-06 19:35:11 +03:00
|
|
|
/* static */
|
|
|
|
CTFontRef gfxMacFont::CreateCTFontFromCGFontWithVariations(
|
2018-04-20 19:18:03 +03:00
|
|
|
CGFontRef aCGFont, CGFloat aSize, bool aInstalledFont,
|
2017-01-06 19:35:11 +03:00
|
|
|
CTFontDescriptorRef aFontDesc) {
|
2017-02-10 00:37:24 +03:00
|
|
|
// Avoid calling potentially buggy variation APIs on pre-Sierra macOS
|
2018-04-19 00:08:41 +03:00
|
|
|
// versions (see bug 1331683).
|
|
|
|
//
|
|
|
|
// And on HighSierra, CTFontCreateWithGraphicsFont properly carries over
|
|
|
|
// variation settings from the CGFont to CTFont, so we don't need to do
|
|
|
|
// the extra work here -- and this seems to avoid Core Text crashiness
|
|
|
|
// seen in bug 1454094.
|
|
|
|
//
|
2018-04-20 19:18:03 +03:00
|
|
|
// However, for installed fonts it seems we DO need to copy the variations
|
|
|
|
// explicitly even on 10.13, otherwise fonts fail to render (as in bug
|
|
|
|
// 1455494) when non-default values are used. Fortunately, the crash
|
|
|
|
// mentioned above occurs with data fonts, not (AFAICT) with system-
|
|
|
|
// installed fonts.
|
|
|
|
//
|
|
|
|
// So we only need to do this "the hard way" on Sierra, and on HighSierra
|
|
|
|
// for system-installed fonts; in other cases just let the standard CTFont
|
|
|
|
// function do its thing.
|
|
|
|
//
|
|
|
|
// NOTE in case this ever needs further adjustment: there is similar logic
|
|
|
|
// in four places in the tree (sadly):
|
|
|
|
// CreateCTFontFromCGFontWithVariations in gfxMacFont.cpp
|
|
|
|
// CreateCTFontFromCGFontWithVariations in ScaledFontMac.cpp
|
|
|
|
// CreateCTFontFromCGFontWithVariations in cairo-quartz-font.c
|
|
|
|
// ctfont_create_exact_copy in SkFontHost_mac.cpp
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-01-06 19:35:11 +03:00
|
|
|
CTFontRef ctFont;
|
2018-04-20 19:18:03 +03:00
|
|
|
if (nsCocoaFeatures::OnSierraExactly() ||
|
|
|
|
(aInstalledFont && nsCocoaFeatures::OnHighSierraOrLater())) {
|
|
|
|
CFDictionaryRef variations = ::CGFontCopyVariations(aCGFont);
|
|
|
|
if (variations) {
|
|
|
|
CFDictionaryRef varAttr = ::CFDictionaryCreate(
|
|
|
|
nullptr, (const void**)&kCTFontVariationAttribute,
|
|
|
|
(const void**)&variations, 1, &kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
::CFRelease(variations);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-20 19:18:03 +03:00
|
|
|
CTFontDescriptorRef varDesc =
|
|
|
|
aFontDesc
|
|
|
|
? ::CTFontDescriptorCreateCopyWithAttributes(aFontDesc, varAttr)
|
|
|
|
: ::CTFontDescriptorCreateWithAttributes(varAttr);
|
|
|
|
::CFRelease(varAttr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-20 19:18:03 +03:00
|
|
|
ctFont = ::CTFontCreateWithGraphicsFont(aCGFont, aSize, nullptr, varDesc);
|
|
|
|
::CFRelease(varDesc);
|
2017-01-06 19:35:11 +03:00
|
|
|
} else {
|
2017-02-25 02:05:42 +03:00
|
|
|
ctFont =
|
|
|
|
::CTFontCreateWithGraphicsFont(aCGFont, aSize, nullptr, aFontDesc);
|
2017-01-06 19:35:11 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2018-04-20 19:18:03 +03:00
|
|
|
ctFont = ::CTFontCreateWithGraphicsFont(aCGFont, aSize, nullptr, aFontDesc);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-04-20 19:18:03 +03:00
|
|
|
|
2017-01-06 19:35:11 +03:00
|
|
|
return ctFont;
|
|
|
|
}
|
|
|
|
|
2018-12-31 14:43:27 +03:00
|
|
|
int32_t gfxMacFont::GetGlyphWidth(uint16_t aGID) {
|
2018-04-06 21:44:05 +03:00
|
|
|
if (mVariationFont) {
|
|
|
|
// Avoid a potential Core Text crash (bug 1450209) by using
|
|
|
|
// CoreGraphics glyph advance API. This is inferior for 'sbix'
|
|
|
|
// fonts, but those won't have variations, so it's OK.
|
|
|
|
int cgAdvance;
|
|
|
|
if (::CGFontGetGlyphAdvances(mCGFont, &aGID, 1, &cgAdvance)) {
|
|
|
|
return cgAdvance * mFUnitsConvFactor * 0x10000;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-04-06 21:44:05 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-11-17 12:59:49 +03:00
|
|
|
if (!mCTFont) {
|
2018-04-20 19:18:03 +03:00
|
|
|
bool isInstalledFont =
|
|
|
|
!mFontEntry->IsUserFont() || mFontEntry->IsLocalUserFont();
|
|
|
|
mCTFont = CreateCTFontFromCGFontWithVariations(mCGFont, mAdjustedSize,
|
|
|
|
isInstalledFont);
|
2014-11-17 12:59:49 +03:00
|
|
|
if (!mCTFont) { // shouldn't happen, but let's be safe
|
|
|
|
NS_WARNING("failed to create CTFontRef to measure glyph width");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-11-17 12:59:49 +03:00
|
|
|
CGSize advance;
|
|
|
|
::CTFontGetAdvancesForGlyphs(mCTFont, kCTFontDefaultOrientation, &aGID,
|
|
|
|
&advance, 1);
|
|
|
|
return advance.width * 0x10000;
|
|
|
|
}
|
|
|
|
|
2019-09-20 19:30:21 +03:00
|
|
|
bool gfxMacFont::GetGlyphBounds(uint16_t aGID, gfxRect* aBounds, bool aTight) {
|
|
|
|
CGRect bb;
|
|
|
|
if (!::CGFontGetGlyphBBoxes(mCGFont, &aGID, 1, &bb)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// broken fonts can return incorrect bounds for some null characters,
|
|
|
|
// see https://bugzilla.mozilla.org/show_bug.cgi?id=534260
|
|
|
|
if (bb.origin.x == -32767 && bb.origin.y == -32767 &&
|
|
|
|
bb.size.width == 65534 && bb.size.height == 65534) {
|
|
|
|
*aBounds = gfxRect(0, 0, 0, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxRect bounds(bb.origin.x, -(bb.origin.y + bb.size.height), bb.size.width,
|
|
|
|
bb.size.height);
|
|
|
|
bounds.Scale(mFUnitsConvFactor);
|
|
|
|
*aBounds = bounds;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-24 21:55:27 +04:00
|
|
|
// Try to initialize font metrics via platform APIs (CG/CT),
|
2010-07-11 23:33:56 +04:00
|
|
|
// and set mIsValid = TRUE on success.
|
|
|
|
// We ONLY call this for local (platform) fonts that are not sfnt format;
|
2011-06-24 21:55:27 +04:00
|
|
|
// for sfnts, including ALL downloadable fonts, we prefer to use
|
|
|
|
// InitMetricsFromSfntTables and avoid platform APIs.
|
|
|
|
void gfxMacFont::InitMetricsFromPlatform() {
|
|
|
|
CTFontRef ctFont =
|
|
|
|
::CTFontCreateWithGraphicsFont(mCGFont, mAdjustedSize, nullptr, nullptr);
|
|
|
|
if (!ctFont) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMetrics.underlineOffset = ::CTFontGetUnderlinePosition(ctFont);
|
|
|
|
mMetrics.underlineSize = ::CTFontGetUnderlineThickness(ctFont);
|
|
|
|
|
|
|
|
mMetrics.externalLeading = ::CTFontGetLeading(ctFont);
|
|
|
|
|
|
|
|
mMetrics.maxAscent = ::CTFontGetAscent(ctFont);
|
|
|
|
mMetrics.maxDescent = ::CTFontGetDescent(ctFont);
|
|
|
|
|
|
|
|
// this is not strictly correct, but neither CTFont nor CGFont seems to
|
|
|
|
// provide maxAdvance, unless we were to iterate over all the glyphs
|
|
|
|
// (which isn't worth the cost here)
|
|
|
|
CGRect r = ::CTFontGetBoundingBox(ctFont);
|
|
|
|
mMetrics.maxAdvance = r.size.width;
|
|
|
|
|
|
|
|
// aveCharWidth is also not provided, so leave it at zero
|
|
|
|
// (fallback code in gfxMacFont::InitMetrics will then try measuring 'x');
|
|
|
|
// this could lead to less-than-"perfect" text field sizing when width is
|
|
|
|
// specified as a number of characters, and the font in use is a non-sfnt
|
|
|
|
// legacy font, but that's a sufficiently obscure edge case that we can
|
|
|
|
// ignore the potential discrepancy.
|
|
|
|
mMetrics.aveCharWidth = 0;
|
|
|
|
|
|
|
|
mMetrics.xHeight = ::CTFontGetXHeight(ctFont);
|
2016-08-18 12:43:54 +03:00
|
|
|
mMetrics.capHeight = ::CTFontGetCapHeight(ctFont);
|
2011-06-24 21:55:27 +04:00
|
|
|
|
|
|
|
::CFRelease(ctFont);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsValid = true;
|
2011-06-24 21:55:27 +04:00
|
|
|
}
|
|
|
|
|
2012-09-24 19:02:49 +04:00
|
|
|
already_AddRefed<ScaledFont> gfxMacFont::GetScaledFont(DrawTarget* aTarget) {
|
2011-11-18 08:00:38 +04:00
|
|
|
if (!mAzureScaledFont) {
|
2017-10-30 03:21:10 +03:00
|
|
|
mAzureScaledFont = Factory::CreateScaledFontForMacFont(
|
|
|
|
GetCGFontRef(), GetUnscaledFont(), GetAdjustedSize(),
|
2020-03-09 17:16:17 +03:00
|
|
|
ToDeviceColor(mFontSmoothingBackgroundColor),
|
2018-01-08 23:43:46 +03:00
|
|
|
!mStyle.useGrayscaleAntialiasing, IsSyntheticBold());
|
2017-10-30 03:21:10 +03:00
|
|
|
if (!mAzureScaledFont) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-04 17:56:40 +03:00
|
|
|
InitializeScaledFont();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-10-30 03:21:10 +03:00
|
|
|
|
|
|
|
RefPtr<ScaledFont> scaledFont(mAzureScaledFont);
|
|
|
|
return scaledFont.forget();
|
2011-11-18 08:00:38 +04:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:15:10 +03:00
|
|
|
bool gfxMacFont::ShouldRoundXOffset(cairo_t* aCairo) const {
|
|
|
|
// Quartz surfaces implement show_glyphs for Quartz fonts
|
|
|
|
return aCairo && cairo_surface_get_type(cairo_get_target(aCairo)) !=
|
|
|
|
CAIRO_SURFACE_TYPE_QUARTZ;
|
|
|
|
}
|
|
|
|
|
2013-10-15 06:19:47 +04:00
|
|
|
void gfxMacFont::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
FontCacheSizes* aSizes) const {
|
|
|
|
gfxFont::AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2012-03-23 16:14:16 +04:00
|
|
|
// mCGFont is shared with the font entry, so not counted here;
|
|
|
|
}
|
|
|
|
|
2013-10-15 06:19:47 +04:00
|
|
|
void gfxMacFont::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
|
|
|
}
|