2018-11-30 19:52:05 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00: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/. */
|
2008-08-06 20:48:55 +00:00
|
|
|
|
2012-06-28 00:15:32 +00:00
|
|
|
#if defined(MOZ_WIDGET_GTK)
|
2008-08-06 20:48:55 +00:00
|
|
|
# include "gfxPlatformGtk.h"
|
2008-08-28 11:05:53 +00:00
|
|
|
# define gfxToolkitPlatform gfxPlatformGtk
|
2009-01-23 06:24:29 +00:00
|
|
|
#elif defined(XP_WIN)
|
|
|
|
# include "gfxWindowsPlatform.h"
|
|
|
|
# define gfxToolkitPlatform gfxWindowsPlatform
|
2010-05-11 17:27:36 +00:00
|
|
|
#elif defined(ANDROID)
|
|
|
|
# include "gfxAndroidPlatform.h"
|
|
|
|
# define gfxToolkitPlatform gfxAndroidPlatform
|
2008-08-28 11:05:53 +00:00
|
|
|
#endif
|
2010-05-11 17:27:36 +00:00
|
|
|
|
2008-08-06 20:48:55 +00:00
|
|
|
#include "gfxTypes.h"
|
|
|
|
#include "gfxFT2Fonts.h"
|
2009-10-30 23:13:41 +00:00
|
|
|
#include "gfxFT2FontBase.h"
|
|
|
|
#include "gfxFT2Utils.h"
|
2011-09-23 11:15:36 +00:00
|
|
|
#include "gfxFT2FontList.h"
|
2014-09-16 09:58:12 +00:00
|
|
|
#include "gfxTextRun.h"
|
2008-08-06 20:48:55 +00:00
|
|
|
#include <locale.h>
|
2012-03-29 21:08:43 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2009-01-18 20:14:14 +00:00
|
|
|
#include "nsTArray.h"
|
2009-09-09 15:35:08 +00:00
|
|
|
#include "nsCRT.h"
|
2012-02-08 22:52:57 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-09-09 15:35:08 +00:00
|
|
|
|
2015-05-19 18:15:34 +00:00
|
|
|
#include "mozilla/Logging.h"
|
2009-09-09 15:35:08 +00:00
|
|
|
#include "prinit.h"
|
2011-06-12 02:30:16 +00:00
|
|
|
|
2013-06-23 12:03:39 +00:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-06-12 02:30:16 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-06-05 17:48:59 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2011-06-12 02:30:16 +00:00
|
|
|
|
2017-08-07 20:20:44 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-12-09 11:57:23 +00:00
|
|
|
/**
|
|
|
|
* gfxFT2Font
|
|
|
|
*/
|
2009-09-10 20:52:40 +00:00
|
|
|
|
2015-12-15 21:56:41 +00:00
|
|
|
bool gfxFT2Font::ShapeText(DrawTarget* aDrawTarget, const char16_t* aText,
|
2014-05-31 07:12:40 +00:00
|
|
|
uint32_t aOffset, uint32_t aLength, Script aScript,
|
2020-11-13 13:15:39 +00:00
|
|
|
nsAtom* aLanguage, bool aVertical,
|
|
|
|
RoundingFlags aRounding,
|
2014-05-31 07:12:40 +00:00
|
|
|
gfxShapedText* aShapedText) {
|
2015-12-15 21:56:41 +00:00
|
|
|
if (!gfxFont::ShapeText(aDrawTarget, aText, aOffset, aLength, aScript,
|
2020-11-13 13:15:39 +00:00
|
|
|
aLanguage, aVertical, aRounding, aShapedText)) {
|
2014-05-31 07:12:40 +00:00
|
|
|
// harfbuzz must have failed(?!), just render raw glyphs
|
2013-01-04 18:35:37 +00:00
|
|
|
AddRange(aText, aOffset, aLength, aShapedText);
|
2015-12-15 21:56:41 +00:00
|
|
|
PostShapingFixup(aDrawTarget, aText, aOffset, aLength, aVertical,
|
2015-12-15 21:56:40 +00:00
|
|
|
aShapedText);
|
2010-12-09 11:58:16 +00:00
|
|
|
}
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2008-08-06 20:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
void gfxFT2Font::AddRange(const char16_t* aText, uint32_t aOffset,
|
2013-01-04 18:35:37 +00:00
|
|
|
uint32_t aLength, gfxShapedText* aShapedText) {
|
2017-11-11 15:32:58 +00:00
|
|
|
typedef gfxShapedText::CompressedGlyph CompressedGlyph;
|
2008-08-06 20:48:55 +00:00
|
|
|
|
2017-11-11 15:32:58 +00:00
|
|
|
const uint32_t appUnitsPerDevUnit = aShapedText->GetAppUnitsPerDevUnit();
|
|
|
|
// we'll pass this in/figure it out dynamically, but at this point there can
|
|
|
|
// be only one face.
|
|
|
|
gfxFT2LockedFace faceLock(this);
|
2009-10-30 23:13:41 +00:00
|
|
|
FT_Face face = faceLock.get();
|
2008-08-06 20:48:55 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
CompressedGlyph* charGlyphs = aShapedText->GetCharacterGlyphs();
|
2009-09-10 20:52:40 +00:00
|
|
|
|
2010-12-09 11:57:23 +00:00
|
|
|
const gfxFT2Font::CachedGlyphData *cgd = nullptr, *cgdNext = nullptr;
|
2009-09-10 20:52:40 +00:00
|
|
|
|
2013-01-04 18:35:37 +00:00
|
|
|
FT_UInt spaceGlyph = GetSpaceGlyph();
|
2008-08-06 20:48:55 +00:00
|
|
|
|
2013-01-04 18:35:37 +00:00
|
|
|
for (uint32_t i = 0; i < aLength; i++, aOffset++) {
|
2010-12-09 11:57:23 +00:00
|
|
|
char16_t ch = aText[i];
|
2009-09-10 20:52:40 +00:00
|
|
|
|
|
|
|
if (ch == 0) {
|
2012-07-30 14:20:58 +00:00
|
|
|
// treat this null byte as a missing glyph, don't create a glyph for it
|
2017-07-27 03:24:48 +00:00
|
|
|
aShapedText->SetMissingGlyph(aOffset, 0, this);
|
|
|
|
continue;
|
2009-09-10 20:52:40 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 03:24:48 +00:00
|
|
|
NS_ASSERTION(!gfxFontGroup::IsInvalidChar(ch), "Invalid char detected");
|
2008-08-06 20:48:55 +00:00
|
|
|
|
|
|
|
if (cgdNext) {
|
2013-01-04 18:35:37 +00:00
|
|
|
cgd = cgdNext;
|
2012-07-30 14:20:58 +00:00
|
|
|
cgdNext = nullptr;
|
2008-08-06 20:48:55 +00:00
|
|
|
} else {
|
2017-11-11 15:32:58 +00:00
|
|
|
cgd = GetGlyphDataForChar(face, ch);
|
2008-08-06 20:48:55 +00:00
|
|
|
}
|
|
|
|
|
2008-08-28 11:05:53 +00:00
|
|
|
FT_UInt gid = cgd->glyphIndex;
|
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 09:30:50 +00:00
|
|
|
int32_t advance = 0;
|
2008-08-06 20:48:55 +00:00
|
|
|
|
|
|
|
if (gid == 0) {
|
|
|
|
advance = -1; // trigger the missing glyphs case below
|
2018-11-30 10:46:48 +00:00
|
|
|
} else {
|
2008-08-06 20:48:55 +00:00
|
|
|
// find next character and its glyph -- in case they exist
|
|
|
|
// and exist in the current font face -- to compute kerning
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t chNext = 0;
|
2008-08-06 20:48:55 +00:00
|
|
|
FT_UInt gidNext = 0;
|
|
|
|
FT_Pos lsbDeltaNext = 0;
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2013-01-04 18:35:37 +00:00
|
|
|
if (FT_HAS_KERNING(face) && i + 1 < aLength) {
|
|
|
|
chNext = aText[i + 1];
|
2008-08-06 20:48:55 +00:00
|
|
|
if (chNext != 0) {
|
2017-07-27 03:24:48 +00:00
|
|
|
cgdNext = GetGlyphDataForChar(face, chNext);
|
2009-09-10 20:52:40 +00:00
|
|
|
gidNext = cgdNext->glyphIndex;
|
|
|
|
if (gidNext && gidNext != spaceGlyph)
|
|
|
|
lsbDeltaNext = cgdNext->lsbDelta;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-10 20:52:40 +00:00
|
|
|
advance = cgd->xAdvance;
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2008-08-06 20:48:55 +00:00
|
|
|
// now add kerning to the current glyph's advance
|
|
|
|
if (chNext && gidNext) {
|
|
|
|
FT_Vector kerning;
|
|
|
|
kerning.x = 0;
|
|
|
|
FT_Get_Kerning(face, gid, gidNext, FT_KERNING_DEFAULT, &kerning);
|
|
|
|
advance += kerning.x;
|
2009-09-10 20:52:40 +00:00
|
|
|
if (cgd->rsbDelta - lsbDeltaNext >= 32) {
|
2008-08-06 20:48:55 +00:00
|
|
|
advance -= 64;
|
|
|
|
} else if (cgd->rsbDelta - lsbDeltaNext < -32) {
|
|
|
|
advance += 64;
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-06 20:48:55 +00:00
|
|
|
|
2017-08-07 20:20:44 +00:00
|
|
|
// convert 26.6 fixed point to app units
|
2018-06-14 23:42:56 +00:00
|
|
|
// round rather than truncate to nearest pixel
|
|
|
|
// because these advances are often scaled
|
2018-07-04 14:56:40 +00:00
|
|
|
advance = ((advance * appUnitsPerDevUnit + 32) >> 6);
|
2017-08-07 20:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (advance >= 0 && CompressedGlyph::IsSimpleAdvance(advance) &&
|
2017-07-27 03:24:48 +00:00
|
|
|
CompressedGlyph::IsSimpleGlyphID(gid)) {
|
|
|
|
charGlyphs[aOffset].SetSimpleGlyph(advance, gid);
|
2012-05-21 21:09:54 +00:00
|
|
|
} else if (gid == 0) {
|
|
|
|
// gid = 0 only happens when the glyph is missing from the font
|
|
|
|
aShapedText->SetMissingGlyph(aOffset, ch, this);
|
2018-11-30 10:46:48 +00:00
|
|
|
} else {
|
2012-05-21 21:09:54 +00:00
|
|
|
gfxTextRun::DetailedGlyph details;
|
2008-08-06 20:48:55 +00:00
|
|
|
details.mGlyphID = gid;
|
2013-01-04 18:35:37 +00:00
|
|
|
NS_ASSERTION(details.mGlyphID == gid,
|
|
|
|
"Seriously weird glyph ID detected!");
|
2012-05-21 21:09:54 +00:00
|
|
|
details.mAdvance = advance;
|
2020-07-30 11:48:06 +00:00
|
|
|
aShapedText->SetDetailedGlyphs(aOffset, 1, &details);
|
2012-05-21 21:09:54 +00:00
|
|
|
}
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-10 20:52:40 +00:00
|
|
|
|
|
|
|
gfxFT2Font::gfxFT2Font(const RefPtr<UnscaledFontFreeType>& aUnscaledFont,
|
2019-09-16 17:07:59 +00:00
|
|
|
RefPtr<mozilla::gfx::SharedFTFace>&& aFTFace,
|
2019-09-16 17:15:10 +00:00
|
|
|
FT2FontEntry* aFontEntry, const gfxFontStyle* aFontStyle,
|
2019-10-01 21:56:30 +00:00
|
|
|
int aLoadFlags)
|
|
|
|
: gfxFT2FontBase(aUnscaledFont, std::move(aFTFace), aFontEntry, aFontStyle,
|
|
|
|
aLoadFlags, aFontStyle->NeedsSyntheticBold(aFontEntry)),
|
2019-09-16 17:12:38 +00:00
|
|
|
mCharGlyphCache(32) {
|
2009-09-10 20:52:40 +00:00
|
|
|
NS_ASSERTION(mFontEntry,
|
|
|
|
"Unable to find font entry for font. Something is whack.");
|
2019-09-16 17:15:10 +00:00
|
|
|
InitMetrics();
|
2009-09-10 20:52:40 +00:00
|
|
|
}
|
|
|
|
|
2012-02-08 22:52:57 +00:00
|
|
|
gfxFT2Font::~gfxFT2Font() {}
|
2009-09-10 20:52:40 +00:00
|
|
|
|
2021-12-09 22:16:17 +00:00
|
|
|
already_AddRefed<ScaledFont> gfxFT2Font::GetScaledFont(
|
|
|
|
const TextRunDrawParams& aRunParams) {
|
2022-03-24 18:58:00 +00:00
|
|
|
if (ScaledFont* scaledFont = mAzureScaledFont) {
|
|
|
|
return do_AddRef(scaledFont);
|
2009-09-10 20:52:40 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 18:58:00 +00:00
|
|
|
RefPtr<ScaledFont> newScaledFont = Factory::CreateScaledFontForFreeTypeFont(
|
|
|
|
GetUnscaledFont(), GetAdjustedSize(), mFTFace,
|
|
|
|
GetStyle()->NeedsSyntheticBold(GetFontEntry()));
|
|
|
|
if (!newScaledFont) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeScaledFont(newScaledFont);
|
|
|
|
|
|
|
|
if (mAzureScaledFont.compareExchange(nullptr, newScaledFont.get())) {
|
|
|
|
Unused << newScaledFont.forget();
|
|
|
|
}
|
|
|
|
ScaledFont* scaledFont = mAzureScaledFont;
|
|
|
|
return do_AddRef(scaledFont);
|
2009-09-10 20:52:40 +00:00
|
|
|
}
|
2012-03-23 12:14:16 +00:00
|
|
|
|
2019-09-16 17:15:10 +00:00
|
|
|
bool gfxFT2Font::ShouldHintMetrics() const {
|
|
|
|
return !gfxPlatform::GetPlatform()->RequiresLinearZoom();
|
|
|
|
}
|
|
|
|
|
2017-08-07 20:20:44 +00:00
|
|
|
void gfxFT2Font::FillGlyphDataForChar(FT_Face face, uint32_t ch,
|
2013-10-15 02:19:47 +00:00
|
|
|
CachedGlyphData* gd) {
|
2019-05-10 16:27:49 +00:00
|
|
|
if (!face->charmap || (face->charmap->encoding != FT_ENCODING_UNICODE &&
|
|
|
|
face->charmap->encoding != FT_ENCODING_MS_SYMBOL)) {
|
|
|
|
if (FT_Err_Ok != FT_Select_Charmap(face, FT_ENCODING_UNICODE) &&
|
|
|
|
FT_Err_Ok != FT_Select_Charmap(face, FT_ENCODING_MS_SYMBOL)) {
|
|
|
|
NS_WARNING("failed to select Unicode or symbol charmap!");
|
|
|
|
}
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|
2009-09-10 20:52:40 +00:00
|
|
|
FT_UInt gid = FT_Get_Char_Index(face, ch);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2009-09-10 20:52:40 +00:00
|
|
|
if (gid == 0) {
|
2013-10-15 02:19:47 +00:00
|
|
|
// this font doesn't support this char!
|
|
|
|
NS_ASSERTION(gid != 0,
|
|
|
|
"We don't have a glyph, but font indicated that it supported "
|
|
|
|
"this char in tables?");
|
|
|
|
gd->glyphIndex = 0;
|
2018-11-30 10:46:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:15:10 +00:00
|
|
|
FT_Error err = Factory::LoadFTGlyph(face, gid, mFTLoadFlags);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
|
|
|
if (err) {
|
2015-07-29 08:50:52 +00:00
|
|
|
// hmm, this is weird, we failed to load a glyph that we had?
|
|
|
|
NS_WARNING("Failed to load glyph that we got from Get_Char_index");
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2009-09-10 20:52:40 +00:00
|
|
|
gd->glyphIndex = 0;
|
2018-11-30 10:46:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-10 20:52:40 +00:00
|
|
|
gd->glyphIndex = gid;
|
|
|
|
gd->lsbDelta = face->glyph->lsb_delta;
|
|
|
|
gd->rsbDelta = face->glyph->rsb_delta;
|
2019-09-20 16:30:21 +00:00
|
|
|
gd->xAdvance = face->glyph->advance.x;
|
|
|
|
if (gd->xAdvance) {
|
|
|
|
gd->xAdvance += GetEmboldenStrength(face).x;
|
|
|
|
}
|
2012-03-23 12:14:16 +00:00
|
|
|
}
|
|
|
|
|
2017-08-07 20:20:44 +00:00
|
|
|
void gfxFT2Font::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
2013-10-15 02:19:47 +00:00
|
|
|
FontCacheSizes* aSizes) const {
|
|
|
|
gfxFont::AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
|
|
|
aSizes->mFontInstances +=
|
2015-07-29 08:50:52 +00:00
|
|
|
mCharGlyphCache.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2012-03-23 12:14:16 +00:00
|
|
|
}
|
2013-06-05 17:48:59 +00:00
|
|
|
|
2017-08-07 20:20:44 +00:00
|
|
|
void gfxFT2Font::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
|
2012-03-23 12:14:16 +00:00
|
|
|
FontCacheSizes* aSizes) const {
|
|
|
|
aSizes->mFontInstances += aMallocSizeOf(this);
|
2017-08-07 20:20:44 +00:00
|
|
|
AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2018-11-30 10:46:48 +00:00
|
|
|
}
|