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"
18 All_font_metrics::All_font_metrics (string path)
20 otf_dict_ = new Scheme_hash_table;
23 PangoFontMap *pfm = pango_ft2_font_map_new ();
26 = G_TYPE_CHECK_INSTANCE_CAST (pfm,
27 PANGO_TYPE_FT2_FONT_MAP,
30 pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
31 pango_dpi_, pango_dpi_);
33 pango_dict_ = new Scheme_hash_table;
36 search_path_.parse_path (path);
39 All_font_metrics::~All_font_metrics ()
41 otf_dict_->unprotect ();
44 pango_dict_->unprotect ();
45 g_object_unref (pango_ft2_fontmap_);
49 All_font_metrics::All_font_metrics (All_font_metrics const &)
56 All_font_metrics::find_pango_font (PangoFontDescription *description,
61 pango_font_description_set_size (description,
63 pango_font_description_get_size (description)));
65 gchar *pango_fn = pango_font_description_to_filename (description);
66 SCM key = ly_symbol2scm (pango_fn);
69 if (!pango_dict_->try_retrieve (key, &val))
71 if (be_verbose_global)
72 progress_indication ("[" + string (pango_fn));
74 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
79 val = pf->self_scm ();
80 pango_dict_->set (key, val);
83 if (be_verbose_global)
84 progress_indication ("]");
86 pf->description_ = scm_cons (SCM_BOOL_F,
87 scm_from_double (1.0));
90 return dynamic_cast<Pango_font *> (unsmob_metrics (val));
96 kpathsea_find_file (string name, string ext)
99 string path = global_path.find (name);
100 if (path.length () > 0)
106 SCM module = scm_c_resolve_module ("scm kpathsea");
107 proc = scm_c_module_lookup (module, "ly:kpathsea-find-file");
108 proc = scm_variable_ref (proc);
111 if (ly_is_procedure (proc))
113 SCM kp_result = scm_call_1 (proc, scm_makfrom0str (name.c_str ()));
114 if (scm_is_string (kp_result))
115 return ly_scm2string (kp_result);
122 All_font_metrics::find_otf (string name)
124 SCM sname = ly_symbol2scm (name.c_str ());
125 SCM name_string = scm_makfrom0str (name.c_str ());
127 if (!otf_dict_->try_retrieve (sname, &val))
131 if (file_name.empty ())
132 file_name = search_path_.find (name + ".otf");
133 if (file_name.empty ())
136 if (be_verbose_global)
137 progress_indication ("[" + file_name);
139 val = Open_type_font::make_otf (file_name);
141 if (be_verbose_global)
142 progress_indication ("]");
144 unsmob_metrics (val)->file_name_ = file_name;
145 unsmob_metrics (val)->description_ = scm_cons (name_string,
146 scm_from_double (1.0));
147 otf_dict_->set (sname, val);
148 unsmob_metrics (val)->unprotect ();
151 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
155 All_font_metrics::find_font (string name)
157 Font_metric *f = find_otf (name);
161 error (_f ("can't find font: `%s'", name.c_str ()));
167 All_font_metrics *all_fonts_global;
169 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
171 "Load the font @var{name}. ")
173 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
175 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
177 return fm->self_scm ();