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 static char const *default_font_str0_ = "cmr10";
20 All_font_metrics::All_font_metrics (string path)
22 otf_dict_ = new Scheme_hash_table;
25 PangoFontMap *pfm = pango_ft2_font_map_new ();
28 = G_TYPE_CHECK_INSTANCE_CAST (pfm,
29 PANGO_TYPE_FT2_FONT_MAP,
32 pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
33 pango_dpi_, pango_dpi_);
35 pango_dict_ = new Scheme_hash_table;
38 search_path_.parse_path (path);
41 All_font_metrics::~All_font_metrics ()
43 otf_dict_->unprotect ();
46 pango_dict_->unprotect ();
47 g_object_unref (pango_ft2_fontmap_);
51 All_font_metrics::All_font_metrics (All_font_metrics const &)
58 All_font_metrics::find_pango_font (PangoFontDescription *description,
63 pango_font_description_set_size (description,
65 pango_font_description_get_size (description)));
67 gchar *pango_fn = pango_font_description_to_filename (description);
68 SCM key = ly_symbol2scm (pango_fn);
71 if (!pango_dict_->try_retrieve (key, &val))
73 if (be_verbose_global)
74 progress_indication ("[" + string (pango_fn));
76 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
81 val = pf->self_scm ();
82 pango_dict_->set (key, val);
85 if (be_verbose_global)
86 progress_indication ("]");
88 pf->description_ = scm_cons (SCM_BOOL_F,
89 scm_from_double (1.0));
92 return dynamic_cast<Pango_font *> (unsmob_metrics (val));
98 kpathsea_find_file (string name, string ext)
101 string path = global_path.find (name);
102 if (path.length () > 0)
108 SCM module = scm_c_resolve_module ("scm kpathsea");
109 proc = scm_c_module_lookup (module, "ly:kpathsea-find-file");
110 proc = scm_variable_ref (proc);
113 if (ly_is_procedure (proc))
115 SCM kp_result = scm_call_1 (proc, scm_makfrom0str (name.c_str ()));
116 if (scm_is_string (kp_result))
117 return ly_scm2string (kp_result);
124 All_font_metrics::find_otf (string name)
126 SCM sname = ly_symbol2scm (name.c_str ());
127 SCM name_string = scm_makfrom0str (name.c_str ());
129 if (!otf_dict_->try_retrieve (sname, &val))
133 if (file_name.empty ())
134 file_name = search_path_.find (name + ".otf");
135 if (file_name.empty ())
138 if (be_verbose_global)
139 progress_indication ("[" + file_name);
141 val = Open_type_font::make_otf (file_name);
143 if (be_verbose_global)
144 progress_indication ("]");
146 unsmob_metrics (val)->file_name_ = file_name;
147 unsmob_metrics (val)->description_ = scm_cons (name_string,
148 scm_from_double (1.0));
149 otf_dict_->set (sname, val);
150 unsmob_metrics (val)->unprotect ();
153 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
157 All_font_metrics::find_font (string name)
159 Font_metric *f = find_otf (name);
164 warning (_f ("can't find font: `%s'", name.c_str ()));
165 warning (_ ("loading default font"));
168 string def_name = default_font_str0_;
172 error (_f ("can't find default font: `%s'", def_name.c_str ()));
173 error (_f ("(search path: `%s')", search_path_.to_string ()));
174 error (_ ("giving up"));
180 All_font_metrics *all_fonts_global;
182 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
184 "Load the font @var{name}. ")
186 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
188 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
190 return fm->self_scm ();