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)
110 SCM kpath = ly_lily_module_constant ("ly:kpathsea-find-file");
111 if (ly_is_procedure (kpath))
113 SCM kp_result = scm_call_1 (kpath, scm_makfrom0str (name.to_str0 ()));
114 if (scm_is_string (kp_result))
115 return ly_scm2string (kp_result);
122 TODO: our AFM handling is broken: the units in an AFM file are
123 relative to the design size (1000 units = 1 designsize). Hence we
124 should include design size when generating an AFM metric.
126 ugr: copied from find_tfm.
129 All_font_metrics::find_afm (String name)
131 SCM sname = ly_symbol2scm (name.to_str0 ());
132 SCM name_string = scm_makfrom0str (name.to_str0 ());
134 if (!afm_dict_->try_retrieve (sname, &val))
138 if (file_name.is_empty ())
139 file_name = search_path_.find (name + ".afm");
141 if (file_name.is_empty ())
143 String p = kpathsea_find_file (name, "afm");
148 if (file_name.is_empty ())
151 if (be_verbose_global)
152 progress_indication ("[" + file_name);
153 val = read_afm_file (file_name);
154 unsmob_metrics (val)->file_name_ = file_name;
156 unsmob_metrics (val)->description_ = scm_cons (name_string,
157 scm_from_double (1.0));
159 if (be_verbose_global)
160 progress_indication ("]");
162 afm_dict_->set (sname, val);
163 unsmob_metrics (val)->unprotect ();
165 Adobe_font_metric *afm
166 = dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
168 /* Only check checksums if there is one. We take the risk that
169 some file has valid checksum 0 */
172 Tex_font_metric *tfm = find_tfm (name);
174 /* FIXME: better warning message
175 (maybe check upon startup for feta16.afm, feta16.tfm?) */
176 if (tfm && tfm->info ().checksum != afm->checksum_)
178 // FIXME: broken sentence
179 String s = _f ("checksum mismatch for font file: `%s'",
180 file_name.to_str0 ());
181 s += " " + _f ("does not match: `%s'",
182 tfm->file_name_.to_str0 ());
184 s += " TFM: " + to_string ((int) tfm->info ().checksum);
185 s += " AFM: " + to_string ((int) afm->checksum_);
187 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
189 s += _ ("Rerun with -V to show font paths.");
191 s += _ ("A script for removing font-files is delivered with the source-code:");
193 s += "buildscripts/clean-fonts.sh";
199 return dynamic_cast<Adobe_font_metric *> (unsmob_metrics (val));
203 All_font_metrics::find_otf (String name)
205 SCM sname = ly_symbol2scm (name.to_str0 ());
206 SCM name_string = scm_makfrom0str (name.to_str0 ());
208 if (!otf_dict_->try_retrieve (sname, &val))
212 if (file_name.is_empty ())
213 file_name = search_path_.find (name + ".otf");
214 if (file_name.is_empty ())
217 if (be_verbose_global)
218 progress_indication ("[" + file_name);
220 val = Open_type_font::make_otf (file_name);
222 if (be_verbose_global)
223 progress_indication ("]");
225 unsmob_metrics (val)->file_name_ = file_name;
226 unsmob_metrics (val)->description_ = scm_cons (name_string,
227 scm_from_double (1.0));
228 otf_dict_->set (sname, val);
229 unsmob_metrics (val)->unprotect ();
232 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
236 All_font_metrics::find_tfm (String name)
238 SCM sname = ly_symbol2scm (name.to_str0 ());
239 SCM name_string = scm_makfrom0str (name.to_str0 ());
241 if (!tfm_dict_->try_retrieve (sname, &val))
245 if (file_name.is_empty ())
247 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
249 String p = kpathsea_find_file (name, "tfm");
254 if (file_name.is_empty ())
255 file_name = search_path_.find (name + ".tfm");
256 if (file_name.is_empty ())
259 if (be_verbose_global)
260 progress_indication ("[" + file_name);
262 val = Tex_font_metric::make_tfm (file_name);
264 if (be_verbose_global)
265 progress_indication ("]");
267 unsmob_metrics (val)->file_name_ = file_name;
268 unsmob_metrics (val)->description_ = scm_cons (name_string,
269 scm_from_double (1.0));
270 tfm_dict_->set (sname, val);
271 unsmob_metrics (val)->unprotect ();
274 return dynamic_cast<Tex_font_metric *> (unsmob_metrics (val));
278 All_font_metrics::find_font (String name)
280 Font_metric *f = find_otf (name);
283 && (name.left_string (4) == "feta"
284 || name.left_string (8) == "parmesan"
285 || name.left_string (2) == "lm"))
300 warning (_f ("can't find font: `%s'", name.to_str0 ()));
301 warning (_ ("loading default font"));
304 String def_name = default_font_str0_;
306 /* We're in emergency recovery mode here anyway, so don't try to do
307 anything smart that runs the risk of failing. */
309 f = find_afm (def_name);
312 f = find_tfm (def_name);
316 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
317 error (_f ("(search path: `%s')", search_path_.to_string ()));
318 error (_ ("giving up"));
324 All_font_metrics *all_fonts_global;
326 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
328 "Load the font @var{name}. ")
330 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
332 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
334 return fm->self_scm ();