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