]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-config-scheme.cc
Run `make grand-replace'.
[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 "string-convert.hh"
12 #include "warn.hh"
13
14 #include <fontconfig/fontconfig.h>
15
16 string
17 display_fontset (FcFontSet *fs)
18 {
19   string retval;
20   
21   int j;
22   for (j = 0; j < fs->nfont; j++)
23     {
24       FcChar8 *font;
25       FcChar8 *str;
26
27       font = FcNameUnparse (fs->fonts[j]);
28       if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &str) == FcResultMatch)
29         retval += String_convert::form_string ("FILE %s\n", str);
30       if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &str) == FcResultMatch)
31         retval += String_convert::form_string ("family %s\n ", str);
32       if (FcPatternGetString (fs->fonts[j],
33                               "designsize", 0, &str) == FcResultMatch)
34         retval += String_convert::form_string ("designsize %s\n ", str);
35       
36       retval += String_convert::form_string ("%s\n", (const char*) font);
37       free (font);
38     }
39   
40   return retval;
41 }
42
43 string
44 display_strlist (char const*what, FcStrList *slist)
45 {
46   string retval;
47   while (FcChar8 *dir = FcStrListNext (slist))
48     {
49       retval += String_convert::form_string ("%s: %s\n", what, dir);
50     }
51   return retval;
52 }
53
54 string
55 display_config (FcConfig *fcc)
56 {
57   string retval;
58   retval += display_strlist ("Config files", FcConfigGetConfigFiles (fcc));
59   retval +=  display_strlist ("Config dir", FcConfigGetConfigDirs (fcc));
60   retval +=  display_strlist ("Font dir", FcConfigGetFontDirs (fcc));
61   return retval;
62 }
63
64 string
65 display_list (FcConfig *fcc)
66 {
67   FcPattern*pat = FcPatternCreate ();
68
69   FcObjectSet *os = 0;
70   if (!os)
71     os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0);
72
73   FcFontSet *fs = FcFontList (fcc, pat, os);
74   FcObjectSetDestroy (os);
75   if (pat)
76     FcPatternDestroy (pat);
77
78   string retval;
79   if (fs)
80     {
81       retval = display_fontset (fs);
82       FcFontSetDestroy (fs);
83     }
84   return retval;
85 }
86
87
88 LY_DEFINE (ly_font_config_get_font_file, "ly:font-config-get-font-file", 1, 0, 0,
89            (SCM name),
90            "Get the file for font @var{name}.")
91 {
92   LY_ASSERT_TYPE (scm_is_string, name, 1);
93   
94   FcPattern*pat = FcPatternCreate ();
95   FcValue val;
96   
97   val.type = FcTypeString;
98   val.u.s = (const FcChar8*)ly_scm2string (name).c_str (); // FC_SLANT_ITALIC;
99   FcPatternAdd (pat, FC_FAMILY, val, FcFalse);
100
101   FcResult result;
102   SCM scm_result = SCM_BOOL_F;
103
104   FcConfigSubstitute (NULL, pat, FcMatchFont);
105   FcDefaultSubstitute (pat);
106   
107   pat = FcFontMatch (NULL, pat, &result);
108   FcChar8 *str = 0;
109   if (FcPatternGetString (pat, FC_FILE, 0, &str) == FcResultMatch)
110     scm_result = scm_from_locale_string ((char const*) str);
111
112   FcPatternDestroy (pat);
113
114   return scm_result;
115 }
116            
117 LY_DEFINE (ly_font_config_display_fonts, "ly:font-config-display-fonts", 0, 0, 0,
118            (),
119            "Dump a list of all fonts visible to FontConfig.")
120 {
121   string str = display_list (NULL);
122   str += display_config (NULL);
123
124   progress_indication (str);
125   
126   return SCM_UNSPECIFIED;
127 }
128
129