2 all-font-metrics.cc -- implement All_font_metrics
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "all-font-metrics.hh"
11 #include "open-type-font.hh"
15 #include "scm-hash.hh"
16 #include "pango-font.hh"
18 static char const *default_font_str0_ = "cmr10";
20 All_font_metrics::All_font_metrics (String path)
22 tfm_dict_ = new Scheme_hash_table;
23 otf_dict_ = new Scheme_hash_table;
26 PangoFontMap *pfm = pango_ft2_font_map_new ();
29 = G_TYPE_CHECK_INSTANCE_CAST (pfm,
30 PANGO_TYPE_FT2_FONT_MAP,
33 pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
34 pango_dpi_, pango_dpi_);
36 pango_dict_ = new Scheme_hash_table;
39 search_path_.parse_path (path);
42 All_font_metrics::~All_font_metrics ()
44 tfm_dict_->unprotect ();
45 otf_dict_->unprotect ();
48 pango_dict_->unprotect ();
49 g_object_unref (pango_ft2_fontmap_);
53 All_font_metrics::All_font_metrics (All_font_metrics const &)
60 All_font_metrics::find_pango_font (PangoFontDescription *description,
65 pango_font_description_set_size (description,
67 pango_font_description_get_size (description)));
69 gchar *pango_fn = pango_font_description_to_filename (description);
70 SCM key = ly_symbol2scm (pango_fn);
73 if (!pango_dict_->try_retrieve (key, &val))
75 if (be_verbose_global)
76 progress_indication ("[" + String (pango_fn));
78 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
83 val = pf->self_scm ();
84 pango_dict_->set (key, val);
87 if (be_verbose_global)
88 progress_indication ("]");
90 pf->description_ = scm_cons (SCM_BOOL_F,
91 scm_from_double (1.0));
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)
110 SCM module = scm_c_resolve_module ("scm kpathsea");
111 proc = scm_c_module_lookup (module, "ly:kpathsea-find-file");
112 proc = scm_variable_ref (proc);
115 if (ly_is_procedure (proc))
117 SCM kp_result = scm_call_1 (proc, scm_makfrom0str (name.c_str ()));
118 if (scm_is_string (kp_result))
119 return ly_scm2string (kp_result);
126 All_font_metrics::find_otf (String name)
128 SCM sname = ly_symbol2scm (name.c_str ());
129 SCM name_string = scm_makfrom0str (name.c_str ());
131 if (!otf_dict_->try_retrieve (sname, &val))
135 if (file_name.empty ())
136 file_name = search_path_.find (name + ".otf");
137 if (file_name.empty ())
140 if (be_verbose_global)
141 progress_indication ("[" + file_name);
143 val = Open_type_font::make_otf (file_name);
145 if (be_verbose_global)
146 progress_indication ("]");
148 unsmob_metrics (val)->file_name_ = file_name;
149 unsmob_metrics (val)->description_ = scm_cons (name_string,
150 scm_from_double (1.0));
151 otf_dict_->set (sname, val);
152 unsmob_metrics (val)->unprotect ();
155 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
159 All_font_metrics::find_tfm (String name)
161 SCM sname = ly_symbol2scm (name.c_str ());
162 SCM name_string = scm_makfrom0str (name.c_str ());
164 if (!tfm_dict_->try_retrieve (sname, &val))
168 if (file_name.empty ())
170 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
172 String p = kpathsea_find_file (name, "tfm");
177 if (file_name.empty ())
178 file_name = search_path_.find (name + ".tfm");
179 if (file_name.empty ())
182 if (be_verbose_global)
183 progress_indication ("[" + file_name);
185 val = Tex_font_metric::make_tfm (file_name);
187 if (be_verbose_global)
188 progress_indication ("]");
190 unsmob_metrics (val)->file_name_ = file_name;
191 unsmob_metrics (val)->description_ = scm_cons (name_string,
192 scm_from_double (1.0));
193 tfm_dict_->set (sname, val);
194 unsmob_metrics (val)->unprotect ();
197 return dynamic_cast<Tex_font_metric *> (unsmob_metrics (val));
201 All_font_metrics::find_font (String name)
203 Font_metric *f = find_otf (name);
212 warning (_f ("can't find font: `%s'", name.c_str ()));
213 warning (_ ("loading default font"));
216 String def_name = default_font_str0_;
219 f = find_tfm (def_name);
223 error (_f ("can't find default font: `%s'", def_name.c_str ()));
224 error (_f ("(search path: `%s')", search_path_.to_string ()));
225 error (_ ("giving up"));
231 All_font_metrics *all_fonts_global;
233 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
235 "Load the font @var{name}. ")
237 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
239 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
241 return fm->self_scm ();