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