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 %lu 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 Open_type_font::~Open_type_font ()
81 get_otf_table (FT_Face face, string tag)
84 FT_Byte *tab = load_table (tag.c_str (), face, &len);
85 string ret ((char const*) tab, len);
92 open_ft_face (string str)
95 int error_code = FT_New_Face (freetype2_library, str.c_str (), 0, &face);
97 if (error_code == FT_Err_Unknown_File_Format)
98 error (_f ("unsupported font format: %s", str.c_str ()));
100 error (_f ("unknown error: %d reading font file: %s", error_code,
106 Open_type_font::make_otf (string str)
108 FT_Face face = open_ft_face (str);
109 Open_type_font *otf = new Open_type_font (face);
111 return otf->self_scm ();
114 Open_type_font::Open_type_font (FT_Face face)
117 lily_character_table_ = SCM_EOL;
118 lily_global_table_ = SCM_EOL;
119 lily_subfonts_ = SCM_EOL;
120 lily_index_to_bbox_table_ = SCM_EOL;
122 lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
123 lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
124 lily_subfonts_ = load_scheme_table ("LILF", face_);
125 index_to_charcode_map_ = make_index_to_charcode_map (face_);
127 lily_index_to_bbox_table_ = scm_c_make_hash_table (257);
131 Open_type_font::derived_mark () const
133 scm_gc_mark (lily_character_table_);
134 scm_gc_mark (lily_global_table_);
135 scm_gc_mark (lily_subfonts_);
136 scm_gc_mark (lily_index_to_bbox_table_);
140 Open_type_font::attachment_point (string glyph_name) const
142 SCM sym = ly_symbol2scm (glyph_name.c_str ());
143 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
146 if (entry == SCM_BOOL_F)
149 SCM char_alist = entry;
150 SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
152 return point_constant * ly_scm2offset (att_scm);
156 Open_type_font::get_indexed_char (size_t signed_idx) const
158 if (SCM_HASHTABLE_P (lily_index_to_bbox_table_))
160 SCM box = scm_hashq_ref (lily_index_to_bbox_table_,
161 scm_from_unsigned (signed_idx), SCM_BOOL_F);
162 Box *box_ptr = Box::unsmob (box);
167 if (SCM_HASHTABLE_P (lily_character_table_))
169 const size_t len = 256;
171 size_t code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
173 warning (_f ("FT_Get_Glyph_Name() returned error: %u", unsigned (code)));
175 SCM sym = ly_symbol2scm (name);
176 SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
178 if (alist != SCM_BOOL_F)
180 SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
183 b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
184 bbox = scm_cdr (bbox);
185 b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
186 bbox = scm_cdr (bbox);
187 b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
188 bbox = scm_cdr (bbox);
189 b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
190 bbox = scm_cdr (bbox);
192 b.scale (point_constant);
194 scm_hashq_set_x (lily_index_to_bbox_table_,
195 scm_from_unsigned (signed_idx),
201 FT_UInt idx = signed_idx;
202 FT_Load_Glyph (face_,
206 FT_Glyph_Metrics m = face_->glyph->metrics;
207 int hb = m.horiBearingX;
208 int vb = m.horiBearingY;
209 Box b (Interval (-hb, m.width - hb),
210 Interval (-vb, m.height - vb));
212 b.scale (design_size () / Real (face_->units_per_EM));
217 Open_type_font::name_to_index (string nm) const
219 char *nm_str = (char *) nm.c_str ();
220 if (size_t idx = FT_Get_Name_Index (face_, nm_str))
227 Open_type_font::index_to_charcode (size_t i) const
229 return ((Open_type_font *) this)->index_to_charcode_map_[i];
233 Open_type_font::count () const
235 return ((Open_type_font *) this)->index_to_charcode_map_.size ();
239 Open_type_font::design_size () const
241 SCM entry = scm_hashq_ref (lily_global_table_,
242 ly_symbol2scm ("design_size"),
245 Hmm. Design size is arbitrary for
246 non-design-size fonts. I vote for 1 -
247 which will trip errors more
250 scm_from_unsigned (1));
251 return scm_to_double (entry) * Real (point_constant);
255 Open_type_font::sub_fonts () const
257 return lily_subfonts_;
261 Open_type_font::get_char_table () const
263 return lily_character_table_;
267 Open_type_font::get_subfonts () const
269 return lily_subfonts_;
273 Open_type_font::get_global_table () const
275 return lily_global_table_;
279 Open_type_font::font_name () const
281 return FT_Get_Postscript_Name (face_);
286 Open_type_font::glyph_list () const
288 SCM retval = SCM_EOL;
291 for (int i = 0; i < face_->num_glyphs; i++)
293 const size_t len = 256;
295 size_t code = FT_Get_Glyph_Name (face_, i, name, len);
297 warning (_f ("FT_Get_Glyph_Name() returned error: %u", unsigned (code)));
299 *tail = scm_cons (scm_makfrom0str (name), SCM_EOL);
300 tail = SCM_CDRLOC (*tail);