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