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