]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
* flower/file-path.cc (find): try to open directly as well, so we
[lilypond.git] / lily / all-font-metrics.cc
1 /*
2   all-font-metrics.cc --  implement All_font_metrics
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "all-font-metrics.hh"
10
11 #include "open-type-font.hh"
12 #include "main.hh"
13 #include "warn.hh"
14 #include "afm.hh"
15 #include "tfm.hh"
16 #include "scm-hash.hh"
17 #include "kpath.hh"
18 #include "pango-font.hh"
19
20 static char const *default_font_str0_ = "cmr10";
21
22 All_font_metrics::All_font_metrics (String path)
23 {
24   afm_dict_ = new Scheme_hash_table;
25   tfm_dict_ = new Scheme_hash_table;
26   otf_dict_ = new Scheme_hash_table;
27
28 #if HAVE_PANGO_FT2
29   PangoFontMap*pfm =     pango_ft2_font_map_new ();
30
31   pango_ft2_fontmap_ =
32     G_TYPE_CHECK_INSTANCE_CAST(pfm,
33                                PANGO_TYPE_FT2_FONT_MAP,
34                                PangoFT2FontMap);
35   pango_dpi_ = 1200;
36   pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
37                                      pango_dpi_, pango_dpi_);
38
39   pango_dict_ = new Scheme_hash_table;
40 #endif
41   
42   search_path_.parse_path (path);
43 }
44
45 All_font_metrics::~All_font_metrics ()
46 {
47   scm_gc_unprotect_object (afm_dict_->self_scm ());
48   scm_gc_unprotect_object (tfm_dict_->self_scm ());
49   scm_gc_unprotect_object (otf_dict_->self_scm ());
50 }
51
52 All_font_metrics::All_font_metrics (All_font_metrics const&)
53 {
54 }
55
56 #if HAVE_PANGO_FT2
57 Pango_font *
58 All_font_metrics::find_pango_font (PangoFontDescription*description)
59 {
60   gchar *fn = pango_font_description_to_filename (description);
61   SCM key = ly_symbol2scm (fn);
62
63   SCM val;
64   if (!pango_dict_->try_retrieve (key, &val))
65     {
66       if (verbose_global_b)
67         progress_indication ("[" + String (fn));
68       Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
69                                        pango_dpi_,
70                                        RIGHT,
71                                        description);
72       val = pf->self_scm();
73       pango_dict_->set (key, val);
74       scm_gc_unprotect_object (val);
75
76       if (verbose_global_b)
77         progress_indication ("]");
78     }
79   g_free (fn); 
80   return dynamic_cast<Pango_font*> (unsmob_metrics (val));
81 }
82 #endif
83
84 /*
85   TODO: our AFM handling is broken: the units in an AFM file are
86   relative to the design size (1000 units = 1 designsize). Hence we
87   should include design size when generating an AFM metric.
88
89   ugr: copied from find_tfm.
90  */
91 Adobe_font_metric *
92 All_font_metrics::find_afm (String name)
93 {
94   SCM sname = ly_symbol2scm (name.to_str0 ());
95   SCM name_string = scm_makfrom0str (name.to_str0 ());
96   SCM val;
97   if (!afm_dict_->try_retrieve (sname, &val))
98     {
99       String file_name;
100
101       if (file_name.is_empty ())
102         file_name = search_path_.find (name  + ".afm");
103
104       if (file_name.is_empty ())
105         {
106           String p = kpathsea_find_afm (name.to_str0 ());
107           if (p.length ())
108             file_name = p;
109         }
110
111       if (file_name.is_empty ())
112         return 0;
113       
114       if (verbose_global_b)
115         progress_indication ("[" + file_name);
116       val = read_afm_file (file_name);
117       unsmob_metrics (val)->file_name_ = file_name;
118       
119       unsmob_metrics (val)->description_ = scm_cons (name_string, 
120                                                      scm_make_real (1.0));
121
122       if (verbose_global_b)
123         progress_indication ("]");
124
125       afm_dict_->set (sname, val);
126       scm_gc_unprotect_object (val);
127
128       Adobe_font_metric *afm
129         = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
130
131       /* Only check checksums if there is one.  We take the risk that
132          some file has valid checksum 0 */
133       if (afm->checksum_)
134         {
135           Tex_font_metric * tfm = find_tfm (name);
136           
137           /* FIXME: better warning message
138              (maybe check upon startup for feta16.afm, feta16.tfm?) */
139           if (tfm && tfm->info_.checksum != afm->checksum_)
140             {
141               // FIXME: broken sentence
142               String s = _f ("checksum mismatch for font file: `%s'",
143                              file_name.to_str0 ());
144               s += " " + _f ("does not match: `%s'",
145                              tfm->file_name_.to_str0 ());
146               s += "\n";
147               s += " TFM: " + to_string ((int) tfm->info_.checksum);
148               s += " AFM: " + to_string ((int) afm->checksum_);
149               s += "\n";
150               s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
151               s += "\n";
152               s += _ ("Rerun with -V to show font paths.");
153               s += "\n";
154               s += _("A script for removing font-files is delivered with the source-code:");
155               s += "\n";
156               s += "buildscripts/clean-fonts.sh";
157               error (s);
158             }
159         }
160     }
161   
162   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
163 }
164
165 Open_type_font*
166 All_font_metrics::find_otf (String name)
167 {
168   SCM sname = ly_symbol2scm (name.to_str0 ());
169   SCM name_string = scm_makfrom0str (name.to_str0 ());
170   SCM val;
171   if (!otf_dict_->try_retrieve (sname, &val))
172     {
173       String file_name;
174       
175       if (file_name.is_empty ())
176         file_name = search_path_.find (name  + ".otf");
177       if (file_name.is_empty ())
178         return 0;
179
180       if (verbose_global_b)
181         progress_indication ("[" + file_name);
182       
183       val = Open_type_font::make_otf (file_name);
184
185       if (verbose_global_b)
186         progress_indication ("]");
187
188       unsmob_metrics (val)->file_name_ = file_name;
189       unsmob_metrics (val)->description_ = scm_cons (name_string,
190                                                      scm_make_real (1.0));
191       otf_dict_->set (sname, val);
192       scm_gc_unprotect_object (val);
193     }
194
195   return dynamic_cast<Open_type_font*> (unsmob_metrics (val));
196 }
197
198 Tex_font_metric*
199 All_font_metrics::find_tfm (String name)
200 {
201   SCM sname = ly_symbol2scm (name.to_str0 ());
202   SCM name_string = scm_makfrom0str (name.to_str0 ());
203   SCM val;
204   if (!tfm_dict_->try_retrieve (sname, &val))
205     {
206       String file_name;
207       
208       if (file_name.is_empty ())
209         {
210           /* FIXME: should add "cork-" prefix to lm* fonts.  How to do
211              that, cleanly?  */
212           String p = kpathsea_find_tfm (name.to_str0 ());
213           if (p.length ())
214             file_name = p;
215         }
216
217       if (file_name.is_empty ())
218         file_name = search_path_.find (name  + ".tfm");
219       if (file_name.is_empty ())
220         return 0;
221
222       if (verbose_global_b)
223         progress_indication ("[" + file_name);
224       
225       val = Tex_font_metric::make_tfm (file_name);
226
227       if (verbose_global_b)
228         progress_indication ("]");
229
230       unsmob_metrics (val)->file_name_ = file_name;
231       unsmob_metrics (val)->description_ = scm_cons (name_string,
232                                                      scm_make_real (1.0));
233       tfm_dict_->set (sname, val);
234       scm_gc_unprotect_object (val);
235     }
236
237   return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
238 }
239
240 Font_metric*
241 All_font_metrics::find_font (String name)
242 {
243   Font_metric *f = find_otf (name);
244    
245   if (!f &&
246       (name.left_string (4) == "feta"
247        || name.left_string (8) == "parmesan"
248        || name.left_string (2) == "lm"))
249     {
250       f = find_afm (name);
251       if (!f)
252         f = find_tfm (name);
253     }
254   else if (!f)
255     {
256       f = find_tfm (name);
257       if (!f)
258         f = find_afm (name);
259     }
260
261   if (!f)
262     {
263       warning (_f ("can't find font: `%s'", name.to_str0 ()));
264       warning (_ ("Loading default font"));
265     }
266   
267   String def_name = default_font_str0_;
268
269   /* We're in emergency recovery mode here anyway, so don't try to do
270      anything smart that runs the risk of failing.  */
271   if (!f)
272     f = find_afm (def_name);
273
274   if (!f)
275     f = find_tfm (def_name);
276
277   if (!f)
278     {
279       error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
280       error (_f ("(search path: `%s')", search_path_.to_string ()));
281       error (_ ("Giving up"));
282     }
283
284   return f;
285 }
286
287 All_font_metrics *all_fonts_global;
288
289
290 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
291            (SCM name),
292            "Load the font @var{name}. ")
293 {
294   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
295
296   Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
297
298   return fm->self_scm ();
299 }
300