lilypond-1.5.10
[lilypond.git] / lily / all-font-metrics.cc
blobb198384426c8a4388b7409a6e4e0da8c7b6fc79f
1 /*
2 all-font-metrics.cc -- implement All_font_metrics
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "config.h"
11 #include "main.hh"
12 #include "all-font-metrics.hh"
13 #include "debug.hh"
14 #include "warn.hh"
15 #include "afm.hh"
16 #include "tfm.hh"
17 #include "lily-guile.hh"
18 #include "scm-hash.hh"
19 #include "kpath.hh"
21 static const char * default_font_sz_ = "cmr10";
23 All_font_metrics::All_font_metrics (String path)
25 afm_p_dict_ = new Scheme_hash_table;
26 tfm_p_dict_ = new Scheme_hash_table;
28 search_path_.parse_path (path);
31 All_font_metrics::~All_font_metrics ()
33 scm_gc_unprotect_object (afm_p_dict_->self_scm ());
34 scm_gc_unprotect_object (tfm_p_dict_->self_scm ());
37 Adobe_font_metric *
38 All_font_metrics::find_afm (String name)
40 SCM sname = ly_symbol2scm (name.ch_C ());
42 SCM name_str = ly_str02scm (name.ch_C ());
44 SCM val;
46 if (!afm_p_dict_->try_retrieve (sname, &val))
48 String path;
50 if (path.empty_b ())
51 path = search_path_.find (name + ".afm");
53 if (path.empty_b ())
55 String p = ly_find_afm (name.ch_C ());
56 if (p.length_i ())
57 path = p;
60 if (path.empty_b ())
61 return 0;
63 if (verbose_global_b)
64 progress_indication ("[" + path);
65 val = read_afm_file (path);
66 unsmob_metrics (val)->path_ = path;
68 unsmob_metrics (val)->description_ = gh_cons (name_str, gh_double2scm (1.0));
70 if (verbose_global_b)
71 progress_indication ("]");
73 afm_p_dict_->set (sname,val);
75 scm_gc_unprotect_object (val);
78 Adobe_font_metric *afm
79 = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
82 only check checksums if there is one. We take the risk that
83 some file has valid checksum 0
85 if (afm->checksum_)
88 Tex_font_metric * tfm = find_tfm (name);
90 /* FIXME: better warning message
91 (maybe check upon startup for feta16.afm, feta16.tfm?)
93 if (tfm && tfm->info_.checksum != afm->checksum_)
95 String s = _f ("checksum mismatch for font file: `%s'",
96 path.ch_C ());
97 s += " " + _f ("does not match: `%s'", tfm->path_.ch_C ()); // FIXME
98 s += "\n";
99 s += " TFM: " + to_str ((int) tfm->info_.checksum);
100 s += " AFM: " + to_str ((int) afm->checksum_);
101 s += "\n";
102 s += _ (" Rebuild all .afm files, and remove all .pk and .tfm files. Rerun with -V to show font paths.");
104 error (s);
109 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
113 Tex_font_metric *
114 All_font_metrics::find_tfm (String name)
116 SCM sname = ly_symbol2scm (name.ch_C ());
117 SCM name_str = ly_str02scm (name.ch_C ());
119 SCM val;
120 if (!tfm_p_dict_->try_retrieve (sname, &val))
122 String path;
124 if (path.empty_b ())
126 String p = ly_find_tfm (name.ch_C ());
127 if (p.length_i ())
128 path = p;
131 if (path.empty_b ())
132 path = search_path_.find (name + ".tfm");
133 if (path.empty_b ())
134 return 0;
137 if (verbose_global_b)
138 progress_indication ("[" + path);
139 val = Tex_font_metric::make_tfm (path);
141 if (verbose_global_b)
142 progress_indication ("]");
144 unsmob_metrics (val)->path_ = path;
145 unsmob_metrics (val)->description_ = gh_cons (name_str, gh_double2scm (1.0));
146 tfm_p_dict_->set (sname, val);
148 scm_gc_unprotect_object (val);
151 return
152 dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
156 Font_metric *
157 All_font_metrics::find_font (String name)
159 Font_metric * f= find_afm (name);
160 if (f)
161 return f;
163 f = find_tfm (name);
164 if (f)
165 return f;
167 warning (_f ("can't find font: `%s'", name.ch_C ()));
168 warning (_ ("Loading default font"));
170 String def_name = default_font_sz_;
173 we're in emergency recovery mode here anyway, so don't try to do
174 anything smart that runs the risk of failing. */
175 f= find_afm (def_name);
176 if (f)
177 return f;
179 f = find_tfm (def_name);
180 if (f)
181 return f;
183 error (_f ("can't find default font: `%s'", def_name.ch_C ()));
184 error (_f ("(search path: `%s')", search_path_.str ()));
185 error (_ ("Giving up"));
187 return 0;