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