]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-config-scheme.cc
Run grand-replace for 2010.
[lilypond.git] / lily / font-config-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "lily-guile.hh"
21 #include "international.hh"
22 #include "main.hh"
23 #include "string-convert.hh"
24 #include "warn.hh"
25
26 #include <fontconfig/fontconfig.h>
27
28 string
29 display_fontset (FcFontSet *fs)
30 {
31   string retval;
32
33   int j;
34   for (j = 0; j < fs->nfont; j++)
35     {
36       FcChar8 *font;
37       FcChar8 *str;
38
39       font = FcNameUnparse (fs->fonts[j]);
40       if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &str) == FcResultMatch)
41         retval += String_convert::form_string ("FILE %s\n", str);
42       if (FcPatternGetString (fs->fonts[j], FC_INDEX, 0, &str) == FcResultMatch)
43         retval += String_convert::form_string ("INDEX %s\n", str);
44       if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &str) == FcResultMatch)
45         retval += String_convert::form_string ("family %s\n ", str);
46       if (FcPatternGetString (fs->fonts[j],
47                               "designsize", 0, &str) == FcResultMatch)
48         retval += String_convert::form_string ("designsize %s\n ", str);
49
50       retval += String_convert::form_string ("%s\n", (const char *)font);
51       free (font);
52     }
53
54   return retval;
55 }
56
57 string
58 display_strlist (char const *what, FcStrList *slist)
59 {
60   string retval;
61   while (FcChar8 *dir = FcStrListNext (slist))
62     {
63       retval += String_convert::form_string ("%s: %s\n", what, dir);
64     }
65   return retval;
66 }
67
68 string
69 display_config (FcConfig *fcc)
70 {
71   string retval;
72   retval += display_strlist ("Config files", FcConfigGetConfigFiles (fcc));
73   retval += display_strlist ("Config dir", FcConfigGetConfigDirs (fcc));
74   retval += display_strlist ("Font dir", FcConfigGetFontDirs (fcc));
75   return retval;
76 }
77
78 string
79 display_list (FcConfig *fcc)
80 {
81   FcPattern *pat = FcPatternCreate ();
82
83   FcObjectSet *os = 0;
84   if (!os)
85     os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *)0);
86
87   FcFontSet *fs = FcFontList (fcc, pat, os);
88   FcObjectSetDestroy (os);
89   if (pat)
90     FcPatternDestroy (pat);
91
92   string retval;
93   if (fs)
94     {
95       retval = display_fontset (fs);
96       FcFontSetDestroy (fs);
97     }
98   return retval;
99 }
100
101
102 LY_DEFINE (ly_font_config_get_font_file, "ly:font-config-get-font-file", 1, 0, 0,
103            (SCM name),
104            "Get the file for font @var{name}.")
105 {
106   LY_ASSERT_TYPE (scm_is_string, name, 1);
107
108   FcPattern *pat = FcPatternCreate ();
109   FcValue val;
110
111   val.type = FcTypeString;
112   val.u.s = (const FcChar8 *)ly_scm2string (name).c_str (); // FC_SLANT_ITALIC;
113   FcPatternAdd (pat, FC_FAMILY, val, FcFalse);
114
115   FcResult result;
116   SCM scm_result = SCM_BOOL_F;
117
118   FcConfigSubstitute (NULL, pat, FcMatchFont);
119   FcDefaultSubstitute (pat);
120
121   pat = FcFontMatch (NULL, pat, &result);
122   FcChar8 *str = 0;
123   if (FcPatternGetString (pat, FC_FILE, 0, &str) == FcResultMatch)
124     scm_result = scm_from_locale_string ((char const *)str);
125
126   FcPatternDestroy (pat);
127
128   return scm_result;
129 }
130         
131 LY_DEFINE (ly_font_config_display_fonts, "ly:font-config-display-fonts", 0, 0, 0,
132            (),
133            "Dump a list of all fonts visible to FontConfig.")
134 {
135   string str = display_list (NULL);
136   str += display_config (NULL);
137
138   progress_indication (str);
139
140   return SCM_UNSPECIFIED;
141 }
142
143 LY_DEFINE (ly_font_config_add_directory, "ly:font-config-add-directory", 1, 0, 0,
144            (SCM dir),
145            "Add directory @var{dir} to FontConfig.")
146 {
147   LY_ASSERT_TYPE (scm_is_string, dir, 1);
148
149   string d = ly_scm2string (dir);
150
151   if (!FcConfigAppFontAddDir (0, (const FcChar8 *)d.c_str ()))
152     error (_f ("failed adding font directory: %s", d.c_str ()));
153   else if (be_verbose_global)
154     message (_f ("adding font directory: %s", d.c_str ()));
155
156   return SCM_UNSPECIFIED;
157 }
158
159 LY_DEFINE (ly_font_config_add_font, "ly:font-config-add-font", 1, 0, 0,
160            (SCM font),
161            "Add font @var{font} to FontConfig.")
162 {
163   LY_ASSERT_TYPE (scm_is_string, font, 1);
164
165   string f = ly_scm2string (font);
166
167   if (!FcConfigAppFontAddFile (0, (const FcChar8 *)f.c_str ()))
168     error (_f ("failed adding font file: %s", f.c_str ()));
169   else if (be_verbose_global)
170     message (_f ("adding font file: %s", f.c_str ()));
171
172   return SCM_UNSPECIFIED;
173 }