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