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