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"
11 //#include "open-type-font.hh"
17 #include "scm-hash.hh"
20 static char const *default_font_str0_ = "cmr10";
22 All_font_metrics::All_font_metrics (String path)
24 afm_p_dict_ = new Scheme_hash_table;
25 tfm_p_dict_ = new Scheme_hash_table;
26 otf_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 ());
35 scm_gc_unprotect_object (otf_p_dict_->self_scm ());
38 All_font_metrics::All_font_metrics (All_font_metrics const&)
43 TODO: our AFM handling is broken: the units in an AFM file are
44 relative to the design size (1000 units = 1 designsize). Hence we
45 should include design size when generating an AFM metric.
47 ugr: copied from find_tfm.
50 All_font_metrics::find_afm (String name)
52 SCM sname = ly_symbol2scm (name.to_str0 ());
53 SCM name_string = scm_makfrom0str (name.to_str0 ());
55 if (!afm_p_dict_->try_retrieve (sname, &val))
59 if (file_name.is_empty ())
60 file_name = search_path_.find (name + ".afm");
62 if (file_name.is_empty ())
64 String p = kpathsea_find_afm (name.to_str0 ());
69 if (file_name.is_empty ())
73 progress_indication ("[" + file_name);
74 val = read_afm_file (file_name);
75 unsmob_metrics (val)->file_name_ = file_name;
77 unsmob_metrics (val)->description_ = scm_cons (name_string,
81 progress_indication ("]");
83 afm_p_dict_->set (sname, val);
84 scm_gc_unprotect_object (val);
86 Adobe_font_metric *afm
87 = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
89 /* Only check checksums if there is one. We take the risk that
90 some file has valid checksum 0 */
93 Tex_font_metric * tfm = find_tfm (name);
95 /* FIXME: better warning message
96 (maybe check upon startup for feta16.afm, feta16.tfm?) */
97 if (tfm && tfm->info_.checksum != afm->checksum_)
99 // FIXME: broken sentence
100 String s = _f ("checksum mismatch for font file: `%s'",
101 file_name.to_str0 ());
102 s += " " + _f ("does not match: `%s'",
103 tfm->file_name_.to_str0 ());
105 s += " TFM: " + to_string ((int) tfm->info_.checksum);
106 s += " AFM: " + to_string ((int) afm->checksum_);
108 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
110 s += _ ("Rerun with -V to show font paths.");
112 s += _("A script for removing font-files is delivered with the source-code:");
114 s += "buildscripts/clean-fonts.sh";
120 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
126 All_font_metrics::find_otf (String name)
128 SCM sname = ly_symbol2scm (name.to_str0 ());
129 SCM name_string = scm_makfrom0str (name.to_str0 ());
131 if (!otf_p_dict_->try_retrieve (sname, &val))
135 if (file_name.is_empty ())
136 file_name = search_path_.find (name + ".otf");
137 if (file_name.is_empty ())
140 if (verbose_global_b)
141 progress_indication ("[" + file_name);
143 val = Open_type_font::make_otf (file_name);
145 if (verbose_global_b)
146 progress_indication ("]");
148 unsmob_metrics (val)->file_name_ = file_name;
149 unsmob_metrics (val)->description_ = scm_cons (name_string,
150 scm_make_real (1.0));
151 otf_p_dict_->set (sname, val);
152 scm_gc_unprotect_object (val);
155 return dynamic_cast<Open_type_font*> (unsmob_metrics (val));
160 All_font_metrics::find_tfm (String name)
162 SCM sname = ly_symbol2scm (name.to_str0 ());
163 SCM name_string = scm_makfrom0str (name.to_str0 ());
165 if (!tfm_p_dict_->try_retrieve (sname, &val))
169 if (file_name.is_empty ())
171 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
173 String p = kpathsea_find_tfm (name.to_str0 ());
178 if (file_name.is_empty ())
179 file_name = search_path_.find (name + ".tfm");
180 if (file_name.is_empty ())
183 if (verbose_global_b)
184 progress_indication ("[" + file_name);
186 val = Tex_font_metric::make_tfm (file_name);
188 if (verbose_global_b)
189 progress_indication ("]");
191 unsmob_metrics (val)->file_name_ = file_name;
192 unsmob_metrics (val)->description_ = scm_cons (name_string,
193 scm_make_real (1.0));
194 tfm_p_dict_->set (sname, val);
195 scm_gc_unprotect_object (val);
198 return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
202 All_font_metrics::find_font (String name)
204 //Font_metric *f = find_otf (name);
208 (name.left_string (4) == "feta"
209 || name.left_string (8) == "parmesan"
210 || name.left_string (2) == "lm"))
225 warning (_f ("can't find font: `%s'", name.to_str0 ()));
226 warning (_ ("Loading default font"));
229 String def_name = default_font_str0_;
231 /* We're in emergency recovery mode here anyway, so don't try to do
232 anything smart that runs the risk of failing. */
234 f = find_afm (def_name);
237 f = find_tfm (def_name);
241 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
242 error (_f ("(search path: `%s')", search_path_.to_string ()));
243 error (_ ("Giving up"));
249 All_font_metrics *all_fonts_global;
252 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
254 "Load the font @var{name}. ")
256 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
258 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
260 return fm->self_scm ();