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