]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font.cc
* scm/framework-ps.scm (write-preamble): embed CFFs if file name
[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
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 /*
90   UGH fix naming
91 */
92 String
93 get_otf_table (FT_Face face, String tag)
94 {
95   FT_ULong len;
96   FT_Byte *tab = load_table (tag.to_str0 (), face,  &len);
97
98   return String (tab, len);
99 }
100
101 FT_Face
102 open_ft_face (String str)
103 {
104   FT_Face face;
105   int error_code = FT_New_Face (freetype2_library, str.to_str0 (), 0, &face);
106
107   if (error_code == FT_Err_Unknown_File_Format)
108     error (_f ("unsupported font format: %s", str.to_str0 ()));
109   else if (error_code)
110     error (_f ("unknown error: %d reading font file: %s", error_code,
111                str.to_str0 ()));
112   return face;
113 }
114
115 SCM
116 Open_type_font::make_otf (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
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
137 void
138 Open_type_font::derived_mark () const
139 {
140   scm_gc_mark (lily_character_table_);
141   scm_gc_mark (lily_global_table_);
142   scm_gc_mark (lily_subfonts_);
143 }
144
145 Offset
146 Open_type_font::attachment_point (String glyph_name) const
147 {
148   SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
149   SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
150
151   Offset o;
152   if (entry == SCM_BOOL_F)
153     return o;
154
155   SCM char_alist = entry;
156   SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
157
158   return point_constant * ly_scm2offset (att_scm);
159 }
160
161 Box
162 Open_type_font::get_indexed_char (int signed_idx) const
163 {
164   if (SCM_HASHTABLE_P (lily_character_table_))
165     {
166       const int len = 256;
167       char name[len];
168       int code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
169       if (code)
170         warning (_f ("FT_Get_Glyph_Name() returned error: %d", code));
171
172       SCM sym = ly_symbol2scm (name);
173       SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
174
175       if (alist != SCM_BOOL_F)
176         {
177           SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
178
179           Box b;
180           b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
181           bbox = scm_cdr (bbox);
182           b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
183           bbox = scm_cdr (bbox);
184           b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
185           bbox = scm_cdr (bbox);
186           b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
187           bbox = scm_cdr (bbox);
188
189           b.scale (point_constant);
190           return b;
191         }
192     }
193
194   FT_UInt idx = signed_idx;
195   FT_Load_Glyph (face_,
196                  idx,
197                  FT_LOAD_NO_SCALE);
198
199   FT_Glyph_Metrics m = face_->glyph->metrics;
200   int hb = m.horiBearingX;
201   int vb = m.horiBearingY;
202   Box b (Interval (-hb, m.width - hb),
203          Interval (-vb, m.height - vb));
204
205   b.scale (design_size () / Real (face_->units_per_EM));
206   return b;
207 }
208
209 int
210 Open_type_font::name_to_index (String nm) const
211 {
212   char *nm_str = (char *) nm.to_str0 ();
213   if (int idx = FT_Get_Name_Index (face_, nm_str))
214     return idx;
215   return -1;
216 }
217
218 unsigned
219 Open_type_font::index_to_charcode (int i) const
220 {
221   return ((Open_type_font *) this)->index_to_charcode_map_[i];
222 }
223
224 int
225 Open_type_font::count () const
226 {
227   return ((Open_type_font *) this)->index_to_charcode_map_.size ();
228 }
229
230 Real
231 Open_type_font::design_size () const
232 {
233   SCM entry = scm_hashq_ref (lily_global_table_,
234                              ly_symbol2scm ("design_size"),
235
236                              /*
237                                Hmm. Design size is arbitrary for
238                                non-design-size fonts. I vote for 1 -
239                                which will trip errors more
240                                quickly. --hwn.
241                              */
242                              scm_from_int (1));
243   return scm_to_double (entry) * Real (point_constant);
244 }
245
246 SCM
247 Open_type_font::sub_fonts () const
248 {
249   return lily_subfonts_;
250 }
251
252 SCM
253 Open_type_font::get_char_table () const
254 {
255   return lily_character_table_;
256 }
257
258 SCM
259 Open_type_font::get_subfonts () const
260 {
261   return lily_subfonts_;
262 }
263
264 SCM
265 Open_type_font::get_global_table () const
266 {
267   return lily_global_table_;
268 }
269
270 String
271 Open_type_font::font_name () const
272 {
273   return FT_Get_Postscript_Name (face_);
274 }
275