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