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