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