]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font.cc
(glyph_list): format errors.
[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--2006 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 ("can't allocate %lu bytes", *length));
34
35       error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length);
36       if (error_code)
37         error (_f ("can't 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
86   return string ((char const*) tab, len);
87 }
88
89 FT_Face
90 open_ft_face (string str)
91 {
92   FT_Face face;
93   int error_code = FT_New_Face (freetype2_library, str.c_str (), 0, &face);
94
95   if (error_code == FT_Err_Unknown_File_Format)
96     error (_f ("unsupported font format: %s", str.c_str ()));
97   else if (error_code)
98     error (_f ("unknown error: %d reading font file: %s", error_code,
99                str.c_str ()));
100   return face;
101 }
102
103 SCM
104 Open_type_font::make_otf (string str)
105 {
106   FT_Face face = open_ft_face (str);
107   Open_type_font *otf = new Open_type_font (face);
108
109   return otf->self_scm ();
110 }
111
112 Open_type_font::Open_type_font (FT_Face face)
113 {
114   face_ = face;
115   lily_character_table_ = SCM_EOL;
116   lily_global_table_ = SCM_EOL;
117   lily_subfonts_ = SCM_EOL;
118   lily_index_to_bbox_table_ = SCM_EOL;
119
120   lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
121   lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
122   lily_subfonts_ = load_scheme_table ("LILF", face_);
123   index_to_charcode_map_ = make_index_to_charcode_map (face_);
124
125   lily_index_to_bbox_table_ = scm_c_make_hash_table (257);
126 }
127
128 void
129 Open_type_font::derived_mark () const
130 {
131   scm_gc_mark (lily_character_table_);
132   scm_gc_mark (lily_global_table_);
133   scm_gc_mark (lily_subfonts_);
134   scm_gc_mark (lily_index_to_bbox_table_);
135 }
136
137 Offset
138 Open_type_font::attachment_point (string glyph_name) const
139 {
140   SCM sym = ly_symbol2scm (glyph_name.c_str ());
141   SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
142
143   Offset o;
144   if (entry == SCM_BOOL_F)
145     return o;
146
147   SCM char_alist = entry;
148   SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
149
150   return point_constant * ly_scm2offset (att_scm);
151 }
152
153 Box
154 Open_type_font::get_indexed_char (size_t signed_idx) const
155 {
156   if (SCM_HASHTABLE_P (lily_index_to_bbox_table_))
157     {
158       SCM box = scm_hashq_ref (lily_index_to_bbox_table_,
159                                scm_from_unsigned (signed_idx), SCM_BOOL_F);
160       Box *box_ptr = Box::unsmob (box);
161       if (box_ptr)
162         return *box_ptr;
163     }
164
165   if (SCM_HASHTABLE_P (lily_character_table_))
166     {
167       const size_t len = 256;
168       char name[len];
169       size_t code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
170       if (code)
171         warning (_f ("FT_Get_Glyph_Name() returned error: %u", unsigned (code)));
172
173       SCM sym = ly_symbol2scm (name);
174       SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
175
176       if (alist != SCM_BOOL_F)
177         {
178           SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
179
180           Box b;
181           b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
182           bbox = scm_cdr (bbox);
183           b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
184           bbox = scm_cdr (bbox);
185           b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
186           bbox = scm_cdr (bbox);
187           b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
188           bbox = scm_cdr (bbox);
189
190           b.scale (point_constant);
191
192           scm_hashq_set_x (lily_index_to_bbox_table_,
193                            scm_from_unsigned (signed_idx),
194                            b.smobbed_copy ());
195           return b;
196         }
197     }
198
199   FT_UInt idx = signed_idx;
200   FT_Load_Glyph (face_,
201                  idx,
202                  FT_LOAD_NO_SCALE);
203
204   FT_Glyph_Metrics m = face_->glyph->metrics;
205   int hb = m.horiBearingX;
206   int vb = m.horiBearingY;
207   Box b (Interval (-hb, m.width - hb),
208          Interval (-vb, m.height - vb));
209
210   b.scale (design_size () / Real (face_->units_per_EM));
211   return b;
212 }
213
214 size_t
215 Open_type_font::name_to_index (string nm) const
216 {
217   char *nm_str = (char *) nm.c_str ();
218   if (size_t idx = FT_Get_Name_Index (face_, nm_str))
219     return idx;
220   
221   return (size_t) -1;
222 }
223
224 size_t
225 Open_type_font::index_to_charcode (size_t i) const
226 {
227   return ((Open_type_font *) this)->index_to_charcode_map_[i];
228 }
229
230 size_t
231 Open_type_font::count () const
232 {
233   return ((Open_type_font *) this)->index_to_charcode_map_.size ();
234 }
235
236 Real
237 Open_type_font::design_size () const
238 {
239   SCM entry = scm_hashq_ref (lily_global_table_,
240                              ly_symbol2scm ("design_size"),
241
242                              /*
243                                Hmm. Design size is arbitrary for
244                                non-design-size fonts. I vote for 1 -
245                                which will trip errors more
246                                quickly. --hwn.
247                              */
248                              scm_from_unsigned (1));
249   return scm_to_double (entry) * Real (point_constant);
250 }
251
252 SCM
253 Open_type_font::sub_fonts () const
254 {
255   return lily_subfonts_;
256 }
257
258 SCM
259 Open_type_font::get_char_table () const
260 {
261   return lily_character_table_;
262 }
263
264 SCM
265 Open_type_font::get_subfonts () const
266 {
267   return lily_subfonts_;
268 }
269
270 SCM
271 Open_type_font::get_global_table () const
272 {
273   return lily_global_table_;
274 }
275
276 string
277 Open_type_font::font_name () const
278 {
279   return FT_Get_Postscript_Name (face_);
280 }
281
282
283 SCM
284 Open_type_font::glyph_list () const
285 {
286   SCM retval = SCM_EOL;
287   SCM *tail = &retval;
288   
289   for (int i = 0; i < face_->num_glyphs; i++)
290     {
291       const size_t len = 256;
292       char name[len];
293       size_t code = FT_Get_Glyph_Name (face_, i, name, len);
294       if (code)
295         warning (_f ("FT_Get_Glyph_Name() returned error: %u", unsigned (code)));
296
297       *tail = scm_cons (scm_makfrom0str (name), SCM_EOL);
298       tail = SCM_CDRLOC (*tail);
299     }
300   
301   return retval;
302 }