Skip to content

Instantly share code, notes, and snippets.

@marguerite
Created February 18, 2025 13:00
Show Gist options
  • Save marguerite/edee185fa2aa005d32ec2061def5491c to your computer and use it in GitHub Desktop.
Save marguerite/edee185fa2aa005d32ec2061def5491c to your computer and use it in GitHub Desktop.
Marguerite's skia fontconfig patch
diff -urN ports.orig/SkFontConfigInterface_direct.cpp ports/SkFontConfigInterface_direct.cpp
--- ports.orig/SkFontConfigInterface_direct.cpp 2025-02-18 20:51:46.586862156 +0800
+++ ports/SkFontConfigInterface_direct.cpp 2025-02-18 20:51:43.910244016 +0800
@@ -588,6 +588,68 @@
return match;
}
+FcPattern* SkFontConfigInterfaceDirect::getFontPattern(FcConfig* config, const char* family_name, int weight) {
+ FcFontSet* system = FcConfigGetFonts(config, FcSetSystem);
+ if (system) {
+ for (int i = 0; i < system->nfont; i++) {
+ FcPattern* pat = system->fonts[i];
+
+ const char* font_format = get_string(pat, FC_FONTFORMAT);
+ if (font_format
+ && 0 != strcmp(font_format, kFontFormatTrueType)
+ && 0 != strcmp(font_format, kFontFormatCFF))
+ {
+ continue;
+ }
+
+ const char* name = get_string(pat, FC_FAMILY);
+ int weight1 = get_int(pat, FC_WEIGHT, 0);
+ if (strcasecmp(name, family_name) == 0 && weight1 == weight) {
+ return pat;
+ }
+ }
+ }
+
+ FcFontSetDestroy(system);
+
+ FcFontSet* application = FcConfigGetFonts(config, FcSetApplication);
+ if (application) {
+ for (int i = 0; i < application->nfont; i++) {
+ FcPattern* pat = application->fonts[i];
+
+ const char* font_format = get_string(pat, FC_FONTFORMAT);
+ if (font_format
+ && 0 != strcmp(font_format, kFontFormatTrueType)
+ && 0 != strcmp(font_format, kFontFormatCFF))
+ {
+ continue;
+ }
+
+ const char* name = get_string(pat, FC_FAMILY);
+ int weight1 = get_int(pat, FC_WEIGHT, 0);
+ if (strcasecmp(name, family_name) == 0 && weight1 == weight) {
+ return pat;
+ }
+ }
+ }
+
+ FcFontSetDestroy(application);
+
+ return NULL;
+}
+
+FcPattern* SkFontConfigInterfaceDirect::getFirstInstalledFamilyName(FcConfig* config, FcPattern* pattern) {
+ for (int i = 0; i < FcPatternObjectCount(pattern); i++) {
+ const char* name = get_string(pattern, FC_FAMILY);
+ int weight = get_int(pattern, FC_WEIGHT, 0);
+ FcPattern* pat = getFontPattern(config, name, weight);
+ if (pat)
+ return pat;
+ }
+
+ return NULL;
+}
+
bool SkFontConfigInterfaceDirect::matchFamilyName(const char familyName[],
SkFontStyle style,
FontIdentity* outIdentity,
@@ -656,15 +718,34 @@
//
// However, we special-case fallback fonts; see IsFallbackFontAllowed().
+ // the first value in donePattern
const char* post_config_family = get_string(pattern, FC_FAMILY);
if (!post_config_family) {
// we can just continue with an empty name, e.g. default font
- post_config_family = "";
+ //post_config_family = "";
+ FcPatternDestroy(pattern);
+ return false;
}
printf("post_config_family: %s\n", post_config_family);
- FcResult result;
+ // the `match` pattern here is a font_pattern, no difference with the one created by FcFontSort and font_set loop
+ FcPattern* match;
+
+ if (IsFallbackFontAllowed(familyStr)) {
+ match = getFirstInstalledFamilyName(fc, pattern);
+ post_config_family = get_string(match, FC_FAMILY);
+ } else {
+ match = getFontPattern(fc, post_config_family, get_int(pattern, FC_WEIGHT, 0));
+ }
+
+ FcPatternDestroy(pattern);
+
+ if (!match) {
+ return false;
+ }
+
+ /*FcResult result;
FcFontSet* font_set = FcFontSort(fc, pattern, 0, nullptr, &result);
if (!font_set) {
printf("FcFontSort didn't get a FontSet\n");
@@ -693,12 +774,13 @@
return false;
}
printf("Got post_config_family: %s\n", post_config_family);
-
+ */
const char* c_filename = get_string(match, FC_FILE);
if (!c_filename) {
- FcFontSetDestroy(font_set);
+ //FcFontSetDestroy(font_set);
return false;
}
+
const char* sysroot = (const char*)FcConfigGetSysRoot(fc);
SkString resolvedFilename;
if (sysroot) {
@@ -709,7 +791,7 @@
int face_index = get_int(match, FC_INDEX, 0);
- FcFontSetDestroy(font_set);
+ //FcFontSetDestroy(font_set);
if (outIdentity) {
outIdentity->fTTCIndex = face_index;
diff -urN ports.orig/SkFontConfigInterface_direct.h ports/SkFontConfigInterface_direct.h
--- ports.orig/SkFontConfigInterface_direct.h 2025-02-18 20:51:50.413459431 +0800
+++ ports/SkFontConfigInterface_direct.h 2025-02-18 20:51:52.886747922 +0800
@@ -30,6 +30,9 @@
SkStreamAsset* openStream(const FontIdentity&) override;
+ FcPattern* getFontPattern(FcConfig* config, const char* family_name, int weight);
+ FcPattern* getFirstInstalledFamilyName(FcConfig* config, FcPattern* pattern);
+
protected:
virtual bool isAccessible(const char* filename);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment