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@xs4all.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 afm_dict_->unprotect ();
47 tfm_dict_->unprotect ();
48 otf_dict_->unprotect ();
51 pango_dict_->unprotect ();
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,
68 pango_font_description_set_size (description,
70 pango_font_description_get_size (description)));
72 gchar *pango_fn = pango_font_description_to_filename (description);
73 SCM key = ly_symbol2scm (pango_fn);
76 if (!pango_dict_->try_retrieve (key, &val))
78 if (be_verbose_global)
79 progress_indication ("[" + String (pango_fn));
81 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
86 val = pf->self_scm ();
87 pango_dict_->set (key, val);
90 if (be_verbose_global)
91 progress_indication ("]");
93 pf->description_ = scm_cons (SCM_BOOL_F,
94 scm_from_double (1.0));
97 return dynamic_cast<Pango_font *> (unsmob_metrics (val));
103 kpathsea_find_file (String name, String ext)
106 String path = global_path.find (name);
107 if (path.length () > 0)
113 SCM module = scm_c_resolve_module ("scm kpathsea");
114 proc = scm_c_module_lookup (module, "ly:kpathsea-find-file");
115 proc = scm_variable_ref (proc);
118 if (ly_is_procedure (proc))
120 SCM kp_result = scm_call_1 (proc, scm_makfrom0str (name.to_str0 ()));
121 if (scm_is_string (kp_result))
122 return ly_scm2string (kp_result);
129 TODO: our AFM handling is broken: the units in an AFM file are
130 relative to the design size (1000 units = 1 designsize). Hence we
131 should include design size when generating an AFM metric.
133 ugr: copied from find_tfm.
136 All_font_metrics::find_afm (String name)
138 SCM sname = ly_symbol2scm (name.to_str0 ());
139 SCM name_string = scm_makfrom0str (name.to_str0 ());
141 if (!afm_dict_->try_retrieve (sname, &val))
145 if (file_name.is_empty ())
146 file_name = search_path_.find (name + ".afm");
148 if (file_name.is_empty ())
150 String p = kpathsea_find_file (name, "afm");
155 if (file_name.is_empty ())
158 if (be_verbose_global)
159 progress_indication ("[" + file_name);
160 val = read_afm_file (file_name);
161 unsmob_metrics (val)->file_name_ = file_name;
163 unsmob_metrics (val)->description_ = scm_cons (name_string,
164 scm_from_double (1.0));
166 if (be_verbose_global)
167 progress_indication ("]");
169 afm_dict_->set (sname, val);
170 unsmob_metrics (val)->unprotect ();
172 Adobe_font_metric *afm
173 = dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
175 /* Only check checksums if there is one. We take the risk that
176 some file has valid checksum 0 */
179 Tex_font_metric *tfm = find_tfm (name);
181 /* FIXME: better warning message
182 (maybe check upon startup for feta16.afm, feta16.tfm?) */
183 if (tfm && tfm->info ().checksum != afm->checksum_)
185 // FIXME: broken sentence
186 String s = _f ("checksum mismatch for font file: `%s'",
187 file_name.to_str0 ());
188 s += " " + _f ("does not match: `%s'",
189 tfm->file_name_.to_str0 ());
191 s += " TFM: " + to_string ((int) tfm->info ().checksum);
192 s += " AFM: " + to_string ((int) afm->checksum_);
194 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
196 s += _ ("Rerun with -V to show font paths.");
198 s += _ ("A script for removing font-files is delivered with the source-code:");
200 s += "buildscripts/clean-fonts.sh";
206 return dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
210 All_font_metrics::find_otf (String name)
212 SCM sname = ly_symbol2scm (name.to_str0 ());
213 SCM name_string = scm_makfrom0str (name.to_str0 ());
215 if (!otf_dict_->try_retrieve (sname, &val))
219 if (file_name.is_empty ())
220 file_name = search_path_.find (name + ".otf");
221 if (file_name.is_empty ())
224 if (be_verbose_global)
225 progress_indication ("[" + file_name);
227 val = Open_type_font::make_otf (file_name);
229 if (be_verbose_global)
230 progress_indication ("]");
232 unsmob_metrics (val)->file_name_ = file_name;
233 unsmob_metrics (val)->description_ = scm_cons (name_string,
234 scm_from_double (1.0));
235 otf_dict_->set (sname, val);
236 unsmob_metrics (val)->unprotect ();
239 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
243 All_font_metrics::find_tfm (String name)
245 SCM sname = ly_symbol2scm (name.to_str0 ());
246 SCM name_string = scm_makfrom0str (name.to_str0 ());
248 if (!tfm_dict_->try_retrieve (sname, &val))
252 if (file_name.is_empty ())
254 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
256 String p = kpathsea_find_file (name, "tfm");
261 if (file_name.is_empty ())
262 file_name = search_path_.find (name + ".tfm");
263 if (file_name.is_empty ())
266 if (be_verbose_global)
267 progress_indication ("[" + file_name);
269 val = Tex_font_metric::make_tfm (file_name);
271 if (be_verbose_global)
272 progress_indication ("]");
274 unsmob_metrics (val)->file_name_ = file_name;
275 unsmob_metrics (val)->description_ = scm_cons (name_string,
276 scm_from_double (1.0));
277 tfm_dict_->set (sname, val);
278 unsmob_metrics (val)->unprotect ();
281 return dynamic_cast<Tex_font_metric *> (unsmob_metrics (val));
285 All_font_metrics::find_font (String name)
287 Font_metric *f = find_otf (name);
290 && (name.left_string (4) == "feta"
291 || name.left_string (8) == "parmesan"
292 || name.left_string (2) == "lm"))
307 warning (_f ("can't find font: `%s'", name.to_str0 ()));
308 warning (_ ("loading default font"));
311 String def_name = default_font_str0_;
313 /* We're in emergency recovery mode here anyway, so don't try to do
314 anything smart that runs the risk of failing. */
316 f = find_afm (def_name);
319 f = find_tfm (def_name);
323 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
324 error (_f ("(search path: `%s')", search_path_.to_string ()));
325 error (_ ("giving up"));
331 All_font_metrics *all_fonts_global;
333 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
335 "Load the font @var{name}. ")
337 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
339 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
341 return fm->self_scm ();