2008-08-07 00:48:55 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
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/. */
|
2008-08-07 00:48:55 +04:00
|
|
|
|
2012-06-28 04:15:32 +04:00
|
|
|
#if defined(MOZ_WIDGET_GTK)
|
2008-08-07 00:48:55 +04:00
|
|
|
#include "gfxPlatformGtk.h"
|
2008-08-28 15:05:53 +04:00
|
|
|
#define gfxToolkitPlatform gfxPlatformGtk
|
2009-01-23 09:24:29 +03:00
|
|
|
#elif defined(XP_WIN)
|
|
|
|
#include "gfxWindowsPlatform.h"
|
|
|
|
#define gfxToolkitPlatform gfxWindowsPlatform
|
2010-05-11 21:27:36 +04:00
|
|
|
#elif defined(ANDROID)
|
|
|
|
#include "gfxAndroidPlatform.h"
|
|
|
|
#define gfxToolkitPlatform gfxAndroidPlatform
|
2008-08-28 15:05:53 +04:00
|
|
|
#endif
|
2010-05-11 21:27:36 +04:00
|
|
|
|
2008-08-07 00:48:55 +04:00
|
|
|
#include "gfxTypes.h"
|
|
|
|
#include "gfxFT2Fonts.h"
|
2009-10-31 02:13:41 +03:00
|
|
|
#include "gfxFT2FontBase.h"
|
|
|
|
#include "gfxFT2Utils.h"
|
2011-09-23 15:15:36 +04:00
|
|
|
#include "gfxFT2FontList.h"
|
2014-09-16 13:58:12 +04:00
|
|
|
#include "gfxTextRun.h"
|
2008-08-07 00:48:55 +04:00
|
|
|
#include <locale.h>
|
2012-03-30 01:08:43 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2009-01-18 23:14:14 +03:00
|
|
|
#include "nsTArray.h"
|
2009-09-09 19:35:08 +04:00
|
|
|
#include "nsUnicodeRange.h"
|
|
|
|
#include "nsCRT.h"
|
2012-02-09 02:52:57 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-09-09 19:35:08 +04:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2009-09-09 19:35:08 +04:00
|
|
|
#include "prinit.h"
|
2011-06-12 06:30:16 +04:00
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-06-12 06:30:16 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-06-05 21:48:59 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2011-06-12 06:30:16 +04:00
|
|
|
|
2017-08-07 23:20:44 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-12-09 14:57:23 +03:00
|
|
|
/**
|
|
|
|
* gfxFT2Font
|
|
|
|
*/
|
2009-09-11 00:52:40 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2015-12-16 00:56:41 +03:00
|
|
|
gfxFT2Font::ShapeText(DrawTarget *aDrawTarget,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *aText,
|
2014-05-31 11:12:40 +04:00
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aLength,
|
2016-04-21 20:58:59 +03:00
|
|
|
Script aScript,
|
2014-10-01 23:25:48 +04:00
|
|
|
bool aVertical,
|
2017-04-03 19:49:17 +03:00
|
|
|
RoundingFlags aRounding,
|
2014-05-31 11:12:40 +04:00
|
|
|
gfxShapedText *aShapedText)
|
2010-12-09 14:57:23 +03:00
|
|
|
{
|
2015-12-16 00:56:41 +03:00
|
|
|
if (!gfxFont::ShapeText(aDrawTarget, aText, aOffset, aLength, aScript,
|
2017-04-03 19:49:17 +03:00
|
|
|
aVertical, aRounding, aShapedText)) {
|
2014-05-31 11:12:40 +04:00
|
|
|
// harfbuzz must have failed(?!), just render raw glyphs
|
2013-01-04 22:35:37 +04:00
|
|
|
AddRange(aText, aOffset, aLength, aShapedText);
|
2015-12-16 00:56:41 +03:00
|
|
|
PostShapingFixup(aDrawTarget, aText, aOffset, aLength,
|
2015-12-16 00:56:40 +03:00
|
|
|
aVertical, aShapedText);
|
2010-12-09 14:58:16 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-08-07 00:48:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-04 19:02:17 +04:00
|
|
|
gfxFT2Font::AddRange(const char16_t *aText, uint32_t aOffset,
|
2013-01-04 22:35:37 +04:00
|
|
|
uint32_t aLength, gfxShapedText *aShapedText)
|
2008-08-07 00:48:55 +04:00
|
|
|
{
|
2017-11-11 18:32:58 +03:00
|
|
|
typedef gfxShapedText::CompressedGlyph CompressedGlyph;
|
|
|
|
|
2013-01-04 22:35:37 +04:00
|
|
|
const uint32_t appUnitsPerDevUnit = aShapedText->GetAppUnitsPerDevUnit();
|
2008-08-07 00:48:55 +04:00
|
|
|
// we'll pass this in/figure it out dynamically, but at this point there can be only one face.
|
2010-12-09 14:57:23 +03:00
|
|
|
gfxFT2LockedFace faceLock(this);
|
2009-10-31 02:13:41 +03:00
|
|
|
FT_Face face = faceLock.get();
|
2008-08-07 00:48:55 +04:00
|
|
|
|
2017-11-11 18:32:58 +03:00
|
|
|
CompressedGlyph* charGlyphs = aShapedText->GetCharacterGlyphs();
|
2008-08-07 00:48:55 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
const gfxFT2Font::CachedGlyphData *cgd = nullptr, *cgdNext = nullptr;
|
2009-09-11 00:52:40 +04:00
|
|
|
|
2010-12-09 14:57:23 +03:00
|
|
|
FT_UInt spaceGlyph = GetSpaceGlyph();
|
2009-09-11 00:52:40 +04:00
|
|
|
|
2013-01-04 22:35:37 +04:00
|
|
|
for (uint32_t i = 0; i < aLength; i++, aOffset++) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t ch = aText[i];
|
2008-08-07 00:48:55 +04:00
|
|
|
|
|
|
|
if (ch == 0) {
|
|
|
|
// treat this null byte as a missing glyph, don't create a glyph for it
|
2013-01-04 22:35:37 +04:00
|
|
|
aShapedText->SetMissingGlyph(aOffset, 0, this);
|
2008-08-07 00:48:55 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-09 14:57:23 +03:00
|
|
|
NS_ASSERTION(!gfxFontGroup::IsInvalidChar(ch), "Invalid char detected");
|
2009-09-11 00:52:40 +04:00
|
|
|
|
|
|
|
if (cgdNext) {
|
|
|
|
cgd = cgdNext;
|
2012-07-30 18:20:58 +04:00
|
|
|
cgdNext = nullptr;
|
2009-09-11 00:52:40 +04:00
|
|
|
} else {
|
2017-07-27 06:24:48 +03:00
|
|
|
cgd = GetGlyphDataForChar(face, ch);
|
2009-09-11 00:52:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
FT_UInt gid = cgd->glyphIndex;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t advance = 0;
|
2008-08-07 00:48:55 +04:00
|
|
|
|
2009-09-11 00:52:40 +04:00
|
|
|
if (gid == 0) {
|
2008-08-07 00:48:55 +04:00
|
|
|
advance = -1; // trigger the missing glyphs case below
|
|
|
|
} else {
|
|
|
|
// find next character and its glyph -- in case they exist
|
|
|
|
// and exist in the current font face -- to compute kerning
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t chNext = 0;
|
2008-08-07 00:48:55 +04:00
|
|
|
FT_UInt gidNext = 0;
|
|
|
|
FT_Pos lsbDeltaNext = 0;
|
|
|
|
|
2013-01-04 22:35:37 +04:00
|
|
|
if (FT_HAS_KERNING(face) && i + 1 < aLength) {
|
|
|
|
chNext = aText[i + 1];
|
2008-08-07 00:48:55 +04:00
|
|
|
if (chNext != 0) {
|
2017-07-27 06:24:48 +03:00
|
|
|
cgdNext = GetGlyphDataForChar(face, chNext);
|
2009-09-11 00:52:40 +04:00
|
|
|
gidNext = cgdNext->glyphIndex;
|
|
|
|
if (gidNext && gidNext != spaceGlyph)
|
|
|
|
lsbDeltaNext = cgdNext->lsbDelta;
|
2008-08-07 00:48:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-11 00:52:40 +04:00
|
|
|
advance = cgd->xAdvance;
|
2008-08-07 00:48:55 +04: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-11 00:52:40 +04:00
|
|
|
if (cgd->rsbDelta - lsbDeltaNext >= 32) {
|
2008-08-07 00:48:55 +04:00
|
|
|
advance -= 64;
|
2009-09-11 00:52:40 +04:00
|
|
|
} else if (cgd->rsbDelta - lsbDeltaNext < -32) {
|
2008-08-07 00:48:55 +04:00
|
|
|
advance += 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 23:18:17 +03:00
|
|
|
// convert 26.6 fixed point to app units
|
|
|
|
// round rather than truncate to nearest pixel
|
|
|
|
// because these advances are often scaled
|
|
|
|
advance = ((advance * appUnitsPerDevUnit + 32) >> 6);
|
2008-08-07 00:48:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (advance >= 0 &&
|
2017-11-11 18:32:58 +03:00
|
|
|
CompressedGlyph::IsSimpleAdvance(advance) &&
|
|
|
|
CompressedGlyph::IsSimpleGlyphID(gid)) {
|
2013-01-04 22:35:37 +04:00
|
|
|
charGlyphs[aOffset].SetSimpleGlyph(advance, gid);
|
2008-08-07 00:48:55 +04:00
|
|
|
} else if (gid == 0) {
|
|
|
|
// gid = 0 only happens when the glyph is missing from the font
|
2013-01-04 22:35:37 +04:00
|
|
|
aShapedText->SetMissingGlyph(aOffset, ch, this);
|
2008-08-07 00:48:55 +04:00
|
|
|
} else {
|
|
|
|
gfxTextRun::DetailedGlyph details;
|
|
|
|
details.mGlyphID = gid;
|
2013-01-04 22:35:37 +04:00
|
|
|
NS_ASSERTION(details.mGlyphID == gid,
|
|
|
|
"Seriously weird glyph ID detected!");
|
2008-08-07 00:48:55 +04:00
|
|
|
details.mAdvance = advance;
|
2017-11-11 18:32:58 +03:00
|
|
|
bool isClusterStart = charGlyphs[aOffset].IsClusterStart();
|
|
|
|
aShapedText->SetGlyphs(aOffset,
|
|
|
|
CompressedGlyph::MakeComplex(isClusterStart,
|
|
|
|
true, 1),
|
|
|
|
&details);
|
2008-08-07 00:48:55 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-11 00:52:40 +04:00
|
|
|
}
|
2008-08-07 00:48:55 +04:00
|
|
|
|
2017-08-07 23:20:44 +03:00
|
|
|
gfxFT2Font::gfxFT2Font(const RefPtr<UnscaledFontFreeType>& aUnscaledFont,
|
2017-04-07 00:41:02 +03:00
|
|
|
cairo_scaled_font_t *aCairoFont,
|
2018-06-15 02:42:56 +03:00
|
|
|
FT_Face aFTFace,
|
2011-09-23 15:16:13 +04:00
|
|
|
FT2FontEntry *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)
|
|
|
|
: gfxFT2FontBase(aUnscaledFont, aCairoFont, aFontEntry, aFontStyle)
|
2014-08-06 17:31:21 +04:00
|
|
|
, mCharGlyphCache(32)
|
2018-06-15 02:42:56 +03:00
|
|
|
, mFTFace(aFTFace)
|
2008-08-07 00:48:55 +04:00
|
|
|
{
|
2008-08-28 15:05:53 +04:00
|
|
|
NS_ASSERTION(mFontEntry, "Unable to find font entry for font. Something is whack.");
|
2017-12-06 16:51:39 +03:00
|
|
|
// TODO: use FreeType emboldening instead of multi-strike?
|
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);
|
2008-08-07 00:48:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFT2Font::~gfxFT2Font()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-07 23:20:44 +03:00
|
|
|
already_AddRefed<ScaledFont>
|
|
|
|
gfxFT2Font::GetScaledFont(DrawTarget *aTarget)
|
|
|
|
{
|
|
|
|
if (!mAzureScaledFont) {
|
|
|
|
NativeFont nativeFont;
|
2018-06-15 02:42:56 +03:00
|
|
|
nativeFont.mType = NativeFontType::FREETYPE_FACE;
|
|
|
|
nativeFont.mFont = mFTFace;
|
|
|
|
|
2017-08-07 23:20:44 +03:00
|
|
|
mAzureScaledFont =
|
2018-06-15 02:42:56 +03:00
|
|
|
Factory::CreateScaledFontForNativeFont(nativeFont,
|
|
|
|
GetUnscaledFont(),
|
|
|
|
GetAdjustedSize(),
|
|
|
|
GetCairoScaledFont());
|
2018-07-04 17:56:40 +03:00
|
|
|
InitializeScaledFont();
|
2017-08-07 23:20:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ScaledFont> scaledFont(mAzureScaledFont);
|
|
|
|
return scaledFont.forget();
|
|
|
|
}
|
|
|
|
|
2009-09-11 00:52:40 +04:00
|
|
|
void
|
2017-07-27 06:24:48 +03:00
|
|
|
gfxFT2Font::FillGlyphDataForChar(FT_Face face, uint32_t ch, CachedGlyphData *gd)
|
2009-09-11 00:52:40 +04:00
|
|
|
{
|
2012-05-22 01:09:54 +04:00
|
|
|
if (!face->charmap || face->charmap->encoding != FT_ENCODING_UNICODE) {
|
|
|
|
FT_Select_Charmap(face, FT_ENCODING_UNICODE);
|
|
|
|
}
|
2009-09-11 00:52:40 +04:00
|
|
|
FT_UInt gid = FT_Get_Char_Index(face, ch);
|
|
|
|
|
|
|
|
if (gid == 0) {
|
|
|
|
// 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;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-09 02:52:57 +04:00
|
|
|
FT_Int32 flags = gfxPlatform::GetPlatform()->FontHintingEnabled() ?
|
|
|
|
FT_LOAD_DEFAULT :
|
|
|
|
(FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING);
|
2018-06-26 18:30:13 +03:00
|
|
|
FT_Error err = Factory::LoadFTGlyph(face, gid, flags);
|
2009-09-11 00:52:40 +04:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
// 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");
|
|
|
|
|
|
|
|
gd->glyphIndex = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gd->glyphIndex = gid;
|
|
|
|
gd->lsbDelta = face->glyph->lsb_delta;
|
|
|
|
gd->rsbDelta = face->glyph->rsb_delta;
|
|
|
|
gd->xAdvance = face->glyph->advance.x;
|
|
|
|
}
|
2012-03-23 16:14:16 +04:00
|
|
|
|
|
|
|
void
|
2017-08-07 23:20:44 +03:00
|
|
|
gfxFT2Font::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
2013-10-15 06:19:47 +04:00
|
|
|
FontCacheSizes* aSizes) const
|
2012-03-23 16:14:16 +04:00
|
|
|
{
|
2013-10-15 06:19:47 +04:00
|
|
|
gfxFont::AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2012-03-23 16:14:16 +04:00
|
|
|
aSizes->mFontInstances +=
|
2015-07-29 11:50:52 +03:00
|
|
|
mCharGlyphCache.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2012-03-23 16:14:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-07 23:20:44 +03:00
|
|
|
gfxFT2Font::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
|
2013-10-15 06:19:47 +04:00
|
|
|
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
|
|
|
}
|
2013-06-05 21:48:59 +04:00
|
|
|
|