]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-config.cc
96247f6f767cddb09db35a7597c1c8c59dce68a8
[lilypond.git] / lily / font-config.cc
1 /*
2   font-config.cc -- implement FontConfig related functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "config.hh"
10
11 #if HAVE_FONTCONFIG
12
13 #include <cstdio>
14 #include <fontconfig/fontconfig.h>
15 #include <sys/stat.h>
16
17 #include "file-path.hh"
18 #include "international.hh"
19 #include "main.hh"
20 #include "warn.hh"
21
22
23 FcConfig *font_config_global = 0;
24
25 void
26 init_fontconfig ()
27 {
28   if (be_verbose_global)
29     message (_ ("Initializing FontConfig..."));
30
31   font_config_global = FcInitLoadConfig ();
32   FcChar8 *cache_file = FcConfigGetCache (font_config_global);
33
34 #if 0
35   // always returns 0 for FC 2.4
36   if (!cache_file)
37     programming_error ("Cannot find file for FontConfig cache.");
38 #endif
39   /*
40     This is a terrible kludge, but there is apparently no way for
41     FontConfig to signal whether it needs to rescan directories.
42    */ 
43   if (cache_file
44       && !is_file ((char const *)cache_file))
45     message (_f ("Rebuilding FontConfig cache %s, this may take a while...", cache_file));
46                         
47   vector<string> dirs;
48
49   /* Extra trailing slash suddenly breaks fontconfig (fc-cache 2.5.0)
50      on windows.  */
51   dirs.push_back (lilypond_datadir + "/fonts/otf");
52   dirs.push_back (lilypond_datadir + "/fonts/type1");
53   
54   for (vsize i = 0; i < dirs.size (); i++)
55     {
56       string dir = dirs[i];
57       if (!FcConfigAppFontAddDir (font_config_global, (FcChar8 *)dir.c_str ()))
58         error (_f ("failed adding font directory: %s", dir.c_str ()));
59       else if (be_verbose_global)
60         message (_f ("adding font directory: %s", dir.c_str ()));
61     }
62   
63   if (be_verbose_global)
64     message (_ ("Building font database."));
65   FcConfigBuildFonts (font_config_global);
66   FcConfigSetCurrent (font_config_global);
67   if (be_verbose_global)
68     message ("\n");
69
70   if (cache_file
71       && !is_file ((char*)cache_file))
72     {
73       /* inhibit future messages. */
74       FILE *f = fopen ((char*)cache_file, "w");
75       if (f)
76         fclose (f);
77     }
78   
79 }
80
81 #else
82
83 void
84 init_fontconfig ()
85 {
86 }
87
88 #endif