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