]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
release commit
[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   SCM kpath = ly_lily_module_constant ("ly:kpathsea-find-file");
111   if (ly_is_procedure (kpath))
112     {
113       SCM kp_result = scm_call_1 (kpath, scm_makfrom0str (name.to_str0 ()));
114       if (scm_is_string (kp_result))
115         return ly_scm2string (kp_result);
116     }
117
118   return "";
119 }
120
121 /*
122   TODO: our AFM handling is broken: the units in an AFM file are
123   relative to the design size (1000 units = 1 designsize). Hence we
124   should include design size when generating an AFM metric.
125
126   ugr: copied from find_tfm.
127 */
128 Adobe_font_metric *
129 All_font_metrics::find_afm (String name)
130 {
131   SCM sname = ly_symbol2scm (name.to_str0 ());
132   SCM name_string = scm_makfrom0str (name.to_str0 ());
133   SCM val;
134   if (!afm_dict_->try_retrieve (sname, &val))
135     {
136       String file_name;
137
138       if (file_name.is_empty ())
139         file_name = search_path_.find (name + ".afm");
140
141       if (file_name.is_empty ())
142         {
143           String p = kpathsea_find_file (name, "afm");
144           if (p.length ())
145             file_name = p;
146         }
147
148       if (file_name.is_empty ())
149         return 0;
150
151       if (be_verbose_global)
152         progress_indication ("[" + file_name);
153       val = read_afm_file (file_name);
154       unsmob_metrics (val)->file_name_ = file_name;
155
156       unsmob_metrics (val)->description_ = scm_cons (name_string,
157                                                      scm_from_double (1.0));
158
159       if (be_verbose_global)
160         progress_indication ("]");
161
162       afm_dict_->set (sname, val);
163       unsmob_metrics (val)->unprotect ();
164
165       Adobe_font_metric *afm
166         = dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
167
168       /* Only check checksums if there is one.  We take the risk that
169          some file has valid checksum 0 */
170       if (afm->checksum_)
171         {
172           Tex_font_metric *tfm = find_tfm (name);
173
174           /* FIXME: better warning message
175              (maybe check upon startup for feta16.afm, feta16.tfm?) */
176           if (tfm && tfm->info ().checksum != afm->checksum_)
177             {
178               // FIXME: broken sentence
179               String s = _f ("checksum mismatch for font file: `%s'",
180                              file_name.to_str0 ());
181               s += " " + _f ("does not match: `%s'",
182                              tfm->file_name_.to_str0 ());
183               s += "\n";
184               s += " TFM: " + to_string ((int) tfm->info ().checksum);
185               s += " AFM: " + to_string ((int) afm->checksum_);
186               s += "\n";
187               s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
188               s += "\n";
189               s += _ ("Rerun with -V to show font paths.");
190               s += "\n";
191               s += _ ("A script for removing font-files is delivered with the source-code:");
192               s += "\n";
193               s += "buildscripts/clean-fonts.sh";
194               error (s);
195             }
196         }
197     }
198
199   return dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
200 }
201
202 Open_type_font *
203 All_font_metrics::find_otf (String name)
204 {
205   SCM sname = ly_symbol2scm (name.to_str0 ());
206   SCM name_string = scm_makfrom0str (name.to_str0 ());
207   SCM val;
208   if (!otf_dict_->try_retrieve (sname, &val))
209     {
210       String file_name;
211
212       if (file_name.is_empty ())
213         file_name = search_path_.find (name + ".otf");
214       if (file_name.is_empty ())
215         return 0;
216
217       if (be_verbose_global)
218         progress_indication ("[" + file_name);
219
220       val = Open_type_font::make_otf (file_name);
221
222       if (be_verbose_global)
223         progress_indication ("]");
224
225       unsmob_metrics (val)->file_name_ = file_name;
226       unsmob_metrics (val)->description_ = scm_cons (name_string,
227                                                      scm_from_double (1.0));
228       otf_dict_->set (sname, val);
229       unsmob_metrics (val)->unprotect ();
230     }
231
232   return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
233 }
234
235 Tex_font_metric *
236 All_font_metrics::find_tfm (String name)
237 {
238   SCM sname = ly_symbol2scm (name.to_str0 ());
239   SCM name_string = scm_makfrom0str (name.to_str0 ());
240   SCM val;
241   if (!tfm_dict_->try_retrieve (sname, &val))
242     {
243       String file_name;
244
245       if (file_name.is_empty ())
246         {
247           /* FIXME: should add "cork-" prefix to lm* fonts.  How to do
248              that, cleanly?  */
249           String p = kpathsea_find_file (name, "tfm");
250           if (p.length ())
251             file_name = p;
252         }
253
254       if (file_name.is_empty ())
255         file_name = search_path_.find (name + ".tfm");
256       if (file_name.is_empty ())
257         return 0;
258
259       if (be_verbose_global)
260         progress_indication ("[" + file_name);
261
262       val = Tex_font_metric::make_tfm (file_name);
263
264       if (be_verbose_global)
265         progress_indication ("]");
266
267       unsmob_metrics (val)->file_name_ = file_name;
268       unsmob_metrics (val)->description_ = scm_cons (name_string,
269                                                      scm_from_double (1.0));
270       tfm_dict_->set (sname, val);
271       unsmob_metrics (val)->unprotect ();
272     }
273
274   return dynamic_cast<Tex_font_metric *> (unsmob_metrics (val));
275 }
276
277 Font_metric *
278 All_font_metrics::find_font (String name)
279 {
280   Font_metric *f = find_otf (name);
281
282   if (!f
283       && (name.left_string (4) == "feta"
284           || name.left_string (8) == "parmesan"
285           || name.left_string (2) == "lm"))
286     {
287       f = find_afm (name);
288       if (!f)
289         f = find_tfm (name);
290     }
291   else if (!f)
292     {
293       f = find_tfm (name);
294       if (!f)
295         f = find_afm (name);
296     }
297
298   if (!f)
299     {
300       warning (_f ("can't find font: `%s'", name.to_str0 ()));
301       warning (_ ("loading default font"));
302     }
303
304   String def_name = default_font_str0_;
305
306   /* We're in emergency recovery mode here anyway, so don't try to do
307      anything smart that runs the risk of failing.  */
308   if (!f)
309     f = find_afm (def_name);
310
311   if (!f)
312     f = find_tfm (def_name);
313
314   if (!f)
315     {
316       error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
317       error (_f ("(search path: `%s')", search_path_.to_string ()));
318       error (_ ("giving up"));
319     }
320
321   return f;
322 }
323
324 All_font_metrics *all_fonts_global;
325
326 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
327            (SCM name),
328            "Load the font @var{name}. ")
329 {
330   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
331
332   Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
333
334   return fm->self_scm ();
335 }
336