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 Index_to_charcode_map const *
20 All_font_metrics::get_index_to_charcode_map (string filename, FT_Face face)
22 if (filename_charcode_maps_map_.find (filename)
23 == filename_charcode_maps_map_.end ())
24 filename_charcode_maps_map_[filename] = make_index_to_charcode_map (face);
26 return &filename_charcode_maps_map_[filename];
30 All_font_metrics::All_font_metrics (string path)
32 otf_dict_ = new Scheme_hash_table;
35 PangoFontMap *pfm = pango_ft2_font_map_new ();
38 = G_TYPE_CHECK_INSTANCE_CAST (pfm,
39 PANGO_TYPE_FT2_FONT_MAP,
42 pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
43 pango_dpi_, pango_dpi_);
45 pango_dict_ = new Scheme_hash_table;
48 search_path_.parse_path (path);
51 All_font_metrics::~All_font_metrics ()
53 otf_dict_->unprotect ();
56 pango_dict_->unprotect ();
57 g_object_unref (pango_ft2_fontmap_);
61 All_font_metrics::All_font_metrics (All_font_metrics const &)
68 All_font_metrics::find_pango_font (PangoFontDescription *description,
73 pango_font_description_set_size (description,
75 pango_font_description_get_size (description)));
77 gchar *pango_fn = pango_font_description_to_filename (description);
78 SCM key = ly_symbol2scm (pango_fn);
81 if (!pango_dict_->try_retrieve (key, &val))
83 if (be_verbose_global)
84 progress_indication ("[" + string (pango_fn));
86 Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
91 val = pf->self_scm ();
92 pango_dict_->set (key, val);
95 if (be_verbose_global)
96 progress_indication ("]");
98 pf->description_ = scm_cons (SCM_BOOL_F,
99 scm_from_double (1.0));
102 return dynamic_cast<Pango_font *> (unsmob_metrics (val));
109 All_font_metrics::find_otf (string name)
111 SCM sname = ly_symbol2scm (name.c_str ());
112 SCM name_string = ly_string2scm (name);
114 if (!otf_dict_->try_retrieve (sname, &val))
118 if (file_name.empty ())
119 file_name = search_path_.find (name + ".otf");
120 if (file_name.empty ())
123 if (be_verbose_global)
124 progress_indication ("[" + file_name);
126 val = Open_type_font::make_otf (file_name);
128 if (be_verbose_global)
129 progress_indication ("]");
131 unsmob_metrics (val)->file_name_ = file_name;
132 unsmob_metrics (val)->description_ = scm_cons (name_string,
133 scm_from_double (1.0));
134 otf_dict_->set (sname, val);
135 unsmob_metrics (val)->unprotect ();
138 return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
142 All_font_metrics::find_font (string name)
144 Font_metric *f = find_otf (name);
148 error (_f ("cannot find font: `%s'", name.c_str ()));
154 All_font_metrics *all_fonts_global;
156 LY_DEFINE (ly_reset_all_fonts, "ly:reset-all-fonts", 0, 0, 0,
158 "Forget all about previously loaded fonts. ")
160 delete all_fonts_global;
161 all_fonts_global = new All_font_metrics (global_path.to_string ());
163 return SCM_UNSPECIFIED;
167 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
169 "Load the font @var{name}. ")
171 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
173 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
175 return fm->self_scm ();