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