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