]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
* 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           String p = kpathsea_find_tfm (name.to_str0 ());
130           if (p.length ())
131             file_name = p;
132         }
133
134       if (file_name.is_empty ())
135         file_name = search_path_.find (name  + ".tfm");
136       if (file_name.is_empty ())
137         return 0;
138
139       if (verbose_global_b)
140         progress_indication ("[" + file_name);
141       
142       val = Tex_font_metric::make_tfm (file_name);
143
144       if (verbose_global_b)
145         progress_indication ("]");
146
147       unsmob_metrics (val)->file_name_ = file_name;
148       unsmob_metrics (val)->description_ = scm_cons (name_string,
149                                                      scm_make_real (1.0));
150       tfm_p_dict_->set (sname, val);
151       scm_gc_unprotect_object (val);
152     }
153
154   return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
155 }
156
157 Font_metric*
158 All_font_metrics::find_font (String name)
159 {
160   Font_metric *f = 0;
161   if ((name.left_string (4) == "feta")
162       || (name.left_string (8) == "parmesan")
163       || (name.left_string (2) == "lm"))
164     {
165       f = find_afm (name);
166       if (!f)
167         f = find_tfm (name);
168     }
169   else
170     {
171       f = find_tfm (name);
172       if (!f)
173         f = find_afm (name);
174     }
175
176   if (!f)
177     {
178       warning (_f ("can't find font: `%s'", name.to_str0 ()));
179       warning (_ ("Loading default font"));
180     }
181   
182   String def_name = default_font_str0_;
183
184   /* We're in emergency recovery mode here anyway, so don't try to do
185      anything smart that runs the risk of failing.  */
186   if (!f)
187     f = find_afm (def_name);
188
189   if (!f)
190     f =  find_tfm (def_name);
191
192   if (!f)
193     {
194       error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
195       error (_f ("(search path: `%s')", search_path_.to_string ()));
196       error (_ ("Giving up"));
197     }
198
199   return f;
200 }
201
202 All_font_metrics *all_fonts_global;
203
204
205 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
206            (SCM name),
207            "Load the font @var{name}. ")
208 {
209   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
210
211   Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
212
213   return fm->self_scm ();
214 }
215