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 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 ("Not enough memory");
31 error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length );
34 error (_f ("Could not load %s font table", tag_str));
44 load_scheme_table (char const *tag_str, FT_Face face)
47 FT_Byte* buffer =load_table (tag_str, face, &length);
52 String contents ((Byte const*)buffer, length);
53 contents = "(quote (" + contents + "))";
55 SCM alist = scm_c_eval_string (contents.to_str0());
56 tab = alist_to_hashq (alist);
63 make_index_to_charcode_map (FT_Face face)
65 Index_to_charcode_map m;
69 charcode = FT_Get_First_Char( face, &gindex );
73 charcode = FT_Get_Next_Char( face, charcode, &gindex );
79 Open_type_font::make_otf (String str)
82 int error_code = FT_New_Face(freetype2_library, str.to_str0(),
85 if (error_code == FT_Err_Unknown_File_Format)
87 error("Unsupported font format");
91 error ("Unknown error reading font file.");
95 Open_type_font * otf = new Open_type_font (face);
98 return otf->self_scm ();
101 Open_type_font::Open_type_font(FT_Face)
103 lily_character_table_ = SCM_EOL;
104 lily_global_table_ = SCM_EOL;
106 lily_character_table_ = load_scheme_table ("LILC", face_);
107 lily_global_table_ = load_scheme_table ("LILY", 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_);
119 Open_type_font::attachment_point (String glyph_name) const
121 SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
122 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
125 if (entry == SCM_BOOL_F)
128 SCM char_alist = entry;
130 SCM att_scm =scm_cdr (scm_assq (char_alist, ly_symbol2scm ("attachment")));
132 return ly_scm2offset (att_scm);
136 Open_type_font::~Open_type_font()
138 FT_Done_Face (face_);
143 Open_type_font::get_indexed_char (int signed_idx) const
145 FT_UInt idx = signed_idx;
146 FT_Load_Glyph (face_,
150 FT_Glyph_Metrics m = face_->glyph->metrics;
151 int hb = m.horiBearingX;
152 int vb = m.horiBearingY;
153 Box b (Interval (-hb, m.width - hb),
154 Interval (-vb, m.height - vb));
156 Real point_constant = 1 PT;
159 b.scale (design_size() * Real (point_constant) / face_->units_per_EM);
164 Open_type_font::name_to_index (String nm) const
166 char * nm_str = (char * )nm.to_str0 ();
167 int idx = FT_Get_Name_Index (face_, nm_str);
177 Open_type_font::design_size () const
179 return scm_to_double (scm_hashq_ref (lily_global_table_, ly_symbol2scm ("staffsize"), SCM_BOOL_F));