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 *fn = pango_font_description_to_filename (description);
72 SCM key = ly_symbol2scm (fn);
75 if (!pango_dict_->try_retrieve (key, &val))
77 if (be_verbose_global)
78 progress_indication ("[" + String (fn));
79 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
83 val = pf->self_scm ();
84 pango_dict_->set (key, val);
85 scm_gc_unprotect_object (val);
87 if (be_verbose_global)
88 progress_indication ("]");
90 pf->description_ = scm_cons (SCM_BOOL_F,
94 return dynamic_cast<Pango_font *> (unsmob_metrics (val));
100 kpathsea_find_file (String name, String ext)
103 String path = global_path.find (name);
104 if (path.length () > 0)
107 SCM kpath = ly_lily_module_constant ("ly:kpathsea-find-file");
108 if (ly_c_procedure_p (kpath))
110 SCM kp_result = scm_call_1 (kpath, scm_makfrom0str (name.to_str0 ()));
111 if (scm_is_string (kp_result))
112 return ly_scm2string (kp_result);
119 TODO: our AFM handling is broken: the units in an AFM file are
120 relative to the design size (1000 units = 1 designsize). Hence we
121 should include design size when generating an AFM metric.
123 ugr: copied from find_tfm.
126 All_font_metrics::find_afm (String name)
128 SCM sname = ly_symbol2scm (name.to_str0 ());
129 SCM name_string = scm_makfrom0str (name.to_str0 ());
131 if (!afm_dict_->try_retrieve (sname, &val))
135 if (file_name.is_empty ())
136 file_name = search_path_.find (name + ".afm");
138 if (file_name.is_empty ())
140 String p = kpathsea_find_file (name, "afm");
145 if (file_name.is_empty ())
148 if (be_verbose_global)
149 progress_indication ("[" + file_name);
150 val = read_afm_file (file_name);
151 unsmob_metrics (val)->file_name_ = file_name;
153 unsmob_metrics (val)->description_ = scm_cons (name_string,
154 scm_make_real (1.0));
156 if (be_verbose_global)
157 progress_indication ("]");
159 afm_dict_->set (sname, val);
160 scm_gc_unprotect_object (val);
162 Adobe_font_metric *afm
163 = dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
165 /* Only check checksums if there is one. We take the risk that
166 some file has valid checksum 0 */
169 Tex_font_metric *tfm = find_tfm (name);
171 /* FIXME: better warning message
172 (maybe check upon startup for feta16.afm, feta16.tfm?) */
173 if (tfm && tfm->info ().checksum != afm->checksum_)
175 // FIXME: broken sentence
176 String s = _f ("checksum mismatch for font file: `%s'",
177 file_name.to_str0 ());
178 s += " " + _f ("does not match: `%s'",
179 tfm->file_name_.to_str0 ());
181 s += " TFM: " + to_string ((int) tfm->info ().checksum);
182 s += " AFM: " + to_string ((int) afm->checksum_);
184 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
186 s += _ ("Rerun with -V to show font paths.");
188 s += _ ("A script for removing font-files is delivered with the source-code:");
190 s += "buildscripts/clean-fonts.sh";
196 return dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
200 All_font_metrics::find_otf (String name)
202 SCM sname = ly_symbol2scm (name.to_str0 ());
203 SCM name_string = scm_makfrom0str (name.to_str0 ());
205 if (!otf_dict_->try_retrieve (sname, &val))
209 if (file_name.is_empty ())
210 file_name = search_path_.find (name + ".otf");
211 if (file_name.is_empty ())
214 if (be_verbose_global)
215 progress_indication ("[" + file_name);
217 val = Open_type_font::make_otf (file_name);
219 if (be_verbose_global)
220 progress_indication ("]");
222 unsmob_metrics (val)->file_name_ = file_name;
223 unsmob_metrics (val)->description_ = scm_cons (name_string,
224 scm_make_real (1.0));
225 otf_dict_->set (sname, val);
226 scm_gc_unprotect_object (val);
229 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
233 All_font_metrics::find_tfm (String name)
235 SCM sname = ly_symbol2scm (name.to_str0 ());
236 SCM name_string = scm_makfrom0str (name.to_str0 ());
238 if (!tfm_dict_->try_retrieve (sname, &val))
242 if (file_name.is_empty ())
244 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
246 String p = kpathsea_find_file (name, "tfm");
251 if (file_name.is_empty ())
252 file_name = search_path_.find (name + ".tfm");
253 if (file_name.is_empty ())
256 if (be_verbose_global)
257 progress_indication ("[" + file_name);
259 val = Tex_font_metric::make_tfm (file_name);
261 if (be_verbose_global)
262 progress_indication ("]");
264 unsmob_metrics (val)->file_name_ = file_name;
265 unsmob_metrics (val)->description_ = scm_cons (name_string,
266 scm_make_real (1.0));
267 tfm_dict_->set (sname, val);
268 scm_gc_unprotect_object (val);
271 return dynamic_cast<Tex_font_metric *> (unsmob_metrics (val));
275 All_font_metrics::find_font (String name)
277 Font_metric *f = find_otf (name);
280 && (name.left_string (4) == "feta"
281 || name.left_string (8) == "parmesan"
282 || name.left_string (2) == "lm"))
297 warning (_f ("can't find font: `%s'", name.to_str0 ()));
298 warning (_ ("loading default font"));
301 String def_name = default_font_str0_;
303 /* We're in emergency recovery mode here anyway, so don't try to do
304 anything smart that runs the risk of failing. */
306 f = find_afm (def_name);
309 f = find_tfm (def_name);
313 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
314 error (_f ("(search path: `%s')", search_path_.to_string ()));
315 error (_ ("giving up"));
321 All_font_metrics *all_fonts_global;
323 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
325 "Load the font @var{name}. ")
327 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
329 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
331 return fm->self_scm ();