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