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 "international.hh"
13 #include "open-type-font.hh"
14 #include "pango-font.hh"
15 #include "scm-hash.hh"
19 static char const *default_font_str0_ = "cmr10";
21 All_font_metrics::All_font_metrics (std::string path)
23 tfm_dict_ = new Scheme_hash_table;
24 otf_dict_ = new Scheme_hash_table;
27 PangoFontMap *pfm = pango_ft2_font_map_new ();
30 = G_TYPE_CHECK_INSTANCE_CAST (pfm,
31 PANGO_TYPE_FT2_FONT_MAP,
34 pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
35 pango_dpi_, pango_dpi_);
37 pango_dict_ = new Scheme_hash_table;
40 search_path_.parse_path (path);
43 All_font_metrics::~All_font_metrics ()
45 tfm_dict_->unprotect ();
46 otf_dict_->unprotect ();
49 pango_dict_->unprotect ();
50 g_object_unref (pango_ft2_fontmap_);
54 All_font_metrics::All_font_metrics (All_font_metrics const &)
61 All_font_metrics::find_pango_font (PangoFontDescription *description,
66 pango_font_description_set_size (description,
68 pango_font_description_get_size (description)));
70 gchar *pango_fn = pango_font_description_to_filename (description);
71 SCM key = ly_symbol2scm (pango_fn);
74 if (!pango_dict_->try_retrieve (key, &val))
76 if (be_verbose_global)
77 progress_indication ("[" + std::string (pango_fn));
79 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
84 val = pf->self_scm ();
85 pango_dict_->set (key, val);
88 if (be_verbose_global)
89 progress_indication ("]");
91 pf->description_ = scm_cons (SCM_BOOL_F,
92 scm_from_double (1.0));
95 return dynamic_cast<Pango_font *> (unsmob_metrics (val));
101 kpathsea_find_file (std::string name, std::string ext)
104 std::string path = global_path.find (name);
105 if (path.length () > 0)
111 SCM module = scm_c_resolve_module ("scm kpathsea");
112 proc = scm_c_module_lookup (module, "ly:kpathsea-find-file");
113 proc = scm_variable_ref (proc);
116 if (ly_is_procedure (proc))
118 SCM kp_result = scm_call_1 (proc, scm_makfrom0str (name.c_str ()));
119 if (scm_is_string (kp_result))
120 return ly_scm2string (kp_result);
127 All_font_metrics::find_otf (std::string name)
129 SCM sname = ly_symbol2scm (name.c_str ());
130 SCM name_string = scm_makfrom0str (name.c_str ());
132 if (!otf_dict_->try_retrieve (sname, &val))
134 std::string file_name;
136 if (file_name.empty ())
137 file_name = search_path_.find (name + ".otf");
138 if (file_name.empty ())
141 if (be_verbose_global)
142 progress_indication ("[" + file_name);
144 val = Open_type_font::make_otf (file_name);
146 if (be_verbose_global)
147 progress_indication ("]");
149 unsmob_metrics (val)->file_name_ = file_name;
150 unsmob_metrics (val)->description_ = scm_cons (name_string,
151 scm_from_double (1.0));
152 otf_dict_->set (sname, val);
153 unsmob_metrics (val)->unprotect ();
156 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
160 All_font_metrics::find_tfm (std::string name)
162 SCM sname = ly_symbol2scm (name.c_str ());
163 SCM name_string = scm_makfrom0str (name.c_str ());
165 if (!tfm_dict_->try_retrieve (sname, &val))
167 std::string file_name;
169 if (file_name.empty ())
171 /* FIXME: should add "cork-" prefix to lm* fonts. How to do
173 std::string p = kpathsea_find_file (name, "tfm");
178 if (file_name.empty ())
179 file_name = search_path_.find (name + ".tfm");
180 if (file_name.empty ())
183 if (be_verbose_global)
184 progress_indication ("[" + file_name);
186 val = Tex_font_metric::make_tfm (file_name);
188 if (be_verbose_global)
189 progress_indication ("]");
191 unsmob_metrics (val)->file_name_ = file_name;
192 unsmob_metrics (val)->description_ = scm_cons (name_string,
193 scm_from_double (1.0));
194 tfm_dict_->set (sname, val);
195 unsmob_metrics (val)->unprotect ();
198 return dynamic_cast<Tex_font_metric *> (unsmob_metrics (val));
202 All_font_metrics::find_font (std::string name)
204 Font_metric *f = find_otf (name);
213 warning (_f ("can't find font: `%s'", name.c_str ()));
214 warning (_ ("loading default font"));
217 std::string def_name = default_font_str0_;
220 f = find_tfm (def_name);
224 error (_f ("can't find default font: `%s'", def_name.c_str ()));
225 error (_f ("(search path: `%s')", search_path_.to_string ()));
226 error (_ ("giving up"));
232 All_font_metrics *all_fonts_global;
234 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
236 "Load the font @var{name}. ")
238 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
240 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
242 return fm->self_scm ();