]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / all-font-metrics.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "all-font-metrics.hh"
21
22 #include "string-convert.hh"
23 #include "international.hh"
24 #include "main.hh"
25 #include "open-type-font.hh"
26 #include "pango-font.hh"
27 #include "scm-hash.hh"
28 #include "warn.hh"
29
30 using std::string;
31
32 Index_to_charcode_map const *
33 All_font_metrics::get_index_to_charcode_map (const string &filename,
34                                              int face_index,
35                                              FT_Face face)
36 {
37   string key = filename + String_convert::int_string (face_index);
38   if (filename_charcode_maps_map_.find (key)
39       == filename_charcode_maps_map_.end ())
40     filename_charcode_maps_map_[key] = make_index_to_charcode_map (face);
41
42   return &filename_charcode_maps_map_[key];
43 }
44
45 All_font_metrics::All_font_metrics (const string &path)
46 {
47 #if HAVE_PANGO_FT2
48   pango_dict_ = new Scheme_hash_table;
49 #endif
50
51   otf_dict_ = new Scheme_hash_table;
52   smobify_self ();
53   otf_dict_->unprotect ();
54
55 #if HAVE_PANGO_FT2
56   pango_dict_->unprotect ();
57   PangoFontMap *pfm = pango_ft2_font_map_new ();
58
59   pango_ft2_fontmap_ = PANGO_FT2_FONT_MAP (pfm);
60
61   pango_dpi_ = PANGO_RESOLUTION;
62   pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
63                                      pango_dpi_, pango_dpi_);
64 #endif
65
66   search_path_.parse_path (path);
67 }
68
69 All_font_metrics::~All_font_metrics ()
70 {
71 #if HAVE_PANGO_FT2
72   g_object_unref (pango_ft2_fontmap_);
73 #endif
74 }
75
76 SCM
77 All_font_metrics::mark_smob () const
78 {
79 #if HAVE_PANGO_FT2
80   if (pango_dict_)
81     scm_gc_mark (pango_dict_->self_scm ());
82 #endif
83   if (otf_dict_)
84     return otf_dict_->self_scm ();
85   return SCM_UNDEFINED;
86 }
87
88 #if HAVE_PANGO_FT2
89
90 Pango_font *
91 All_font_metrics::find_pango_font (PangoFontDescription const *description,
92                                    Real output_scale
93                                   )
94 {
95   gchar *pango_fn = pango_font_description_to_filename (description);
96   SCM key = ly_symbol2scm (pango_fn);
97
98   SCM val;
99   if (!pango_dict_->try_retrieve (key, &val))
100     {
101       debug_output ("[" + string (pango_fn), true); // start on a new line
102
103       Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
104                                        description,
105                                        output_scale
106                                       );
107
108       val = pf->self_scm ();
109       pango_dict_->set (key, val);
110       pf->unprotect ();
111
112       debug_output ("]", false);
113
114       pf->description_ = scm_cons (SCM_BOOL_F,
115                                    scm_from_double (1.0));
116     }
117   g_free (pango_fn);
118   return unsmob<Pango_font> (val);
119 }
120
121 #endif
122
123 Open_type_font *
124 All_font_metrics::find_otf (const string &name)
125 {
126   SCM sname = ly_symbol2scm (name.c_str ());
127   SCM val;
128   if (!otf_dict_->try_retrieve (sname, &val))
129     {
130       string file_name;
131
132       if (file_name.empty ())
133         file_name = search_path_.find (name + ".otf");
134       if (file_name.empty ())
135         return 0;
136
137       debug_output ("[" + file_name, true); // start on a new line
138
139       val = Open_type_font::make_otf (file_name);
140
141       debug_output ("]", false);
142
143       unsmob<Font_metric> (val)->file_name_ = file_name;
144       SCM name_string = ly_string2scm (name);
145       unsmob<Font_metric> (val)->description_ = scm_cons (name_string,
146                                                      scm_from_double (1.0));
147       otf_dict_->set (sname, val);
148       unsmob<Font_metric> (val)->unprotect ();
149     }
150
151   return unsmob<Open_type_font> (val);
152 }
153
154 Font_metric *
155 All_font_metrics::find_font (const string &name)
156 {
157   Font_metric *f = find_otf (name);
158
159   if (!f)
160     {
161       error (_f ("cannot find font: `%s'", name.c_str ()));
162     }
163
164   return f;
165 }