]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / open-type-font.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--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 "open-type-font.hh"
21 #include "freetype.hh"
22
23 #include <cstdio>
24
25 using namespace std;
26
27 #include FT_TRUETYPE_TABLES_H
28
29 #include "dimensions.hh"
30 #include "international.hh"
31 #include "modified-font-metric.hh"
32 #include "warn.hh"
33
34 using std::map;
35 using std::string;
36
37 FT_Byte *
38 load_table (char const *tag_str, FT_Face face, FT_ULong *length)
39 {
40   *length = 0;
41   FT_ULong tag = FT_MAKE_TAG (tag_str[0], tag_str[1], tag_str[2], tag_str[3]);
42
43   FT_Error error_code = FT_Load_Sfnt_Table (face, tag, 0, NULL, length);
44   if (!error_code)
45     {
46       FT_Byte *buffer = (FT_Byte *) malloc (*length);
47       if (buffer == NULL)
48         error (_f ("cannot allocate %lu bytes", *length));
49
50       error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length);
51       if (error_code)
52         error (_f ("cannot load font table: %s", tag_str));
53
54       return buffer;
55     }
56   else
57     programming_error (_f ("FreeType error: %s",
58                            freetype_error_string (error_code).c_str ()
59                           ));
60
61   return 0;
62 }
63
64 string
65 Open_type_font::get_otf_table (const string &tag) const
66 {
67   return ::get_otf_table (face_, tag);
68 }
69
70 SCM
71 load_scheme_table (char const *tag_str, FT_Face face)
72 {
73   FT_ULong length = 0;
74   FT_Byte *buffer = load_table (tag_str, face, &length);
75
76   SCM tab = SCM_EOL;
77   if (buffer)
78     {
79       string contents ((char const *)buffer, length);
80       contents = "(quote (" + contents + "))";
81
82 #if GUILEV2
83       tab = scm_eval_string (scm_from_utf8_string (contents.c_str ()));
84 #else
85       tab = scm_c_eval_string (contents.c_str ());
86 #endif
87       free (buffer);
88     }
89   return tab;
90 }
91
92 Open_type_font::~Open_type_font ()
93 {
94   FT_Done_Face (face_);
95 }
96
97 /*
98   UGH fix naming
99 */
100 string
101 get_otf_table (FT_Face face, const string &tag)
102 {
103   FT_ULong len;
104   FT_Byte *tab = load_table (tag.c_str (), face, &len);
105   string ret ((char const *) tab, len);
106   free (tab);
107
108   return ret;
109 }
110
111 FT_Face
112 open_ft_face (const string &str, FT_Long idx)
113 {
114   FT_Face face;
115   FT_Error error_code = FT_New_Face (freetype2_library, str.c_str (), idx, &face);
116
117   if (error_code == FT_Err_Unknown_File_Format)
118     error (_f ("unsupported font format: %s", str.c_str ()));
119   else if (error_code)
120     error (_f ("error reading font file %s: %s",
121                str.c_str (),
122                freetype_error_string (error_code).c_str ()));
123   return face;
124 }
125
126 SCM
127 Open_type_font::make_otf (const string &str)
128 {
129   FT_Face face = open_ft_face (str, 0 /* index */);
130   Open_type_font *otf = new Open_type_font (face);
131
132   return otf->self_scm ();
133 }
134
135 Open_type_font::Open_type_font (FT_Face face)
136 {
137   face_ = face;
138   lily_character_table_ = SCM_EOL;
139   lily_global_table_ = SCM_EOL;
140   lily_subfonts_ = SCM_EOL;
141   lily_index_to_bbox_table_ = SCM_EOL;
142
143   lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
144   lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
145   lily_subfonts_ = load_scheme_table ("LILF", face_);
146   index_to_charcode_map_ = make_index_to_charcode_map (face_);
147
148   lily_index_to_bbox_table_ = scm_c_make_hash_table (257);
149 }
150
151 void
152 Open_type_font::derived_mark () const
153 {
154   scm_gc_mark (lily_character_table_);
155   scm_gc_mark (lily_global_table_);
156   scm_gc_mark (lily_subfonts_);
157   scm_gc_mark (lily_index_to_bbox_table_);
158 }
159
160 Offset
161 Open_type_font::attachment_point (const string &glyph_name) const
162 {
163   SCM sym = ly_symbol2scm (glyph_name.c_str ());
164   SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
165
166   Offset o;
167   if (scm_is_false (entry))
168     return o;
169
170   SCM char_alist = entry;
171   SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
172
173   return point_constant * ly_scm2offset (att_scm);
174 }
175
176 Box
177 Open_type_font::get_indexed_char_dimensions (size_t signed_idx) const
178 {
179   if (SCM_HASHTABLE_P (lily_index_to_bbox_table_))
180     {
181       SCM box = scm_hashq_ref (lily_index_to_bbox_table_,
182                                scm_from_unsigned_integer (signed_idx), SCM_BOOL_F);
183       Box *box_ptr = unsmob<Box> (box);
184       if (box_ptr)
185         return *box_ptr;
186     }
187
188   if (SCM_HASHTABLE_P (lily_character_table_))
189     {
190       const size_t len = 256;
191       char name[len];
192       FT_Error code = FT_Get_Glyph_Name (face_, FT_UInt (signed_idx),
193                                          name, FT_UInt (len));
194       if (code)
195         warning (_f ("FT_Get_Glyph_Name () Freetype error: %s",
196                      freetype_error_string (code)));
197
198       SCM sym = ly_symbol2scm (name);
199       SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
200
201       if (scm_is_true (alist))
202         {
203           SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
204
205           Box b;
206           b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
207           bbox = scm_cdr (bbox);
208           b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
209           bbox = scm_cdr (bbox);
210           b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
211           bbox = scm_cdr (bbox);
212           b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
213           bbox = scm_cdr (bbox);
214
215           b.scale (point_constant);
216
217           scm_hashq_set_x (lily_index_to_bbox_table_,
218                            scm_from_unsigned_integer (signed_idx),
219                            b.smobbed_copy ());
220           return b;
221         }
222     }
223
224   Box b = get_unscaled_indexed_char_dimensions (signed_idx);
225
226   b.scale (design_size () / Real (face_->units_per_EM));
227   return b;
228 }
229
230 Real
231 Open_type_font::get_units_per_EM () const
232 {
233   return face_->units_per_EM;
234 }
235
236 size_t
237 Open_type_font::name_to_index (string nm) const
238 {
239   char *nm_str = (char *) nm.c_str ();
240   if (FT_UInt idx = FT_Get_Name_Index (face_, nm_str))
241     return (size_t) idx;
242
243   return (size_t) - 1;
244 }
245
246 Box
247 Open_type_font::get_unscaled_indexed_char_dimensions (size_t signed_idx) const
248 {
249   return ly_FT_get_unscaled_indexed_char_dimensions (face_, signed_idx);
250 }
251
252 Box
253 Open_type_font::get_glyph_outline_bbox (size_t signed_idx) const
254 {
255   return ly_FT_get_glyph_outline_bbox (face_, signed_idx);
256 }
257
258 SCM
259 Open_type_font::get_glyph_outline (size_t signed_idx) const
260 {
261   return ly_FT_get_glyph_outline (face_, signed_idx);
262 }
263
264 size_t
265 Open_type_font::index_to_charcode (size_t i) const
266 {
267   map<FT_UInt, FT_ULong>::const_iterator iter;
268   iter = index_to_charcode_map_.find (FT_UInt (i));
269
270   if (iter != index_to_charcode_map_.end ())
271     return (size_t) iter->second;
272   else
273     {
274       programming_error ("Invalid index for character");
275       return 0;
276     }
277 }
278
279 size_t
280 Open_type_font::count () const
281 {
282   return index_to_charcode_map_.size ();
283 }
284
285 Real
286 Open_type_font::design_size () const
287 {
288   SCM entry = scm_hashq_ref (lily_global_table_,
289                              ly_symbol2scm ("design_size"),
290
291                              /*
292                                Hmm. Design size is arbitrary for
293                                non-design-size fonts. I vote for 1 -
294                                which will trip errors more
295                                quickly. --hwn.
296                              */
297                              scm_from_unsigned_integer (1));
298   return scm_to_double (entry) * Real (point_constant);
299 }
300
301 SCM
302 Open_type_font::sub_fonts () const
303 {
304   return lily_subfonts_;
305 }
306
307 SCM
308 Open_type_font::get_char_table () const
309 {
310   return lily_character_table_;
311 }
312
313 SCM
314 Open_type_font::get_subfonts () const
315 {
316   return lily_subfonts_;
317 }
318
319 SCM
320 Open_type_font::get_global_table () const
321 {
322   return lily_global_table_;
323 }
324
325 string
326 Open_type_font::font_name () const
327 {
328   return FT_Get_Postscript_Name (face_);
329 }
330
331 SCM
332 Open_type_font::glyph_list () const
333 {
334   SCM retval = SCM_EOL;
335   SCM *tail = &retval;
336
337   for (int i = 0; i < face_->num_glyphs; i++)
338     {
339       const size_t len = 256;
340       char name[len];
341       FT_Error code = FT_Get_Glyph_Name (face_, i, name, len);
342       if (code)
343         warning (_f ("FT_Get_Glyph_Name () error: %s",
344                      freetype_error_string (code).c_str ()));
345
346       *tail = scm_cons (scm_from_ascii_string (name), SCM_EOL);
347       tail = SCM_CDRLOC (*tail);
348     }
349
350   return retval;
351 }