]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
Merge branch 'master' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond into lilypond...
[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--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "all-font-metrics.hh"
10
11 #include "string-convert.hh"
12 #include "international.hh"
13 #include "main.hh"
14 #include "open-type-font.hh"
15 #include "pango-font.hh"
16 #include "scm-hash.hh"
17 #include "warn.hh"
18
19
20 Index_to_charcode_map const *
21 All_font_metrics::get_index_to_charcode_map (string filename,
22                                              int face_index,
23                                              FT_Face face)
24 {
25   string key = filename + String_convert::int_string (face_index);
26   if (filename_charcode_maps_map_.find (key)
27       == filename_charcode_maps_map_.end ())
28     filename_charcode_maps_map_[key] = make_index_to_charcode_map (face);
29
30   return &filename_charcode_maps_map_[key];
31 }
32
33
34 All_font_metrics::All_font_metrics (string path)
35 {
36   otf_dict_ = new Scheme_hash_table;
37
38 #if HAVE_PANGO_FT2
39   PangoFontMap *pfm = pango_ft2_font_map_new ();
40
41   pango_ft2_fontmap_
42     = G_TYPE_CHECK_INSTANCE_CAST (pfm,
43                                   PANGO_TYPE_FT2_FONT_MAP,
44                                   PangoFT2FontMap);
45   pango_dpi_ = 1200;
46   pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
47                                      pango_dpi_, pango_dpi_);
48
49   pango_dict_ = new Scheme_hash_table;
50 #endif
51
52   search_path_.parse_path (path);
53 }
54
55 All_font_metrics::~All_font_metrics ()
56 {
57   otf_dict_->unprotect ();
58
59 #if HAVE_PANGO_FT2
60   pango_dict_->unprotect ();
61   g_object_unref (pango_ft2_fontmap_);
62 #endif
63 }
64
65 All_font_metrics::All_font_metrics (All_font_metrics const &)
66 {
67 }
68
69 #if HAVE_PANGO_FT2
70
71 Pango_font *
72 All_font_metrics::find_pango_font (PangoFontDescription const *description,
73                                    Real output_scale
74                                    )
75 {
76   gchar *pango_fn = pango_font_description_to_filename (description);
77   SCM key = ly_symbol2scm (pango_fn);
78
79   SCM val;
80   if (!pango_dict_->try_retrieve (key, &val))
81     {
82       if (be_verbose_global)
83         progress_indication ("[" + string (pango_fn));
84
85       Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
86                                        description,
87                                        output_scale
88                                        );
89       
90       val = pf->self_scm ();
91       pango_dict_->set (key, val);
92       pf->unprotect ();
93
94       if (be_verbose_global)
95         progress_indication ("]");
96
97       pf->description_ = scm_cons (SCM_BOOL_F,
98                                    scm_from_double (1.0));
99     }
100   g_free (pango_fn);
101   return dynamic_cast<Pango_font *> (unsmob_metrics (val));
102 }
103
104 #endif
105
106
107 Open_type_font *
108 All_font_metrics::find_otf (string name)
109 {
110   SCM sname = ly_symbol2scm (name.c_str ());
111   SCM val;
112   if (!otf_dict_->try_retrieve (sname, &val))
113     {
114       string file_name;
115
116       if (file_name.empty ())
117         file_name = search_path_.find (name + ".otf");
118       if (file_name.empty ())
119         return 0;
120
121       if (be_verbose_global)
122         progress_indication ("[" + file_name);
123
124       val = Open_type_font::make_otf (file_name);
125
126       if (be_verbose_global)
127         progress_indication ("]");
128
129       unsmob_metrics (val)->file_name_ = file_name;
130       SCM name_string = ly_string2scm (name);
131       unsmob_metrics (val)->description_ = scm_cons (name_string,
132                                                      scm_from_double (1.0));
133       otf_dict_->set (sname, val);
134       unsmob_metrics (val)->unprotect ();
135     }
136
137   return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
138 }
139
140 Font_metric *
141 All_font_metrics::find_font (string name)
142 {
143   Font_metric *f = find_otf (name);
144
145   if (!f)
146     {
147       error (_f ("cannot find font: `%s'", name.c_str ()));
148     }
149
150   return f;
151 }
152
153 All_font_metrics *all_fonts_global;