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"
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 ("Not enough memory");
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 SCM alist = scm_c_eval_string (contents.to_str0());
59 tab = alist_to_hashq (alist);
66 make_index_to_charcode_map (FT_Face face)
68 Index_to_charcode_map m;
72 charcode = FT_Get_First_Char( face, &gindex );
76 charcode = FT_Get_Next_Char( face, charcode, &gindex );
82 Open_type_font::make_otf (String str)
85 int error_code = FT_New_Face(freetype2_library, str.to_str0(),
88 if (error_code == FT_Err_Unknown_File_Format)
90 error("Unsupported font format");
94 error ("Unknown error reading font file.");
98 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;
110 lily_character_table_ = load_scheme_table ("LILC", face_);
111 lily_global_table_ = load_scheme_table ("LILY", face_);
112 index_to_charcode_map_ = make_index_to_charcode_map (face_);
116 Open_type_font::derived_mark () const
118 scm_gc_mark (lily_character_table_);
119 scm_gc_mark (lily_global_table_);
123 Open_type_font::attachment_point (String glyph_name) const
125 SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
126 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
129 if (entry == SCM_BOOL_F)
132 SCM char_alist = entry;
134 SCM att_scm =scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
136 return ly_scm2offset (att_scm);
140 Open_type_font::~Open_type_font()
142 FT_Done_Face (face_);
147 Open_type_font::get_indexed_char (int signed_idx) const
149 FT_UInt idx = signed_idx;
150 FT_Load_Glyph (face_,
154 FT_Glyph_Metrics m = face_->glyph->metrics;
155 int hb = m.horiBearingX;
156 int vb = m.horiBearingY;
157 Box b (Interval (-hb, m.width - hb),
158 Interval (-vb, m.height - vb));
162 b.scale (design_size() * Real (point_constant) / face_->units_per_EM);
167 Open_type_font::name_to_index (String nm) const
169 char * nm_str = (char * )nm.to_str0 ();
170 int idx = FT_Get_Name_Index (face_, nm_str);
180 Open_type_font::design_size () const
182 return point_constant * scm_to_double (scm_hashq_ref (lily_global_table_, ly_symbol2scm ("staffsize"), SCM_BOOL_F));