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