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/. */
|
2009-10-07 18:13:40 +04:00
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2014-06-30 22:05:29 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "gfxAndroidPlatform.h"
|
2012-10-03 23:29:10 +04:00
|
|
|
#include "mozilla/Omnijar.h"
|
2015-10-20 20:13:35 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2018-02-17 02:30:47 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2011-09-23 15:16:13 +04:00
|
|
|
|
|
|
|
#include "nsXULAppAPI.h"
|
2011-09-23 15:15:36 +04:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <android/log.h>
|
|
|
|
#define ALOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gecko", ##args)
|
|
|
|
|
2011-09-23 13:34:42 +04:00
|
|
|
#include "ft2build.h"
|
|
|
|
#include FT_FREETYPE_H
|
2011-09-23 15:16:13 +04:00
|
|
|
#include FT_TRUETYPE_TAGS_H
|
|
|
|
#include FT_TRUETYPE_TABLES_H
|
2018-01-31 20:07:49 +03:00
|
|
|
#include FT_MULTIPLE_MASTERS_H
|
2011-09-23 15:16:13 +04:00
|
|
|
#include "cairo-ft.h"
|
|
|
|
|
|
|
|
#include "gfxFT2FontList.h"
|
2011-09-23 13:34:42 +04:00
|
|
|
#include "gfxFT2Fonts.h"
|
2018-01-31 20:07:49 +03:00
|
|
|
#include "gfxFT2Utils.h"
|
2011-09-23 15:16:13 +04:00
|
|
|
#include "gfxUserFontSet.h"
|
|
|
|
#include "gfxFontUtils.h"
|
2019-07-12 18:34:04 +03:00
|
|
|
#include "SharedFontList-impl.h"
|
2020-02-12 21:34:33 +03:00
|
|
|
#include "harfbuzz/hb-ot.h" // for name ID constants
|
2011-09-23 13:34:42 +04:00
|
|
|
|
2009-10-07 18:13:40 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2016-09-28 22:42:41 +03:00
|
|
|
#include "nsIObserverService.h"
|
2009-10-07 18:13:40 +04:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
2016-09-28 22:42:41 +03:00
|
|
|
#include "nsCRT.h"
|
2009-10-07 18:13:40 +04:00
|
|
|
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsMemory.h"
|
2021-09-08 15:18:17 +03:00
|
|
|
#include "nsPresContext.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "gfxFontConstants.h"
|
2009-10-07 18:13:40 +04:00
|
|
|
|
2019-10-04 23:45:09 +03:00
|
|
|
#include "mozilla/EndianUtils.h"
|
2013-06-25 12:14:03 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-09-23 15:15:36 +04:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
2021-03-08 19:23:39 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2014-06-30 22:05:29 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
2011-09-23 15:15:36 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-05-07 14:03:08 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2022-11-18 18:41:47 +03:00
|
|
|
# include "AndroidBuild.h"
|
2020-05-07 14:03:08 +03:00
|
|
|
# include "mozilla/jni/Utils.h"
|
|
|
|
# include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2017-04-07 00:41:02 +03:00
|
|
|
using namespace mozilla::gfx;
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2015-10-30 01:34:06 +03:00
|
|
|
static LazyLogModule sFontInfoLog("fontInfoLog");
|
2009-10-07 18:13:40 +04:00
|
|
|
|
2011-09-23 15:16:13 +04:00
|
|
|
#undef LOG
|
2015-10-30 01:34:06 +03:00
|
|
|
#define LOG(args) MOZ_LOG(sFontInfoLog, mozilla::LogLevel::Debug, args)
|
|
|
|
#define LOG_ENABLED() MOZ_LOG_TEST(sFontInfoLog, mozilla::LogLevel::Debug)
|
2009-10-07 18:13:40 +04:00
|
|
|
|
2018-09-12 22:34:57 +03:00
|
|
|
static __inline void BuildKeyNameFromFontName(nsACString& aName) {
|
2009-10-07 18:13:40 +04:00
|
|
|
ToLowerCase(aName);
|
|
|
|
}
|
|
|
|
|
2013-03-04 21:03:14 +04:00
|
|
|
// Helper to access the FT_Face for a given FT2FontEntry,
|
|
|
|
// creating a temporary face if the entry does not have one yet.
|
|
|
|
// This allows us to read font names, tables, etc if necessary
|
|
|
|
// without permanently instantiating a freetype face and consuming
|
|
|
|
// memory long-term.
|
2013-06-25 12:14:03 +04:00
|
|
|
// This may fail (resulting in a null FT_Face), e.g. if it fails to
|
|
|
|
// allocate memory to uncompress a font from omnijar.
|
2019-09-16 20:07:59 +03:00
|
|
|
already_AddRefed<SharedFTFace> FT2FontEntry::GetFTFace(bool aCommit) {
|
|
|
|
if (mFTFace) {
|
2023-03-31 16:54:38 +03:00
|
|
|
// Create a new reference, and return it.
|
|
|
|
RefPtr<SharedFTFace> face(mFTFace);
|
|
|
|
return face.forget();
|
2019-09-16 20:07:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(!mFilename.IsEmpty(),
|
|
|
|
"can't use GetFTFace for fonts without a filename");
|
|
|
|
|
|
|
|
// A relative path (no initial "/") means this is a resource in
|
|
|
|
// omnijar, not an installed font on the device.
|
|
|
|
// The NS_ASSERTIONs here should never fail, as the resource must have
|
|
|
|
// been read successfully during font-list initialization or we'd never
|
|
|
|
// have created the font entry. The only legitimate runtime failure
|
|
|
|
// here would be memory allocation, in which case mFace remains null.
|
|
|
|
RefPtr<SharedFTFace> face;
|
|
|
|
if (mFilename[0] != '/') {
|
2020-09-14 20:00:53 +03:00
|
|
|
RefPtr<nsZipArchive> reader = Omnijar::GetReader(Omnijar::Type::GRE);
|
2019-09-16 20:07:59 +03:00
|
|
|
nsZipItem* item = reader->GetItem(mFilename.get());
|
|
|
|
NS_ASSERTION(item, "failed to find zip entry");
|
|
|
|
|
|
|
|
uint32_t bufSize = item->RealSize();
|
|
|
|
uint8_t* fontDataBuf = static_cast<uint8_t*>(malloc(bufSize));
|
|
|
|
if (fontDataBuf) {
|
2020-09-14 20:00:53 +03:00
|
|
|
nsZipCursor cursor(item, reader, fontDataBuf, bufSize);
|
2019-09-16 20:07:59 +03:00
|
|
|
cursor.Copy(&bufSize);
|
|
|
|
NS_ASSERTION(bufSize == item->RealSize(), "error reading bundled font");
|
|
|
|
RefPtr<FTUserFontData> ufd = new FTUserFontData(fontDataBuf, bufSize);
|
|
|
|
face = ufd->CloneFace(mFTFontIndex);
|
|
|
|
if (!face) {
|
2013-06-25 12:14:03 +04:00
|
|
|
NS_WARNING("failed to create freetype face");
|
2019-09-16 20:07:59 +03:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-16 20:07:59 +03:00
|
|
|
} else {
|
2022-11-10 20:24:11 +03:00
|
|
|
RefPtr<FTUserFontData> fd = new FTUserFontData(mFilename.get());
|
|
|
|
face = fd->CloneFace(mFTFontIndex);
|
2019-09-16 20:07:59 +03:00
|
|
|
if (!face) {
|
|
|
|
NS_WARNING("failed to create freetype face");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (FT_Err_Ok != FT_Select_Charmap(face->GetFace(), FT_ENCODING_UNICODE) &&
|
|
|
|
FT_Err_Ok !=
|
|
|
|
FT_Select_Charmap(face->GetFace(), FT_ENCODING_MS_SYMBOL)) {
|
2019-05-10 19:27:49 +03:00
|
|
|
NS_WARNING("failed to select Unicode or symbol charmap");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:07:59 +03:00
|
|
|
if (aCommit) {
|
2023-03-31 16:54:38 +03:00
|
|
|
if (mFTFace.compareExchange(nullptr, face.get())) {
|
|
|
|
// The reference we created is now owned by mFTFace.
|
|
|
|
Unused << face.forget();
|
|
|
|
} else {
|
|
|
|
// We lost a race! Just discard our new face and use the existing one.
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2023-03-31 16:54:38 +03:00
|
|
|
// Create a new reference, and return it.
|
|
|
|
face = mFTFace;
|
2019-09-16 20:07:59 +03:00
|
|
|
return face.forget();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-09-16 20:07:59 +03:00
|
|
|
FTUserFontData* FT2FontEntry::GetUserFontData() {
|
2023-03-31 16:54:38 +03:00
|
|
|
if (SharedFTFace* face = mFTFace) {
|
|
|
|
return static_cast<FTUserFontData*>(face->GetData());
|
2013-03-04 21:03:14 +04:00
|
|
|
}
|
2019-09-16 20:07:59 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2013-03-04 21:03:14 +04:00
|
|
|
|
2011-09-23 15:16:13 +04:00
|
|
|
/*
|
|
|
|
* FT2FontEntry
|
|
|
|
* gfxFontEntry subclass corresponding to a specific face that can be
|
|
|
|
* rendered by freetype. This is associated with a face index in a
|
|
|
|
* file (normally a .ttf/.otf file holding a single face, but in principle
|
|
|
|
* there could be .ttc files with multiple faces).
|
|
|
|
* The FT2FontEntry can create the necessary FT_Face on demand, and can
|
|
|
|
* then create a Cairo font_face and scaled_font for drawing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FT2FontEntry::~FT2FontEntry() {
|
2018-08-02 00:39:05 +03:00
|
|
|
if (mMMVar) {
|
2023-03-31 16:54:38 +03:00
|
|
|
SharedFTFace* face = mFTFace;
|
|
|
|
FT_Done_MM_Var(face->GetFace()->glyph->library, mMMVar);
|
|
|
|
}
|
|
|
|
if (mFTFace) {
|
|
|
|
auto face = mFTFace.exchange(nullptr);
|
|
|
|
NS_IF_RELEASE(face);
|
2019-09-16 10:44:20 +03:00
|
|
|
}
|
2011-09-23 15:16:13 +04:00
|
|
|
}
|
|
|
|
|
2017-08-01 13:25:35 +03:00
|
|
|
gfxFontEntry* FT2FontEntry::Clone() const {
|
|
|
|
MOZ_ASSERT(!IsUserFont(), "we can only clone installed fonts!");
|
|
|
|
FT2FontEntry* fe = new FT2FontEntry(Name());
|
|
|
|
fe->mFilename = mFilename;
|
|
|
|
fe->mFTFontIndex = mFTFontIndex;
|
2018-04-25 09:18:23 +03:00
|
|
|
fe->mWeightRange = mWeightRange;
|
2018-04-25 09:18:23 +03:00
|
|
|
fe->mStretchRange = mStretchRange;
|
|
|
|
fe->mStyleRange = mStyleRange;
|
2017-08-01 13:25:35 +03:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:07:59 +03:00
|
|
|
gfxFont* FT2FontEntry::CreateFontInstance(const gfxFontStyle* aStyle) {
|
|
|
|
RefPtr<SharedFTFace> face = GetFTFace(true);
|
|
|
|
if (!face) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-20 12:59:20 +03:00
|
|
|
if (face->GetFace()->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
|
|
|
|
// Resolve variations from entry (descriptor) and style (property)
|
|
|
|
AutoTArray<gfxFontVariation, 8> settings;
|
|
|
|
GetVariationsForStyle(settings, aStyle ? *aStyle : gfxFontStyle());
|
|
|
|
|
|
|
|
// If variations are present, we will not use our cached mFTFace
|
|
|
|
// but always create a new one as it will have custom variation
|
|
|
|
// coordinates applied.
|
|
|
|
if (!settings.IsEmpty()) {
|
|
|
|
// Create a separate FT_Face because we need to apply custom
|
|
|
|
// variation settings to it.
|
|
|
|
RefPtr<SharedFTFace> varFace;
|
|
|
|
if (!mFilename.IsEmpty() && mFilename[0] == '/') {
|
|
|
|
varFace =
|
|
|
|
Factory::NewSharedFTFace(nullptr, mFilename.get(), mFTFontIndex);
|
|
|
|
} else {
|
|
|
|
varFace = face->GetData()->CloneFace(mFTFontIndex);
|
|
|
|
}
|
|
|
|
if (varFace) {
|
|
|
|
gfxFT2FontBase::SetupVarCoords(GetMMVar(), settings,
|
|
|
|
varFace->GetFace());
|
|
|
|
face = std::move(varFace);
|
|
|
|
}
|
2019-09-16 20:07:59 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-15 02:42:56 +03:00
|
|
|
|
2019-10-02 00:56:30 +03:00
|
|
|
int loadFlags = gfxPlatform::GetPlatform()->FontHintingEnabled()
|
|
|
|
? FT_LOAD_DEFAULT
|
|
|
|
: (FT_LOAD_NO_AUTOHINT | FT_LOAD_NO_HINTING);
|
|
|
|
if (face->GetFace()->face_flags & FT_FACE_FLAG_TRICKY) {
|
|
|
|
loadFlags &= ~FT_LOAD_NO_AUTOHINT;
|
2013-06-25 12:14:03 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-18 21:22:09 +03:00
|
|
|
RefPtr<UnscaledFontFreeType> unscaledFont(mUnscaledFont);
|
2017-04-07 00:41:02 +03:00
|
|
|
if (!unscaledFont) {
|
2023-03-31 16:54:38 +03:00
|
|
|
RefPtr<SharedFTFace> origFace(mFTFace);
|
|
|
|
unscaledFont =
|
|
|
|
!mFilename.IsEmpty() && mFilename[0] == '/'
|
|
|
|
? new UnscaledFontFreeType(mFilename.BeginReading(), mFTFontIndex,
|
|
|
|
std::move(origFace))
|
|
|
|
: new UnscaledFontFreeType(std::move(origFace));
|
2017-04-07 00:41:02 +03:00
|
|
|
mUnscaledFont = unscaledFont;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-09-16 20:07:59 +03:00
|
|
|
gfxFont* font =
|
2019-10-02 00:56:30 +03:00
|
|
|
new gfxFT2Font(unscaledFont, std::move(face), this, aStyle, loadFlags);
|
2011-09-23 15:16:13 +04:00
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
2018-09-12 22:34:57 +03:00
|
|
|
FT2FontEntry* FT2FontEntry::CreateFontEntry(
|
|
|
|
const nsACString& aFontName, WeightRange aWeight, StretchRange aStretch,
|
2014-09-08 11:23:19 +04:00
|
|
|
SlantStyleRange aStyle, const uint8_t* aFontData, uint32_t aLength) {
|
2011-09-23 15:16:13 +04:00
|
|
|
// Ownership of aFontData is passed in here; the fontEntry must
|
|
|
|
// retain it as long as the FT_Face needs it, and ensure it is
|
|
|
|
// eventually deleted.
|
2019-09-16 20:07:59 +03:00
|
|
|
RefPtr<FTUserFontData> ufd = new FTUserFontData(aFontData, aLength);
|
|
|
|
RefPtr<SharedFTFace> face = ufd->CloneFace();
|
2017-05-18 04:56:24 +03:00
|
|
|
if (!face) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-05-22 01:09:54 +04:00
|
|
|
}
|
2014-09-08 11:23:20 +04:00
|
|
|
// Create our FT2FontEntry, which inherits the name of the userfont entry
|
2012-05-24 12:15:54 +04:00
|
|
|
// as it's not guaranteed that the face has valid names (bug 737315)
|
2019-09-16 20:07:59 +03:00
|
|
|
FT2FontEntry* fe =
|
2020-02-12 21:34:33 +03:00
|
|
|
FT2FontEntry::CreateFontEntry(aFontName, nullptr, 0, nullptr);
|
2011-09-23 15:16:13 +04:00
|
|
|
if (fe) {
|
2023-03-31 16:54:38 +03:00
|
|
|
fe->mFTFace = face.forget().take(); // mFTFace takes ownership.
|
2018-04-25 09:18:23 +03:00
|
|
|
fe->mStyleRange = aStyle;
|
|
|
|
fe->mWeightRange = aWeight;
|
|
|
|
fe->mStretchRange = aStretch;
|
2014-09-08 11:23:20 +04:00
|
|
|
fe->mIsDataUserFont = true;
|
2011-09-23 15:16:13 +04:00
|
|
|
}
|
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
FT2FontEntry* FT2FontEntry::CreateFontEntry(const FontListEntry& aFLE) {
|
|
|
|
FT2FontEntry* fe = new FT2FontEntry(aFLE.faceName());
|
|
|
|
fe->mFilename = aFLE.filepath();
|
|
|
|
fe->mFTFontIndex = aFLE.index();
|
2018-04-25 09:18:23 +03:00
|
|
|
fe->mWeightRange = WeightRange::FromScalar(aFLE.weightRange());
|
|
|
|
fe->mStretchRange = StretchRange::FromScalar(aFLE.stretchRange());
|
|
|
|
fe->mStyleRange = SlantStyleRange::FromScalar(aFLE.styleRange());
|
2011-09-23 15:16:13 +04:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
// Extract font entry properties from an hb_face_t
|
|
|
|
static void SetPropertiesFromFace(gfxFontEntry* aFontEntry,
|
|
|
|
const hb_face_t* aFace) {
|
|
|
|
// OS2 width class to CSS 'stretch' mapping from
|
|
|
|
// https://docs.microsoft.com/en-gb/typography/opentype/spec/os2#uswidthclass
|
|
|
|
const float kOS2WidthToStretch[] = {
|
|
|
|
100, // (invalid, treat as normal)
|
|
|
|
50, // Ultra-condensed
|
|
|
|
62.5, // Extra-condensed
|
|
|
|
75, // Condensed
|
|
|
|
87.5, // Semi-condensed
|
|
|
|
100, // Normal
|
|
|
|
112.5, // Semi-expanded
|
|
|
|
125, // Expanded
|
|
|
|
150, // Extra-expanded
|
|
|
|
200 // Ultra-expanded
|
|
|
|
};
|
2011-09-23 15:16:13 +04:00
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
// Get the macStyle field from the 'head' table
|
2022-08-22 16:36:16 +03:00
|
|
|
gfxFontUtils::AutoHBBlob headBlob(
|
|
|
|
hb_face_reference_table(aFace, HB_TAG('h', 'e', 'a', 'd')));
|
2020-02-12 21:34:33 +03:00
|
|
|
unsigned int len;
|
2022-08-22 16:36:16 +03:00
|
|
|
const char* data = hb_blob_get_data(headBlob, &len);
|
2020-02-12 21:34:33 +03:00
|
|
|
uint16_t style = 0;
|
|
|
|
if (len >= sizeof(HeadTable)) {
|
|
|
|
const HeadTable* head = reinterpret_cast<const HeadTable*>(data);
|
|
|
|
style = head->macStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the OS/2 table for weight & width fields
|
2022-08-22 16:36:16 +03:00
|
|
|
gfxFontUtils::AutoHBBlob os2blob(
|
|
|
|
hb_face_reference_table(aFace, HB_TAG('O', 'S', '/', '2')));
|
|
|
|
data = hb_blob_get_data(os2blob, &len);
|
2020-02-12 21:34:33 +03:00
|
|
|
uint16_t os2weight = 400;
|
|
|
|
float stretch = 100.0;
|
|
|
|
if (len >= offsetof(OS2Table, fsType)) {
|
|
|
|
const OS2Table* os2 = reinterpret_cast<const OS2Table*>(data);
|
|
|
|
os2weight = os2->usWeightClass;
|
|
|
|
uint16_t os2width = os2->usWidthClass;
|
|
|
|
if (os2width < ArrayLength(kOS2WidthToStretch)) {
|
|
|
|
stretch = kOS2WidthToStretch[os2width];
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-03-04 21:03:11 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
aFontEntry->mStyleRange = SlantStyleRange(
|
2022-06-13 03:59:23 +03:00
|
|
|
(style & 2) ? FontSlantStyle::ITALIC : FontSlantStyle::NORMAL);
|
|
|
|
aFontEntry->mWeightRange = WeightRange(FontWeight::FromInt(int(os2weight)));
|
|
|
|
aFontEntry->mStretchRange = StretchRange(FontStretch::FromFloat(stretch));
|
2022-03-08 19:02:41 +03:00
|
|
|
|
|
|
|
// For variable fonts, update the style/weight/stretch attributes if the
|
|
|
|
// corresponding variation axes are present.
|
|
|
|
aFontEntry->SetupVariationRanges();
|
2013-03-04 21:03:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used to create the font entry for installed faces on the device,
|
|
|
|
// when iterating over the fonts directories.
|
2020-02-12 21:34:33 +03:00
|
|
|
// We use the hb_face_t to retrieve the details needed for the font entry,
|
|
|
|
// but do *not* save a reference to it, nor create a cairo face.
|
2013-03-04 21:03:11 +04:00
|
|
|
/* static */
|
2020-02-12 21:34:33 +03:00
|
|
|
FT2FontEntry* FT2FontEntry::CreateFontEntry(const nsACString& aName,
|
2019-09-16 20:07:59 +03:00
|
|
|
const char* aFilename,
|
|
|
|
uint8_t aIndex,
|
2020-02-12 21:34:33 +03:00
|
|
|
const hb_face_t* aFace) {
|
2013-03-04 21:03:11 +04:00
|
|
|
FT2FontEntry* fe = new FT2FontEntry(aName);
|
|
|
|
fe->mFilename = aFilename;
|
|
|
|
fe->mFTFontIndex = aIndex;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
if (aFace) {
|
|
|
|
SetPropertiesFromFace(fe, aFace);
|
|
|
|
} else {
|
|
|
|
// If nullptr is passed for aFace, the caller is intending to override
|
|
|
|
// these attributes anyway. We just set defaults here to be safe.
|
2022-06-13 03:59:23 +03:00
|
|
|
fe->mStyleRange = SlantStyleRange(FontSlantStyle::NORMAL);
|
|
|
|
fe->mWeightRange = WeightRange(FontWeight::NORMAL);
|
|
|
|
fe->mStretchRange = StretchRange(FontStretch::NORMAL);
|
2012-05-24 12:15:54 +04:00
|
|
|
}
|
2020-02-12 21:34:33 +03:00
|
|
|
|
|
|
|
return fe;
|
2012-05-24 12:15:54 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 15:16:13 +04:00
|
|
|
FT2FontEntry* gfxFT2Font::GetFontEntry() {
|
|
|
|
return static_cast<FT2FontEntry*>(mFontEntry.get());
|
|
|
|
}
|
|
|
|
|
2014-05-05 22:59:55 +04:00
|
|
|
// Copied/modified from similar code in gfxMacPlatformFontList.mm:
|
|
|
|
// Complex scripts will not render correctly unless Graphite or OT
|
|
|
|
// layout tables are present.
|
|
|
|
// For OpenType, we also check that the GSUB table supports the relevant
|
|
|
|
// script tag, to avoid using things like Arial Unicode MS for Lao (it has
|
|
|
|
// the characters, but lacks OpenType support).
|
|
|
|
|
|
|
|
// TODO: consider whether we should move this to gfxFontEntry and do similar
|
|
|
|
// cmap-masking on all platforms to avoid using fonts that won't shape
|
|
|
|
// properly.
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
nsresult FT2FontEntry::ReadCMAP(FontInfoData* aFontInfoData) {
|
2020-08-26 17:33:56 +03:00
|
|
|
if (mCharacterMap || mShmemCharacterMap) {
|
2011-09-23 15:16:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxCharacterMap> charmap = new gfxCharacterMap();
|
2011-09-23 15:16:13 +04:00
|
|
|
|
2020-02-09 07:18:22 +03:00
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
2022-04-08 20:06:36 +03:00
|
|
|
uint32_t uvsOffset = 0;
|
2022-08-22 16:36:16 +03:00
|
|
|
gfxFontUtils::AutoHBBlob cmapBlob(GetFontTable(TTAG_cmap));
|
|
|
|
if (cmapBlob) {
|
2020-02-09 07:18:22 +03:00
|
|
|
unsigned int length;
|
|
|
|
const char* data = hb_blob_get_data(cmapBlob, &length);
|
|
|
|
rv = gfxFontUtils::ReadCMAP((const uint8_t*)data, length, *charmap,
|
2022-04-08 20:06:36 +03:00
|
|
|
uvsOffset);
|
2014-05-05 22:59:55 +04:00
|
|
|
}
|
2022-04-08 20:06:36 +03:00
|
|
|
mUVSOffset.exchange(uvsOffset);
|
2014-05-05 22:59:55 +04:00
|
|
|
|
2014-06-02 15:46:49 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mIsDataUserFont && !HasGraphiteTables()) {
|
|
|
|
// For downloadable fonts, trust the author and don't
|
|
|
|
// try to munge the cmap based on script shaping support.
|
|
|
|
|
2012-04-19 03:59:43 +04:00
|
|
|
// We also assume a Graphite font knows what it's doing,
|
|
|
|
// and provides whatever shaping is needed for the
|
|
|
|
// characters it supports, so only check/clear the
|
|
|
|
// complex-script ranges for non-Graphite fonts
|
2011-09-23 15:16:13 +04:00
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
// for layout support, check for the presence of opentype layout tables
|
|
|
|
bool hasGSUB = HasFontTable(TRUETYPE_TAG('G', 'S', 'U', 'B'));
|
2011-09-23 15:16:13 +04:00
|
|
|
|
2013-03-04 21:03:14 +04:00
|
|
|
for (const ScriptRange* sr = gfxPlatformFontList::sComplexScriptRanges;
|
|
|
|
sr->rangeStart; sr++) {
|
2012-05-24 12:16:01 +04:00
|
|
|
// check to see if the cmap includes complex script codepoints
|
|
|
|
if (charmap->TestRange(sr->rangeStart, sr->rangeEnd)) {
|
|
|
|
// We check for GSUB here, as GPOS alone would not be ok.
|
2018-12-08 16:44:55 +03:00
|
|
|
if (hasGSUB && SupportsScriptInGSUB(sr->tags, sr->numTags)) {
|
2012-05-24 12:16:01 +04:00
|
|
|
continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-05-24 12:16:01 +04:00
|
|
|
charmap->ClearRange(sr->rangeStart, sr->rangeEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-05-24 12:16:01 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-09-23 15:16:13 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
2013-03-04 21:03:14 +04:00
|
|
|
// Hack for the SamsungDevanagari font, bug 1012365:
|
2012-05-24 12:16:01 +04:00
|
|
|
// pretend the font supports U+0972.
|
2014-06-02 15:46:49 +04:00
|
|
|
if (!charmap->test(0x0972) && charmap->test(0x0905) &&
|
2012-05-24 12:16:01 +04:00
|
|
|
charmap->test(0x0945)) {
|
|
|
|
charmap->set(0x0972);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
#endif
|
2011-09-23 15:16:13 +04:00
|
|
|
|
2022-04-08 20:06:36 +03:00
|
|
|
bool setCharMap = true;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-04-19 03:59:43 +04:00
|
|
|
gfxPlatformFontList* pfl = gfxPlatformFontList::PlatformFontList();
|
2020-08-26 17:33:56 +03:00
|
|
|
fontlist::FontList* sharedFontList = pfl->SharedFontList();
|
|
|
|
if (!IsUserFont() && mShmemFace) {
|
|
|
|
mShmemFace->SetCharacterMap(sharedFontList, charmap); // async
|
2022-04-08 20:06:36 +03:00
|
|
|
if (TrySetShmemCharacterMap()) {
|
|
|
|
setCharMap = false;
|
2020-08-26 17:33:56 +03:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-08 20:06:36 +03:00
|
|
|
charmap = pfl->FindCharMap(charmap);
|
2020-08-26 17:33:56 +03:00
|
|
|
}
|
2022-04-08 20:06:36 +03:00
|
|
|
mHasCmapTable = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2012-04-19 03:59:43 +04:00
|
|
|
// if error occurred, initialize to null cmap
|
2022-04-08 20:06:36 +03:00
|
|
|
charmap = new gfxCharacterMap();
|
|
|
|
mHasCmapTable = false;
|
|
|
|
}
|
|
|
|
if (setCharMap) {
|
|
|
|
if (mCharacterMap.compareExchange(nullptr, charmap.get())) {
|
2022-07-21 17:58:13 +03:00
|
|
|
// We forget rather than addref because we don't use the charmap below.
|
2022-04-08 20:06:36 +03:00
|
|
|
Unused << charmap.forget();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2020-08-26 17:33:56 +03:00
|
|
|
|
2011-09-23 15:16:13 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-12-02 20:13:59 +03:00
|
|
|
hb_face_t* FT2FontEntry::CreateHBFace() const {
|
|
|
|
hb_face_t* result = nullptr;
|
|
|
|
|
|
|
|
if (mFilename[0] == '/') {
|
|
|
|
// An absolute path means a normal file in the filesystem, so we can use
|
|
|
|
// hb_blob_create_from_file to read it.
|
2022-08-22 16:36:16 +03:00
|
|
|
gfxFontUtils::AutoHBBlob fileBlob(
|
|
|
|
hb_blob_create_from_file(mFilename.get()));
|
2020-12-02 20:13:59 +03:00
|
|
|
if (hb_blob_get_length(fileBlob) > 0) {
|
|
|
|
result = hb_face_create(fileBlob, mFTFontIndex);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// A relative path means an omnijar resource, which we may need to
|
|
|
|
// decompress to a temporary buffer.
|
|
|
|
RefPtr<nsZipArchive> reader = Omnijar::GetReader(Omnijar::Type::GRE);
|
|
|
|
nsZipItem* item = reader->GetItem(mFilename.get());
|
|
|
|
MOZ_ASSERT(item, "failed to find zip entry");
|
|
|
|
if (item) {
|
|
|
|
// TODO(jfkthame):
|
|
|
|
// Check whether the item is compressed; if not, we could just get a
|
|
|
|
// pointer without needing to allocate a buffer and copy the data.
|
|
|
|
// (Currently this configuration isn't used for Gecko on Android.)
|
|
|
|
uint32_t length = item->RealSize();
|
|
|
|
uint8_t* buffer = static_cast<uint8_t*>(malloc(length));
|
|
|
|
if (buffer) {
|
|
|
|
nsZipCursor cursor(item, reader, buffer, length);
|
|
|
|
cursor.Copy(&length);
|
|
|
|
MOZ_ASSERT(length == item->RealSize(), "error reading font");
|
|
|
|
if (length == item->RealSize()) {
|
2022-08-22 16:36:16 +03:00
|
|
|
gfxFontUtils::AutoHBBlob blob(
|
2020-12-02 20:13:59 +03:00
|
|
|
hb_blob_create((const char*)buffer, length,
|
2022-08-22 16:36:16 +03:00
|
|
|
HB_MEMORY_MODE_READONLY, buffer, free));
|
2020-12-02 20:13:59 +03:00
|
|
|
result = hb_face_create(blob, mFTFontIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-30 14:56:27 +03:00
|
|
|
bool FT2FontEntry::HasFontTable(uint32_t aTableTag) {
|
2023-03-31 16:54:38 +03:00
|
|
|
{
|
|
|
|
AutoReadLock lock(mLock);
|
|
|
|
if (mAvailableTables.Count() > 0) {
|
|
|
|
return mAvailableTables.Contains(aTableTag);
|
|
|
|
}
|
2020-12-02 20:13:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't created a FreeType face already, try to avoid that by
|
|
|
|
// reading the available table tags via harfbuzz and caching in a hashset.
|
2023-03-31 16:54:38 +03:00
|
|
|
AutoWriteLock lock(mLock);
|
2020-12-02 20:13:59 +03:00
|
|
|
if (!mFTFace && !mFilename.IsEmpty()) {
|
|
|
|
hb_face_t* face = CreateHBFace();
|
|
|
|
if (face) {
|
|
|
|
// Read table tags in batches; 32 should be enough for most fonts in a
|
|
|
|
// single operation.
|
|
|
|
const unsigned TAG_BUF_LENGTH = 32;
|
|
|
|
hb_tag_t tags[TAG_BUF_LENGTH];
|
|
|
|
unsigned int startOffset = 0;
|
|
|
|
unsigned int totalTables = 0;
|
|
|
|
do {
|
|
|
|
unsigned int count = TAG_BUF_LENGTH;
|
|
|
|
// Updates count to the number of table tags actually retrieved
|
|
|
|
totalTables = hb_face_get_table_tags(face, startOffset, &count, tags);
|
|
|
|
startOffset += count;
|
|
|
|
while (count-- > 0) {
|
2021-03-23 13:36:36 +03:00
|
|
|
mAvailableTables.Insert(tags[count]);
|
2020-12-02 20:13:59 +03:00
|
|
|
}
|
|
|
|
} while (startOffset < totalTables);
|
|
|
|
hb_face_destroy(face);
|
|
|
|
} else {
|
|
|
|
// Failed to create the HarfBuzz face! The font is probably broken.
|
|
|
|
// Put a dummy entry in mAvailableTables so that we don't bother
|
|
|
|
// re-trying here.
|
2021-03-23 13:36:36 +03:00
|
|
|
mAvailableTables.Insert(uint32_t(-1));
|
2020-12-02 20:13:59 +03:00
|
|
|
}
|
2021-03-23 13:36:36 +03:00
|
|
|
return mAvailableTables.Contains(aTableTag);
|
2020-12-02 20:13:59 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 14:56:27 +03:00
|
|
|
RefPtr<SharedFTFace> face = GetFTFace();
|
2020-10-30 18:06:01 +03:00
|
|
|
return gfxFT2FontEntryBase::FaceHasTable(face, aTableTag);
|
2020-10-30 14:56:27 +03:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
nsresult FT2FontEntry::CopyFontTable(uint32_t aTableTag,
|
|
|
|
nsTArray<uint8_t>& aBuffer) {
|
2019-09-16 20:07:59 +03:00
|
|
|
RefPtr<SharedFTFace> face = GetFTFace();
|
2020-10-30 18:06:01 +03:00
|
|
|
return gfxFT2FontEntryBase::CopyFaceTable(face, aTableTag, aBuffer);
|
2013-06-25 12:14:03 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 17:06:45 +03:00
|
|
|
hb_blob_t* FT2FontEntry::GetFontTable(uint32_t aTableTag) {
|
2019-09-16 20:07:59 +03:00
|
|
|
if (FTUserFontData* userFontData = GetUserFontData()) {
|
2020-02-09 07:18:22 +03:00
|
|
|
// If there's a cairo font face, we may be able to return a blob
|
2013-06-25 12:14:03 +04:00
|
|
|
// that just wraps a range of the attached user font data
|
2019-09-16 20:07:59 +03:00
|
|
|
if (userFontData->FontData()) {
|
2016-08-19 15:57:05 +03:00
|
|
|
return gfxFontUtils::GetTableFromFontData(userFontData->FontData(),
|
2013-03-04 21:03:14 +04:00
|
|
|
aTableTag);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-09 07:18:22 +03:00
|
|
|
// If the FT_Face hasn't been instantiated, try to read table directly
|
|
|
|
// via harfbuzz API to avoid expensive FT_Face creation.
|
|
|
|
if (!mFTFace && !mFilename.IsEmpty()) {
|
2020-12-02 20:13:59 +03:00
|
|
|
hb_face_t* face = CreateHBFace();
|
|
|
|
if (face) {
|
|
|
|
hb_blob_t* result = hb_face_reference_table(face, aTableTag);
|
|
|
|
hb_face_destroy(face);
|
2020-02-09 07:18:22 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, use the default method (which in turn will call our
|
|
|
|
// implementation of CopyFontTable).
|
2013-06-25 12:14:03 +04:00
|
|
|
return gfxFontEntry::GetFontTable(aTableTag);
|
|
|
|
}
|
|
|
|
|
2018-04-26 19:08:18 +03:00
|
|
|
bool FT2FontEntry::HasVariations() {
|
2023-03-31 16:54:38 +03:00
|
|
|
switch (mHasVariations) {
|
|
|
|
case HasVariationsState::No:
|
|
|
|
return false;
|
|
|
|
case HasVariationsState::Yes:
|
|
|
|
return true;
|
|
|
|
case HasVariationsState::Uninitialized:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedFTFace* face = mFTFace;
|
|
|
|
bool hasVariations =
|
|
|
|
face ? face->GetFace()->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS
|
|
|
|
: gfxPlatform::HasVariationFontSupport() &&
|
|
|
|
HasFontTable(TRUETYPE_TAG('f', 'v', 'a', 'r'));
|
|
|
|
mHasVariations =
|
|
|
|
hasVariations ? HasVariationsState::Yes : HasVariationsState::No;
|
|
|
|
return hasVariations;
|
2018-04-26 19:08:18 +03:00
|
|
|
}
|
|
|
|
|
2018-04-26 19:08:18 +03:00
|
|
|
void FT2FontEntry::GetVariationAxes(nsTArray<gfxFontVariationAxis>& aAxes) {
|
|
|
|
if (!HasVariations()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-31 16:54:38 +03:00
|
|
|
if (FT_MM_Var* mmVar = GetMMVar()) {
|
|
|
|
gfxFT2Utils::GetVariationAxes(mmVar, aAxes);
|
2018-04-26 19:08:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FT2FontEntry::GetVariationInstances(
|
|
|
|
nsTArray<gfxFontVariationInstance>& aInstances) {
|
|
|
|
if (!HasVariations()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-31 16:54:38 +03:00
|
|
|
if (FT_MM_Var* mmVar = GetMMVar()) {
|
|
|
|
gfxFT2Utils::GetVariationInstances(this, mmVar, aInstances);
|
2018-08-02 00:39:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FT_MM_Var* FT2FontEntry::GetMMVar() {
|
|
|
|
if (mMMVarInitialized) {
|
|
|
|
return mMMVar;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2023-03-31 16:54:38 +03:00
|
|
|
AutoWriteLock lock(mLock);
|
2019-09-16 20:07:59 +03:00
|
|
|
RefPtr<SharedFTFace> face = GetFTFace(true);
|
2013-03-04 21:03:14 +04:00
|
|
|
if (!face) {
|
2018-08-02 00:39:05 +03:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2019-09-16 20:07:59 +03:00
|
|
|
if (FT_Err_Ok != FT_Get_MM_Var(face->GetFace(), &mMMVar)) {
|
2018-08-02 00:39:05 +03:00
|
|
|
mMMVar = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2023-03-31 16:54:38 +03:00
|
|
|
mMMVarInitialized = true;
|
2018-08-02 00:39:05 +03:00
|
|
|
return mMMVar;
|
2018-04-26 19:08:18 +03:00
|
|
|
}
|
|
|
|
|
2013-10-15 06:19:47 +04:00
|
|
|
void FT2FontEntry::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
FontListSizes* aSizes) const {
|
|
|
|
gfxFontEntry::AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2012-03-28 01:38:39 +04:00
|
|
|
aSizes->mFontListSize +=
|
|
|
|
mFilename.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-10-15 06:19:47 +04:00
|
|
|
void FT2FontEntry::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
|
|
|
|
FontListSizes* aSizes) const {
|
2012-03-28 01:38:39 +04:00
|
|
|
aSizes->mFontListSize += aMallocSizeOf(this);
|
2013-10-15 06:19:47 +04:00
|
|
|
AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
|
2012-03-28 01:38:39 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 15:16:13 +04:00
|
|
|
/*
|
|
|
|
* FT2FontFamily
|
|
|
|
* A standard gfxFontFamily; just adds a method used to support sending
|
|
|
|
* the font list from chrome to content via IPC.
|
|
|
|
*/
|
|
|
|
|
2019-07-10 06:29:40 +03:00
|
|
|
void FT2FontFamily::AddFacesToFontList(nsTArray<FontListEntry>* aFontList) {
|
2022-04-19 17:20:21 +03:00
|
|
|
AutoReadLock lock(mLock);
|
2011-09-23 15:16:13 +04:00
|
|
|
for (int i = 0, n = mAvailableFonts.Length(); i < n; ++i) {
|
|
|
|
const FT2FontEntry* fe =
|
|
|
|
static_cast<const FT2FontEntry*>(mAvailableFonts[i].get());
|
|
|
|
if (!fe) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-04-02 15:36:24 +03:00
|
|
|
aFontList->AppendElement(FontListEntry(
|
|
|
|
Name(), fe->Name(), fe->mFilename, fe->Weight().AsScalar(),
|
|
|
|
fe->Stretch().AsScalar(), fe->SlantStyle().AsScalar(), fe->mFTFontIndex,
|
|
|
|
Visibility()));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-09-23 15:16:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Startup cache support for the font list:
|
|
|
|
* We store the list of families and faces, with their style attributes and the
|
|
|
|
* corresponding font files, in the startup cache.
|
|
|
|
* This allows us to recreate the gfxFT2FontList collection of families and
|
|
|
|
* faces without instantiating Freetype faces for each font file (in order to
|
|
|
|
* find their attributes), leading to significantly quicker startup.
|
|
|
|
*/
|
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
#define CACHE_KEY "font.cached-list"
|
|
|
|
|
2019-07-12 18:34:30 +03:00
|
|
|
void gfxFT2FontList::CollectInitData(const FontListEntry& aFLE,
|
|
|
|
const nsCString& aPSName,
|
|
|
|
const nsCString& aFullName,
|
2019-07-12 18:34:04 +03:00
|
|
|
StandardFile aStdFile) {
|
2019-07-12 18:34:30 +03:00
|
|
|
nsAutoCString key(aFLE.familyName());
|
|
|
|
BuildKeyNameFromFontName(key);
|
2021-03-01 12:59:29 +03:00
|
|
|
mFaceInitData
|
|
|
|
.LookupOrInsertWith(
|
|
|
|
key,
|
|
|
|
[&] {
|
|
|
|
mFamilyInitData.AppendElement(
|
|
|
|
fontlist::Family::InitData{key, aFLE.familyName()});
|
|
|
|
return MakeUnique<nsTArray<fontlist::Face::InitData>>();
|
|
|
|
})
|
|
|
|
->AppendElement(fontlist::Face::InitData{
|
|
|
|
aFLE.filepath(), aFLE.index(), false,
|
|
|
|
WeightRange::FromScalar(aFLE.weightRange()),
|
|
|
|
StretchRange::FromScalar(aFLE.stretchRange()),
|
|
|
|
SlantStyleRange::FromScalar(aFLE.styleRange())});
|
2019-07-12 18:34:30 +03:00
|
|
|
nsAutoCString psname(aPSName), fullname(aFullName);
|
|
|
|
if (!psname.IsEmpty()) {
|
|
|
|
ToLowerCase(psname);
|
2021-02-26 12:11:46 +03:00
|
|
|
mLocalNameTable.InsertOrUpdate(
|
|
|
|
psname, fontlist::LocalFaceRec::InitData(key, aFLE.filepath()));
|
2019-07-12 18:34:30 +03:00
|
|
|
}
|
|
|
|
if (!fullname.IsEmpty()) {
|
|
|
|
ToLowerCase(fullname);
|
|
|
|
if (fullname != psname) {
|
2021-02-26 12:11:46 +03:00
|
|
|
mLocalNameTable.InsertOrUpdate(
|
2020-06-12 15:07:05 +03:00
|
|
|
fullname, fontlist::LocalFaceRec::InitData(key, aFLE.filepath()));
|
2019-07-12 18:34:30 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-12 18:34:04 +03:00
|
|
|
}
|
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
class FontNameCache {
|
|
|
|
public:
|
2019-07-12 18:33:35 +03:00
|
|
|
// Delimiters used in the cached font-list records we store in startupCache
|
|
|
|
static const char kFileSep = 0x1c;
|
|
|
|
static const char kGroupSep = 0x1d;
|
|
|
|
static const char kRecordSep = 0x1e;
|
|
|
|
static const char kFieldSep = 0x1f;
|
|
|
|
|
|
|
|
// Separator for font property ranges; we only look for this within a
|
|
|
|
// field that holds a serialized FontPropertyValue or Range, so there's no
|
|
|
|
// risk of conflicting with printable characters in font names.
|
|
|
|
// Note that this must be a character that will terminate strtof() parsing
|
|
|
|
// of a number.
|
|
|
|
static const char kRangeSep = ':';
|
|
|
|
|
2016-09-28 22:42:41 +03:00
|
|
|
// Creates the object but does NOT load the cached data from the startup
|
|
|
|
// cache; call Init() after creation to do that.
|
2015-06-01 08:14:44 +03:00
|
|
|
FontNameCache() : mMap(&mOps, sizeof(FNCMapEntry), 0), mWriteNeeded(false) {
|
|
|
|
// HACK ALERT: it's weird to assign |mOps| after we passed a pointer to
|
|
|
|
// it to |mMap|'s constructor. A more normal approach here would be to
|
|
|
|
// have a static |sOps| member. Unfortunately, this mysteriously but
|
|
|
|
// consistently makes Fennec start-up slower, so we take this
|
|
|
|
// unorthodox approach instead. It's safe because PLDHashTable's
|
|
|
|
// constructor doesn't dereference the pointer; it just makes a copy of
|
|
|
|
// it.
|
2015-05-15 07:48:43 +03:00
|
|
|
mOps = (PLDHashTableOps){StringHash, HashMatchEntry, MoveEntry,
|
2015-09-15 00:23:47 +03:00
|
|
|
PLDHashTable::ClearEntryStub, nullptr};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-04 04:29:00 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess(),
|
2016-09-28 22:42:41 +03:00
|
|
|
"FontNameCache should only be used in chrome process");
|
2011-09-23 15:15:36 +04:00
|
|
|
mCache = mozilla::scache::StartupCache::GetSingleton();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-25 04:38:47 +03:00
|
|
|
~FontNameCache() { WriteCache(); }
|
|
|
|
|
2019-07-12 18:34:04 +03:00
|
|
|
size_t EntryCount() const { return mMap.EntryCount(); }
|
|
|
|
|
|
|
|
void DropStaleEntries() {
|
2021-03-17 18:49:46 +03:00
|
|
|
for (auto iter = mMap.ConstIter(); !iter.Done(); iter.Next()) {
|
2019-07-12 18:34:04 +03:00
|
|
|
auto entry = static_cast<FNCMapEntry*>(iter.Get());
|
|
|
|
if (!entry->mFileExists) {
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:38:47 +03:00
|
|
|
void WriteCache() {
|
2011-09-23 15:15:36 +04:00
|
|
|
if (!mWriteNeeded || !mCache) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-25 04:38:47 +03:00
|
|
|
LOG(("Writing FontNameCache:"));
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString buf;
|
2021-03-17 18:49:46 +03:00
|
|
|
for (auto iter = mMap.ConstIter(); !iter.Done(); iter.Next()) {
|
2015-06-10 23:07:41 +03:00
|
|
|
auto entry = static_cast<FNCMapEntry*>(iter.Get());
|
2019-07-12 18:34:04 +03:00
|
|
|
MOZ_ASSERT(entry->mFileExists);
|
2015-06-10 23:07:41 +03:00
|
|
|
buf.Append(entry->mFilename);
|
2019-07-12 18:33:35 +03:00
|
|
|
buf.Append(kGroupSep);
|
2015-06-10 23:07:41 +03:00
|
|
|
buf.Append(entry->mFaces);
|
2019-07-12 18:33:35 +03:00
|
|
|
buf.Append(kGroupSep);
|
2015-06-10 23:07:41 +03:00
|
|
|
buf.AppendInt(entry->mTimestamp);
|
2019-07-12 18:33:35 +03:00
|
|
|
buf.Append(kGroupSep);
|
2015-06-10 23:07:41 +03:00
|
|
|
buf.AppendInt(entry->mFilesize);
|
2019-07-12 18:33:35 +03:00
|
|
|
buf.Append(kFileSep);
|
2015-06-10 23:07:41 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-12-14 00:47:56 +03:00
|
|
|
LOG(("putting FontNameCache to " CACHE_KEY ", length %zu",
|
2019-05-25 04:38:47 +03:00
|
|
|
buf.Length() + 1));
|
2022-12-01 13:46:34 +03:00
|
|
|
mCache->PutBuffer(CACHE_KEY, UniqueFreePtr<char[]>(ToNewCString(buf)),
|
2018-02-17 02:30:47 +03:00
|
|
|
buf.Length() + 1);
|
2019-05-25 04:38:47 +03:00
|
|
|
mWriteNeeded = false;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-28 22:42:41 +03:00
|
|
|
// This may be called more than once (if we re-load the font list).
|
2011-09-23 15:15:36 +04:00
|
|
|
void Init() {
|
2015-05-21 07:23:55 +03:00
|
|
|
if (!mCache) {
|
2011-09-23 15:15:36 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size;
|
2019-10-04 23:45:09 +03:00
|
|
|
const char* cur;
|
|
|
|
if (NS_FAILED(mCache->GetBuffer(CACHE_KEY, &cur, &size))) {
|
2019-05-25 04:38:47 +03:00
|
|
|
LOG(("no cache of " CACHE_KEY));
|
2011-09-23 15:15:36 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-25 04:38:47 +03:00
|
|
|
LOG(("got: %u bytes from the cache " CACHE_KEY, size));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-28 22:42:41 +03:00
|
|
|
mMap.Clear();
|
|
|
|
mWriteNeeded = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
while (const char* fileEnd = strchr(cur, kFileSep)) {
|
|
|
|
// The cached record for one file is at [cur, fileEnd].
|
|
|
|
|
|
|
|
// Find end of field that starts at aStart, terminated by kGroupSep or
|
|
|
|
// end of record.
|
|
|
|
auto endOfField = [=](const char* aStart) -> const char* {
|
|
|
|
MOZ_ASSERT(aStart <= fileEnd);
|
|
|
|
const char* end = static_cast<const char*>(
|
|
|
|
memchr(aStart, kGroupSep, fileEnd - aStart));
|
|
|
|
if (end) {
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
return fileEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Advance aStart and aEnd to indicate the range of the next field and
|
|
|
|
// return true, or just return false if already at end of record.
|
|
|
|
auto nextField = [=](const char*& aStart, const char*& aEnd) -> bool {
|
|
|
|
if (aEnd < fileEnd) {
|
|
|
|
aStart = aEnd + 1;
|
|
|
|
aEnd = endOfField(aStart);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* end = endOfField(cur);
|
|
|
|
nsCString filename(cur, end - cur);
|
|
|
|
if (!nextField(cur, end)) {
|
2011-09-23 15:15:36 +04:00
|
|
|
break;
|
|
|
|
}
|
2019-07-12 18:33:35 +03:00
|
|
|
nsCString faceList(cur, end - cur);
|
|
|
|
if (!nextField(cur, end)) {
|
2011-09-23 15:15:36 +04:00
|
|
|
break;
|
|
|
|
}
|
2019-07-12 18:33:35 +03:00
|
|
|
uint32_t timestamp = strtoul(cur, nullptr, 10);
|
|
|
|
if (!nextField(cur, end)) {
|
2011-09-23 15:15:36 +04:00
|
|
|
break;
|
|
|
|
}
|
2019-07-12 18:33:35 +03:00
|
|
|
uint32_t filesize = strtoul(cur, nullptr, 10);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-15 00:23:12 +03:00
|
|
|
auto mapEntry =
|
|
|
|
static_cast<FNCMapEntry*>(mMap.Add(filename.get(), fallible));
|
2011-09-23 15:15:36 +04:00
|
|
|
if (mapEntry) {
|
|
|
|
mapEntry->mFilename.Assign(filename);
|
|
|
|
mapEntry->mTimestamp = timestamp;
|
|
|
|
mapEntry->mFilesize = filesize;
|
|
|
|
mapEntry->mFaces.Assign(faceList);
|
|
|
|
// entries from the startupcache are marked "non-existing"
|
|
|
|
// until we have confirmed that the file still exists
|
2011-10-17 18:59:28 +04:00
|
|
|
mapEntry->mFileExists = false;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
cur = fileEnd + 1;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
void GetInfoForFile(const nsCString& aFileName, nsCString& aFaceList,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t* aTimestamp, uint32_t* aFilesize) {
|
2015-05-21 10:34:25 +03:00
|
|
|
auto entry = static_cast<FNCMapEntry*>(mMap.Search(aFileName.get()));
|
2015-01-27 01:25:13 +03:00
|
|
|
if (entry) {
|
2011-09-23 15:15:36 +04:00
|
|
|
*aTimestamp = entry->mTimestamp;
|
|
|
|
*aFilesize = entry->mFilesize;
|
|
|
|
aFaceList.Assign(entry->mFaces);
|
|
|
|
// this entry does correspond to an existing file
|
|
|
|
// (although it might not be up-to-date, in which case
|
|
|
|
// it will get overwritten via CacheFileInfo)
|
2011-10-17 18:59:28 +04:00
|
|
|
entry->mFileExists = true;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
void CacheFileInfo(const nsCString& aFileName, const nsCString& aFaceList,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aTimestamp, uint32_t aFilesize) {
|
2015-09-15 00:23:12 +03:00
|
|
|
auto entry = static_cast<FNCMapEntry*>(mMap.Add(aFileName.get(), fallible));
|
2011-09-23 15:15:36 +04:00
|
|
|
if (entry) {
|
|
|
|
entry->mFilename.Assign(aFileName);
|
|
|
|
entry->mTimestamp = aTimestamp;
|
|
|
|
entry->mFilesize = aFilesize;
|
|
|
|
entry->mFaces.Assign(aFaceList);
|
2011-10-17 18:59:28 +04:00
|
|
|
entry->mFileExists = true;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mWriteNeeded = true;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
private:
|
|
|
|
mozilla::scache::StartupCache* mCache;
|
2015-05-20 02:46:17 +03:00
|
|
|
PLDHashTable mMap;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mWriteNeeded;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-15 07:48:43 +03:00
|
|
|
PLDHashTableOps mOps;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-06-26 17:34:24 +03:00
|
|
|
struct FNCMapEntry : public PLDHashEntryHdr {
|
2011-09-23 15:15:36 +04:00
|
|
|
public:
|
|
|
|
nsCString mFilename;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mTimestamp;
|
|
|
|
uint32_t mFilesize;
|
2011-09-23 15:15:36 +04:00
|
|
|
nsCString mFaces;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mFileExists;
|
2020-06-26 17:34:24 +03:00
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-03-16 07:33:44 +03:00
|
|
|
static PLDHashNumber StringHash(const void* key) {
|
2011-09-23 15:15:36 +04:00
|
|
|
return HashString(reinterpret_cast<const char*>(key));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-03-16 07:33:44 +03:00
|
|
|
static bool HashMatchEntry(const PLDHashEntryHdr* aHdr, const void* key) {
|
2011-09-23 15:15:36 +04:00
|
|
|
const FNCMapEntry* entry = static_cast<const FNCMapEntry*>(aHdr);
|
|
|
|
return entry->mFilename.Equals(reinterpret_cast<const char*>(key));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
static void MoveEntry(PLDHashTable* table, const PLDHashEntryHdr* aFrom,
|
|
|
|
PLDHashEntryHdr* aTo) {
|
|
|
|
FNCMapEntry* to = static_cast<FNCMapEntry*>(aTo);
|
|
|
|
const FNCMapEntry* from = static_cast<const FNCMapEntry*>(aFrom);
|
|
|
|
to->mFilename.Assign(from->mFilename);
|
|
|
|
to->mTimestamp = from->mTimestamp;
|
|
|
|
to->mFilesize = from->mFilesize;
|
|
|
|
to->mFaces.Assign(from->mFaces);
|
|
|
|
to->mFileExists = from->mFileExists;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-07 18:13:40 +04:00
|
|
|
/***************************************************************
|
|
|
|
*
|
|
|
|
* gfxFT2FontList
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
// For Mobile, we use gfxFT2Fonts, and we build the font list by directly
|
|
|
|
// scanning the system's Fonts directory for OpenType and TrueType files.
|
2009-10-07 18:13:40 +04:00
|
|
|
|
2016-09-28 22:42:41 +03:00
|
|
|
#define JAR_LAST_MODIFED_TIME "jar-last-modified-time"
|
|
|
|
|
|
|
|
class WillShutdownObserver : public nsIObserver {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-28 22:42:41 +03:00
|
|
|
explicit WillShutdownObserver(gfxFT2FontList* aFontList)
|
|
|
|
: mFontList(aFontList) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-11-20 02:16:24 +03:00
|
|
|
void Remove() {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID);
|
|
|
|
}
|
|
|
|
mFontList = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-11-20 02:16:24 +03:00
|
|
|
|
2016-09-28 22:42:41 +03:00
|
|
|
protected:
|
2019-04-11 15:36:51 +03:00
|
|
|
virtual ~WillShutdownObserver() = default;
|
2016-09-28 22:42:41 +03:00
|
|
|
|
|
|
|
gfxFT2FontList* mFontList;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(WillShutdownObserver, nsIObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WillShutdownObserver::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
if (!nsCRT::strcmp(aTopic, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID)) {
|
|
|
|
mFontList->WillShutdown();
|
|
|
|
} else {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected notification topic");
|
2016-09-28 22:42:41 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-10-07 18:13:40 +04:00
|
|
|
gfxFT2FontList::gfxFT2FontList() : mJarModifiedTime(0) {
|
2016-09-28 22:42:41 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
mObserver = new WillShutdownObserver(this);
|
|
|
|
obs->AddObserver(mObserver, NS_XPCOM_WILL_SHUTDOWN_OBSERVER_ID, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFT2FontList::~gfxFT2FontList() {
|
2022-05-24 18:05:05 +03:00
|
|
|
AutoLock lock(mLock);
|
2016-09-28 22:42:41 +03:00
|
|
|
if (mObserver) {
|
2018-11-20 02:16:24 +03:00
|
|
|
mObserver->Remove();
|
2016-09-28 22:42:41 +03:00
|
|
|
}
|
2009-10-07 18:13:40 +04:00
|
|
|
}
|
|
|
|
|
2019-07-12 18:34:04 +03:00
|
|
|
bool gfxFT2FontList::AppendFacesFromCachedFaceList(CollectFunc aCollectFace,
|
|
|
|
const nsCString& aFileName,
|
2014-06-30 22:05:29 +04:00
|
|
|
const nsCString& aFaceList,
|
2011-09-23 15:15:36 +04:00
|
|
|
StandardFile aStdFile) {
|
2019-07-12 18:33:35 +03:00
|
|
|
const char* start = aFaceList.get();
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
while (const char* recEnd = strchr(start, FontNameCache::kRecordSep)) {
|
|
|
|
auto endOfField = [=](const char* aStart) -> const char* {
|
|
|
|
MOZ_ASSERT(aStart <= recEnd);
|
|
|
|
const char* end = static_cast<const char*>(
|
|
|
|
memchr(aStart, FontNameCache::kFieldSep, recEnd - aStart));
|
|
|
|
if (end) {
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
return recEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto nextField = [=](const char*& aStart, const char*& aEnd) -> bool {
|
|
|
|
if (aEnd < recEnd) {
|
|
|
|
aStart = aEnd + 1;
|
|
|
|
aEnd = endOfField(aStart);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* end = endOfField(start);
|
|
|
|
nsAutoCString familyName(start, end - start);
|
2019-07-12 18:34:04 +03:00
|
|
|
nsAutoCString key(familyName);
|
|
|
|
ToLowerCase(key);
|
2019-07-09 01:07:08 +03:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
if (!nextField(start, end)) {
|
2011-09-23 15:15:36 +04:00
|
|
|
break;
|
|
|
|
}
|
2019-07-12 18:33:35 +03:00
|
|
|
nsAutoCString faceName(start, end - start);
|
2011-09-23 15:15:36 +04:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
if (!nextField(start, end)) {
|
2018-04-25 09:18:23 +03:00
|
|
|
break;
|
2012-05-25 18:03:42 +04:00
|
|
|
}
|
2019-07-12 18:33:35 +03:00
|
|
|
uint32_t index = strtoul(start, nullptr, 10);
|
2012-05-25 18:03:42 +04:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
if (!nextField(start, end)) {
|
2012-05-25 18:03:42 +04:00
|
|
|
break;
|
|
|
|
}
|
2022-06-13 03:59:23 +03:00
|
|
|
|
|
|
|
auto readIntPair = [&](int32_t& aStart, int32_t& aEnd) {
|
|
|
|
char* limit = nullptr;
|
|
|
|
aStart = strtol(start, &limit, 10);
|
|
|
|
if (*limit == FontNameCache::kRangeSep && limit + 1 < end) {
|
|
|
|
aEnd = strtof(limit + 1, nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int32_t minStyle, maxStyle;
|
|
|
|
readIntPair(minStyle, maxStyle);
|
2012-05-25 18:03:42 +04:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
if (!nextField(start, end)) {
|
2015-09-12 06:30:14 +03:00
|
|
|
break;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2022-06-13 03:59:23 +03:00
|
|
|
|
|
|
|
int32_t minWeight, maxWeight;
|
|
|
|
readIntPair(minWeight, maxWeight);
|
2011-09-23 15:15:36 +04:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
if (!nextField(start, end)) {
|
2015-09-12 06:30:14 +03:00
|
|
|
break;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2022-06-13 03:59:23 +03:00
|
|
|
|
|
|
|
int32_t minStretch, maxStretch;
|
|
|
|
readIntPair(minStretch, maxStretch);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-07-12 18:34:30 +03:00
|
|
|
if (!nextField(start, end)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsAutoCString psname(start, end - start);
|
|
|
|
|
|
|
|
if (!nextField(start, end)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsAutoCString fullname(start, end - start);
|
|
|
|
|
2020-04-02 15:36:24 +03:00
|
|
|
if (!nextField(start, end)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FontVisibility visibility = FontVisibility(strtoul(start, nullptr, 10));
|
|
|
|
|
2022-06-13 03:59:23 +03:00
|
|
|
FontListEntry fle(familyName, faceName, aFileName,
|
|
|
|
WeightRange(FontWeight::FromRaw(minWeight),
|
|
|
|
FontWeight::FromRaw(maxWeight))
|
|
|
|
.AsScalar(),
|
|
|
|
StretchRange(FontStretch::FromRaw(minStretch),
|
|
|
|
FontStretch::FromRaw(maxStretch))
|
|
|
|
.AsScalar(),
|
|
|
|
SlantStyleRange(FontSlantStyle::FromRaw(minStyle),
|
|
|
|
FontSlantStyle::FromRaw(maxStyle))
|
|
|
|
.AsScalar(),
|
|
|
|
index, visibility);
|
2019-07-12 18:34:04 +03:00
|
|
|
|
2019-07-12 18:34:30 +03:00
|
|
|
aCollectFace(fle, psname, fullname, aStdFile);
|
2019-07-12 18:33:35 +03:00
|
|
|
count++;
|
2019-07-08 22:47:03 +03:00
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
start = recEnd + 1;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2019-07-12 18:33:35 +03:00
|
|
|
|
|
|
|
return count > 0;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
|
|
|
|
2019-07-12 18:33:35 +03:00
|
|
|
void FT2FontEntry::AppendToFaceList(nsCString& aFaceList,
|
2019-07-12 18:34:30 +03:00
|
|
|
const nsACString& aFamilyName,
|
|
|
|
const nsACString& aPSName,
|
2020-04-02 15:36:24 +03:00
|
|
|
const nsACString& aFullName,
|
|
|
|
FontVisibility aVisibility) {
|
2013-06-25 12:14:03 +04:00
|
|
|
aFaceList.Append(aFamilyName);
|
2019-07-12 18:33:35 +03:00
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
|
|
|
aFaceList.Append(Name());
|
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
|
|
|
aFaceList.AppendInt(mFTFontIndex);
|
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
2022-06-13 03:59:23 +03:00
|
|
|
aFaceList.AppendInt(SlantStyle().Min().Raw());
|
2019-07-12 18:33:35 +03:00
|
|
|
aFaceList.Append(FontNameCache::kRangeSep);
|
2022-06-13 03:59:23 +03:00
|
|
|
aFaceList.AppendInt(SlantStyle().Max().Raw());
|
2019-07-12 18:33:35 +03:00
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
2022-06-13 03:59:23 +03:00
|
|
|
aFaceList.AppendInt(Weight().Min().Raw());
|
2019-07-12 18:33:35 +03:00
|
|
|
aFaceList.Append(FontNameCache::kRangeSep);
|
2022-06-13 03:59:23 +03:00
|
|
|
aFaceList.AppendInt(Weight().Max().Raw());
|
2019-07-12 18:33:35 +03:00
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
2022-06-13 03:59:23 +03:00
|
|
|
aFaceList.AppendInt(Stretch().Min().Raw());
|
2019-07-12 18:33:35 +03:00
|
|
|
aFaceList.Append(FontNameCache::kRangeSep);
|
2022-06-13 03:59:23 +03:00
|
|
|
aFaceList.AppendInt(Stretch().Max().Raw());
|
2019-07-12 18:34:30 +03:00
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
|
|
|
aFaceList.Append(aPSName);
|
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
|
|
|
aFaceList.Append(aFullName);
|
2020-04-02 15:36:24 +03:00
|
|
|
aFaceList.Append(FontNameCache::kFieldSep);
|
|
|
|
aFaceList.AppendInt(int(aVisibility));
|
2019-07-12 18:33:35 +03:00
|
|
|
aFaceList.Append(FontNameCache::kRecordSep);
|
2009-10-07 18:13:40 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
void FT2FontEntry::CheckForBrokenFont(gfxFontFamily* aFamily) {
|
2020-06-23 18:21:36 +03:00
|
|
|
// note if the family is in the "bad underline" blocklist
|
2013-06-25 12:14:03 +04:00
|
|
|
if (aFamily->IsBadUnderlineFamily()) {
|
|
|
|
mIsBadUnderlineFont = true;
|
|
|
|
}
|
2019-07-12 18:34:30 +03:00
|
|
|
nsAutoCString familyKey(aFamily->Name());
|
|
|
|
BuildKeyNameFromFontName(familyKey);
|
|
|
|
CheckForBrokenFont(familyKey);
|
2019-07-12 18:34:04 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-07-12 18:34:30 +03:00
|
|
|
void FT2FontEntry::CheckForBrokenFont(const nsACString& aFamilyKey) {
|
2013-06-25 12:14:03 +04:00
|
|
|
// bug 721719 - set the IgnoreGSUB flag on entries for Roboto
|
|
|
|
// because of unwanted on-by-default "ae" ligature.
|
|
|
|
// (See also AppendFaceFromFontListEntry.)
|
2019-07-12 18:34:30 +03:00
|
|
|
if (aFamilyKey.EqualsLiteral("roboto")) {
|
2014-03-20 10:43:31 +04:00
|
|
|
mIgnoreGSUB = true;
|
2019-07-12 18:34:04 +03:00
|
|
|
return;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-03-20 10:43:31 +04:00
|
|
|
// bug 706888 - set the IgnoreGSUB flag on the broken version of
|
2018-11-15 19:11:45 +03:00
|
|
|
// Droid Sans Arabic from certain phones, as identified by the
|
|
|
|
// font checksum in the 'head' table
|
2019-07-12 18:34:30 +03:00
|
|
|
if (aFamilyKey.EqualsLiteral("droid sans arabic")) {
|
2019-09-16 20:07:59 +03:00
|
|
|
RefPtr<SharedFTFace> face = GetFTFace();
|
2013-06-25 12:14:03 +04:00
|
|
|
if (face) {
|
2019-09-16 20:07:59 +03:00
|
|
|
const TT_Header* head = static_cast<const TT_Header*>(
|
|
|
|
FT_Get_Sfnt_Table(face->GetFace(), ft_sfnt_head));
|
2013-06-25 12:14:03 +04:00
|
|
|
if (head && head->CheckSum_Adjust == 0xe445242) {
|
2018-04-25 09:18:23 +03:00
|
|
|
mIgnoreGSUB = true;
|
2012-10-03 23:29:10 +04:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-10-03 23:29:10 +04:00
|
|
|
}
|
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
void gfxFT2FontList::AppendFacesFromBlob(
|
|
|
|
const nsCString& aFileName, StandardFile aStdFile, hb_blob_t* aBlob,
|
|
|
|
FontNameCache* aCache, uint32_t aTimestamp, uint32_t aFilesize) {
|
|
|
|
nsCString newFaceList;
|
|
|
|
uint32_t numFaces = 1;
|
|
|
|
unsigned int length;
|
|
|
|
const char* data = hb_blob_get_data(aBlob, &length);
|
|
|
|
// Check for a possible TrueType Collection
|
|
|
|
if (length >= sizeof(TTCHeader)) {
|
|
|
|
const TTCHeader* ttc = reinterpret_cast<const TTCHeader*>(data);
|
|
|
|
if (ttc->ttcTag == TRUETYPE_TAG('t', 't', 'c', 'f')) {
|
|
|
|
numFaces = ttc->numFonts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned int index = 0; index < numFaces; index++) {
|
|
|
|
hb_face_t* face = hb_face_create(aBlob, index);
|
|
|
|
if (face != hb_face_get_empty()) {
|
|
|
|
AddFaceToList(aFileName, index, aStdFile, face, newFaceList);
|
|
|
|
}
|
|
|
|
hb_face_destroy(face);
|
|
|
|
}
|
|
|
|
if (aCache && !newFaceList.IsEmpty()) {
|
|
|
|
aCache->CacheFileInfo(aFileName, newFaceList, aTimestamp, aFilesize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
void gfxFT2FontList::AppendFacesFromFontFile(const nsCString& aFileName,
|
|
|
|
FontNameCache* aCache,
|
|
|
|
StandardFile aStdFile) {
|
|
|
|
nsCString cachedFaceList;
|
|
|
|
uint32_t filesize = 0, timestamp = 0;
|
|
|
|
if (aCache) {
|
|
|
|
aCache->GetInfoForFile(aFileName, cachedFaceList, ×tamp, &filesize);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
struct stat s;
|
2013-06-25 12:14:03 +04:00
|
|
|
int statRetval = stat(aFileName.get(), &s);
|
|
|
|
if (!cachedFaceList.IsEmpty() && 0 == statRetval &&
|
|
|
|
uint32_t(s.st_mtime) == timestamp && s.st_size == filesize) {
|
2019-07-12 18:34:30 +03:00
|
|
|
CollectFunc unshared =
|
|
|
|
[](const FontListEntry& aFLE, const nsCString& aPSName,
|
|
|
|
const nsCString& aFullName, StandardFile aStdFile) {
|
2022-04-19 17:20:21 +03:00
|
|
|
auto* pfl = PlatformFontList();
|
|
|
|
pfl->mLock.AssertCurrentThreadIn();
|
|
|
|
pfl->AppendFaceFromFontListEntry(aFLE, aStdFile);
|
2019-07-12 18:34:30 +03:00
|
|
|
};
|
|
|
|
CollectFunc shared = [](const FontListEntry& aFLE, const nsCString& aPSName,
|
|
|
|
const nsCString& aFullName, StandardFile aStdFile) {
|
|
|
|
PlatformFontList()->CollectInitData(aFLE, aPSName, aFullName, aStdFile);
|
2019-07-12 18:34:04 +03:00
|
|
|
};
|
|
|
|
if (AppendFacesFromCachedFaceList(SharedFontList() ? shared : unshared,
|
|
|
|
aFileName, cachedFaceList, aStdFile)) {
|
2019-07-12 18:33:35 +03:00
|
|
|
LOG(("using cached font info for %s", aFileName.get()));
|
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2022-08-22 16:36:16 +03:00
|
|
|
gfxFontUtils::AutoHBBlob fileBlob(hb_blob_create_from_file(aFileName.get()));
|
2020-02-12 21:34:33 +03:00
|
|
|
if (hb_blob_get_length(fileBlob) > 0) {
|
|
|
|
LOG(("reading font info via harfbuzz for %s", aFileName.get()));
|
|
|
|
AppendFacesFromBlob(aFileName, aStdFile, fileBlob,
|
|
|
|
0 == statRetval ? aCache : nullptr, s.st_mtime,
|
|
|
|
s.st_size);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:29:10 +04:00
|
|
|
|
2015-10-20 20:13:35 +03:00
|
|
|
void gfxFT2FontList::FindFontsInOmnijar(FontNameCache* aCache) {
|
|
|
|
bool jarChanged = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-20 20:13:35 +03:00
|
|
|
mozilla::scache::StartupCache* cache =
|
2013-06-25 12:14:03 +04:00
|
|
|
mozilla::scache::StartupCache::GetSingleton();
|
2019-10-04 23:45:09 +03:00
|
|
|
const char* cachedModifiedTimeBuf;
|
2013-06-25 12:14:03 +04:00
|
|
|
uint32_t longSize;
|
|
|
|
if (cache &&
|
2015-10-20 20:13:35 +03:00
|
|
|
NS_SUCCEEDED(cache->GetBuffer(JAR_LAST_MODIFED_TIME,
|
|
|
|
&cachedModifiedTimeBuf, &longSize)) &&
|
2013-06-25 12:14:03 +04:00
|
|
|
longSize == sizeof(int64_t)) {
|
|
|
|
nsCOMPtr<nsIFile> jarFile = Omnijar::GetPath(Omnijar::Type::GRE);
|
2016-09-28 22:42:41 +03:00
|
|
|
jarFile->GetLastModifiedTime(&mJarModifiedTime);
|
2019-10-04 23:45:09 +03:00
|
|
|
if (mJarModifiedTime > LittleEndian::readInt64(cachedModifiedTimeBuf)) {
|
2013-06-25 12:14:03 +04:00
|
|
|
jarChanged = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 20:13:35 +03:00
|
|
|
static const char* sJarSearchPaths[] = {
|
|
|
|
"res/fonts/*.ttf$",
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2020-09-14 20:00:53 +03:00
|
|
|
RefPtr<nsZipArchive> reader = Omnijar::GetReader(Omnijar::Type::GRE);
|
2013-06-25 12:14:03 +04:00
|
|
|
for (unsigned i = 0; i < ArrayLength(sJarSearchPaths); i++) {
|
|
|
|
nsZipFind* find;
|
|
|
|
if (NS_SUCCEEDED(reader->FindInit(sJarSearchPaths[i], &find))) {
|
|
|
|
const char* path;
|
|
|
|
uint16_t len;
|
|
|
|
while (NS_SUCCEEDED(find->FindNext(&path, &len))) {
|
2018-09-12 22:34:57 +03:00
|
|
|
nsCString entryName(path, len);
|
2013-06-25 12:14:03 +04:00
|
|
|
AppendFacesFromOmnijarEntry(reader, entryName, aCache, jarChanged);
|
|
|
|
}
|
|
|
|
delete find;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:29:10 +04:00
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
static void GetName(hb_face_t* aFace, hb_ot_name_id_t aNameID,
|
|
|
|
nsACString& aName) {
|
|
|
|
unsigned int n = 0;
|
|
|
|
n = hb_ot_name_get_utf8(aFace, aNameID, HB_LANGUAGE_INVALID, &n, nullptr);
|
|
|
|
if (n) {
|
|
|
|
aName.SetLength(n++); // increment n to account for NUL terminator
|
|
|
|
n = hb_ot_name_get_utf8(aFace, aNameID, HB_LANGUAGE_INVALID, &n,
|
|
|
|
aName.BeginWriting());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given the harfbuzz face corresponding to an entryName and face index,
|
2013-06-25 12:14:03 +04:00
|
|
|
// add the face to the available font list and to the faceList string
|
2017-05-18 04:56:24 +03:00
|
|
|
void gfxFT2FontList::AddFaceToList(const nsCString& aEntryName, uint32_t aIndex,
|
2020-02-12 21:34:33 +03:00
|
|
|
StandardFile aStdFile, hb_face_t* aFace,
|
2013-06-25 12:14:03 +04:00
|
|
|
nsCString& aFaceList) {
|
2020-02-12 21:34:33 +03:00
|
|
|
nsAutoCString familyName;
|
|
|
|
bool preferTypographicNames = true;
|
|
|
|
GetName(aFace, HB_OT_NAME_ID_TYPOGRAPHIC_FAMILY, familyName);
|
|
|
|
if (familyName.IsEmpty()) {
|
|
|
|
preferTypographicNames = false;
|
|
|
|
GetName(aFace, HB_OT_NAME_ID_FONT_FAMILY, familyName);
|
|
|
|
}
|
|
|
|
if (familyName.IsEmpty()) {
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
nsAutoCString fullname;
|
|
|
|
GetName(aFace, HB_OT_NAME_ID_FULL_NAME, fullname);
|
|
|
|
if (fullname.IsEmpty()) {
|
|
|
|
// Construct fullname from family + style
|
|
|
|
fullname = familyName;
|
|
|
|
nsAutoCString styleName;
|
|
|
|
if (preferTypographicNames) {
|
|
|
|
GetName(aFace, HB_OT_NAME_ID_TYPOGRAPHIC_SUBFAMILY, styleName);
|
|
|
|
}
|
|
|
|
if (styleName.IsEmpty()) {
|
|
|
|
GetName(aFace, HB_OT_NAME_ID_FONT_SUBFAMILY, styleName);
|
|
|
|
}
|
|
|
|
if (!styleName.IsEmpty() && !styleName.EqualsLiteral("Regular")) {
|
|
|
|
fullname.Append(' ');
|
|
|
|
fullname.Append(styleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 18:34:04 +03:00
|
|
|
// Build the font entry name and create an FT2FontEntry,
|
|
|
|
// but do -not- keep a reference to the FT_Face.
|
|
|
|
// (When using the shared font list, this entry will not be retained,
|
|
|
|
// it is used only to call AppendToFaceList.)
|
2016-08-23 03:06:07 +03:00
|
|
|
RefPtr<FT2FontEntry> fe =
|
2020-02-12 21:34:33 +03:00
|
|
|
FT2FontEntry::CreateFontEntry(fullname, aEntryName.get(), aIndex, aFace);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
if (fe) {
|
2019-07-12 18:34:04 +03:00
|
|
|
fe->mStandardFace = (aStdFile == kStandard);
|
2019-07-12 18:34:30 +03:00
|
|
|
nsAutoCString familyKey(familyName);
|
|
|
|
BuildKeyNameFromFontName(familyKey);
|
|
|
|
|
2020-04-02 15:36:24 +03:00
|
|
|
FontVisibility visibility = FontVisibility::Unknown;
|
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
nsAutoCString psname;
|
|
|
|
GetName(aFace, HB_OT_NAME_ID_POSTSCRIPT_NAME, psname);
|
2019-07-12 18:34:30 +03:00
|
|
|
|
2019-07-12 18:34:04 +03:00
|
|
|
if (SharedFontList()) {
|
2019-07-12 18:34:30 +03:00
|
|
|
FontListEntry fle(familyName, fe->Name(), fe->mFilename,
|
|
|
|
fe->Weight().AsScalar(), fe->Stretch().AsScalar(),
|
2020-04-02 15:36:24 +03:00
|
|
|
fe->SlantStyle().AsScalar(), fe->mFTFontIndex,
|
|
|
|
visibility);
|
2019-07-12 18:34:30 +03:00
|
|
|
CollectInitData(fle, psname, fullname, aStdFile);
|
2019-07-12 18:34:04 +03:00
|
|
|
} else {
|
2021-03-01 12:59:29 +03:00
|
|
|
RefPtr<gfxFontFamily> family =
|
|
|
|
mFontFamilies.LookupOrInsertWith(familyKey, [&] {
|
|
|
|
auto family = MakeRefPtr<FT2FontFamily>(familyName, visibility);
|
|
|
|
if (mSkipSpaceLookupCheckFamilies.Contains(familyKey)) {
|
|
|
|
family->SetSkipSpaceFeatureCheck(true);
|
|
|
|
}
|
|
|
|
if (mBadUnderlineFamilyNames.ContainsSorted(familyKey)) {
|
|
|
|
family->SetBadUnderlineFamily();
|
|
|
|
}
|
|
|
|
return family;
|
|
|
|
});
|
2019-07-12 18:34:04 +03:00
|
|
|
family->AddFontEntry(fe);
|
|
|
|
fe->CheckForBrokenFont(family);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2020-04-02 15:36:24 +03:00
|
|
|
fe->AppendToFaceList(aFaceList, familyName, psname, fullname, visibility);
|
2013-06-25 12:14:03 +04:00
|
|
|
if (LOG_ENABLED()) {
|
2018-04-25 09:18:23 +03:00
|
|
|
nsAutoCString weightString;
|
|
|
|
fe->Weight().ToString(weightString);
|
2018-04-25 09:18:23 +03:00
|
|
|
nsAutoCString stretchString;
|
|
|
|
fe->Stretch().ToString(stretchString);
|
2018-11-30 13:46:48 +03:00
|
|
|
LOG(
|
2013-06-25 12:14:03 +04:00
|
|
|
("(fontinit) added (%s) to family (%s)"
|
2018-04-25 09:18:23 +03:00
|
|
|
" with style: %s weight: %s stretch: %s",
|
2019-07-12 18:34:04 +03:00
|
|
|
fe->Name().get(), familyName.get(),
|
2013-06-25 12:14:03 +04:00
|
|
|
fe->IsItalic() ? "italic" : "normal", weightString.get(),
|
2018-04-25 09:18:23 +03:00
|
|
|
stretchString.get()));
|
2013-06-25 12:14:03 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:29:10 +04:00
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
void gfxFT2FontList::AppendFacesFromOmnijarEntry(nsZipArchive* aArchive,
|
2013-06-25 12:14:03 +04:00
|
|
|
const nsCString& aEntryName,
|
2014-06-30 22:05:29 +04:00
|
|
|
FontNameCache* aCache,
|
2013-06-25 12:14:03 +04:00
|
|
|
bool aJarChanged) {
|
2015-09-12 06:30:14 +03:00
|
|
|
nsCString faceList;
|
2013-06-25 12:14:03 +04:00
|
|
|
if (aCache && !aJarChanged) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t filesize, timestamp;
|
2015-09-12 06:30:14 +03:00
|
|
|
aCache->GetInfoForFile(aEntryName, faceList, ×tamp, &filesize);
|
2013-06-25 12:14:03 +04:00
|
|
|
if (faceList.Length() > 0) {
|
2019-07-12 18:34:30 +03:00
|
|
|
CollectFunc unshared =
|
|
|
|
[](const FontListEntry& aFLE, const nsCString& aPSName,
|
|
|
|
const nsCString& aFullName, StandardFile aStdFile) {
|
2022-04-19 17:20:21 +03:00
|
|
|
auto* pfl = PlatformFontList();
|
|
|
|
pfl->mLock.AssertCurrentThreadIn();
|
|
|
|
pfl->AppendFaceFromFontListEntry(aFLE, aStdFile);
|
2019-07-12 18:34:30 +03:00
|
|
|
};
|
|
|
|
CollectFunc shared = [](const FontListEntry& aFLE,
|
|
|
|
const nsCString& aPSName,
|
|
|
|
const nsCString& aFullName,
|
2019-07-12 18:34:04 +03:00
|
|
|
StandardFile aStdFile) {
|
2019-07-12 18:34:30 +03:00
|
|
|
PlatformFontList()->CollectInitData(aFLE, aPSName, aFullName, aStdFile);
|
2019-07-12 18:34:04 +03:00
|
|
|
};
|
|
|
|
if (AppendFacesFromCachedFaceList(SharedFontList() ? shared : unshared,
|
|
|
|
aEntryName, faceList, kStandard)) {
|
2019-07-12 18:33:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
nsZipItem* item = aArchive->GetItem(aEntryName.get());
|
|
|
|
NS_ASSERTION(item, "failed to find zip entry");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
uint32_t bufSize = item->RealSize();
|
2020-02-12 21:34:33 +03:00
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
// We use fallible allocation here; if there's not enough RAM, we'll simply
|
|
|
|
// ignore the bundled fonts and fall back to the device's installed fonts.
|
2020-02-12 21:34:33 +03:00
|
|
|
char* buffer = static_cast<char*>(malloc(bufSize));
|
|
|
|
if (!buffer) {
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-14 20:00:53 +03:00
|
|
|
nsZipCursor cursor(item, aArchive, (uint8_t*)buffer, bufSize);
|
2013-06-25 12:14:03 +04:00
|
|
|
uint8_t* data = cursor.Copy(&bufSize);
|
2020-02-12 21:34:33 +03:00
|
|
|
MOZ_ASSERT(data && bufSize == item->RealSize(), "error reading bundled font");
|
2013-06-25 12:14:03 +04:00
|
|
|
if (!data) {
|
2018-11-30 13:46:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:36:16 +03:00
|
|
|
gfxFontUtils::AutoHBBlob blob(
|
|
|
|
hb_blob_create(buffer, bufSize, HB_MEMORY_MODE_READONLY, buffer, free));
|
2020-02-12 21:34:33 +03:00
|
|
|
AppendFacesFromBlob(aEntryName, kStandard, blob, aCache, 0, bufSize);
|
2012-10-03 23:29:10 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
// Called on each family after all fonts are added to the list;
|
2019-07-12 18:34:04 +03:00
|
|
|
// if aSortFaces is true this will sort faces to give priority to "standard"
|
|
|
|
// font files.
|
2022-04-19 17:20:21 +03:00
|
|
|
void FT2FontFamily::FinalizeMemberList(bool aSortFaces) {
|
|
|
|
AutoWriteLock lock(mLock);
|
2013-06-25 12:14:03 +04:00
|
|
|
|
2022-04-19 17:20:21 +03:00
|
|
|
SetHasStyles(true);
|
2013-06-25 12:14:03 +04:00
|
|
|
|
2015-09-24 18:31:30 +03:00
|
|
|
if (aSortFaces) {
|
2022-04-19 17:20:21 +03:00
|
|
|
SortAvailableFonts();
|
2013-06-25 12:14:03 +04:00
|
|
|
}
|
2022-04-19 17:20:21 +03:00
|
|
|
CheckForSimpleFamily();
|
2013-06-25 12:14:03 +04:00
|
|
|
}
|
2012-10-03 23:29:10 +04:00
|
|
|
|
2009-10-07 18:13:40 +04:00
|
|
|
void gfxFT2FontList::FindFonts() {
|
2019-07-12 18:33:03 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
// Chrome process: get the cached list (if any)
|
2016-09-28 22:42:41 +03:00
|
|
|
if (!mFontNameCache) {
|
|
|
|
mFontNameCache = MakeUnique<FontNameCache>();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-09-28 22:42:41 +03:00
|
|
|
mFontNameCache->Init();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-05-07 14:03:08 +03:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
// Android API 29+ provides system font and font matcher API for native code.
|
|
|
|
typedef void* (*_ASystemFontIterator_open)();
|
|
|
|
typedef void* (*_ASystemFontIterator_next)(void*);
|
|
|
|
typedef void (*_ASystemFontIterator_close)(void*);
|
|
|
|
typedef const char* (*_AFont_getFontFilePath)(const void*);
|
|
|
|
typedef void (*_AFont_close)(void*);
|
|
|
|
|
|
|
|
static _ASystemFontIterator_open systemFontIterator_open = nullptr;
|
|
|
|
static _ASystemFontIterator_next systemFontIterator_next = nullptr;
|
|
|
|
static _ASystemFontIterator_close systemFontIterator_close = nullptr;
|
|
|
|
static _AFont_getFontFilePath font_getFontFilePath = nullptr;
|
|
|
|
static _AFont_close font_close = nullptr;
|
|
|
|
|
|
|
|
static bool firstTime = true;
|
|
|
|
|
2022-03-22 15:05:05 +03:00
|
|
|
nsAutoCString androidFontsRoot = [&] {
|
|
|
|
// ANDROID_ROOT is the root of the android system, typically /system;
|
|
|
|
// font files are in /$ANDROID_ROOT/fonts/
|
|
|
|
nsAutoCString root;
|
|
|
|
char* androidRoot = PR_GetEnv("ANDROID_ROOT");
|
|
|
|
if (androidRoot) {
|
|
|
|
root = androidRoot;
|
|
|
|
} else {
|
|
|
|
root = "/system"_ns;
|
|
|
|
}
|
|
|
|
root.AppendLiteral("/fonts");
|
|
|
|
return root;
|
|
|
|
}();
|
|
|
|
|
2020-05-07 14:03:08 +03:00
|
|
|
if (firstTime) {
|
|
|
|
if (jni::GetAPIVersion() >= 29) {
|
|
|
|
void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
|
|
|
|
MOZ_ASSERT(handle);
|
|
|
|
|
|
|
|
systemFontIterator_open =
|
|
|
|
(_ASystemFontIterator_open)dlsym(handle, "ASystemFontIterator_open");
|
|
|
|
systemFontIterator_next =
|
|
|
|
(_ASystemFontIterator_next)dlsym(handle, "ASystemFontIterator_next");
|
|
|
|
systemFontIterator_close = (_ASystemFontIterator_close)dlsym(
|
|
|
|
handle, "ASystemFontIterator_close");
|
|
|
|
font_getFontFilePath =
|
|
|
|
(_AFont_getFontFilePath)dlsym(handle, "AFont_getFontFilePath");
|
|
|
|
font_close = (_AFont_close)dlsym(handle, "AFont_close");
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!systemFontIterator_next) ||
|
|
|
|
NS_WARN_IF(!systemFontIterator_close) ||
|
|
|
|
NS_WARN_IF(!font_getFontFilePath) || NS_WARN_IF(!font_close)) {
|
|
|
|
// Since any functions aren't resolved, use old way to enumerate fonts.
|
|
|
|
systemFontIterator_open = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
firstTime = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useSystemFontAPI = !!systemFontIterator_open;
|
2022-11-18 18:41:47 +03:00
|
|
|
|
|
|
|
if (useSystemFontAPI &&
|
|
|
|
!StaticPrefs::
|
|
|
|
gfx_font_list_use_font_match_api_force_enabled_AtStartup()) {
|
|
|
|
// OPPO, realme and OnePlus device seem to crash when using font match API
|
|
|
|
// (Bug 1787551).
|
|
|
|
nsCString manufacturer = java::sdk::Build::MANUFACTURER()->ToCString();
|
|
|
|
if (manufacturer.EqualsLiteral("OPPO") ||
|
|
|
|
manufacturer.EqualsLiteral("realme") ||
|
|
|
|
manufacturer.EqualsLiteral("OnePlus")) {
|
|
|
|
useSystemFontAPI = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:03:08 +03:00
|
|
|
if (useSystemFontAPI) {
|
|
|
|
void* iter = systemFontIterator_open();
|
|
|
|
if (iter) {
|
|
|
|
void* font = systemFontIterator_next(iter);
|
|
|
|
while (font) {
|
2022-03-22 15:05:05 +03:00
|
|
|
nsDependentCString path(font_getFontFilePath(font));
|
2020-05-07 14:03:08 +03:00
|
|
|
AppendFacesFromFontFile(path, mFontNameCache.get(), kStandard);
|
|
|
|
font_close(font);
|
|
|
|
font = systemFontIterator_next(iter);
|
|
|
|
}
|
|
|
|
|
2022-10-04 09:23:58 +03:00
|
|
|
if (!StaticPrefs::gfx_font_rendering_colr_v1_enabled()) {
|
|
|
|
// We turn off COLRv1 fonts support. Newer android versions have
|
|
|
|
// COLRv1 emoji font, and a legacy and hidden CBDT font we understand,
|
|
|
|
// so try to find NotoColorEmojiLegacy.ttf explicitly for now.
|
|
|
|
nsAutoCString legacyEmojiFont(androidFontsRoot);
|
|
|
|
legacyEmojiFont.Append("/NotoColorEmojiLegacy.ttf");
|
|
|
|
AppendFacesFromFontFile(legacyEmojiFont, mFontNameCache.get(),
|
|
|
|
kStandard);
|
|
|
|
}
|
2022-03-22 15:05:05 +03:00
|
|
|
|
2020-05-07 14:03:08 +03:00
|
|
|
systemFontIterator_close(iter);
|
|
|
|
} else {
|
|
|
|
useSystemFontAPI = false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2020-05-07 14:03:08 +03:00
|
|
|
if (!useSystemFontAPI)
|
|
|
|
#endif
|
|
|
|
{
|
2022-03-22 15:05:05 +03:00
|
|
|
FindFontsInDir(androidFontsRoot, mFontNameCache.get());
|
2020-05-07 14:03:08 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-06-25 12:14:03 +04:00
|
|
|
// Look for fonts stored in omnijar, unless we're on a low-memory
|
|
|
|
// device where we don't want to spend the RAM to decompress them.
|
|
|
|
// (Prefs may disable this, or force-enable it even with low memory.)
|
2021-01-18 23:04:36 +03:00
|
|
|
if (StaticPrefs::gfx_bundled_fonts_activate_AtStartup() > 0 ||
|
|
|
|
(StaticPrefs::gfx_bundled_fonts_activate_AtStartup() < 0 &&
|
2022-09-28 18:17:47 +03:00
|
|
|
!nsMemory::IsLowMemoryPlatform())) {
|
2021-03-08 19:23:39 +03:00
|
|
|
TimeStamp start = TimeStamp::Now();
|
2017-07-06 17:06:45 +03:00
|
|
|
FindFontsInOmnijar(mFontNameCache.get());
|
2021-03-08 19:23:39 +03:00
|
|
|
TimeStamp end = TimeStamp::Now();
|
|
|
|
Telemetry::Accumulate(Telemetry::FONTLIST_BUNDLEDFONTS_ACTIVATE,
|
|
|
|
(end - start).ToMilliseconds());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2012-12-18 15:18:56 +04:00
|
|
|
// Look for downloaded fonts in a profile-agnostic "fonts" directory.
|
2015-09-22 16:55:56 +03:00
|
|
|
nsCOMPtr<nsIProperties> dirSvc =
|
|
|
|
do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
|
|
|
|
if (dirSvc) {
|
|
|
|
nsCOMPtr<nsIFile> appDir;
|
|
|
|
nsresult rv = dirSvc->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(appDir));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2013-06-25 12:14:03 +04:00
|
|
|
appDir->AppendNative("fonts"_ns);
|
2015-09-22 16:55:56 +03:00
|
|
|
nsCString localPath;
|
|
|
|
if (NS_SUCCEEDED(appDir->GetNativePath(localPath))) {
|
2017-07-06 17:06:45 +03:00
|
|
|
FindFontsInDir(localPath, mFontNameCache.get());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 15:18:56 +04:00
|
|
|
// look for locally-added fonts in a "fonts" subdir of the profile
|
2013-06-25 12:14:03 +04:00
|
|
|
nsCOMPtr<nsIFile> localDir;
|
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_LOCAL_50_DIR,
|
|
|
|
getter_AddRefs(localDir));
|
|
|
|
if (NS_SUCCEEDED(rv) && NS_SUCCEEDED(localDir->Append(u"fonts"_ns))) {
|
2015-09-22 16:55:56 +03:00
|
|
|
nsCString localPath;
|
2012-10-03 23:28:43 +04:00
|
|
|
rv = localDir->GetNativePath(localPath);
|
2015-09-22 16:55:56 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-07-06 17:06:45 +03:00
|
|
|
FindFontsInDir(localPath, mFontNameCache.get());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 18:34:04 +03:00
|
|
|
mFontNameCache->DropStaleEntries();
|
|
|
|
if (!mFontNameCache->EntryCount()) {
|
|
|
|
// if we can't find any usable fonts, we are doomed!
|
|
|
|
MOZ_CRASH("No font files found");
|
2015-09-24 18:31:30 +03:00
|
|
|
}
|
2019-07-12 18:34:04 +03:00
|
|
|
|
2019-05-25 04:38:47 +03:00
|
|
|
// Write out FontCache data if needed
|
|
|
|
WriteCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void gfxFT2FontList::WriteCache() {
|
|
|
|
if (mFontNameCache) {
|
|
|
|
mFontNameCache->WriteCache();
|
|
|
|
}
|
|
|
|
mozilla::scache::StartupCache* cache =
|
|
|
|
mozilla::scache::StartupCache::GetSingleton();
|
|
|
|
if (cache && mJarModifiedTime > 0) {
|
|
|
|
const size_t bufSize = sizeof(mJarModifiedTime);
|
2022-12-01 13:46:34 +03:00
|
|
|
auto buf = UniqueFreePtr<char[]>(
|
|
|
|
reinterpret_cast<char*>(malloc(sizeof(char) * bufSize)));
|
2019-10-04 23:45:09 +03:00
|
|
|
LittleEndian::writeInt64(buf.get(), mJarModifiedTime);
|
2019-05-25 04:38:47 +03:00
|
|
|
|
|
|
|
LOG(("WriteCache: putting Jar, length %zu", bufSize));
|
|
|
|
cache->PutBuffer(JAR_LAST_MODIFED_TIME, std::move(buf), bufSize);
|
|
|
|
}
|
|
|
|
LOG(("Done with writecache"));
|
2012-10-03 23:27:25 +04:00
|
|
|
}
|
|
|
|
|
2014-06-30 22:05:29 +04:00
|
|
|
void gfxFT2FontList::FindFontsInDir(const nsCString& aDir,
|
2017-07-06 17:06:45 +03:00
|
|
|
FontNameCache* aFNC) {
|
2011-09-23 15:15:36 +04:00
|
|
|
static const char* sStandardFonts[] = {"DroidSans.ttf",
|
|
|
|
"DroidSans-Bold.ttf",
|
|
|
|
"DroidSerif-Regular.ttf",
|
|
|
|
"DroidSerif-Bold.ttf",
|
|
|
|
"DroidSerif-Italic.ttf",
|
|
|
|
"DroidSerif-BoldItalic.ttf",
|
|
|
|
"DroidSansMono.ttf",
|
|
|
|
"DroidSansArabic.ttf",
|
|
|
|
"DroidSansHebrew.ttf",
|
|
|
|
"DroidSansThai.ttf",
|
|
|
|
"MTLmr3m.ttf",
|
|
|
|
"MTLc3m.ttf",
|
2013-05-11 12:00:42 +04:00
|
|
|
"NanumGothic.ttf",
|
2022-03-22 15:05:05 +03:00
|
|
|
"NotoColorEmoji.ttf",
|
|
|
|
"NotoColorEmojiFlags.ttf",
|
|
|
|
"NotoColorEmojiLegacy.ttf",
|
2011-09-23 15:15:36 +04:00
|
|
|
"DroidSansJapanese.ttf",
|
|
|
|
"DroidSansFallback.ttf"};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-10-03 23:27:25 +04:00
|
|
|
DIR* d = opendir(aDir.get());
|
2011-09-23 15:15:36 +04:00
|
|
|
if (!d) {
|
2012-10-03 23:27:25 +04:00
|
|
|
return;
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-31 19:44:31 +04:00
|
|
|
struct dirent* ent = nullptr;
|
|
|
|
while ((ent = readdir(d)) != nullptr) {
|
2014-03-25 14:07:31 +04:00
|
|
|
const char* ext = strrchr(ent->d_name, '.');
|
|
|
|
if (!ext) {
|
2011-09-23 15:15:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcasecmp(ext, ".ttf") == 0 || strcasecmp(ext, ".otf") == 0 ||
|
2014-03-25 14:07:31 +04:00
|
|
|
strcasecmp(ext, ".woff") == 0 || strcasecmp(ext, ".ttc") == 0) {
|
2011-09-23 15:15:36 +04:00
|
|
|
bool isStdFont = false;
|
2012-10-03 23:27:25 +04:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(sStandardFonts) && !isStdFont;
|
|
|
|
i++) {
|
2011-09-23 15:15:36 +04:00
|
|
|
isStdFont = strcmp(sStandardFonts[i], ent->d_name) == 0;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2022-03-22 15:05:05 +03:00
|
|
|
nsAutoCString s(aDir);
|
2011-09-23 15:15:36 +04:00
|
|
|
s.Append('/');
|
2014-03-25 14:07:31 +04:00
|
|
|
s.Append(ent->d_name);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
// Add the face(s) from this file to our font list;
|
|
|
|
// note that if we have cached info for this file in fnc,
|
|
|
|
// and the file is unchanged, we won't actually need to read it.
|
|
|
|
// If the file is new/changed, this will update the FontNameCache.
|
2017-07-06 17:06:45 +03:00
|
|
|
AppendFacesFromFontFile(s, aFNC, isStdFont ? kStandard : kUnknown);
|
2010-07-16 10:03:45 +04:00
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-10-03 23:27:25 +04:00
|
|
|
closedir(d);
|
2011-09-23 12:53:13 +04:00
|
|
|
}
|
|
|
|
|
2011-09-23 15:15:36 +04:00
|
|
|
void gfxFT2FontList::AppendFaceFromFontListEntry(const FontListEntry& aFLE,
|
2014-06-30 22:05:29 +04:00
|
|
|
StandardFile aStdFile) {
|
2011-09-23 15:16:13 +04:00
|
|
|
FT2FontEntry* fe = FT2FontEntry::CreateFontEntry(aFLE);
|
2011-09-23 15:15:36 +04:00
|
|
|
if (fe) {
|
2019-07-12 18:34:30 +03:00
|
|
|
nsAutoCString key(aFLE.familyName());
|
|
|
|
BuildKeyNameFromFontName(key);
|
2014-06-30 22:05:29 +04:00
|
|
|
fe->mStandardFace = (aStdFile == kStandard);
|
2021-03-01 12:59:29 +03:00
|
|
|
RefPtr<gfxFontFamily> family = mFontFamilies.LookupOrInsertWith(key, [&] {
|
|
|
|
auto family =
|
|
|
|
MakeRefPtr<FT2FontFamily>(aFLE.familyName(), aFLE.visibility());
|
2019-07-12 18:34:30 +03:00
|
|
|
if (mSkipSpaceLookupCheckFamilies.Contains(key)) {
|
2014-03-20 10:43:31 +04:00
|
|
|
family->SetSkipSpaceFeatureCheck(true);
|
|
|
|
}
|
2019-07-12 18:34:30 +03:00
|
|
|
if (mBadUnderlineFamilyNames.ContainsSorted(key)) {
|
2011-09-23 15:15:36 +04:00
|
|
|
family->SetBadUnderlineFamily();
|
|
|
|
}
|
2021-03-01 12:59:29 +03:00
|
|
|
return family;
|
|
|
|
});
|
2011-09-23 15:15:36 +04:00
|
|
|
family->AddFontEntry(fe);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-12-19 13:42:25 +04:00
|
|
|
fe->CheckForBrokenFont(family);
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 23:00:31 +03:00
|
|
|
void gfxFT2FontList::ReadSystemFontList(dom::SystemFontList* aList) {
|
2022-04-19 17:20:21 +03:00
|
|
|
AutoLock lock(mLock);
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& entry : mFontFamilies) {
|
|
|
|
auto family = static_cast<FT2FontFamily*>(entry.GetData().get());
|
2021-05-01 23:00:31 +03:00
|
|
|
family->AddFacesToFontList(&aList->entries());
|
2015-09-24 18:31:30 +03:00
|
|
|
}
|
2011-09-23 15:15:36 +04:00
|
|
|
}
|
|
|
|
|
2018-09-12 22:34:57 +03:00
|
|
|
static void LoadSkipSpaceLookupCheck(
|
2021-03-23 13:36:36 +03:00
|
|
|
nsTHashSet<nsCString>& aSkipSpaceLookupCheck) {
|
2018-09-18 11:34:24 +03:00
|
|
|
AutoTArray<nsCString, 5> skiplist;
|
2014-03-26 12:03:56 +04:00
|
|
|
gfxFontUtils::GetPrefsFontList(
|
|
|
|
"font.whitelist.skip_default_features_space_check", skiplist);
|
2014-03-20 10:43:31 +04:00
|
|
|
uint32_t numFonts = skiplist.Length();
|
|
|
|
for (uint32_t i = 0; i < numFonts; i++) {
|
|
|
|
ToLowerCase(skiplist[i]);
|
2021-03-23 13:36:36 +03:00
|
|
|
aSkipSpaceLookupCheck.Insert(skiplist[i]);
|
2014-03-20 10:43:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:06:07 +03:00
|
|
|
nsresult gfxFT2FontList::InitFontListForPlatform() {
|
2014-03-20 10:43:31 +04:00
|
|
|
LoadSkipSpaceLookupCheck(mSkipSpaceLookupCheckFamilies);
|
|
|
|
|
2019-07-12 18:33:03 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
2019-07-12 18:34:04 +03:00
|
|
|
// This will populate/update mFontNameCache and store it in the
|
|
|
|
// startupCache for future startups.
|
2019-07-12 18:33:03 +03:00
|
|
|
FindFonts();
|
2019-07-12 18:34:04 +03:00
|
|
|
|
|
|
|
// Finalize the families by sorting faces into standard order
|
|
|
|
// and marking "simple" families.
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& entry : mFontFamilies) {
|
2022-04-19 17:20:21 +03:00
|
|
|
auto* family = static_cast<FT2FontFamily*>(entry.GetData().get());
|
|
|
|
family->FinalizeMemberList(/* aSortFaces */ true);
|
2019-07-12 18:34:04 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 18:33:03 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-09 15:27:45 +03:00
|
|
|
// Content process: use font list passed from the chrome process via
|
|
|
|
// the GetXPCOMProcessAttributes message.
|
|
|
|
auto& fontList = dom::ContentChild::GetSingleton()->SystemFontList();
|
2021-05-01 23:00:31 +03:00
|
|
|
for (FontListEntry& fle : fontList.entries()) {
|
2019-07-12 18:33:03 +03:00
|
|
|
// We don't need to identify "standard" font files here,
|
|
|
|
// as the faces are already sorted.
|
2020-03-09 15:27:45 +03:00
|
|
|
AppendFaceFromFontListEntry(fle, kUnknown);
|
2019-07-12 18:33:03 +03:00
|
|
|
}
|
2020-03-09 15:27:45 +03:00
|
|
|
|
2019-07-12 18:34:04 +03:00
|
|
|
// We don't need to sort faces (because they were already sorted by the
|
|
|
|
// chrome process, so we just maintain the existing order)
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& entry : mFontFamilies) {
|
2022-04-19 17:20:21 +03:00
|
|
|
auto* family = static_cast<FT2FontFamily*>(entry.GetData().get());
|
|
|
|
family->FinalizeMemberList(/* aSortFaces */ false);
|
2019-07-12 18:33:03 +03:00
|
|
|
}
|
2019-07-08 22:47:03 +03:00
|
|
|
|
2019-07-12 18:33:03 +03:00
|
|
|
LOG(("got font list from chrome process: %" PRIdPTR " faces in %" PRIu32
|
|
|
|
" families",
|
2021-05-01 23:00:31 +03:00
|
|
|
fontList.entries().Length(), mFontFamilies.Count()));
|
|
|
|
fontList.entries().Clear();
|
2020-03-09 15:27:45 +03:00
|
|
|
|
2019-07-09 01:07:08 +03:00
|
|
|
return NS_OK;
|
2019-07-08 22:47:03 +03:00
|
|
|
}
|
|
|
|
|
2019-07-12 18:34:04 +03:00
|
|
|
void gfxFT2FontList::InitSharedFontListForPlatform() {
|
|
|
|
if (!XRE_IsParentProcess()) {
|
|
|
|
// Content processes will access the shared-memory data created by the
|
|
|
|
// parent, so don't need to scan for available fonts themselves.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will populate mFontNameCache with entries for all the available font
|
|
|
|
// files, and record them in mFamilies (unshared list) or mFamilyInitData and
|
|
|
|
// mFaceInitData (shared font list).
|
|
|
|
FindFonts();
|
|
|
|
|
|
|
|
mozilla::fontlist::FontList* list = SharedFontList();
|
|
|
|
list->SetFamilyNames(mFamilyInitData);
|
|
|
|
|
|
|
|
auto families = list->Families();
|
|
|
|
for (uint32_t i = 0; i < mFamilyInitData.Length(); i++) {
|
|
|
|
auto faceList = mFaceInitData.Get(mFamilyInitData[i].mKey);
|
|
|
|
MOZ_ASSERT(faceList);
|
|
|
|
families[i].AddFaces(list, *faceList);
|
|
|
|
}
|
|
|
|
|
|
|
|
mFamilyInitData.Clear();
|
|
|
|
mFaceInitData.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontEntry* gfxFT2FontList::CreateFontEntry(fontlist::Face* aFace,
|
|
|
|
const fontlist::Family* aFamily) {
|
|
|
|
fontlist::FontList* list = SharedFontList();
|
|
|
|
nsAutoCString desc(aFace->mDescriptor.AsString(list));
|
2020-07-06 17:29:23 +03:00
|
|
|
FT2FontEntry* fe =
|
|
|
|
FT2FontEntry::CreateFontEntry(desc, desc.get(), aFace->mIndex, nullptr);
|
|
|
|
fe->InitializeFrom(aFace, aFamily);
|
2019-07-12 18:34:04 +03:00
|
|
|
fe->CheckForBrokenFont(aFamily->Key().AsString(list));
|
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
2015-09-24 18:31:30 +03:00
|
|
|
// called for each family name, based on the assumption that the
|
2009-10-07 18:13:40 +04:00
|
|
|
// first part of the full name is the family name
|
|
|
|
|
2021-09-08 15:18:17 +03:00
|
|
|
gfxFontEntry* gfxFT2FontList::LookupLocalFont(nsPresContext* aPresContext,
|
|
|
|
const nsACString& aFontName,
|
2018-04-25 09:18:23 +03:00
|
|
|
WeightRange aWeightForEntry,
|
|
|
|
StretchRange aStretchForEntry,
|
|
|
|
SlantStyleRange aStyleForEntry) {
|
2022-04-19 17:20:21 +03:00
|
|
|
AutoLock lock(mLock);
|
|
|
|
|
2019-07-12 18:34:30 +03:00
|
|
|
if (SharedFontList()) {
|
2021-09-08 15:18:17 +03:00
|
|
|
return LookupInSharedFaceNameList(aPresContext, aFontName, aWeightForEntry,
|
2019-07-12 18:34:30 +03:00
|
|
|
aStretchForEntry, aStyleForEntry);
|
|
|
|
}
|
2022-04-19 17:20:21 +03:00
|
|
|
|
2009-10-07 18:13:40 +04:00
|
|
|
// walk over list of names
|
2015-09-24 18:31:30 +03:00
|
|
|
FT2FontEntry* fontEntry = nullptr;
|
2021-09-08 15:18:17 +03:00
|
|
|
FontVisibility level =
|
|
|
|
aPresContext ? aPresContext->GetFontVisibility() : FontVisibility::User;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const RefPtr<gfxFontFamily>& fontFamily : mFontFamilies.Values()) {
|
2021-09-08 15:18:17 +03:00
|
|
|
if (!IsVisibleToCSS(*fontFamily, level)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-09-24 18:31:30 +03:00
|
|
|
// Check family name, based on the assumption that the
|
|
|
|
// first part of the full name is the family name
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-24 18:31:30 +03:00
|
|
|
// does the family name match up to the length of the family name?
|
2018-09-12 22:34:57 +03:00
|
|
|
const nsCString& family = fontFamily->Name();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-12 22:34:57 +03:00
|
|
|
const nsAutoCString fullNameFamily(
|
|
|
|
Substring(aFontName, 0, family.Length()));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-09-24 18:31:30 +03:00
|
|
|
// if so, iterate over faces in this family to see if there is a match
|
2020-05-27 21:11:12 +03:00
|
|
|
if (family.Equals(fullNameFamily, nsCaseInsensitiveCStringComparator)) {
|
2022-05-10 12:52:14 +03:00
|
|
|
gfxFontEntry* fe =
|
|
|
|
fontFamily->FindFont(aFontName, nsCaseInsensitiveCStringComparator);
|
|
|
|
if (fe) {
|
|
|
|
fontEntry = static_cast<FT2FontEntry*>(fe);
|
|
|
|
goto searchDone;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-09-24 18:31:30 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2009-10-07 18:13:40 +04:00
|
|
|
|
2015-09-24 18:31:30 +03:00
|
|
|
searchDone:
|
|
|
|
if (!fontEntry) {
|
2014-04-08 12:10:41 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-04-08 12:10:41 +04:00
|
|
|
// Clone the font entry so that we can then set its style descriptors
|
2014-09-08 11:23:20 +04:00
|
|
|
// from the userfont entry rather than the actual font.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-04-08 12:10:41 +04:00
|
|
|
// Ensure existence of mFTFace in the original entry
|
2019-09-16 20:07:59 +03:00
|
|
|
RefPtr<SharedFTFace> face = fontEntry->GetFTFace(true);
|
|
|
|
if (!face) {
|
2014-04-08 12:10:41 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-02-12 21:34:33 +03:00
|
|
|
FT2FontEntry* fe = FT2FontEntry::CreateFontEntry(
|
|
|
|
fontEntry->Name(), fontEntry->mFilename.get(), fontEntry->mFTFontIndex,
|
|
|
|
nullptr);
|
2014-04-08 12:10:41 +04:00
|
|
|
if (fe) {
|
2018-04-25 09:18:23 +03:00
|
|
|
fe->mStyleRange = aStyleForEntry;
|
|
|
|
fe->mWeightRange = aWeightForEntry;
|
|
|
|
fe->mStretchRange = aStretchForEntry;
|
2014-09-08 11:23:20 +04:00
|
|
|
fe->mIsLocalUserFont = true;
|
2014-04-08 12:10:41 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-04-08 12:10:41 +04:00
|
|
|
return fe;
|
2009-10-07 18:13:40 +04:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:18:17 +03:00
|
|
|
FontFamily gfxFT2FontList::GetDefaultFontForPlatform(
|
|
|
|
nsPresContext* aPresContext, const gfxFontStyle* aStyle,
|
|
|
|
nsAtom* aLanguage) {
|
2019-04-27 18:37:58 +03:00
|
|
|
FontFamily ff;
|
2016-10-27 03:17:10 +03:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
2021-09-08 15:18:17 +03:00
|
|
|
ff = FindFamily(aPresContext, "Roboto"_ns);
|
2019-04-27 18:37:58 +03:00
|
|
|
if (ff.IsNull()) {
|
2021-09-08 15:18:17 +03:00
|
|
|
ff = FindFamily(aPresContext, "Droid Sans"_ns);
|
2012-06-21 10:45:55 +04:00
|
|
|
}
|
2009-10-07 18:13:40 +04:00
|
|
|
#endif
|
|
|
|
/* TODO: what about Qt or other platforms that may use this? */
|
2019-04-27 18:37:58 +03:00
|
|
|
return ff;
|
2009-10-07 18:13:40 +04:00
|
|
|
}
|
|
|
|
|
2018-09-12 22:34:57 +03:00
|
|
|
gfxFontEntry* gfxFT2FontList::MakePlatformFont(const nsACString& aFontName,
|
2018-04-25 09:18:23 +03:00
|
|
|
WeightRange aWeightForEntry,
|
|
|
|
StretchRange aStretchForEntry,
|
|
|
|
SlantStyleRange aStyleForEntry,
|
2014-09-08 11:23:19 +04:00
|
|
|
const uint8_t* aFontData,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLength) {
|
2009-10-07 18:13:40 +04:00
|
|
|
// The FT2 font needs the font data to persist, so we do NOT free it here
|
|
|
|
// but instead pass ownership to the font entry.
|
|
|
|
// Deallocation will happen later, when the font face is destroyed.
|
2018-04-25 09:18:23 +03:00
|
|
|
return FT2FontEntry::CreateFontEntry(aFontName, aWeightForEntry,
|
|
|
|
aStretchForEntry, aStyleForEntry,
|
|
|
|
aFontData, aLength);
|
2009-10-07 18:13:40 +04:00
|
|
|
}
|
2011-09-23 15:16:13 +04:00
|
|
|
|
2020-04-02 15:36:24 +03:00
|
|
|
gfxFontFamily* gfxFT2FontList::CreateFontFamily(
|
|
|
|
const nsACString& aName, FontVisibility aVisibility) const {
|
|
|
|
return new FT2FontFamily(aName, aVisibility);
|
2017-08-01 13:25:35 +03:00
|
|
|
}
|
|
|
|
|
2016-09-28 22:42:41 +03:00
|
|
|
void gfxFT2FontList::WillShutdown() {
|
2019-05-25 04:38:47 +03:00
|
|
|
LOG(("WillShutdown"));
|
|
|
|
WriteCache();
|
2016-09-28 22:42:41 +03:00
|
|
|
mFontNameCache = nullptr;
|
|
|
|
}
|