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