]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
Merge branch 'jneeman' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond into jneeman
[lilypond.git] / lily / all-font-metrics.cc
1 /*
2   all-font-metrics.cc -- implement All_font_metrics
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "all-font-metrics.hh"
10
11 #include "international.hh"
12 #include "main.hh"
13 #include "open-type-font.hh"
14 #include "pango-font.hh"
15 #include "scm-hash.hh"
16 #include "warn.hh"
17
18
19 Index_to_charcode_map const *
20 All_font_metrics::get_index_to_charcode_map (string filename, FT_Face face)
21 {
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);
25
26   return &filename_charcode_maps_map_[filename];
27 }
28
29
30 All_font_metrics::All_font_metrics (string path)
31 {
32   otf_dict_ = new Scheme_hash_table;
33
34 #if HAVE_PANGO_FT2
35   PangoFontMap *pfm = pango_ft2_font_map_new ();
36
37   pango_ft2_fontmap_
38     = G_TYPE_CHECK_INSTANCE_CAST (pfm,
39                                   PANGO_TYPE_FT2_FONT_MAP,
40                                   PangoFT2FontMap);
41   pango_dpi_ = 1200;
42   pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
43                                      pango_dpi_, pango_dpi_);
44
45   pango_dict_ = new Scheme_hash_table;
46 #endif
47
48   search_path_.parse_path (path);
49 }
50
51 All_font_metrics::~All_font_metrics ()
52 {
53   otf_dict_->unprotect ();
54
55 #if HAVE_PANGO_FT2
56   pango_dict_->unprotect ();
57   g_object_unref (pango_ft2_fontmap_);
58 #endif
59 }
60
61 All_font_metrics::All_font_metrics (All_font_metrics const &)
62 {
63 }
64
65 #if HAVE_PANGO_FT2
66
67 Pango_font *
68 All_font_metrics::find_pango_font (PangoFontDescription *description,
69                                    Real magnification,
70                                    Real output_scale
71                                    )
72 {
73   pango_font_description_set_size (description,
74                                    gint (magnification *
75                                          pango_font_description_get_size (description)));
76
77   gchar *pango_fn = pango_font_description_to_filename (description);
78   SCM key = ly_symbol2scm (pango_fn);
79
80   SCM val;
81   if (!pango_dict_->try_retrieve (key, &val))
82     {
83       if (be_verbose_global)
84         progress_indication ("[" + string (pango_fn));
85
86       Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
87                                        description,
88                                        output_scale
89                                        );
90       
91       val = pf->self_scm ();
92       pango_dict_->set (key, val);
93       pf->unprotect ();
94
95       if (be_verbose_global)
96         progress_indication ("]");
97
98       pf->description_ = scm_cons (SCM_BOOL_F,
99                                    scm_from_double (1.0));
100     }
101   g_free (pango_fn);
102   return dynamic_cast<Pango_font *> (unsmob_metrics (val));
103 }
104
105 #endif
106
107 string
108 kpathsea_find_file (string name, string ext)
109 {
110   name += "." + ext;
111   string path = global_path.find (name);
112   if (path.length () > 0)
113     return path;
114
115   static SCM proc;
116   if (!proc)
117     {
118       SCM module = scm_c_resolve_module ("scm kpathsea");
119       proc = scm_c_module_lookup (module, "ly:kpathsea-find-file");
120       proc = scm_variable_ref (proc);
121     }
122
123   if (ly_is_procedure (proc))
124     {
125       SCM kp_result = scm_call_1 (proc, scm_makfrom0str (name.c_str ()));
126       if (scm_is_string (kp_result))
127         return ly_scm2string (kp_result);
128     }
129
130   return "";
131 }
132
133 Open_type_font *
134 All_font_metrics::find_otf (string name)
135 {
136   SCM sname = ly_symbol2scm (name.c_str ());
137   SCM name_string = scm_makfrom0str (name.c_str ());
138   SCM val;
139   if (!otf_dict_->try_retrieve (sname, &val))
140     {
141       string file_name;
142
143       if (file_name.empty ())
144         file_name = search_path_.find (name + ".otf");
145       if (file_name.empty ())
146         return 0;
147
148       if (be_verbose_global)
149         progress_indication ("[" + file_name);
150
151       val = Open_type_font::make_otf (file_name);
152
153       if (be_verbose_global)
154         progress_indication ("]");
155
156       unsmob_metrics (val)->file_name_ = file_name;
157       unsmob_metrics (val)->description_ = scm_cons (name_string,
158                                                      scm_from_double (1.0));
159       otf_dict_->set (sname, val);
160       unsmob_metrics (val)->unprotect ();
161     }
162
163   return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
164 }
165
166 Font_metric *
167 All_font_metrics::find_font (string name)
168 {
169   Font_metric *f = find_otf (name);
170
171   if (!f)
172     {
173       error (_f ("can't find font: `%s'", name.c_str ()));
174     }
175
176   return f;
177 }
178
179 All_font_metrics *all_fonts_global;
180
181 LY_DEFINE (ly_reset_all_fonts, "ly:reset-all-fonts", 0, 0, 0,
182            (),
183            "Forget all about previously loaded fonts. ")
184 {
185   delete all_fonts_global;
186   all_fonts_global = new All_font_metrics (global_path.to_string ());
187
188   return SCM_UNSPECIFIED;
189 }
190
191
192 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
193            (SCM name),
194            "Load the font @var{name}. ")
195 {
196   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
197
198   Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
199
200   return fm->self_scm ();
201 }
202
203