Skip to content

Commit

Permalink
Bug 1826412: Move the Linux Distro stuff to nsIGfxInfo also r=jfkthame
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrittervg committed Feb 5, 2024
1 parent 41e2755 commit c2bb94d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 71 deletions.
89 changes: 31 additions & 58 deletions gfx/thebes/gfxFcPlatformFontList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "mozilla/TimeStamp.h"
#include "nsGkAtoms.h"
#include "nsIConsoleService.h"
#include "nsIGfxInfo.h"
#include "mozilla/Components.h"
#include "nsString.h"
#include "nsStringFwd.h"
#include "nsUnicodeProperties.h"
Expand Down Expand Up @@ -1671,6 +1673,18 @@ void gfxFcPlatformFontList::ReadSystemFontList(dom::SystemFontList* retValue) {
}
}

static nsIGfxInfo::FontVisibilityDeviceDetermination sFontVisibilityDevice =
nsIGfxInfo::FontVisibilityDeviceDetermination::Unassigned;

void AssignFontVisibilityDevice() {
if (sFontVisibilityDevice ==
nsIGfxInfo::FontVisibilityDeviceDetermination::Unassigned) {
nsCOMPtr<nsIGfxInfo> gfxInfo = components::GfxInfo::Service();
NS_ENSURE_SUCCESS_VOID(
gfxInfo->GetFontVisibilityDetermination(&sFontVisibilityDevice));
}
}

// Per family array of faces.
class FacesData {
using FaceInitArray = AutoTArray<fontlist::Face::InitData, 8>;
Expand Down Expand Up @@ -1957,7 +1971,10 @@ void gfxFcPlatformFontList::InitSharedFontListForPlatform() {
FcFontSet* systemFonts = FcConfigGetFonts(nullptr, FcSetSystem);
auto numBaseFamilies = addFontSetFamilies(systemFonts, policy.get(),
/* aAppFonts = */ false);
if (GetDistroID() != DistroID::Unknown && numBaseFamilies < 3) {
AssignFontVisibilityDevice();
if (numBaseFamilies < 3 &&
sFontVisibilityDevice !=
nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Unknown) {
// If we found fewer than 3 known FontVisibility::Base families in the
// system (ignoring app-bundled fonts), we must be dealing with a very
// non-standard configuration; disable the distro-specific font
Expand All @@ -1983,72 +2000,27 @@ void gfxFcPlatformFontList::InitSharedFontListForPlatform() {
}
}

gfxFcPlatformFontList::DistroID gfxFcPlatformFontList::GetDistroID() const {
// Helper called to initialize sResult the first time this is used.
auto getDistroID = []() {
DistroID result = DistroID::Unknown;
int versionMajor = 0;
FILE* fp = fopen("/etc/os-release", "r");
if (fp) {
char buf[512];
while (fgets(buf, sizeof(buf), fp)) {
if (strncmp(buf, "VERSION_ID=\"", 12) == 0) {
versionMajor = strtol(buf + 12, nullptr, 10);
if (result != DistroID::Unknown) {
break;
}
}
if (strncmp(buf, "ID=", 3) == 0) {
if (strncmp(buf + 3, "ubuntu", 6) == 0) {
result = DistroID::Ubuntu_any;
} else if (strncmp(buf + 3, "fedora", 6) == 0) {
result = DistroID::Fedora_any;
}
if (versionMajor) {
break;
}
}
}
fclose(fp);
}
if (result == DistroID::Ubuntu_any) {
if (versionMajor == 20) {
result = DistroID::Ubuntu_20;
} else if (versionMajor == 22) {
result = DistroID::Ubuntu_22;
}
} else if (result == DistroID::Fedora_any) {
if (versionMajor == 38) {
result = DistroID::Fedora_38;
} else if (versionMajor == 39) {
result = DistroID::Fedora_39;
}
}
return result;
};
static DistroID sResult = getDistroID();
return sResult;
}

FontVisibility gfxFcPlatformFontList::GetVisibilityForFamily(
const nsACString& aName) const {
auto distro = GetDistroID();
switch (distro) {
case DistroID::Ubuntu_any:
case DistroID::Ubuntu_22:
AssignFontVisibilityDevice();

switch (sFontVisibilityDevice) {
case nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Ubuntu_any:
case nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Ubuntu_22:
if (FamilyInList(aName, kBaseFonts_Ubuntu_22_04)) {
return FontVisibility::Base;
}
if (FamilyInList(aName, kLangFonts_Ubuntu_22_04)) {
return FontVisibility::LangPack;
}
if (distro == DistroID::Ubuntu_22) {
if (sFontVisibilityDevice ==
nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Ubuntu_22) {
return FontVisibility::User;
}
// For Ubuntu_any, we fall through to also check the 20_04 lists.
[[fallthrough]];

case DistroID::Ubuntu_20:
case nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Ubuntu_20:
if (FamilyInList(aName, kBaseFonts_Ubuntu_20_04)) {
return FontVisibility::Base;
}
Expand All @@ -2057,18 +2029,19 @@ FontVisibility gfxFcPlatformFontList::GetVisibilityForFamily(
}
return FontVisibility::User;

case DistroID::Fedora_any:
case DistroID::Fedora_39:
case nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Fedora_any:
case nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Fedora_39:
if (FamilyInList(aName, kBaseFonts_Fedora_39)) {
return FontVisibility::Base;
}
if (distro == DistroID::Fedora_39) {
if (sFontVisibilityDevice ==
nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Fedora_39) {
return FontVisibility::User;
}
// For Fedora_any, fall through to also check Fedora 38 list.
[[fallthrough]];

case DistroID::Fedora_38:
case nsIGfxInfo::FontVisibilityDeviceDetermination::Linux_Fedora_38:
if (FamilyInList(aName, kBaseFonts_Fedora_38)) {
return FontVisibility::Base;
}
Expand Down
13 changes: 0 additions & 13 deletions gfx/thebes/gfxFcPlatformFontList.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,6 @@ class gfxFcPlatformFontList final : public gfxPlatformFontList {
nsAtom* aLanguage = nullptr)
MOZ_REQUIRES(mLock) override;

enum class DistroID : int8_t {
Unknown,
Ubuntu_any,
Ubuntu_20,
Ubuntu_22,
Fedora_any,
Fedora_38,
Fedora_39,
// To be extended with any distros that ship a useful base set of fonts
// that we want to explicitly support.
};
DistroID GetDistroID() const; // -> DistroID::Unknown if we can't tell

FontVisibility GetVisibilityForFamily(const nsACString& aName) const;

gfxFontFamily* CreateFontFamily(const nsACString& aName,
Expand Down
53 changes: 53 additions & 0 deletions widget/GfxInfoBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,59 @@ std::pair<Device, nsString> GfxInfoBase::GetFontVisibilityDeterminationPair() {
androidProduct.get(), androidProductIsChromebook ? "yes" : "no");

#elif defined(XP_LINUX)
ret.first = Device::Linux_Unknown;

long versionMajor = 0;
FILE* fp = fopen("/etc/os-release", "r");
if (fp) {
char buf[512];
while (fgets(buf, sizeof(buf), fp)) {
if (strncmp(buf, "VERSION_ID=\"", 12) == 0) {
ret.second.AppendPrintf("VERSION_ID=%.11s", buf + 11);
versionMajor = strtol(buf + 12, nullptr, 10);
if (ret.first != Device::Linux_Unknown) {
break;
}
}

if (strncmp(buf, "ID=", 3) == 0) {
ret.second.AppendPrintf("ID=%.6s", buf + 3);
if (strncmp(buf + 3, "ubuntu", 6) == 0) {
ret.first = Device::Linux_Ubuntu_any;
} else if (strncmp(buf + 3, "fedora", 6) == 0) {
ret.first = Device::Linux_Fedora_any;
}

if (versionMajor) {
break;
}
}
}
fclose(fp);
}
if (ret.first == Device::Linux_Ubuntu_any) {
if (versionMajor == 20) {
ret.first = Device::Linux_Ubuntu_20;
ret.second.Insert(u"Ubuntu 20 - ", 0);
} else if (versionMajor == 22) {
ret.first = Device::Linux_Ubuntu_22;
ret.second.Insert(u"Ubuntu 22 - ", 0);
} else {
ret.second.Insert(u"Ubuntu Unknown - ", 0);
}
} else if (ret.first == Device::Linux_Fedora_any) {
if (versionMajor == 38) {
ret.first = Device::Linux_Fedora_38;
ret.second.Insert(u"Fedora 38 - ", 0);
} else if (versionMajor == 39) {
ret.first = Device::Linux_Fedora_39;
ret.second.Insert(u"Fedora 39 - ", 0);
} else {
ret.second.Insert(u"Fedora Unknown - ", 0);
}
} else {
ret.second.Insert(u"Linux Unknown - ", 0);
}

#elif defined(XP_MACOSX)
ret.first = Device::MacOS_Platform;
Expand Down

0 comments on commit c2bb94d

Please sign in to comment.