2 open-type-font.cc -- implement Open_type_font
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 #include <freetype/tttables.h>
16 #include "open-type-font.hh"
17 #include "dimensions.hh"
18 #include "modified-font-metric.hh"
20 const Real point_constant = 1 PT;
23 load_table (char const *tag_str, FT_Face face, FT_ULong *length)
25 FT_ULong tag = FT_MAKE_TAG(tag_str[0], tag_str[1], tag_str[2], tag_str[3]);
27 int error_code = FT_Load_Sfnt_Table (face, tag, 0, NULL, length);
30 FT_Byte *buffer = (FT_Byte*) malloc (*length);
32 error (_f ("Cannot allocate %d bytes", *length));
34 error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length );
37 error (_f ("Could not load %s font table", tag_str));
47 load_scheme_table (char const *tag_str, FT_Face face)
50 FT_Byte *buffer = load_table (tag_str, face, &length);
55 String contents ((Byte const*)buffer, length);
56 contents = "(quote (" + contents + "))";
58 tab = scm_c_eval_string (contents.to_str0 ());
66 make_index_to_charcode_map (FT_Face face)
68 Index_to_charcode_map m;
72 for (charcode = FT_Get_First_Char (face, &gindex); gindex != 0;
73 charcode = FT_Get_Next_Char (face, charcode, &gindex))
78 Open_type_font::~Open_type_font()
84 Open_type_font::make_otf (String str)
87 int error_code = FT_New_Face(freetype2_library, str.to_str0 (), 0, &face);
89 if (error_code == FT_Err_Unknown_File_Format)
91 error (_f ("Unsupported font format: %s", str.to_str0 ()));
95 error (_f ("Unknown error: %d reading font file: %s", error_code,
99 Open_type_font *otf = new Open_type_font (face);
101 return otf->self_scm ();
104 Open_type_font::Open_type_font (FT_Face face)
107 lily_character_table_ = SCM_EOL;
108 lily_global_table_ = SCM_EOL;
109 lily_subfonts_ = SCM_EOL;
111 lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
112 lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
113 lily_subfonts_ = load_scheme_table ("LILF", face_);
114 index_to_charcode_map_ = make_index_to_charcode_map (face_);
118 Open_type_font::derived_mark () const
120 scm_gc_mark (lily_character_table_);
121 scm_gc_mark (lily_global_table_);
122 scm_gc_mark (lily_subfonts_);
126 Open_type_font::attachment_point (String glyph_name) const
128 SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
129 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
132 if (entry == SCM_BOOL_F)
135 SCM char_alist = entry;
136 SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
138 return point_constant * ly_scm2offset (att_scm);
142 Open_type_font::get_indexed_char (int signed_idx) const
144 if (SCM_HASHTABLE_P (lily_character_table_))
148 int code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
151 warning ("FT_Get_Glyph_Name() returned error");
154 SCM sym = ly_symbol2scm (name);
155 SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
157 if (alist != SCM_BOOL_F)
159 SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
162 b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
163 bbox = scm_cdr (bbox);
164 b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
165 bbox = scm_cdr (bbox);
166 b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
167 bbox = scm_cdr (bbox);
168 b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
169 bbox = scm_cdr (bbox);
171 b.scale (point_constant);
176 FT_UInt idx = signed_idx;
177 FT_Load_Glyph (face_,
181 FT_Glyph_Metrics m = face_->glyph->metrics;
182 int hb = m.horiBearingX;
183 int vb = m.horiBearingY;
184 Box b (Interval (-hb, m.width - hb),
185 Interval (-vb, m.height - vb));
187 b.scale (design_size () * Real (point_constant) / face_->units_per_EM);
192 Open_type_font::name_to_index (String nm) const
194 char *nm_str = (char*) nm.to_str0 ();
195 if (int idx = FT_Get_Name_Index (face_, nm_str))
201 Open_type_font::index_to_charcode (int i) const
203 return ((Open_type_font*) this)->index_to_charcode_map_[i];
208 Open_type_font::glyph_name_to_index (String glyph_name) const
210 return ((Open_type_font*) this)->glyph_name_to_charcode_map_[glyph_name];
215 Open_type_font::count () const
217 return ((Open_type_font*) this)->index_to_charcode_map_.size ();
221 Open_type_font::design_size () const
223 SCM entry = scm_hashq_ref (lily_global_table_,
224 ly_symbol2scm ("staffsize"),
226 return scm_to_double (entry);
231 Open_type_font::sub_fonts () const
233 return lily_subfonts_;
236 LY_DEFINE (ly_font_sub_fonts, "ly:font-sub-fonts", 1, 0, 0,
238 "Given the font metric @var{font} of an OpenType font, return the "
239 "names of the subfonts within @var{font}.")
241 Font_metric *fm = unsmob_metrics (font);
242 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
243 return fm->sub_fonts ();
246 LY_DEFINE (ly_otf_font_glyph_info, "ly:otf-font-glyph-info", 2, 0, 0,
247 (SCM font, SCM glyph),
248 "Given the font metric @var{font} of an OpenType font, return the "
249 "information about named glyph @var{glyph} (a string)")
251 Modified_font_metric * fm
252 = dynamic_cast<Modified_font_metric*> (unsmob_metrics (font));
253 Open_type_font * otf = dynamic_cast<Open_type_font*> (fm->original_font ());
254 SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OTF font-metric");
255 SCM_ASSERT_TYPE (scm_is_string (glyph), glyph, SCM_ARG1,
256 __FUNCTION__, "string");
258 SCM sym = scm_string_to_symbol (glyph);
259 return scm_hashq_ref (otf->get_char_table (), sym, SCM_EOL);
263 Open_type_font::get_char_table () const
265 return lily_character_table_;
269 Open_type_font::get_subfonts () const
271 return lily_subfonts_;
275 Open_type_font::get_global_table () const
277 return lily_global_table_;