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 scm_gc_unprotect_object (pango_dict_->self_scm ());
53 g_object_unref (pango_ft2_fontmap_);
57 All_font_metrics::All_font_metrics (All_font_metrics const&)
63 All_font_metrics::find_pango_font (PangoFontDescription*description)
65 gchar *fn = pango_font_description_to_filename (description);
66 SCM key = ly_symbol2scm (fn);
69 if (!pango_dict_->try_retrieve (key, &val))
71 if (be_verbose_global)
72 progress_indication ("[" + String (fn));
73 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
76 val = pf->self_scm ();
77 pango_dict_->set (key, val);
78 scm_gc_unprotect_object (val);
80 if (be_verbose_global)
81 progress_indication ("]");
83 pf->description_ = scm_cons (SCM_BOOL_F,
87 return dynamic_cast<Pango_font*> (unsmob_metrics (val));
92 TODO: our AFM handling is broken: the units in an AFM file are
93 relative to the design size (1000 units = 1 designsize). Hence we
94 should include design size when generating an AFM metric.
96 ugr: copied from find_tfm.
99 All_font_metrics::find_afm (String name)
101 SCM sname = ly_symbol2scm (name.to_str0 ());
102 SCM name_string = scm_makfrom0str (name.to_str0 ());
104 if (!afm_dict_->try_retrieve (sname, &val))
108 if (file_name.is_empty ())
109 file_name = search_path_.find (name + ".afm");
111 if (file_name.is_empty ())
113 String p = kpathsea_find_afm (name.to_str0 ());
118 if (file_name.is_empty ())
121 if (be_verbose_global)
122 progress_indication ("[" + file_name);
123 val = read_afm_file (file_name);
124 unsmob_metrics (val)->file_name_ = file_name;
126 unsmob_metrics (val)->description_ = scm_cons (name_string,
127 scm_make_real (1.0));
129 if (be_verbose_global)
130 progress_indication ("]");
132 afm_dict_->set (sname, val);
133 scm_gc_unprotect_object (val);
135 Adobe_font_metric *afm
136 = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
138 /* Only check checksums if there is one. We take the risk that
139 some file has valid checksum 0 */
142 Tex_font_metric * tfm = find_tfm (name);
144 /* FIXME: better warning message
145 (maybe check upon startup for feta16.afm, feta16.tfm?) */
146 if (tfm && tfm->info ().checksum != afm->checksum_)
148 // FIXME: broken sentence
149 String s = _f ("checksum mismatch for font file: `%s'",
150 file_name.to_str0 ());
151 s += " " + _f ("does not match: `%s'",
152 tfm->file_name_.to_str0 ());
154 s += " TFM: " + to_string ((int) tfm->info ().checksum);
155 s += " AFM: " + to_string ((int) afm->checksum_);
157 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
159 s += _ ("Rerun with -V to show font paths.");
161 s += _("A script for removing font-files is delivered with the source-code:");
163 s += "buildscripts/clean-fonts.sh";
169 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
173 All_font_metrics::find_otf (String name)
175 SCM sname = ly_symbol2scm (name.to_str0 ());
176 SCM name_string = scm_makfrom0str (name.to_str0 ());
178 if (!otf_dict_->try_retrieve (sname, &val))
182 if (file_name.is_empty ())
183 file_name = search_path_.find (name + ".otf");
184 if (file_name.is_empty ())
187 if (be_verbose_global)
188 progress_indication ("[" + file_name);
190 val = Open_type_font::make_otf (file_name);
192 if (be_verbose_global)
193 progress_indication ("]");
195 unsmob_metrics (val)->file_name_ = file_name;
196 unsmob_metrics (val)->description_ = scm_cons (name_string,
197 scm_make_real (1.0));
198 otf_dict_->set (sname, val);
199 scm_gc_unprotect_object (val);
202 return dynamic_cast<Open_type_font*> (unsmob_metrics (val));
206 All_font_metrics::find_tfm (String name)
208 SCM sname = ly_symbol2scm (name.to_str0 ());
209 SCM name_string = scm_makfrom0str (name.to_str0 ());
211 if (!tfm_dict_->try_retrieve (sname, &val))
215 if (file_name.is_empty ())
217 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
219 String p = kpathsea_find_tfm (name.to_str0 ());
224 if (file_name.is_empty ())
225 file_name = search_path_.find (name + ".tfm");
226 if (file_name.is_empty ())
229 if (be_verbose_global)
230 progress_indication ("[" + file_name);
232 val = Tex_font_metric::make_tfm (file_name);
234 if (be_verbose_global)
235 progress_indication ("]");
237 unsmob_metrics (val)->file_name_ = file_name;
238 unsmob_metrics (val)->description_ = scm_cons (name_string,
239 scm_make_real (1.0));
240 tfm_dict_->set (sname, val);
241 scm_gc_unprotect_object (val);
244 return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
248 All_font_metrics::find_font (String name)
250 Font_metric *f = find_otf (name);
253 (name.left_string (4) == "feta"
254 || name.left_string (8) == "parmesan"
255 || name.left_string (2) == "lm"))
270 warning (_f ("can't find font: `%s'", name.to_str0 ()));
271 warning (_ ("Loading default font"));
274 String def_name = default_font_str0_;
276 /* We're in emergency recovery mode here anyway, so don't try to do
277 anything smart that runs the risk of failing. */
279 f = find_afm (def_name);
282 f = find_tfm (def_name);
286 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
287 error (_f ("(search path: `%s')", search_path_.to_string ()));
288 error (_ ("Giving up"));
294 All_font_metrics *all_fonts_global;
297 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
299 "Load the font @var{name}. ")
301 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
303 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
305 return fm->self_scm ();