2 font-config-scheme.cc -- implement FontConfig bindings.
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "lily-guile.hh"
11 #include "international.hh"
13 #include "string-convert.hh"
16 #include <fontconfig/fontconfig.h>
19 display_fontset (FcFontSet *fs)
24 for (j = 0; j < fs->nfont; j++)
29 font = FcNameUnparse (fs->fonts[j]);
30 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &str) == FcResultMatch)
31 retval += String_convert::form_string ("FILE %s\n", str);
32 if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &str) == FcResultMatch)
33 retval += String_convert::form_string ("family %s\n ", str);
34 if (FcPatternGetString (fs->fonts[j],
35 "designsize", 0, &str) == FcResultMatch)
36 retval += String_convert::form_string ("designsize %s\n ", str);
38 retval += String_convert::form_string ("%s\n", (const char *)font);
46 display_strlist (char const *what, FcStrList *slist)
49 while (FcChar8 *dir = FcStrListNext (slist))
51 retval += String_convert::form_string ("%s: %s\n", what, dir);
57 display_config (FcConfig *fcc)
60 retval += display_strlist ("Config files", FcConfigGetConfigFiles (fcc));
61 retval += display_strlist ("Config dir", FcConfigGetConfigDirs (fcc));
62 retval += display_strlist ("Font dir", FcConfigGetFontDirs (fcc));
67 display_list (FcConfig *fcc)
69 FcPattern *pat = FcPatternCreate ();
73 os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *)0);
75 FcFontSet *fs = FcFontList (fcc, pat, os);
76 FcObjectSetDestroy (os);
78 FcPatternDestroy (pat);
83 retval = display_fontset (fs);
84 FcFontSetDestroy (fs);
90 LY_DEFINE (ly_font_config_get_font_file, "ly:font-config-get-font-file", 1, 0, 0,
92 "Get the file for font @var{name}.")
94 LY_ASSERT_TYPE (scm_is_string, name, 1);
96 FcPattern *pat = FcPatternCreate ();
99 val.type = FcTypeString;
100 val.u.s = (const FcChar8 *)ly_scm2string (name).c_str (); // FC_SLANT_ITALIC;
101 FcPatternAdd (pat, FC_FAMILY, val, FcFalse);
104 SCM scm_result = SCM_BOOL_F;
106 FcConfigSubstitute (NULL, pat, FcMatchFont);
107 FcDefaultSubstitute (pat);
109 pat = FcFontMatch (NULL, pat, &result);
111 if (FcPatternGetString (pat, FC_FILE, 0, &str) == FcResultMatch)
112 scm_result = scm_from_locale_string ((char const *)str);
114 FcPatternDestroy (pat);
119 LY_DEFINE (ly_font_config_display_fonts, "ly:font-config-display-fonts", 0, 0, 0,
121 "Dump a list of all fonts visible to FontConfig.")
123 string str = display_list (NULL);
124 str += display_config (NULL);
126 progress_indication (str);
128 return SCM_UNSPECIFIED;
131 LY_DEFINE (ly_font_config_add_directory, "ly:font-config-add-directory", 1, 0, 0,
133 "Add directory @var{dir} to FontConfig.")
135 LY_ASSERT_TYPE (scm_is_string, dir, 1);
137 string d = ly_scm2string (dir);
139 if (!FcConfigAppFontAddDir (0, (const FcChar8 *)d.c_str ()))
140 error (_f ("failed adding font directory: %s", d.c_str ()));
141 else if (be_verbose_global)
142 message (_f ("adding font directory: %s", d.c_str ()));
144 return SCM_UNSPECIFIED;
147 LY_DEFINE (ly_font_config_add_font, "ly:font-config-add-font", 1, 0, 0,
149 "Add font @var{font} to FontConfig.")
151 LY_ASSERT_TYPE (scm_is_string, font, 1);
153 string f = ly_scm2string (font);
155 if (!FcConfigAppFontAddFile (0, (const FcChar8 *)f.c_str ()))
156 error (_f ("failed adding font file: %s", f.c_str ()));
157 else if (be_verbose_global)
158 message (_f ("adding font file: %s", f.c_str ()));
160 return SCM_UNSPECIFIED;