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);
86 return string ((char const*) tab, len);
90 open_ft_face (string str)
93 int error_code = FT_New_Face (freetype2_library, str.c_str (), 0, &face);
95 if (error_code == FT_Err_Unknown_File_Format)
96 error (_f ("unsupported font format: %s", str.c_str ()));
98 error (_f ("unknown error: %d reading font file: %s", error_code,
104 Open_type_font::make_otf (string str)
106 FT_Face face = open_ft_face (str);
107 Open_type_font *otf = new Open_type_font (face);
109 return otf->self_scm ();
112 Open_type_font::Open_type_font (FT_Face face)
115 lily_character_table_ = SCM_EOL;
116 lily_global_table_ = SCM_EOL;
117 lily_subfonts_ = SCM_EOL;
118 lily_index_to_bbox_table_ = SCM_EOL;
120 lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
121 lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
122 lily_subfonts_ = load_scheme_table ("LILF", face_);
123 index_to_charcode_map_ = make_index_to_charcode_map (face_);
125 lily_index_to_bbox_table_ = scm_c_make_hash_table (257);
129 Open_type_font::derived_mark () const
131 scm_gc_mark (lily_character_table_);
132 scm_gc_mark (lily_global_table_);
133 scm_gc_mark (lily_subfonts_);
134 scm_gc_mark (lily_index_to_bbox_table_);
138 Open_type_font::attachment_point (string glyph_name) const
140 SCM sym = ly_symbol2scm (glyph_name.c_str ());
141 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
144 if (entry == SCM_BOOL_F)
147 SCM char_alist = entry;
148 SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
150 return point_constant * ly_scm2offset (att_scm);
154 Open_type_font::get_indexed_char (size_t signed_idx) const
156 if (SCM_HASHTABLE_P (lily_index_to_bbox_table_))
158 SCM box = scm_hashq_ref (lily_index_to_bbox_table_,
159 scm_from_unsigned (signed_idx), SCM_BOOL_F);
160 Box *box_ptr = Box::unsmob (box);
165 if (SCM_HASHTABLE_P (lily_character_table_))
167 const size_t len = 256;
169 size_t code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
171 warning (_f ("FT_Get_Glyph_Name() returned error: %u", unsigned (code)));
173 SCM sym = ly_symbol2scm (name);
174 SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
176 if (alist != SCM_BOOL_F)
178 SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
181 b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
182 bbox = scm_cdr (bbox);
183 b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
184 bbox = scm_cdr (bbox);
185 b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
186 bbox = scm_cdr (bbox);
187 b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
188 bbox = scm_cdr (bbox);
190 b.scale (point_constant);
192 scm_hashq_set_x (lily_index_to_bbox_table_,
193 scm_from_unsigned (signed_idx),
199 FT_UInt idx = signed_idx;
200 FT_Load_Glyph (face_,
204 FT_Glyph_Metrics m = face_->glyph->metrics;
205 int hb = m.horiBearingX;
206 int vb = m.horiBearingY;
207 Box b (Interval (-hb, m.width - hb),
208 Interval (-vb, m.height - vb));
210 b.scale (design_size () / Real (face_->units_per_EM));
215 Open_type_font::name_to_index (string nm) const
217 char *nm_str = (char *) nm.c_str ();
218 if (size_t idx = FT_Get_Name_Index (face_, nm_str))
225 Open_type_font::index_to_charcode (size_t i) const
227 return ((Open_type_font *) this)->index_to_charcode_map_[i];
231 Open_type_font::count () const
233 return ((Open_type_font *) this)->index_to_charcode_map_.size ();
237 Open_type_font::design_size () const
239 SCM entry = scm_hashq_ref (lily_global_table_,
240 ly_symbol2scm ("design_size"),
243 Hmm. Design size is arbitrary for
244 non-design-size fonts. I vote for 1 -
245 which will trip errors more
248 scm_from_unsigned (1));
249 return scm_to_double (entry) * Real (point_constant);
253 Open_type_font::sub_fonts () const
255 return lily_subfonts_;
259 Open_type_font::get_char_table () const
261 return lily_character_table_;
265 Open_type_font::get_subfonts () const
267 return lily_subfonts_;
271 Open_type_font::get_global_table () const
273 return lily_global_table_;
277 Open_type_font::font_name () const
279 return FT_Get_Postscript_Name (face_);
284 Open_type_font::glyph_list () const
286 SCM retval = SCM_EOL;
289 for (int i = 0; i < face_->num_glyphs; i++)
291 const size_t len = 256;
293 size_t code = FT_Get_Glyph_Name (face_, i, name, len);
295 warning (_f ("FT_Get_Glyph_Name() returned error: %u", unsigned (code)));
297 *tail = scm_cons (scm_makfrom0str (name), SCM_EOL);
298 tail = SCM_CDRLOC (*tail);