*** empty log message ***
[lilypond.git] / lily / all-font-metrics.cc
blobe6eddd883f42dcdd368974e49a0495e2f2bdda97
1 /*
3 all-font-metrics.cc -- implement All_font_metrics
5 source file of the GNU LilyPond music typesetter
7 (c) 1999--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
11 #include "config.h"
12 #include "main.hh"
13 #include "all-font-metrics.hh"
15 #include "warn.hh"
16 #include "afm.hh"
17 #include "tfm.hh"
18 #include "lily-guile.hh"
19 #include "scm-hash.hh"
20 #include "kpath.hh"
22 static const char * default_font_str0_ = "cmr10";
24 All_font_metrics::All_font_metrics (String path)
26 afm_p_dict_ = new Scheme_hash_table;
27 tfm_p_dict_ = new Scheme_hash_table;
29 search_path_.parse_path (path);
32 All_font_metrics::~All_font_metrics ()
34 scm_gc_unprotect_object (afm_p_dict_->self_scm ());
35 scm_gc_unprotect_object (tfm_p_dict_->self_scm ());
39 TODO: our AFM handling is broken: the units in an AFM file are
40 relative to the design size (1000 units = 1 designsize). Hence we
41 should include design size when generating an AFM metric.
43 Adobe_font_metric *
44 All_font_metrics::find_afm (String name)
46 SCM sname = ly_symbol2scm (name.to_str0 ());
48 SCM name_string = scm_makfrom0str (name.to_str0 ());
50 SCM val;
52 if (!afm_p_dict_->try_retrieve (sname, &val))
54 String path;
56 if (path.empty_b ())
57 path = search_path_.find (name + ".afm");
59 if (path.empty_b ())
61 String p = kpathsea_find_afm (name.to_str0 ());
62 if (p.length ())
63 path = p;
66 if (path.empty_b ())
67 return 0;
69 if (verbose_global_b)
70 progress_indication ("[" + path);
71 val = read_afm_file (path);
72 unsmob_metrics (val)->path_ = path;
74 unsmob_metrics (val)->description_ = gh_cons (name_string, gh_double2scm (1.0));
76 if (verbose_global_b)
77 progress_indication ("]");
79 afm_p_dict_->set (sname,val);
81 scm_gc_unprotect_object (val);
84 Adobe_font_metric *afm
85 = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
88 only check checksums if there is one. We take the risk that
89 some file has valid checksum 0
91 if (afm->checksum_)
93 Tex_font_metric * tfm = find_tfm (name);
95 /* FIXME: better warning message
96 (maybe check upon startup for feta16.afm, feta16.tfm?)
98 if (tfm && tfm->info_.checksum != afm->checksum_)
100 String s = _f ("checksum mismatch for font file: `%s'",
101 path.to_str0 ());
102 s += " " + _f ("does not match: `%s'", tfm->path_.to_str0 ()); // FIXME
103 s += "\n";
104 s += " TFM: " + to_string ((int) tfm->info_.checksum);
105 s += " AFM: " + to_string ((int) afm->checksum_);
106 s += "\n";
107 s += _ (" Rebuild all .afm files, and remove all .pk and .tfm files.\nRerun with -V to show font paths.\n");
108 s += _("A script for removing font-files is delivered with the source-code,\n"
109 "in buildscripts/clean-fonts.sh");
110 error (s);
115 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
119 Tex_font_metric *
120 All_font_metrics::find_tfm (String name)
122 SCM sname = ly_symbol2scm (name.to_str0 ());
123 SCM name_string = scm_makfrom0str (name.to_str0 ());
125 SCM val;
126 if (!tfm_p_dict_->try_retrieve (sname, &val))
128 String path;
130 if (path.empty_b ())
132 String p = kpathsea_find_tfm (name.to_str0 ());
133 if (p.length ())
134 path = p;
137 if (path.empty_b ())
138 path = search_path_.find (name + ".tfm");
139 if (path.empty_b ())
140 return 0;
142 if (verbose_global_b)
143 progress_indication ("[" + path);
145 val = Tex_font_metric::make_tfm (path);
147 if (verbose_global_b)
148 progress_indication ("]");
150 unsmob_metrics (val)->path_ = path;
151 unsmob_metrics (val)->description_ = gh_cons (name_string, gh_double2scm (1.0));
152 tfm_p_dict_->set (sname, val);
154 scm_gc_unprotect_object (val);
157 return
158 dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
163 Font_metric *
164 All_font_metrics::find_font (String name)
166 if ((name.left_string (4) == "feta") ||
167 (name.left_string (8) == "parmesan"))
169 Font_metric * f = find_afm (name);
170 if (f)
171 return f;
172 else
173 f =find_tfm (name);
174 if (f)
175 return f ;
177 else
179 Font_metric * f = find_tfm (name);
180 if (f)
181 return f;
182 else
183 f =find_afm (name);
184 if (f)
185 return f ;
188 warning (_f ("can't find font: `%s'", name.to_str0 ()));
189 warning (_ ("Loading default font"));
191 String def_name = default_font_str0_;
194 we're in emergency recovery mode here anyway, so don't try to do
195 anything smart that runs the risk of failing. */
196 Font_metric* f= find_afm (def_name);
197 if (f)
198 return f;
200 f = find_tfm (def_name);
201 if (f)
202 return f;
204 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
205 error (_f ("(search path: `%s')", search_path_.to_string ()));
206 error (_ ("Giving up"));
208 return 0;