2 open-type-font.cc -- implement Open_type_font
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "open-type-font.hh"
15 #include <freetype/tttables.h>
17 #include "dimensions.hh"
18 #include "international.hh"
19 #include "modified-font-metric.hh"
23 load_table (char const *tag_str, FT_Face face, FT_ULong *length)
26 FT_ULong tag = FT_MAKE_TAG (tag_str[0], tag_str[1], tag_str[2], tag_str[3]);
28 int error_code = FT_Load_Sfnt_Table (face, tag, 0, NULL, length);
31 FT_Byte *buffer = (FT_Byte *) malloc (*length);
33 error (_f ("can't allocate %d bytes", *length));
35 error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length);
37 error (_f ("can't load font table: %s", tag_str));
42 programming_error ("Cannot find OpenType table.");
48 Open_type_font::get_otf_table (string tag) const
50 return ::get_otf_table (face_, tag);
54 load_scheme_table (char const *tag_str, FT_Face face)
57 FT_Byte *buffer = load_table (tag_str, face, &length);
62 string contents ((char const*)buffer, length);
63 contents = "(quote (" + contents + "))";
65 tab = scm_c_eval_string (contents.c_str ());
72 make_index_to_charcode_map (FT_Face face)
74 Index_to_charcode_map m;
78 for (charcode = FT_Get_First_Char (face, &gindex); gindex != 0;
79 charcode = FT_Get_Next_Char (face, charcode, &gindex))
84 Open_type_font::~Open_type_font ()
93 get_otf_table (FT_Face face, string tag)
96 FT_Byte *tab = load_table (tag.c_str (), face, &len);
98 return string ((char const*) tab, len);
102 open_ft_face (string str)
105 int error_code = FT_New_Face (freetype2_library, str.c_str (), 0, &face);
107 if (error_code == FT_Err_Unknown_File_Format)
108 error (_f ("unsupported font format: %s", str.c_str ()));
110 error (_f ("unknown error: %d reading font file: %s", error_code,
116 Open_type_font::make_otf (string str)
118 FT_Face face = open_ft_face (str);
119 Open_type_font *otf = new Open_type_font (face);
121 return otf->self_scm ();
124 Open_type_font::Open_type_font (FT_Face face)
127 lily_character_table_ = SCM_EOL;
128 lily_global_table_ = SCM_EOL;
129 lily_subfonts_ = SCM_EOL;
130 lily_index_to_bbox_table_ = SCM_EOL;
132 lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
133 lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
134 lily_subfonts_ = load_scheme_table ("LILF", face_);
135 index_to_charcode_map_ = make_index_to_charcode_map (face_);
137 lily_index_to_bbox_table_ = scm_c_make_hash_table (257);
141 Open_type_font::derived_mark () const
143 scm_gc_mark (lily_character_table_);
144 scm_gc_mark (lily_global_table_);
145 scm_gc_mark (lily_subfonts_);
146 scm_gc_mark (lily_index_to_bbox_table_);
150 Open_type_font::attachment_point (string glyph_name) const
152 SCM sym = ly_symbol2scm (glyph_name.c_str ());
153 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
156 if (entry == SCM_BOOL_F)
159 SCM char_alist = entry;
160 SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
162 return point_constant * ly_scm2offset (att_scm);
166 Open_type_font::get_indexed_char (size_t signed_idx) const
168 if (SCM_HASHTABLE_P (lily_index_to_bbox_table_))
170 SCM box = scm_hashq_ref (lily_index_to_bbox_table_,
171 scm_from_unsigned (signed_idx), SCM_BOOL_F);
172 Box *box_ptr = Box::unsmob (box);
177 if (SCM_HASHTABLE_P (lily_character_table_))
179 const size_t len = 256;
181 size_t code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
183 warning (_f ("FT_Get_Glyph_Name() returned error: %d", code));
185 SCM sym = ly_symbol2scm (name);
186 SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
188 if (alist != SCM_BOOL_F)
190 SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
193 b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
194 bbox = scm_cdr (bbox);
195 b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
196 bbox = scm_cdr (bbox);
197 b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
198 bbox = scm_cdr (bbox);
199 b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
200 bbox = scm_cdr (bbox);
202 b.scale (point_constant);
204 scm_hashq_set_x (lily_index_to_bbox_table_,
205 scm_from_unsigned (signed_idx),
211 FT_UInt idx = signed_idx;
212 FT_Load_Glyph (face_,
216 FT_Glyph_Metrics m = face_->glyph->metrics;
217 int hb = m.horiBearingX;
218 int vb = m.horiBearingY;
219 Box b (Interval (-hb, m.width - hb),
220 Interval (-vb, m.height - vb));
222 b.scale (design_size () / Real (face_->units_per_EM));
227 Open_type_font::name_to_index (string nm) const
229 char *nm_str = (char *) nm.c_str ();
230 if (size_t idx = FT_Get_Name_Index (face_, nm_str))
237 Open_type_font::index_to_charcode (size_t i) const
239 return ((Open_type_font *) this)->index_to_charcode_map_[i];
243 Open_type_font::count () const
245 return ((Open_type_font *) this)->index_to_charcode_map_.size ();
249 Open_type_font::design_size () const
251 SCM entry = scm_hashq_ref (lily_global_table_,
252 ly_symbol2scm ("design_size"),
255 Hmm. Design size is arbitrary for
256 non-design-size fonts. I vote for 1 -
257 which will trip errors more
260 scm_from_unsigned (1));
261 return scm_to_double (entry) * Real (point_constant);
265 Open_type_font::sub_fonts () const
267 return lily_subfonts_;
271 Open_type_font::get_char_table () const
273 return lily_character_table_;
277 Open_type_font::get_subfonts () const
279 return lily_subfonts_;
283 Open_type_font::get_global_table () const
285 return lily_global_table_;
289 Open_type_font::font_name () const
291 return FT_Get_Postscript_Name (face_);
296 Open_type_font::glyph_list () const
298 SCM retval = SCM_EOL;
301 for (int i = 0; i < face_->num_glyphs; i++)
303 const size_t len = 256;
305 size_t code = FT_Get_Glyph_Name (face_, i, name, len);
307 warning (_f ("FT_Get_Glyph_Name() returned error: %d", code));
309 *tail = scm_cons (scm_makfrom0str (name), SCM_EOL);
310 tail = SCM_CDRLOC (*tail);