2 open-type-font.cc -- implement Open_type_font
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include <freetype/tttables.h>
15 #include "open-type-font.hh"
16 #include "dimensions.hh"
17 #include "modified-font-metric.hh"
20 load_table (char const *tag_str, FT_Face face, FT_ULong *length)
22 FT_ULong tag = FT_MAKE_TAG (tag_str[0], tag_str[1], tag_str[2], tag_str[3]);
24 int error_code = FT_Load_Sfnt_Table (face, tag, 0, NULL, length);
27 FT_Byte *buffer = (FT_Byte*) malloc (*length);
29 error (_f ("Cannot allocate %d bytes", *length));
31 error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length );
33 error (_f ("Could not load %s font table", tag_str));
42 load_scheme_table (char const *tag_str, FT_Face face)
45 FT_Byte *buffer = load_table (tag_str, face, &length);
50 String contents ((Byte const*)buffer, length);
51 contents = "(quote (" + contents + "))";
53 tab = scm_c_eval_string (contents.to_str0 ());
60 make_index_to_charcode_map (FT_Face face)
62 Index_to_charcode_map m;
66 for (charcode = FT_Get_First_Char (face, &gindex); gindex != 0;
67 charcode = FT_Get_Next_Char (face, charcode, &gindex))
72 Open_type_font::~Open_type_font()
78 Open_type_font::make_otf (String str)
81 int error_code = FT_New_Face(freetype2_library, str.to_str0 (), 0, &face);
83 if (error_code == FT_Err_Unknown_File_Format)
85 error (_f ("Unsupported font format: %s", str.to_str0 ()));
89 error (_f ("Unknown error: %d reading font file: %s", error_code,
93 Open_type_font *otf = new Open_type_font (face);
95 return otf->self_scm ();
98 Open_type_font::Open_type_font (FT_Face face)
101 lily_character_table_ = SCM_EOL;
102 lily_global_table_ = SCM_EOL;
103 lily_subfonts_ = SCM_EOL;
105 lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
106 lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
107 lily_subfonts_ = load_scheme_table ("LILF", face_);
108 index_to_charcode_map_ = make_index_to_charcode_map (face_);
112 Open_type_font::derived_mark () const
114 scm_gc_mark (lily_character_table_);
115 scm_gc_mark (lily_global_table_);
116 scm_gc_mark (lily_subfonts_);
120 Open_type_font::attachment_point (String glyph_name) const
122 SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
123 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
126 if (entry == SCM_BOOL_F)
129 SCM char_alist = entry;
130 SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
132 return point_constant * ly_scm2offset (att_scm);
136 Open_type_font::get_indexed_char (int signed_idx) const
138 if (SCM_HASHTABLE_P (lily_character_table_))
142 int code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
144 warning (_f ("FT_Get_Glyph_Name() returned error: %d", code));
146 SCM sym = ly_symbol2scm (name);
147 SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
149 if (alist != SCM_BOOL_F)
151 SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
154 b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
155 bbox = scm_cdr (bbox);
156 b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
157 bbox = scm_cdr (bbox);
158 b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
159 bbox = scm_cdr (bbox);
160 b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
161 bbox = scm_cdr (bbox);
163 b.scale (point_constant);
168 FT_UInt idx = signed_idx;
169 FT_Load_Glyph (face_,
173 FT_Glyph_Metrics m = face_->glyph->metrics;
174 int hb = m.horiBearingX;
175 int vb = m.horiBearingY;
176 Box b (Interval (-hb, m.width - hb),
177 Interval (-vb, m.height - vb));
179 b.scale (design_size () / Real (face_->units_per_EM));
184 Open_type_font::name_to_index (String nm) const
186 char *nm_str = (char*) nm.to_str0 ();
187 if (int idx = FT_Get_Name_Index (face_, nm_str))
193 Open_type_font::index_to_charcode (int i) const
195 return ((Open_type_font*) this)->index_to_charcode_map_[i];
199 Open_type_font::count () const
201 return ((Open_type_font*) this)->index_to_charcode_map_.size ();
205 Open_type_font::design_size () const
207 SCM entry = scm_hashq_ref (lily_global_table_,
208 ly_symbol2scm ("design_size"),
211 Hmm. Design size is arbitrary for
212 non-design-size fonts. I vote for 1 -
213 which will trip errors more
217 return scm_to_double (entry) * Real (point_constant);
222 Open_type_font::sub_fonts () const
224 return lily_subfonts_;
228 Open_type_font::get_char_table () const
230 return lily_character_table_;
234 Open_type_font::get_subfonts () const
236 return lily_subfonts_;
240 Open_type_font::get_global_table () const
242 return lily_global_table_;
246 Open_type_font::font_name () const
248 return FT_Get_Postscript_Name (face_);