2 all-font-metrics.cc -- implement All_font_metrics
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "all-font-metrics.hh"
11 #include "open-type-font.hh"
16 #include "scm-hash.hh"
17 #include "pango-font.hh"
19 static char const *default_font_str0_ = "cmr10";
21 All_font_metrics::All_font_metrics (String path)
23 afm_dict_ = new Scheme_hash_table;
24 tfm_dict_ = new Scheme_hash_table;
25 otf_dict_ = new Scheme_hash_table;
28 PangoFontMap *pfm = pango_ft2_font_map_new ();
31 = G_TYPE_CHECK_INSTANCE_CAST (pfm,
32 PANGO_TYPE_FT2_FONT_MAP,
35 pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
36 pango_dpi_, pango_dpi_);
38 pango_dict_ = new Scheme_hash_table;
41 search_path_.parse_path (path);
44 All_font_metrics::~All_font_metrics ()
46 scm_gc_unprotect_object (afm_dict_->self_scm ());
47 scm_gc_unprotect_object (tfm_dict_->self_scm ());
48 scm_gc_unprotect_object (otf_dict_->self_scm ());
51 scm_gc_unprotect_object (pango_dict_->self_scm ());
52 g_object_unref (pango_ft2_fontmap_);
56 All_font_metrics::All_font_metrics (All_font_metrics const &)
63 All_font_metrics::find_pango_font (PangoFontDescription *description,
67 pango_font_description_set_size (description,
69 pango_font_description_get_size (description)));
71 gchar *pango_fn = pango_font_description_to_filename (description);
72 SCM key = ly_symbol2scm (pango_fn);
75 if (!pango_dict_->try_retrieve (key, &val))
77 if (be_verbose_global)
78 progress_indication ("[" + String (pango_fn));
80 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
84 val = pf->self_scm ();
85 pango_dict_->set (key, val);
86 scm_gc_unprotect_object (val);
88 if (be_verbose_global)
89 progress_indication ("]");
91 pf->description_ = scm_cons (SCM_BOOL_F,
95 return dynamic_cast<Pango_font *> (unsmob_metrics (val));
101 kpathsea_find_file (String name, String ext)
104 String path = global_path.find (name);
105 if (path.length () > 0)
108 SCM kpath = ly_lily_module_constant ("ly:kpathsea-find-file");
109 if (ly_c_procedure_p (kpath))
111 SCM kp_result = scm_call_1 (kpath, scm_makfrom0str (name.to_str0 ()));
112 if (scm_is_string (kp_result))
113 return ly_scm2string (kp_result);
120 TODO: our AFM handling is broken: the units in an AFM file are
121 relative to the design size (1000 units = 1 designsize). Hence we
122 should include design size when generating an AFM metric.
124 ugr: copied from find_tfm.
127 All_font_metrics::find_afm (String name)
129 SCM sname = ly_symbol2scm (name.to_str0 ());
130 SCM name_string = scm_makfrom0str (name.to_str0 ());
132 if (!afm_dict_->try_retrieve (sname, &val))
136 if (file_name.is_empty ())
137 file_name = search_path_.find (name + ".afm");
139 if (file_name.is_empty ())
141 String p = kpathsea_find_file (name, "afm");
146 if (file_name.is_empty ())
149 if (be_verbose_global)
150 progress_indication ("[" + file_name);
151 val = read_afm_file (file_name);
152 unsmob_metrics (val)->file_name_ = file_name;
154 unsmob_metrics (val)->description_ = scm_cons (name_string,
155 scm_make_real (1.0));
157 if (be_verbose_global)
158 progress_indication ("]");
160 afm_dict_->set (sname, val);
161 scm_gc_unprotect_object (val);
163 Adobe_font_metric *afm
164 = dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
166 /* Only check checksums if there is one. We take the risk that
167 some file has valid checksum 0 */
170 Tex_font_metric *tfm = find_tfm (name);
172 /* FIXME: better warning message
173 (maybe check upon startup for feta16.afm, feta16.tfm?) */
174 if (tfm && tfm->info ().checksum != afm->checksum_)
176 // FIXME: broken sentence
177 String s = _f ("checksum mismatch for font file: `%s'",
178 file_name.to_str0 ());
179 s += " " + _f ("does not match: `%s'",
180 tfm->file_name_.to_str0 ());
182 s += " TFM: " + to_string ((int) tfm->info ().checksum);
183 s += " AFM: " + to_string ((int) afm->checksum_);
185 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
187 s += _ ("Rerun with -V to show font paths.");
189 s += _ ("A script for removing font-files is delivered with the source-code:");
191 s += "buildscripts/clean-fonts.sh";
197 return dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
201 All_font_metrics::find_otf (String name)
203 SCM sname = ly_symbol2scm (name.to_str0 ());
204 SCM name_string = scm_makfrom0str (name.to_str0 ());
206 if (!otf_dict_->try_retrieve (sname, &val))
210 if (file_name.is_empty ())
211 file_name = search_path_.find (name + ".otf");
212 if (file_name.is_empty ())
215 if (be_verbose_global)
216 progress_indication ("[" + file_name);
218 val = Open_type_font::make_otf (file_name);
220 if (be_verbose_global)
221 progress_indication ("]");
223 unsmob_metrics (val)->file_name_ = file_name;
224 unsmob_metrics (val)->description_ = scm_cons (name_string,
225 scm_make_real (1.0));
226 otf_dict_->set (sname, val);
227 scm_gc_unprotect_object (val);
230 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
234 All_font_metrics::find_tfm (String name)
236 SCM sname = ly_symbol2scm (name.to_str0 ());
237 SCM name_string = scm_makfrom0str (name.to_str0 ());
239 if (!tfm_dict_->try_retrieve (sname, &val))
243 if (file_name.is_empty ())
245 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
247 String p = kpathsea_find_file (name, "tfm");
252 if (file_name.is_empty ())
253 file_name = search_path_.find (name + ".tfm");
254 if (file_name.is_empty ())
257 if (be_verbose_global)
258 progress_indication ("[" + file_name);
260 val = Tex_font_metric::make_tfm (file_name);
262 if (be_verbose_global)
263 progress_indication ("]");
265 unsmob_metrics (val)->file_name_ = file_name;
266 unsmob_metrics (val)->description_ = scm_cons (name_string,
267 scm_make_real (1.0));
268 tfm_dict_->set (sname, val);
269 scm_gc_unprotect_object (val);
272 return dynamic_cast<Tex_font_metric *> (unsmob_metrics (val));
276 All_font_metrics::find_font (String name)
278 Font_metric *f = find_otf (name);
281 && (name.left_string (4) == "feta"
282 || name.left_string (8) == "parmesan"
283 || name.left_string (2) == "lm"))
298 warning (_f ("can't find font: `%s'", name.to_str0 ()));
299 warning (_ ("loading default font"));
302 String def_name = default_font_str0_;
304 /* We're in emergency recovery mode here anyway, so don't try to do
305 anything smart that runs the risk of failing. */
307 f = find_afm (def_name);
310 f = find_tfm (def_name);
314 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
315 error (_f ("(search path: `%s')", search_path_.to_string ()));
316 error (_ ("giving up"));
322 All_font_metrics *all_fonts_global;
324 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
326 "Load the font @var{name}. ")
328 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
330 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
332 return fm->self_scm ();