static AliasStrength strengthOfFirstAlias(const FcPattern& original) { // Ideally there would exist a call like // FcResult FcPatternIsWeak(pattern, object, id, FcBool* isWeak); // // However, there is no such call and as of Fc 2.11.0 even FcPatternEquals ignores the weak bit. // Currently, the only reliable way of finding the weak bit is by its effect on matching. // The weak bit only affects the matching of FC_FAMILY and FC_POSTSCRIPT_NAME object values. // A element with the weak bit is scored after FC_LANG, without the weak bit is scored before. // Note that the weak bit is stored on the element, not on the value it holds. FcValue value; FcResult result = FcPatternGet(&original, FC_FAMILY, 0, &value); if (result != FcResultMatch) return AliasStrength::Done; RefPtr<FcPattern> pattern = adoptRef(FcPatternDuplicate(&original)); FcBool hasMultipleFamilies = true; while (hasMultipleFamilies) hasMultipleFamilies = FcPatternRemove(pattern.get(), FC_FAMILY, 1); // Create a font set with two patterns. // 1. the same FC_FAMILY as pattern and a lang object with only 'nomatchlang'. // 2. a different FC_FAMILY from pattern and a lang object with only 'matchlang'. FcUniquePtr<FcFontSet> fontSet(FcFontSetCreate()); FcUniquePtr<FcLangSet> strongLangSet(FcLangSetCreate()); FcLangSetAdd(strongLangSet.get(), reinterpret_cast<const FcChar8*>("nomatchlang")); // Ownership of this FcPattern will be transferred with FcFontSetAdd. FcPattern* strong = FcPatternDuplicate(pattern.get()); FcPatternAddLangSet(strong, FC_LANG, strongLangSet.get()); FcUniquePtr<FcLangSet> weakLangSet(FcLangSetCreate()); FcLangSetAdd(weakLangSet.get(), reinterpret_cast<const FcChar8*>("matchlang")); // Ownership of this FcPattern will be transferred via FcFontSetAdd. FcPattern* weak = FcPatternCreate(); FcPatternAddString(weak, FC_FAMILY, reinterpret_cast<const FcChar8*>("nomatchstring")); FcPatternAddLangSet(weak, FC_LANG, weakLangSet.get()); FcFontSetAdd(fontSet.get(), strong); FcFontSetAdd(fontSet.get(), weak); // Add 'matchlang' to the copy of the pattern. FcPatternAddLangSet(pattern.get(), FC_LANG, weakLangSet.get()); // Run a match against the copy of the pattern. // If the first element was weak, then we should match the pattern with 'matchlang'. // If the first element was strong, then we should match the pattern with 'nomatchlang'. // Note that this config is only used for FcFontRenderPrepare, which we don't even want. // However, there appears to be no way to match/sort without it. RefPtr<FcConfig> config = adoptRef(FcConfigCreate()); FcFontSet* fontSets[1] = { fontSet.get() }; RefPtr<FcPattern> match = adoptRef(FcFontSetMatch(config.get(), fontSets, 1, pattern.get(), &result)); FcLangSet* matchLangSet; FcPatternGetLangSet(match.get(), FC_LANG, 0, &matchLangSet); return FcLangEqual == FcLangSetHasLang(matchLangSet, reinterpret_cast<const FcChar8*>("matchlang")) ? AliasStrength::Weak : AliasStrength::Strong; }
FcFontSet *fcinfo_language_font_index(const FcChar8 *lang, const FcPattern *filter) { FcFontSet *fontset; FcPattern *pattern; FcLangSet *langset; FcInit(); pattern = FcPatternDuplicate(filter); langset = FcLangSetCreate(); FcLangSetAdd(langset, lang); FcPatternAddLangSet(pattern, FC_LANG, langset); fontset = fcinfo(NULL, pattern, FcFalse, 2, FC_FAMILY, FC_STYLE); /*result = fcinfo_match(fontset, filter, FcFalse); this is redundant right now */ /* but language index could want to display spcimen or so in the future be sure to include also size, charset and file FcObjects when turning this on: see above call of fcinfo() */ /* we don't need to remove ethio16f-uni.pcf, johabg16.pcf and similar as they have reported empty charset, so they do not cover any language */ FcPatternDestroy(pattern); FcLangSetDestroy(langset); return fontset; }
QStringList QFontconfigDatabase::fallbacksForFamily(const QString family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script) const { QStringList fallbackFamilies; FcPattern *pattern = FcPatternCreate(); if (!pattern) return fallbackFamilies; FcValue value; value.type = FcTypeString; QByteArray cs = family.toUtf8(); value.u.s = (const FcChar8 *)cs.data(); FcPatternAdd(pattern,FC_FAMILY,value,true); int slant_value = FC_SLANT_ROMAN; if (style == QFont::StyleItalic) slant_value = FC_SLANT_ITALIC; else if (style == QFont::StyleOblique) slant_value = FC_SLANT_OBLIQUE; FcPatternAddInteger(pattern, FC_SLANT, slant_value); if (script != QUnicodeTables::Common && *specialLanguages[script] != '\0') { Q_ASSERT(script < QUnicodeTables::ScriptCount); FcLangSet *ls = FcLangSetCreate(); FcLangSetAdd(ls, (const FcChar8*)specialLanguages[script]); FcPatternAddLangSet(pattern, FC_LANG, ls); FcLangSetDestroy(ls); } const char *stylehint = getFcFamilyForStyleHint(styleHint); if (stylehint) { value.u.s = (const FcChar8 *)stylehint; FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); } FcConfigSubstitute(0, pattern, FcMatchPattern); FcDefaultSubstitute(pattern); FcResult result = FcResultMatch; FcFontSet *fontSet = FcFontSort(0,pattern,FcFalse,0,&result); FcPatternDestroy(pattern); if (fontSet) { for (int i = 0; i < fontSet->nfont; i++) { FcChar8 *value = 0; if (FcPatternGetString(fontSet->fonts[i], FC_FAMILY, 0, &value) != FcResultMatch) continue; // capitalize(value); QString familyName = QString::fromUtf8((const char *)value); if (!fallbackFamilies.contains(familyName,Qt::CaseInsensitive)) { fallbackFamilies << familyName; } } FcFontSetDestroy(fontSet); } // qDebug() << "fallbackFamilies for:" << family << fallbackFamilies; return fallbackFamilies; }
FcStrSet *fcinfo_languages(const FcPattern *filter) { FcObjectSet *objectset; FcFontSet *fontset; FcLangSet *union_langset; FcStrSet *result; int f; FcInit(); objectset = FcObjectSetBuild(FC_LANG, NULL); fontset = FcFontList(NULL, (FcPattern *)filter, objectset); FcObjectSetDestroy(objectset); union_langset = FcLangSetCreate(); for (f = 0; f < fontset->nfont; f++) { FcLangSet *langset; FcStrList *strlist; FcChar8 *lang; if (FcPatternGetLangSet(fontset->fonts[f], FC_LANG, 0, &langset) != FcResultMatch) continue; if (! langset) continue; strlist = FcStrListCreate(FcLangSetGetLangs(langset)); while ((lang = FcStrListNext(strlist))) if (FcLangSetHasLang(union_langset, lang)) FcLangSetAdd(union_langset, FcStrCopy(lang)); } result = FcLangSetGetLangs(union_langset); FcFontSetDestroy(fontset); FcLangSetDestroy(union_langset); return result; }
int BC_Resources::init_fontconfig(const char *search_path) { if( fontlist ) return 0; fontlist = new ArrayList<BC_FontEntry*>; #define get_str(str,sep,ptr,cond) do { char *out = str; \ while( *ptr && !strchr(sep,*ptr) && (cond) ) *out++ = *ptr++; \ *out = 0; \ } while(0) #define skip_str(sep, ptr) do { \ while( *ptr && strchr(sep,*ptr) ) ++ptr; \ } while(0) char find_command[BCTEXTLEN]; sprintf(find_command, "find %s -name 'fonts.dir' -print -exec cat {} \\;", search_path); FILE *in = popen(find_command, "r"); FT_Library freetype_library = 0; // FT_Face freetype_face = 0; // FT_Init_FreeType(&freetype_library); char line[BCTEXTLEN], current_dir[BCTEXTLEN]; current_dir[0] = 0; while( !feof(in) && fgets(line, BCTEXTLEN, in) ) { if(!strlen(line)) break; char *in_ptr = line; // Get current directory if(line[0] == '/') { get_str(current_dir, "\n", in_ptr,1); for( int i=strlen(current_dir); --i>=0 && current_dir[i]!='/'; ) current_dir[i] = 0; continue; } //printf("TitleMain::build_fonts %s\n", line); BC_FontEntry *entry = new BC_FontEntry; char string[BCTEXTLEN]; // Path get_str(string, "\n", in_ptr, in_ptr[0]!=' ' || in_ptr[1]!='-'); entry->path = cstrcat(2, current_dir, string); // Foundary skip_str(" -", in_ptr); get_str(string, " -\n", in_ptr, 1); if( !string[0] ) { delete entry; continue; } entry->foundry = cstrdup(string); if(*in_ptr == '-') in_ptr++; // Family get_str(string, "-\n", in_ptr, 1); if( !string[0] ) { delete entry; continue; } entry->family = cstrdup(string); if(*in_ptr == '-') in_ptr++; // Weight get_str(string, "-\n", in_ptr, 1); entry->weight = cstrdup(string); if(*in_ptr == '-') in_ptr++; // Slant get_str(string, "-\n", in_ptr, 1); entry->slant = cstrdup(string); if(*in_ptr == '-') in_ptr++; // SWidth get_str(string, "-\n", in_ptr, 1); entry->swidth = cstrdup(string); if(*in_ptr == '-') in_ptr++; // Adstyle get_str(string, "-\n", in_ptr, 1); entry->adstyle = cstrdup(string); if(*in_ptr == '-') in_ptr++; // pixelsize get_str(string, "-\n", in_ptr, 1); entry->pixelsize = atol(string); if(*in_ptr == '-') in_ptr++; // pointsize get_str(string, "-\n", in_ptr, 1); entry->pointsize = atol(string); if(*in_ptr == '-') in_ptr++; // xres get_str(string, "-\n", in_ptr, 1); entry->xres = atol(string); if(*in_ptr == '-') in_ptr++; // yres get_str(string, "-\n", in_ptr, 1); entry->yres = atol(string); if(*in_ptr == '-') in_ptr++; // spacing get_str(string, "-\n", in_ptr, 1); entry->spacing = cstrdup(string); if(*in_ptr == '-') in_ptr++; // avg_width get_str(string, "-\n", in_ptr, 1); entry->avg_width = atol(string); if(*in_ptr == '-') in_ptr++; // registry get_str(string, "-\n", in_ptr, 1); entry->registry = cstrdup(string); if(*in_ptr == '-') in_ptr++; // encoding get_str(string, "-\n", in_ptr, 1); entry->encoding = cstrdup(string); if(*in_ptr == '-') in_ptr++; // Add to list //printf("TitleMain::build_fonts 1 %s\n", entry->path); // This takes a real long time to do. Instead just take all fonts // if(!load_freetype_face(freetype_library, // freetype_face, entry->path) ) // Fix parameters sprintf(line, "%s (%s)", entry->family, entry->foundry); entry->displayname = cstrdup(line); if(!strcasecmp(entry->weight, "demibold")) { entry->fixed_style |= BC_FONT_BOLD; entry->style |= FL_WEIGHT_DEMIBOLD; } else if(!strcasecmp(entry->weight, "bold")) { entry->fixed_style |= BC_FONT_BOLD; entry->style |= FL_WEIGHT_BOLD; } else { entry->style |= FL_WEIGHT_NORMAL; } if(!strcasecmp(entry->slant, "r")) { entry->style |= FL_SLANT_ROMAN; } else if(!strcasecmp(entry->slant, "i")) { entry->style |= FL_SLANT_ITALIC; entry->fixed_style |= BC_FONT_ITALIC; } else if(!strcasecmp(entry->slant, "o")) { entry->style |= FL_SLANT_OBLIQUE; entry->fixed_style |= BC_FONT_ITALIC; } if(!strcasecmp(entry->swidth, "normal")) entry->style = FL_WIDTH_NORMAL; else if(!strcasecmp(entry->swidth, "ultracondensed")) entry->style = FL_WIDTH_ULTRACONDENSED; else if(!strcasecmp(entry->swidth, "extracondensed")) entry->style = FL_WIDTH_EXTRACONDENSED; else if(!strcasecmp(entry->swidth, "condensed")) entry->style = FL_WIDTH_CONDENSED; else if(!strcasecmp(entry->swidth, "semicondensed")) entry->style = FL_WIDTH_SEMICONDENSED; else if(!strcasecmp(entry->swidth, "semiexpanded")) entry->style = FL_WIDTH_SEMIEXPANDED; else if(!strcasecmp(entry->swidth, "expanded")) entry->style = FL_WIDTH_EXPANDED; else if(!strcasecmp(entry->swidth, "extraexpanded")) entry->style = FL_WIDTH_EXTRAEXPANDED; else if(!strcasecmp(entry->swidth, "ultraexpanded")) entry->style = FL_WIDTH_ULTRAEXPANDED; else entry->style = FL_WIDTH_NORMAL; fontlist->append(entry); // printf("TitleMain::build_fonts %s: success\n", entry->path); //printf("TitleMain::build_fonts 2\n"); } pclose(in); // Load all the fonts from fontconfig FcPattern *pat; FcFontSet *fs; FcObjectSet *os; FcChar8 *family, *file, *foundry, *style, *format; int slant, spacing, width, weight; int force_style = 0; // if you want limit search to TrueType put 1 int limit_to_trutype = 1; FcConfig *config; int i; char tmpstring[BCTEXTLEN]; if(!FcInit()) return 1; config = FcConfigGetCurrent(); FcConfigSetRescanInterval(config, 0); pat = FcPatternCreate(); os = FcObjectSetBuild ( FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT, FC_WIDTH, FC_SLANT, FC_FONTFORMAT, FC_SPACING, FC_STYLE, (char *) 0); FcPatternAddBool(pat, FC_SCALABLE, true); if(language[0]) { char langstr[LEN_LANG * 3]; strcpy(langstr, language); if(region[0]) { strcat(langstr, "-"); strcat(langstr, region); } FcLangSet *ls = FcLangSetCreate(); if(FcLangSetAdd(ls, (const FcChar8*)langstr)) if(FcPatternAddLangSet(pat, FC_LANG, ls)) FcLangSetDestroy(ls); } fs = FcFontList(config, pat, os); FcPatternDestroy(pat); FcObjectSetDestroy(os); for (i = 0; fs && i < fs->nfont; i++) { FcPattern *font = fs->fonts[i]; force_style = 0; FcPatternGetString(font, FC_FONTFORMAT, 0, &format); //on this point you can limit font search if(limit_to_trutype && strcmp((char *)format, "TrueType")) continue; sprintf(tmpstring, "%s", format); BC_FontEntry *entry = new BC_FontEntry; if(FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) { entry->path = cstrdup((char*)file); } if(FcPatternGetString(font, FC_FOUNDRY, 0, &foundry) == FcResultMatch) { entry->foundry = cstrdup((char*)foundry); } if(FcPatternGetInteger(font, FC_WEIGHT, 0, &weight) == FcResultMatch) { switch(weight) { case FC_WEIGHT_THIN: case FC_WEIGHT_EXTRALIGHT: case FC_WEIGHT_LIGHT: case FC_WEIGHT_BOOK: force_style = 1; entry->weight = cstrdup("medium"); break; case FC_WEIGHT_NORMAL: case FC_WEIGHT_MEDIUM: default: entry->weight = cstrdup("medium"); break; case FC_WEIGHT_BLACK: case FC_WEIGHT_SEMIBOLD: case FC_WEIGHT_BOLD: entry->weight = cstrdup("bold"); entry->fixed_style |= BC_FONT_BOLD; break; case FC_WEIGHT_EXTRABOLD: case FC_WEIGHT_EXTRABLACK: force_style = 1; entry->weight = cstrdup("bold"); entry->fixed_style |= BC_FONT_BOLD; break; } } if(FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch) entry->family = cstrdup((char*)family); if(FcPatternGetInteger(font, FC_SLANT, 0, &slant) == FcResultMatch) { switch(slant) { case FC_SLANT_ROMAN: default: entry->slant = cstrdup("r"); entry->style |= FL_SLANT_ROMAN; break; case FC_SLANT_ITALIC: entry->slant = cstrdup("i"); entry->style |= FL_SLANT_ITALIC; entry->fixed_style |= BC_FONT_ITALIC; break; case FC_SLANT_OBLIQUE: entry->slant = cstrdup("o"); entry->style |= FL_SLANT_OBLIQUE; entry->fixed_style |= BC_FONT_ITALIC; break; } } if(FcPatternGetInteger(font, FC_WIDTH, 0, &width) == FcResultMatch) { switch(width) { case FC_WIDTH_ULTRACONDENSED: entry->swidth = cstrdup("ultracondensed"); break; case FC_WIDTH_EXTRACONDENSED: entry->swidth = cstrdup("extracondensed"); break; case FC_WIDTH_CONDENSED: entry->swidth = cstrdup("condensed"); break; case FC_WIDTH_SEMICONDENSED: entry->swidth = cstrdup("semicondensed"); break; case FC_WIDTH_NORMAL: default: entry->swidth = cstrdup("normal"); break; case FC_WIDTH_SEMIEXPANDED: entry->swidth = cstrdup("semiexpanded"); break; case FC_WIDTH_EXPANDED: entry->swidth = cstrdup("expanded"); break; case FC_WIDTH_EXTRAEXPANDED: entry->swidth = cstrdup("extraexpanded"); break; case FC_WIDTH_ULTRAEXPANDED: entry->swidth = cstrdup("ultraexpanded"); break; } } if(FcPatternGetInteger(font, FC_SPACING, 0, &spacing) == FcResultMatch) { switch(spacing) { case 0: default: entry->spacing = cstrdup("p"); break; case 90: entry->spacing = cstrdup("d"); break; case 100: entry->spacing = cstrdup("m"); break; case 110: entry->spacing = cstrdup("c"); break; } } // Add fake stuff for compatibility entry->adstyle = cstrdup(" "); entry->pixelsize = 0; entry->pointsize = 0; entry->xres = 0; entry->yres = 0; entry->avg_width = 0; entry->registry = cstrdup("utf"); entry->encoding = cstrdup("8"); if(!FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch) force_style = 0; // If font has a style unmanaged by titler plugin, force style to be displayed on name // in this way we can shown all available fonts styles. if(force_style) { sprintf(tmpstring, "%s (%s)", entry->family, style); entry->displayname = cstrdup(tmpstring); } else { if(strcmp(entry->foundry, "unknown")) { sprintf(tmpstring, "%s (%s)", entry->family, entry->foundry); entry->displayname = cstrdup(tmpstring); } else { sprintf(tmpstring, "%s", entry->family); entry->displayname = cstrdup(tmpstring); } } fontlist->append(entry); } FcFontSetDestroy(fs); if(freetype_library) FT_Done_FreeType(freetype_library); // for(int i = 0; i < fonts->total; i++) // fonts->values[i]->dump(); FcConfigAppFontAddDir(0, (const FcChar8*)search_path); FcConfigSetRescanInterval(0, 0); os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_FOUNDRY, FC_WEIGHT, FC_WIDTH, FC_SLANT, FC_SPACING, FC_STYLE, (char *)0); pat = FcPatternCreate(); FcPatternAddBool(pat, FC_SCALABLE, true); if(language[0]) { char langstr[LEN_LANG * 3]; strcpy(langstr, language); if(region[0]) { strcat(langstr, "-"); strcat(langstr, region); } FcLangSet *ls = FcLangSetCreate(); if(FcLangSetAdd(ls, (const FcChar8*)langstr)) if(FcPatternAddLangSet(pat, FC_LANG, ls)) FcLangSetDestroy(ls); } fs = FcFontList(0, pat, os); FcPatternDestroy(pat); FcObjectSetDestroy(os); for(int i = 0; i < fs->nfont; i++) { FcPattern *font = fs->fonts[i]; BC_FontEntry *entry = new BC_FontEntry; FcChar8 *strvalue; if(FcPatternGetString(font, FC_FILE, 0, &strvalue) == FcResultMatch) { entry->path = new char[strlen((char*)strvalue) + 1]; strcpy(entry->path, (char*)strvalue); } if(FcPatternGetString(font, FC_FOUNDRY, 0, &strvalue) == FcResultMatch) { entry->foundry = new char[strlen((char*)strvalue) + 1]; strcpy(entry->foundry, (char *)strvalue); } if(FcPatternGetString(font, FC_FAMILY, 0, &strvalue) == FcResultMatch) { entry->family = new char[strlen((char*)strvalue) + 2]; strcpy(entry->family, (char*)strvalue); } int intvalue; if(FcPatternGetInteger(font, FC_SLANT, 0, &intvalue) == FcResultMatch) { switch(intvalue) { case FC_SLANT_ROMAN: default: entry->style |= FL_SLANT_ROMAN; break; case FC_SLANT_ITALIC: entry->style |= FL_SLANT_ITALIC; break; case FC_SLANT_OBLIQUE: entry->style |= FL_SLANT_OBLIQUE; break; } } if(FcPatternGetInteger(font, FC_WEIGHT, 0, &intvalue) == FcResultMatch) { switch(intvalue) { case FC_WEIGHT_THIN: entry->style |= FL_WEIGHT_THIN; break; case FC_WEIGHT_EXTRALIGHT: entry->style |= FL_WEIGHT_EXTRALIGHT; break; case FC_WEIGHT_LIGHT: entry->style |= FL_WEIGHT_LIGHT; break; case FC_WEIGHT_BOOK: entry->style |= FL_WEIGHT_BOOK; break; case FC_WEIGHT_NORMAL: default: entry->style |= FL_WEIGHT_NORMAL; break; case FC_WEIGHT_MEDIUM: entry->style |= FL_WEIGHT_MEDIUM; break; case FC_WEIGHT_DEMIBOLD: entry->style |= FL_WEIGHT_DEMIBOLD; break; case FC_WEIGHT_BOLD: entry->style |= FL_WEIGHT_BOLD; break; case FC_WEIGHT_EXTRABOLD: entry->style |= FL_WEIGHT_EXTRABOLD; break; case FC_WEIGHT_BLACK: entry->style |= FL_WEIGHT_BLACK; break; case FC_WEIGHT_EXTRABLACK: entry->style |= FL_WEIGHT_EXTRABLACK; break; } } if(FcPatternGetInteger(font, FC_WIDTH, 0, &intvalue) == FcResultMatch) { switch(intvalue) { case FC_WIDTH_ULTRACONDENSED: entry->style |= FL_WIDTH_ULTRACONDENSED; break; case FC_WIDTH_EXTRACONDENSED: entry->style |= FL_WIDTH_EXTRACONDENSED; break; case FC_WIDTH_CONDENSED: entry->style |= FL_WIDTH_CONDENSED; break; case FC_WIDTH_SEMICONDENSED: entry->style = FL_WIDTH_SEMICONDENSED; break; case FC_WIDTH_NORMAL: default: entry->style |= FL_WIDTH_NORMAL; break; case FC_WIDTH_SEMIEXPANDED: entry->style |= FL_WIDTH_SEMIEXPANDED; break; case FC_WIDTH_EXPANDED: entry->style |= FL_WIDTH_EXPANDED; break; case FC_WIDTH_EXTRAEXPANDED: entry->style |= FL_WIDTH_EXTRAEXPANDED; break; case FC_WIDTH_ULTRAEXPANDED: entry->style |= FL_WIDTH_ULTRAEXPANDED; break; } } if(FcPatternGetInteger(font, FC_SPACING, 0, &intvalue) == FcResultMatch) { switch(intvalue) { case FC_PROPORTIONAL: default: entry->style |= FL_PROPORTIONAL; break; case FC_DUAL: entry->style |= FL_DUAL; break; case FC_MONO: entry->style |= FL_MONO; break; case FC_CHARCELL: entry->style |= FL_CHARCELL; break; } } if(entry->foundry && strcmp(entry->foundry, "unknown")) { char tempstr[BCTEXTLEN]; sprintf(tempstr, "%s (%s)", entry->family, entry->foundry); entry->displayname = new char[strlen(tempstr) + 1]; strcpy(entry->displayname, tempstr); } else { entry->displayname = new char[strlen(entry->family) + 1]; strcpy(entry->displayname, entry->family); } fontlist->append(entry); } FcFontSetDestroy(fs); return 0; }
QStringList QFontconfigDatabase::fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QChar::Script script) const { QStringList fallbackFamilies; FcPattern *pattern = FcPatternCreate(); if (!pattern) return fallbackFamilies; FcValue value; value.type = FcTypeString; QByteArray cs = family.toUtf8(); value.u.s = (const FcChar8 *)cs.data(); FcPatternAdd(pattern,FC_FAMILY,value,true); int slant_value = FC_SLANT_ROMAN; if (style == QFont::StyleItalic) slant_value = FC_SLANT_ITALIC; else if (style == QFont::StyleOblique) slant_value = FC_SLANT_OBLIQUE; FcPatternAddInteger(pattern, FC_SLANT, slant_value); Q_ASSERT(uint(script) < QChar::ScriptCount); if (*specialLanguages[script] != '\0') { FcLangSet *ls = FcLangSetCreate(); FcLangSetAdd(ls, (const FcChar8*)specialLanguages[script]); FcPatternAddLangSet(pattern, FC_LANG, ls); FcLangSetDestroy(ls); } else if (!family.isEmpty()) { // If script is Common or Han, then it may include languages like CJK, // we should attach system default language set to the pattern // to obtain correct font fallback list (i.e. if LANG=zh_CN // then we normally want to use a Chinese font for CJK text; // while a Japanese font should be used for that if LANG=ja) FcPattern *dummy = FcPatternCreate(); FcDefaultSubstitute(dummy); FcChar8 *lang = 0; FcResult res = FcPatternGetString(dummy, FC_LANG, 0, &lang); if (res == FcResultMatch) FcPatternAddString(pattern, FC_LANG, lang); FcPatternDestroy(dummy); } const char *stylehint = getFcFamilyForStyleHint(styleHint); if (stylehint) { value.u.s = (const FcChar8 *)stylehint; FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue); } FcConfigSubstitute(0, pattern, FcMatchPattern); FcDefaultSubstitute(pattern); FcResult result = FcResultMatch; FcFontSet *fontSet = FcFontSort(0,pattern,FcFalse,0,&result); FcPatternDestroy(pattern); if (fontSet) { for (int i = 0; i < fontSet->nfont; i++) { FcChar8 *value = 0; if (FcPatternGetString(fontSet->fonts[i], FC_FAMILY, 0, &value) != FcResultMatch) continue; // capitalize(value); QString familyName = QString::fromUtf8((const char *)value); if (!fallbackFamilies.contains(familyName,Qt::CaseInsensitive) && familyName.compare(family, Qt::CaseInsensitive)) { fallbackFamilies << familyName; } } FcFontSetDestroy(fontSet); } // qDebug() << "fallbackFamilies for:" << family << style << styleHint << script << fallbackFamilies; return fallbackFamilies; }
void WebFontInfo::fallbackFontForChar(WebUChar32 c, const char* preferredLocale, WebFallbackFont* fallbackFont) { FcCharSet* cset = FcCharSetCreate(); FcCharSetAddChar(cset, c); FcPattern* pattern = FcPatternCreate(); FcValue fcvalue; fcvalue.type = FcTypeCharSet; fcvalue.u.c = cset; FcPatternAdd(pattern, FC_CHARSET, fcvalue, FcFalse); fcvalue.type = FcTypeBool; fcvalue.u.b = FcTrue; FcPatternAdd(pattern, FC_SCALABLE, fcvalue, FcFalse); if (preferredLocale) { FcLangSet* langset = FcLangSetCreate(); FcLangSetAdd(langset, reinterpret_cast<const FcChar8 *>(preferredLocale)); FcPatternAddLangSet(pattern, FC_LANG, langset); FcLangSetDestroy(langset); } FcConfigSubstitute(0, pattern, FcMatchPattern); FcDefaultSubstitute(pattern); FcResult result; FcFontSet* fontSet = FcFontSort(0, pattern, 0, 0, &result); FcPatternDestroy(pattern); FcCharSetDestroy(cset); if (!fontSet) { fallbackFont->name = WebCString(); fallbackFont->isBold = false; fallbackFont->isItalic = false; return; } // Older versions of fontconfig have a bug where they cannot select // only scalable fonts so we have to manually filter the results. for (int i = 0; i < fontSet->nfont; ++i) { FcPattern* current = fontSet->fonts[i]; FcBool isScalable; if (FcPatternGetBool(current, FC_SCALABLE, 0, &isScalable) != FcResultMatch || !isScalable) continue; // fontconfig can also return fonts which are unreadable FcChar8* cFilename; if (FcPatternGetString(current, FC_FILE, 0, &cFilename) != FcResultMatch) continue; if (access(reinterpret_cast<char*>(cFilename), R_OK)) continue; const char* fontFilename = reinterpret_cast<char*>(cFilename); fallbackFont->filename = WebCString(fontFilename, strlen(fontFilename)); // Index into font collection. int ttcIndex; if (FcPatternGetInteger(current, FC_INDEX, 0, &ttcIndex) != FcResultMatch && ttcIndex < 0) continue; fallbackFont->ttcIndex = ttcIndex; FcChar8* familyName; if (FcPatternGetString(current, FC_FAMILY, 0, &familyName) == FcResultMatch) { const char* charFamily = reinterpret_cast<char*>(familyName); fallbackFont->name = WebCString(charFamily, strlen(charFamily)); } int weight; if (FcPatternGetInteger(current, FC_WEIGHT, 0, &weight) == FcResultMatch) fallbackFont->isBold = weight >= FC_WEIGHT_BOLD; else fallbackFont->isBold = false; int slant; if (FcPatternGetInteger(current, FC_SLANT, 0, &slant) == FcResultMatch) fallbackFont->isItalic = slant != FC_SLANT_ROMAN; else fallbackFont->isItalic = false; FcFontSetDestroy(fontSet); return; } FcFontSetDestroy(fontSet); }
WRATHFontFetch::font_handle font_config_magic_class:: fetch_font_entry(const WRATHFontConfig::InFontSpecification &in_spec) { WRATHAutoLockMutex(m_fc_mutex); /* Create an FcPattern from in_spec: */ FcPattern *fc_filter; FcLangSet *fc_langs(NULL); fc_filter=FcPatternCreate(); /* Awkward moments in documentation: the desription of FcFontSetMatch at http://www.freedesktop.org/software/fontconfig/fontconfig-devel/fcfontsetmatch.html states that: " This function should be called only after FcConfigSubstitute and FcDefaultSubstitute have been called for pattern; otherwise the results will not be correct" where the documentation for FcConfigSubstitute is a what the heck moment. Worse, calling FcDefaultSubstitute initially gives incorrect results in tests, for now we do NOT call FcDefaultSubstitute and we do NOT call FcConfigSubstitute. */ //FcDefaultSubstitute(fc_filter); //FcConfigSubstitute(NULL, fc_filter, FcMatchPattern); FcPatternHelper(fc_filter, in_spec.m_family_name, FC_FAMILY); FcPatternHelper(fc_filter, in_spec.m_foundary_name, FC_FOUNDRY); FcPatternHelper(fc_filter, in_spec.m_style, FC_STYLE); FcPatternHelper(fc_filter, in_spec.m_weight, FC_WEIGHT); FcPatternHelper(fc_filter, in_spec.m_slant, FC_SLANT); if(!in_spec.m_languages.empty()) { fc_langs=FcLangSetCreate(); for(std::set<std::string>::iterator iter=in_spec.m_languages.begin(), end=in_spec.m_languages.end(); iter!=end; ++iter) { FcLangSetAdd(fc_langs, reinterpret_cast<const FcChar8*>(iter->c_str())); } FcPatternAddLangSet(fc_filter, FC_LANG, fc_langs); } FcPattern *fc_font_choice(NULL); FcResult fc_result; WRATHFontFetch::font_handle R; fc_font_choice=FcFontSetMatch(NULL, &m_fc_font_list, 1, fc_filter, &fc_result); if(fc_font_choice!=NULL) { FcMagicValue<FcChar8*> file_name; FcMagicValue<int> font_index; FcGetMagicValue(fc_font_choice, FC_FILE, file_name); FcGetMagicValue(fc_font_choice, FC_INDEX, font_index); if(file_name.m_exists) { const char *fname(reinterpret_cast<const char*>(file_name.m_value)); if(!font_index.m_exists) { font_index.m_value=0; } R=WRATHFontDatabase::fetch_font_entry(fname, font_index.m_value); } FcPatternDestroy(fc_font_choice); } if(!in_spec.m_languages.empty()) { FcLangSetDestroy(fc_langs); } FcPatternDestroy(fc_filter); return R; }