]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
4159cd5a32356f52be6d2d85105fee8919e83bfc
[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 static char const *default_font_str0_ = "cmr10";
19
20 All_font_metrics::All_font_metrics (string path)
21 {
22   otf_dict_ = new Scheme_hash_table;
23
24 #if HAVE_PANGO_FT2
25   PangoFontMap *pfm = pango_ft2_font_map_new ();
26
27   pango_ft2_fontmap_
28     = G_TYPE_CHECK_INSTANCE_CAST (pfm,
29                                   PANGO_TYPE_FT2_FONT_MAP,
30                                   PangoFT2FontMap);
31   pango_dpi_ = 1200;
32   pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
33                                      pango_dpi_, pango_dpi_);
34
35   pango_dict_ = new Scheme_hash_table;
36 #endif
37
38   search_path_.parse_path (path);
39 }
40
41 All_font_metrics::~All_font_metrics ()
42 {
43   otf_dict_->unprotect ();
44
45 #if HAVE_PANGO_FT2
46   pango_dict_->unprotect ();
47   g_object_unref (pango_ft2_fontmap_);
48 #endif
49 }
50
51 All_font_metrics::All_font_metrics (All_font_metrics const &)
52 {
53 }
54
55 #if HAVE_PANGO_FT2
56
57 Pango_font *
58 All_font_metrics::find_pango_font (PangoFontDescription *description,
59                                    Real magnification,
60                                    Real output_scale
61                                    )
62 {
63   pango_font_description_set_size (description,
64                                    gint (magnification *
65                                          pango_font_description_get_size (description)));
66
67   gchar *pango_fn = pango_font_description_to_filename (description);
68   SCM key = ly_symbol2scm (pango_fn);
69
70   SCM val;
71   if (!pango_dict_->try_retrieve (key, &val))
72     {
73       if (be_verbose_global)
74         progress_indication ("[" + string (pango_fn));
75
76       Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
77                                        description,
78                                        output_scale
79                                        );
80       
81       val = pf->self_scm ();
82       pango_dict_->set (key, val);
83       pf->unprotect ();
84
85       if (be_verbose_global)
86         progress_indication ("]");
87
88       pf->description_ = scm_cons (SCM_BOOL_F,
89                                    scm_from_double (1.0));
90     }
91   g_free (pango_fn);
92   return dynamic_cast<Pango_font *> (unsmob_metrics (val));
93 }
94
95 #endif
96
97 string
98 kpathsea_find_file (string name, string ext)
99 {
100   name += "." + ext;
101   string path = global_path.find (name);
102   if (path.length () > 0)
103     return path;
104
105   static SCM proc;
106   if (!proc)
107     {
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);
111     }
112
113   if (ly_is_procedure (proc))
114     {
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);
118     }
119
120   return "";
121 }
122
123 Open_type_font *
124 All_font_metrics::find_otf (string name)
125 {
126   SCM sname = ly_symbol2scm (name.c_str ());
127   SCM name_string = scm_makfrom0str (name.c_str ());
128   SCM val;
129   if (!otf_dict_->try_retrieve (sname, &val))
130     {
131       string file_name;
132
133       if (file_name.empty ())
134         file_name = search_path_.find (name + ".otf");
135       if (file_name.empty ())
136         return 0;
137
138       if (be_verbose_global)
139         progress_indication ("[" + file_name);
140
141       val = Open_type_font::make_otf (file_name);
142
143       if (be_verbose_global)
144         progress_indication ("]");
145
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 ();
151     }
152
153   return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
154 }
155
156 Font_metric *
157 All_font_metrics::find_font (string name)
158 {
159   Font_metric *f = find_otf (name);
160
161
162   if (!f)
163     {
164       warning (_f ("can't find font: `%s'", name.c_str ()));
165       warning (_ ("loading default font"));
166     }
167
168   string def_name = default_font_str0_;
169
170   if (!f)
171     {
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"));
175     }
176
177   return f;
178 }
179
180 All_font_metrics *all_fonts_global;
181
182 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
183            (SCM name),
184            "Load the font @var{name}. ")
185 {
186   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
187
188   Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
189
190   return fm->self_scm ();
191 }
192