Bug 1665373 - Don't use nsAutoRef in fontconfig font list. r=jfkthame

There was a weird mix of nsAutoRef and nsCountedRef going on... Use RefPtr<>
for ref-counted things, and UniquePtr for non-refcounted ones.

Differential Revision: https://phabricator.services.mozilla.com/D90400
This commit is contained in:
Emilio Cobos Álvarez 2020-09-16 23:11:13 +00:00
Родитель 8e6234de91
Коммит b61d81f571
2 изменённых файлов: 43 добавлений и 42 удалений

Просмотреть файл

@ -70,18 +70,6 @@ using namespace mozilla::unicode;
#define LOG_CMAPDATA_ENABLED() \
MOZ_LOG_TEST(gfxPlatform::GetLog(eGfxLog_cmapdata), LogLevel::Debug)
template <>
class nsAutoRefTraits<FcFontSet> : public nsPointerRefTraits<FcFontSet> {
public:
static void Release(FcFontSet* ptr) { FcFontSetDestroy(ptr); }
};
template <>
class nsAutoRefTraits<FcObjectSet> : public nsPointerRefTraits<FcObjectSet> {
public:
static void Release(FcObjectSet* ptr) { FcObjectSetDestroy(ptr); }
};
static const FcChar8* ToFcChar8Ptr(const char* aStr) {
return reinterpret_cast<const FcChar8*>(aStr);
}
@ -258,7 +246,7 @@ gfxFontEntry* gfxFontconfigFontEntry::Clone() const {
return new gfxFontconfigFontEntry(Name(), mFontPattern, mIgnoreFcCharmap);
}
static FcPattern* CreatePatternForFace(FT_Face aFace) {
static already_AddRefed<FcPattern> CreatePatternForFace(FT_Face aFace) {
// Use fontconfig to fill out the pattern from the FTFace.
// The "file" argument cannot be nullptr (in fontconfig-2.6.0 at
// least). The dummy file passed here is removed below.
@ -269,10 +257,11 @@ static FcPattern* CreatePatternForFace(FT_Face aFace) {
// "blanks", effectively assuming that, if the font has a blank glyph,
// then the author intends any associated character to be rendered
// blank.
FcPattern* pattern = FcFreeTypeQueryFace(aFace, ToFcChar8Ptr(""), 0, nullptr);
RefPtr<FcPattern> pattern =
dont_AddRef(FcFreeTypeQueryFace(aFace, ToFcChar8Ptr(""), 0, nullptr));
// given that we have a FT_Face, not really sure this is possible...
if (!pattern) {
pattern = FcPatternCreate();
pattern = dont_AddRef(FcPatternCreate());
}
FcPatternDel(pattern, FC_FILE);
FcPatternDel(pattern, FC_INDEX);
@ -281,7 +270,7 @@ static FcPattern* CreatePatternForFace(FT_Face aFace) {
// that when creating a cairo font face.
FcPatternAddFTFace(pattern, FC_FT_FACE, aFace);
return pattern;
return pattern.forget();
}
static already_AddRefed<SharedFTFace> CreateFaceForPattern(
@ -790,7 +779,7 @@ static double ChooseFontSize(gfxFontconfigFontEntry* aEntry,
gfxFont* gfxFontconfigFontEntry::CreateFontInstance(
const gfxFontStyle* aFontStyle) {
nsAutoRef<FcPattern> pattern(FcPatternCreate());
RefPtr<FcPattern> pattern = dont_AddRef(FcPatternCreate());
if (!pattern) {
NS_WARNING("Failed to create Fontconfig pattern for font instance");
return nullptr;
@ -825,8 +814,8 @@ gfxFont* gfxFontconfigFontEntry::CreateFontInstance(
}
PreparePattern(pattern, aFontStyle->printerFont);
nsAutoRef<FcPattern> renderPattern(
FcFontRenderPrepare(nullptr, pattern, mFontPattern));
RefPtr<FcPattern> renderPattern =
dont_AddRef(FcFontRenderPrepare(nullptr, pattern, mFontPattern));
if (!renderPattern) {
NS_WARNING("Failed to prepare Fontconfig pattern for font instance");
return nullptr;
@ -1061,8 +1050,7 @@ void gfxFontconfigFontFamily::AddFontPattern(FcPattern* aFontPattern) {
}
}
nsCountedRef<FcPattern> pattern(aFontPattern);
mFontPatterns.AppendElement(pattern);
mFontPatterns.AppendElement(aFontPattern);
}
static const double kRejectDistance = 10000.0;
@ -1801,12 +1789,12 @@ static void GetSystemFontList(nsTArray<nsString>& aListOfFonts,
nsAtom* aLangGroup) {
aListOfFonts.Clear();
nsAutoRef<FcPattern> pat(FcPatternCreate());
RefPtr<FcPattern> pat = dont_AddRef(FcPatternCreate());
if (!pat) {
return;
}
nsAutoRef<FcObjectSet> os(FcObjectSetBuild(FC_FAMILY, nullptr));
UniquePtr<FcObjectSet> os(FcObjectSetBuild(FC_FAMILY, nullptr));
if (!os) {
return;
}
@ -1820,7 +1808,7 @@ static void GetSystemFontList(nsTArray<nsString>& aListOfFonts,
FcPatternAddString(pat, FC_LANG, ToFcChar8Ptr(fcLang.get()));
}
nsAutoRef<FcFontSet> fs(FcFontList(nullptr, pat, os));
UniquePtr<FcFontSet> fs(FcFontList(nullptr, pat, os.get()));
if (!fs) {
return;
}
@ -1987,13 +1975,13 @@ bool gfxFcPlatformFontList::FindAndAddFamilies(
// It wasn't in the cache, so we need to ask fontconfig...
const FcChar8* kSentinelName = ToFcChar8Ptr("-moz-sentinel");
FcChar8* sentinelFirstFamily = nullptr;
nsAutoRef<FcPattern> sentinelSubst(FcPatternCreate());
RefPtr<FcPattern> sentinelSubst = dont_AddRef(FcPatternCreate());
FcPatternAddString(sentinelSubst, FC_FAMILY, kSentinelName);
FcConfigSubstitute(nullptr, sentinelSubst, FcMatchPattern);
FcPatternGetString(sentinelSubst, FC_FAMILY, 0, &sentinelFirstFamily);
// substitutions for font, -moz-sentinel pattern
nsAutoRef<FcPattern> fontWithSentinel(FcPatternCreate());
RefPtr<FcPattern> fontWithSentinel = dont_AddRef(FcPatternCreate());
FcPatternAddString(fontWithSentinel, FC_FAMILY,
ToFcChar8Ptr(familyName.get()));
FcPatternAddString(fontWithSentinel, FC_FAMILY, kSentinelName);
@ -2036,12 +2024,12 @@ bool gfxFcPlatformFontList::GetStandardFamilyName(const nsCString& aFontName,
return true;
}
nsAutoRef<FcPattern> pat(FcPatternCreate());
RefPtr<FcPattern> pat = dont_AddRef(FcPatternCreate());
if (!pat) {
return true;
}
nsAutoRef<FcObjectSet> os(FcObjectSetBuild(FC_FAMILY, nullptr));
UniquePtr<FcObjectSet> os(FcObjectSetBuild(FC_FAMILY, nullptr));
if (!os) {
return true;
}
@ -2049,7 +2037,7 @@ bool gfxFcPlatformFontList::GetStandardFamilyName(const nsCString& aFontName,
// add the family name to the pattern
FcPatternAddString(pat, FC_FAMILY, ToFcChar8Ptr(aFontName.get()));
nsAutoRef<FcFontSet> givenFS(FcFontList(nullptr, pat, os));
UniquePtr<FcFontSet> givenFS(FcFontList(nullptr, pat, os.get()));
if (!givenFS) {
return true;
}
@ -2088,7 +2076,7 @@ bool gfxFcPlatformFontList::GetStandardFamilyName(const nsCString& aFontName,
FcPatternDel(pat, FC_FAMILY);
FcPatternAddString(pat, FC_FAMILY, (FcChar8*)candidates[j].get());
nsAutoRef<FcFontSet> candidateFS(FcFontList(nullptr, pat, os));
UniquePtr<FcFontSet> candidateFS(FcFontList(nullptr, pat, os.get()));
if (!candidateFS) {
return true;
}
@ -2198,7 +2186,7 @@ gfxPlatformFontList::PrefFontList* gfxFcPlatformFontList::FindGenericFamilies(
}
// if not found, ask fontconfig to pick the appropriate font
nsAutoRef<FcPattern> genericPattern(FcPatternCreate());
RefPtr<FcPattern> genericPattern = dont_AddRef(FcPatternCreate());
FcPatternAddString(genericPattern, FC_FAMILY, ToFcChar8Ptr(aGeneric.get()));
// -- prefer scalable fonts
@ -2215,7 +2203,7 @@ gfxPlatformFontList::PrefFontList* gfxFcPlatformFontList::FindGenericFamilies(
// -- sort to get the closest matches
FcResult result;
nsAutoRef<FcFontSet> faces(
UniquePtr<FcFontSet> faces(
FcFontSort(nullptr, genericPattern, FcFalse, nullptr, &result));
if (!faces) {

Просмотреть файл

@ -10,7 +10,8 @@
#include "gfxPlatformFontList.h"
#include "mozilla/FontPropertyTypes.h"
#include "mozilla/mozalloc.h"
#include "nsAutoRef.h"
#include "mozilla/RefPtr.h"
#include "mozilla/UniquePtr.h"
#include "nsClassHashtable.h"
#include <fontconfig/fontconfig.h>
@ -27,22 +28,35 @@ namespace mozilla {
namespace dom {
class SystemFontListEntry;
};
}; // namespace mozilla
template <>
class nsAutoRefTraits<FcPattern> : public nsPointerRefTraits<FcPattern> {
class RefPtrTraits<FcPattern> {
public:
static void Release(FcPattern* ptr) { FcPatternDestroy(ptr); }
static void AddRef(FcPattern* ptr) { FcPatternReference(ptr); }
};
template <>
class nsAutoRefTraits<FcConfig> : public nsPointerRefTraits<FcConfig> {
class RefPtrTraits<FcConfig> {
public:
static void Release(FcConfig* ptr) { FcConfigDestroy(ptr); }
static void AddRef(FcConfig* ptr) { FcConfigReference(ptr); }
};
template <>
class DefaultDelete<FcFontSet> {
public:
void operator()(FcFontSet* aPtr) { FcFontSetDestroy(aPtr); }
};
template <>
class DefaultDelete<FcObjectSet> {
public:
void operator()(FcObjectSet* aPtr) { FcObjectSetDestroy(aPtr); }
};
}; // namespace mozilla
// The names for the font entry and font classes should really
// the common 'Fc' abbreviation but the gfxPangoFontGroup code already
// defines versions of these, so use the verbose name for now.
@ -100,7 +114,7 @@ class gfxFontconfigFontEntry final : public gfxFT2FontEntryBase {
nsTArray<uint8_t>& aBuffer) override;
// pattern for a single face of a family
nsCountedRef<FcPattern> mFontPattern;
RefPtr<FcPattern> mFontPattern;
// FTFace - initialized when needed
RefPtr<mozilla::gfx::SharedFTFace> mFTFace;
@ -185,7 +199,7 @@ class gfxFontconfigFontFamily final : public gfxFontFamily {
// helper for FilterForFontList
bool SupportsLangGroup(nsAtom* aLangGroup) const;
nsTArray<nsCountedRef<FcPattern>> mFontPatterns;
nsTArray<RefPtr<FcPattern>> mFontPatterns;
bool mContainsAppFonts;
bool mHasNonScalableFaces;
@ -211,7 +225,7 @@ class gfxFontconfigFont final : public gfxFT2FontBase {
private:
virtual ~gfxFontconfigFont();
nsCountedRef<FcPattern> mPattern;
RefPtr<FcPattern> mPattern;
};
class gfxFcPlatformFontList final : public gfxPlatformFontList {
@ -336,8 +350,7 @@ class gfxFcPlatformFontList final : public gfxPlatformFontList {
// to avoid enumerating all fonts, maintain a mapping of local font
// names to family
nsBaseHashtable<nsCStringHashKey, nsCountedRef<FcPattern>, FcPattern*>
mLocalNames;
nsBaseHashtable<nsCStringHashKey, RefPtr<FcPattern>, FcPattern*> mLocalNames;
// caching generic/lang ==> font family list
nsClassHashtable<nsCStringHashKey, PrefFontList> mGenericMappings;
@ -352,7 +365,7 @@ class gfxFcPlatformFontList final : public gfxPlatformFontList {
mFcSubstituteCache;
nsCOMPtr<nsITimer> mCheckFontUpdatesTimer;
nsCountedRef<FcConfig> mLastConfig;
RefPtr<FcConfig> mLastConfig;
// By default, font prefs under Linux are set to simply lookup
// via fontconfig the appropriate font for serif/sans-serif/monospace.