2 all-font-metrics.cc -- implement All_font_metrics
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "all-font-metrics.hh"
16 #include "scm-hash.hh"
19 static const char *default_font_str0_ = "cmr10";
21 All_font_metrics::All_font_metrics (String path)
23 afm_p_dict_ = new Scheme_hash_table;
24 tfm_p_dict_ = new Scheme_hash_table;
26 search_path_.parse_path (path);
29 All_font_metrics::~All_font_metrics ()
31 scm_gc_unprotect_object (afm_p_dict_->self_scm ());
32 scm_gc_unprotect_object (tfm_p_dict_->self_scm ());
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.
40 ugr: copied from find_tfm.
43 All_font_metrics::find_afm (String name)
45 SCM sname = ly_symbol2scm (name.to_str0 ());
46 SCM name_string = scm_makfrom0str (name.to_str0 ());
48 if (!afm_p_dict_->try_retrieve (sname, &val))
52 if (file_name.is_empty ())
53 file_name = search_path_.find (name + ".afm");
55 if (file_name.is_empty ())
57 String p = kpathsea_find_afm (name.to_str0 ());
62 if (file_name.is_empty ())
66 progress_indication ("[" + file_name);
67 val = read_afm_file (file_name);
68 unsmob_metrics (val)->file_name_ = file_name;
70 unsmob_metrics (val)->description_ = scm_cons (name_string,
74 progress_indication ("]");
76 afm_p_dict_->set (sname, val);
77 scm_gc_unprotect_object (val);
79 Adobe_font_metric *afm
80 = 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 */
86 Tex_font_metric * tfm = find_tfm (name);
88 /* FIXME: better warning message
89 (maybe check upon startup for feta16.afm, feta16.tfm?) */
90 if (tfm && tfm->info_.checksum != afm->checksum_)
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 ());
98 s += " TFM: " + to_string ((int) tfm->info_.checksum);
99 s += " AFM: " + to_string ((int) afm->checksum_);
101 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
103 s += _ ("Rerun with -V to show font paths.");
105 s += _("A script for removing font-files is delivered with the source-code:");
107 s += "buildscripts/clean-fonts.sh";
113 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
118 All_font_metrics::find_tfm (String name)
120 SCM sname = ly_symbol2scm (name.to_str0 ());
121 SCM name_string = scm_makfrom0str (name.to_str0 ());
123 if (!tfm_p_dict_->try_retrieve (sname, &val))
127 if (file_name.is_empty ())
129 String p = kpathsea_find_tfm (name.to_str0 ());
134 if (file_name.is_empty ())
135 file_name = search_path_.find (name + ".tfm");
136 if (file_name.is_empty ())
139 if (verbose_global_b)
140 progress_indication ("[" + file_name);
142 val = Tex_font_metric::make_tfm (file_name);
144 if (verbose_global_b)
145 progress_indication ("]");
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);
154 return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
158 All_font_metrics::find_font (String name)
161 if ((name.left_string (4) == "feta")
162 || (name.left_string (8) == "parmesan")
163 || (name.left_string (2) == "lm"))
178 warning (_f ("can't find font: `%s'", name.to_str0 ()));
179 warning (_ ("Loading default font"));
182 String def_name = default_font_str0_;
184 /* We're in emergency recovery mode here anyway, so don't try to do
185 anything smart that runs the risk of failing. */
187 f = find_afm (def_name);
190 f = find_tfm (def_name);
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"));
202 All_font_metrics *all_fonts_global;
205 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
207 "Load the font @var{name}. ")
209 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
211 Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
213 return fm->self_scm ();