2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2014-01-29 11:39:01 +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/. */
|
|
|
|
|
|
|
|
#ifndef GFX_FONT_INFO_LOADER_H
|
|
|
|
#define GFX_FONT_INFO_LOADER_H
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsITimer.h"
|
2014-01-29 11:39:01 +04:00
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsString.h"
|
2020-11-23 19:12:02 +03:00
|
|
|
#include "gfxFontEntry.h"
|
2016-02-05 04:24:00 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2014-01-29 11:39:01 +04:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2020-11-23 19:12:02 +03:00
|
|
|
#include "nsISupports.h"
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
// data retrieved for a given face
|
|
|
|
|
|
|
|
struct FontFaceData {
|
2018-09-12 22:34:57 +03:00
|
|
|
nsCString mFullName;
|
|
|
|
nsCString mPostscriptName;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxCharacterMap> mCharacterMap;
|
2020-04-01 16:45:40 +03:00
|
|
|
uint32_t mUVSOffset = 0;
|
2014-01-29 11:39:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// base class used to contain cached system-wide font info.
|
|
|
|
// methods in this class are called on off-main threads so
|
|
|
|
// all methods use only static methods or other thread-safe
|
|
|
|
// font data access API's. specifically, no use is made of
|
|
|
|
// gfxPlatformFontList, gfxFontFamily, gfxFamily or any
|
|
|
|
// harfbuzz API methods within FontInfoData subclasses.
|
|
|
|
|
|
|
|
class FontInfoData {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontInfoData)
|
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
FontInfoData(bool aLoadOtherNames, bool aLoadFaceNames, bool aLoadCmaps)
|
|
|
|
: mCanceled(false),
|
|
|
|
mLoadOtherNames(aLoadOtherNames),
|
|
|
|
mLoadFaceNames(aLoadFaceNames),
|
|
|
|
mLoadCmaps(aLoadCmaps) {
|
2014-01-29 11:39:01 +04:00
|
|
|
MOZ_COUNT_CTOR(FontInfoData);
|
2014-04-04 20:27:02 +04:00
|
|
|
}
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2014-04-04 20:27:02 +04:00
|
|
|
protected:
|
|
|
|
// Protected destructor, to discourage deletion outside of Release():
|
2020-02-20 14:40:14 +03:00
|
|
|
MOZ_COUNTED_DTOR_VIRTUAL(FontInfoData)
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2014-04-04 20:27:02 +04:00
|
|
|
public:
|
2014-01-29 11:39:01 +04:00
|
|
|
virtual void Load();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// loads font data for all fonts of a given family
|
|
|
|
// (called on async thread)
|
|
|
|
virtual void LoadFontFamilyData(const nsACString& aFamilyName) = 0;
|
|
|
|
|
|
|
|
// -- methods overriden by platform-specific versions --
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// fetches cmap data for a particular font from cached font data
|
2018-09-12 22:34:57 +03:00
|
|
|
virtual already_AddRefed<gfxCharacterMap> GetCMAP(const nsACString& aFontName,
|
|
|
|
uint32_t& aUVSOffset) {
|
2014-01-29 11:39:01 +04:00
|
|
|
FontFaceData faceData;
|
|
|
|
if (!mFontFaceData.Get(aFontName, &faceData) || !faceData.mCharacterMap) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
aUVSOffset = faceData.mUVSOffset;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<gfxCharacterMap> cmap = faceData.mCharacterMap;
|
2014-01-29 11:39:01 +04:00
|
|
|
return cmap.forget();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// fetches fullname/postscript names from cached font data
|
2018-09-12 22:34:57 +03:00
|
|
|
virtual void GetFaceNames(const nsACString& aFontName, nsACString& aFullName,
|
|
|
|
nsACString& aPostscriptName) {
|
|
|
|
FontFaceData faceData;
|
2014-01-29 11:39:01 +04:00
|
|
|
if (!mFontFaceData.Get(aFontName, &faceData)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2018-09-12 22:34:57 +03:00
|
|
|
aFullName = faceData.mFullName;
|
|
|
|
aPostscriptName = faceData.mPostscriptName;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 22:34:57 +03:00
|
|
|
// fetches localized family name data from cached font data
|
2020-05-05 13:56:14 +03:00
|
|
|
const nsTArray<nsCString>* GetOtherFamilyNames(
|
|
|
|
const nsACString& aFamilyName) {
|
2021-03-01 12:59:30 +03:00
|
|
|
return mOtherFamilyNames.Lookup(aFamilyName).DataPtrOrNull();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-01-29 11:39:01 +04:00
|
|
|
|
2016-02-05 04:24:00 +03:00
|
|
|
nsTArray<nsCString> mFontFamiliesToLoad;
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// currently non-issue but beware,
|
|
|
|
// this is also set during cleanup after finishing
|
|
|
|
mozilla::Atomic<bool> mCanceled;
|
|
|
|
|
|
|
|
// time spent on the loader thread
|
|
|
|
mozilla::TimeDuration mLoadTime;
|
|
|
|
|
|
|
|
struct FontCounts {
|
|
|
|
uint32_t families;
|
|
|
|
uint32_t fonts;
|
|
|
|
uint32_t cmaps;
|
|
|
|
uint32_t facenames;
|
|
|
|
uint32_t othernames;
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
FontCounts mLoadStats;
|
|
|
|
|
|
|
|
bool mLoadOtherNames;
|
|
|
|
bool mLoadFaceNames;
|
2018-09-12 22:34:57 +03:00
|
|
|
bool mLoadCmaps;
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
// face name ==> per-face data
|
2021-03-10 13:47:47 +03:00
|
|
|
nsTHashMap<nsCStringHashKey, FontFaceData> mFontFaceData;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// canonical family name ==> array of localized family names
|
2021-03-10 13:47:47 +03:00
|
|
|
nsTHashMap<nsCStringHashKey, CopyableTArray<nsCString> > mOtherFamilyNames;
|
2014-01-29 11:39:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// gfxFontInfoLoader - helper class for loading font info on async thread
|
|
|
|
// For large, "all fonts on system" data, data needed on a given platform
|
|
|
|
// (e.g. localized names, face names, cmaps) are loaded async.
|
|
|
|
|
|
|
|
// helper class for loading in font info on a separate async thread
|
2020-10-03 19:03:54 +03:00
|
|
|
// once async thread completes, completion process is run on the main
|
|
|
|
// thread's idle queue in short slices
|
2014-01-29 11:39:01 +04:00
|
|
|
|
|
|
|
class gfxFontInfoLoader {
|
|
|
|
public:
|
|
|
|
// state transitions:
|
|
|
|
// initial ---StartLoader with delay---> timer on delay
|
2020-10-03 19:03:54 +03:00
|
|
|
// initial ---StartLoader without delay---> timer off
|
|
|
|
// timer on delay ---LoaderTimerFire---> timer off
|
2014-01-29 11:39:01 +04:00
|
|
|
// timer on delay ---CancelLoader---> timer off
|
|
|
|
// timer off ---StartLoader with delay---> timer on delay
|
2020-10-03 19:03:54 +03:00
|
|
|
// timer off ---StartLoader without delay---> timer off
|
2014-01-29 11:39:01 +04:00
|
|
|
typedef enum {
|
|
|
|
stateInitial,
|
|
|
|
stateTimerOnDelay,
|
2014-01-29 11:39:01 +04:00
|
|
|
stateAsyncLoad,
|
2014-01-29 11:39:01 +04:00
|
|
|
stateTimerOff
|
|
|
|
} TimerState;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-03 19:03:54 +03:00
|
|
|
gfxFontInfoLoader() : mState(stateInitial) {
|
2014-08-01 23:37:53 +04:00
|
|
|
MOZ_COUNT_CTOR(gfxFontInfoLoader);
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
virtual ~gfxFontInfoLoader();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-03 19:03:54 +03:00
|
|
|
// start timer with an initial delay
|
|
|
|
void StartLoader(uint32_t aDelay);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-03 19:03:54 +03:00
|
|
|
// Finalize - async load complete, transfer data (on idle)
|
2014-01-29 11:39:01 +04:00
|
|
|
virtual void FinalizeLoader(FontInfoData* aFontInfo);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// cancel the timer and cleanup
|
|
|
|
void CancelLoader();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2020-10-03 14:56:08 +03:00
|
|
|
protected:
|
2020-10-03 19:03:54 +03:00
|
|
|
friend class FinalizeLoaderRunnable;
|
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
class ShutdownObserver : public nsIObserver {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-08-08 05:17:30 +04:00
|
|
|
explicit ShutdownObserver(gfxFontInfoLoader* aLoader) : mLoader(aLoader) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-06-23 22:49:07 +04:00
|
|
|
protected:
|
2019-04-11 15:36:51 +03:00
|
|
|
virtual ~ShutdownObserver() = default;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
gfxFontInfoLoader* mLoader;
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// CreateFontInfo - create platform-specific object used
|
|
|
|
// to load system-wide font info
|
|
|
|
virtual already_AddRefed<FontInfoData> CreateFontInfoData() {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// Init - initialization before async loader thread runs
|
2014-01-29 11:39:01 +04:00
|
|
|
virtual void InitLoader() = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// LoadFontInfo - transfer font info data within a time limit, return
|
|
|
|
// true when done
|
|
|
|
virtual bool LoadFontInfo() = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// Cleanup - finish and cleanup after done, including possible reflows
|
|
|
|
virtual void CleanupLoader() { mFontInfo = nullptr; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
static void DelayedStartCallback(nsITimer* aTimer, void* aThis) {
|
|
|
|
gfxFontInfoLoader* loader = static_cast<gfxFontInfoLoader*>(aThis);
|
2020-10-03 19:03:54 +03:00
|
|
|
loader->StartLoader(0);
|
2014-01-29 11:39:01 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
void LoadFontInfoTimerFire();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
void AddShutdownObserver();
|
2014-01-29 11:39:01 +04:00
|
|
|
void RemoveShutdownObserver();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
nsCOMPtr<nsIObserver> mObserver;
|
2014-01-29 11:39:01 +04:00
|
|
|
nsCOMPtr<nsIThread> mFontLoaderThread;
|
2014-01-29 11:39:01 +04:00
|
|
|
TimerState mState;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// after async font loader completes, data is stored here
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FontInfoData> mFontInfo;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-29 11:39:01 +04:00
|
|
|
// time spent on the loader thread
|
|
|
|
mozilla::TimeDuration mLoadTime;
|
2014-01-29 11:39:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* GFX_FONT_INFO_LOADER_H */
|