]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / lily / all-font-metrics.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2009 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
31 Index_to_charcode_map const *
32 All_font_metrics::get_index_to_charcode_map (string filename,
33                                              int face_index,
34                                              FT_Face face)
35 {
36   string key = filename + String_convert::int_string (face_index);
37   if (filename_charcode_maps_map_.find (key)
38       == filename_charcode_maps_map_.end ())
39     filename_charcode_maps_map_[key] = make_index_to_charcode_map (face);
40
41   return &filename_charcode_maps_map_[key];
42 }
43
44
45 All_font_metrics::All_font_metrics (string path)
46 {
47   otf_dict_ = new Scheme_hash_table;
48
49 #if HAVE_PANGO_FT2
50   PangoFontMap *pfm = pango_ft2_font_map_new ();
51
52   pango_ft2_fontmap_
53     = G_TYPE_CHECK_INSTANCE_CAST (pfm,
54                                   PANGO_TYPE_FT2_FONT_MAP,
55                                   PangoFT2FontMap);
56   pango_dpi_ = 1200;
57   pango_ft2_font_map_set_resolution (pango_ft2_fontmap_,
58                                      pango_dpi_, pango_dpi_);
59
60   pango_dict_ = new Scheme_hash_table;
61 #endif
62
63   search_path_.parse_path (path);
64 }
65
66 All_font_metrics::~All_font_metrics ()
67 {
68   otf_dict_->unprotect ();
69
70 #if HAVE_PANGO_FT2
71   pango_dict_->unprotect ();
72   g_object_unref (pango_ft2_fontmap_);
73 #endif
74 }
75
76 All_font_metrics::All_font_metrics (All_font_metrics const &)
77 {
78 }
79
80 #if HAVE_PANGO_FT2
81
82 Pango_font *
83 All_font_metrics::find_pango_font (PangoFontDescription const *description,
84                                    Real output_scale
85                                    )
86 {
87   gchar *pango_fn = pango_font_description_to_filename (description);
88   SCM key = ly_symbol2scm (pango_fn);
89
90   SCM val;
91   if (!pango_dict_->try_retrieve (key, &val))
92     {
93       if (be_verbose_global)
94         progress_indication ("\n[" + string (pango_fn));
95
96       Pango_font *pf = new Pango_font (pango_ft2_fontmap_,
97                                        description,
98                                        output_scale
99                                        );
100       
101       val = pf->self_scm ();
102       pango_dict_->set (key, val);
103       pf->unprotect ();
104
105       if (be_verbose_global)
106         progress_indication ("]");
107
108       pf->description_ = scm_cons (SCM_BOOL_F,
109                                    scm_from_double (1.0));
110     }
111   g_free (pango_fn);
112   return dynamic_cast<Pango_font *> (unsmob_metrics (val));
113 }
114
115 #endif
116
117
118 Open_type_font *
119 All_font_metrics::find_otf (string name)
120 {
121   SCM sname = ly_symbol2scm (name.c_str ());
122   SCM val;
123   if (!otf_dict_->try_retrieve (sname, &val))
124     {
125       string file_name;
126
127       if (file_name.empty ())
128         file_name = search_path_.find (name + ".otf");
129       if (file_name.empty ())
130         return 0;
131
132       if (be_verbose_global)
133         progress_indication ("\n[" + file_name);
134
135       val = Open_type_font::make_otf (file_name);
136
137       if (be_verbose_global)
138         progress_indication ("]");
139
140       unsmob_metrics (val)->file_name_ = file_name;
141       SCM name_string = ly_string2scm (name);
142       unsmob_metrics (val)->description_ = scm_cons (name_string,
143                                                      scm_from_double (1.0));
144       otf_dict_->set (sname, val);
145       unsmob_metrics (val)->unprotect ();
146     }
147
148   return dynamic_cast<Open_type_font *> (unsmob_metrics (val));
149 }
150
151 Font_metric *
152 All_font_metrics::find_font (string name)
153 {
154   Font_metric *f = find_otf (name);
155
156   if (!f)
157     {
158       error (_f ("cannot find font: `%s'", name.c_str ()));
159     }
160
161   return f;
162 }
163
164 All_font_metrics *all_fonts_global;