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"
16 #include "scm-hash.hh"
18 #include "pango-font.hh"
20 static char const *default_font_str0_ = "cmr10";
22 All_font_metrics::All_font_metrics (String path)
24 afm_dict_ = new Scheme_hash_table;
25 tfm_dict_ = new Scheme_hash_table;
26 otf_dict_ = new Scheme_hash_table;
29 PangoFontMap*pfm = pango_ft2_font_map_new ();
32 G_TYPE_CHECK_INSTANCE_CAST(pfm,
33 PANGO_TYPE_FT2_FONT_MAP,
36 pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
37 pango_dpi_, pango_dpi_);
39 pango_dict_ = new Scheme_hash_table;
42 search_path_.parse_path (path);
45 All_font_metrics::~All_font_metrics ()
47 scm_gc_unprotect_object (afm_dict_->self_scm ());
48 scm_gc_unprotect_object (tfm_dict_->self_scm ());
49 scm_gc_unprotect_object (otf_dict_->self_scm ());
52 All_font_metrics::All_font_metrics (All_font_metrics const&)
58 All_font_metrics::find_pango_font (PangoFontDescription*description)
60 gchar *fn = pango_font_description_to_filename (description);
61 SCM key = ly_symbol2scm (fn);
64 if (!pango_dict_->try_retrieve (key, &val))
67 progress_indication ("[" + String (fn));
68 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
73 pango_dict_->set (key, val);
74 scm_gc_unprotect_object (val);
77 progress_indication ("]");
80 return dynamic_cast<Pango_font*> (unsmob_metrics (val));
85 TODO: our AFM handling is broken: the units in an AFM file are
86 relative to the design size (1000 units = 1 designsize). Hence we
87 should include design size when generating an AFM metric.
89 ugr: copied from find_tfm.
92 All_font_metrics::find_afm (String name)
94 SCM sname = ly_symbol2scm (name.to_str0 ());
95 SCM name_string = scm_makfrom0str (name.to_str0 ());
97 if (!afm_dict_->try_retrieve (sname, &val))
101 if (file_name.is_empty ())
102 file_name = search_path_.find (name + ".afm");
104 if (file_name.is_empty ())
106 String p = kpathsea_find_afm (name.to_str0 ());
111 if (file_name.is_empty ())
114 if (verbose_global_b)
115 progress_indication ("[" + file_name);
116 val = read_afm_file (file_name);
117 unsmob_metrics (val)->file_name_ = file_name;
119 unsmob_metrics (val)->description_ = scm_cons (name_string,
120 scm_make_real (1.0));
122 if (verbose_global_b)
123 progress_indication ("]");
125 afm_dict_->set (sname, val);
126 scm_gc_unprotect_object (val);
128 Adobe_font_metric *afm
129 = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
131 /* Only check checksums if there is one. We take the risk that
132 some file has valid checksum 0 */
135 Tex_font_metric * tfm = find_tfm (name);
137 /* FIXME: better warning message
138 (maybe check upon startup for feta16.afm, feta16.tfm?) */
139 if (tfm && tfm->info_.checksum != afm->checksum_)
141 // FIXME: broken sentence
142 String s = _f ("checksum mismatch for font file: `%s'",
143 file_name.to_str0 ());
144 s += " " + _f ("does not match: `%s'",
145 tfm->file_name_.to_str0 ());
147 s += " TFM: " + to_string ((int) tfm->info_.checksum);
148 s += " AFM: " + to_string ((int) afm->checksum_);
150 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
152 s += _ ("Rerun with -V to show font paths.");
154 s += _("A script for removing font-files is delivered with the source-code:");
156 s += "buildscripts/clean-fonts.sh";
162 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
166 All_font_metrics::find_otf (String name)
168 SCM sname = ly_symbol2scm (name.to_str0 ());
169 SCM name_string = scm_makfrom0str (name.to_str0 ());
171 if (!otf_dict_->try_retrieve (sname, &val))
175 if (file_name.is_empty ())
176 file_name = search_path_.find (name + ".otf");
177 if (file_name.is_empty ())
180 if (verbose_global_b)
181 progress_indication ("[" + file_name);
183 val = Open_type_font::make_otf (file_name);
185 if (verbose_global_b)
186 progress_indication ("]");
188 unsmob_metrics (val)->file_name_ = file_name;
189 unsmob_metrics (val)->description_ = scm_cons (name_string,
190 scm_make_real (1.0));
191 otf_dict_->set (sname, val);
192 scm_gc_unprotect_object (val);
195 return dynamic_cast<Open_type_font*> (unsmob_metrics (val));
199 All_font_metrics::find_tfm (String name)
201 SCM sname = ly_symbol2scm (name.to_str0 ());
202 SCM name_string = scm_makfrom0str (name.to_str0 ());
204 if (!tfm_dict_->try_retrieve (sname, &val))
208 if (file_name.is_empty ())
210 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
212 String p = kpathsea_find_tfm (name.to_str0 ());
217 if (file_name.is_empty ())
218 file_name = search_path_.find (name + ".tfm");
219 if (file_name.is_empty ())
222 if (verbose_global_b)
223 progress_indication ("[" + file_name);
225 val = Tex_font_metric::make_tfm (file_name);
227 if (verbose_global_b)
228 progress_indication ("]");
230 unsmob_metrics (val)->file_name_ = file_name;
231 unsmob_metrics (val)->description_ = scm_cons (name_string,
232 scm_make_real (1.0));
233 tfm_dict_->set (sname, val);
234 scm_gc_unprotect_object (val);
237 return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
241 All_font_metrics::find_font (String name)
243 Font_metric *f = find_otf (name);
246 (name.left_string (4) == "feta"
247 || name.left_string (8) == "parmesan"
248 || name.left_string (2) == "lm"))
263 warning (_f ("can't find font: `%s'", name.to_str0 ()));
264 warning (_ ("Loading default font"));
267 String def_name = default_font_str0_;
269 /* We're in emergency recovery mode here anyway, so don't try to do
270 anything smart that runs the risk of failing. */
272 f = find_afm (def_name);
275 f = find_tfm (def_name);
279 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
280 error (_f ("(search path: `%s')", search_path_.to_string ()));
281 error (_ ("Giving up"));
287 All_font_metrics *all_fonts_global;
290 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
292 "Load the font @var{name}. ")
294 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
296 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
298 return fm->self_scm ();