]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-config-scheme.cc
Merge branch 'master' of carldsorensen@git.sv.gnu.org:/srv/git/lilypond into ralph
[lilypond.git] / lily / font-config-scheme.cc
1 /*
2   font-config-scheme.cc -- implement FontConfig bindings.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "lily-guile.hh"
11 #include "international.hh"
12 #include "main.hh"
13 #include "string-convert.hh"
14 #include "warn.hh"
15
16 #include <fontconfig/fontconfig.h>
17
18 string
19 display_fontset (FcFontSet *fs)
20 {
21   string retval;
22
23   int j;
24   for (j = 0; j < fs->nfont; j++)
25     {
26       FcChar8 *font;
27       FcChar8 *str;
28
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);
37
38       retval += String_convert::form_string ("%s\n", (const char *)font);
39       free (font);
40     }
41
42   return retval;
43 }
44
45 string
46 display_strlist (char const *what, FcStrList *slist)
47 {
48   string retval;
49   while (FcChar8 *dir = FcStrListNext (slist))
50     {
51       retval += String_convert::form_string ("%s: %s\n", what, dir);
52     }
53   return retval;
54 }
55
56 string
57 display_config (FcConfig *fcc)
58 {
59   string retval;
60   retval += display_strlist ("Config files", FcConfigGetConfigFiles (fcc));
61   retval += display_strlist ("Config dir", FcConfigGetConfigDirs (fcc));
62   retval += display_strlist ("Font dir", FcConfigGetFontDirs (fcc));
63   return retval;
64 }
65
66 string
67 display_list (FcConfig *fcc)
68 {
69   FcPattern *pat = FcPatternCreate ();
70
71   FcObjectSet *os = 0;
72   if (!os)
73     os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *)0);
74
75   FcFontSet *fs = FcFontList (fcc, pat, os);
76   FcObjectSetDestroy (os);
77   if (pat)
78     FcPatternDestroy (pat);
79
80   string retval;
81   if (fs)
82     {
83       retval = display_fontset (fs);
84       FcFontSetDestroy (fs);
85     }
86   return retval;
87 }
88
89
90 LY_DEFINE (ly_font_config_get_font_file, "ly:font-config-get-font-file", 1, 0, 0,
91            (SCM name),
92            "Get the file for font @var{name}.")
93 {
94   LY_ASSERT_TYPE (scm_is_string, name, 1);
95
96   FcPattern *pat = FcPatternCreate ();
97   FcValue val;
98
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);
102
103   FcResult result;
104   SCM scm_result = SCM_BOOL_F;
105
106   FcConfigSubstitute (NULL, pat, FcMatchFont);
107   FcDefaultSubstitute (pat);
108
109   pat = FcFontMatch (NULL, pat, &result);
110   FcChar8 *str = 0;
111   if (FcPatternGetString (pat, FC_FILE, 0, &str) == FcResultMatch)
112     scm_result = scm_from_locale_string ((char const *)str);
113
114   FcPatternDestroy (pat);
115
116   return scm_result;
117 }
118         
119 LY_DEFINE (ly_font_config_display_fonts, "ly:font-config-display-fonts", 0, 0, 0,
120            (),
121            "Dump a list of all fonts visible to FontConfig.")
122 {
123   string str = display_list (NULL);
124   str += display_config (NULL);
125
126   progress_indication (str);
127
128   return SCM_UNSPECIFIED;
129 }
130
131 LY_DEFINE (ly_font_config_add_directory, "ly:font-config-add-directory", 1, 0, 0,
132            (SCM dir),
133            "Add directory @var{dir} to FontConfig.")
134 {
135   LY_ASSERT_TYPE (scm_is_string, dir, 1);
136
137   string d = ly_scm2string (dir);
138
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 ()));
143
144   return SCM_UNSPECIFIED;
145 }
146
147 LY_DEFINE (ly_font_config_add_font, "ly:font-config-add-font", 1, 0, 0,
148            (SCM font),
149            "Add font @var{font} to FontConfig.")
150 {
151   LY_ASSERT_TYPE (scm_is_string, font, 1);
152
153   string f = ly_scm2string (font);
154
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 ()));
159
160   return SCM_UNSPECIFIED;
161 }