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>
12 #include <freetype/tttables.h>
15 #include "open-type-font.hh"
16 #include "dimensions.hh"
19 load_table (char const *tag_str, FT_Face face)
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));
45 Open_type_font::make_otf (String str)
47 Open_type_font * otf = new Open_type_font;
48 int error_code = FT_New_Face(freetype2_library, str.to_str0(),
51 if (error_code == FT_Err_Unknown_File_Format)
53 error("Unsupported font format");
57 error ("Unknown error reading font file.");
61 FT_Byte* buffer =load_table ("LILC", otf->face_);
64 String contents ((char const*)buffer);
65 contents = "(quote " + contents + ")";
67 SCM alist = scm_c_eval_string (contents.to_str0());
68 otf->lily_character_table_ = alist_to_hashq (alist);
72 return otf->self_scm ();
75 Open_type_font::Open_type_font()
77 lily_character_table_ = SCM_EOL;
81 Open_type_font::derived_mark () const
83 scm_gc_mark (lily_character_table_);
87 Open_type_font::attachment_point (String glyph_name) const
89 SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
90 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
93 if (entry == SCM_BOOL_F)
96 SCM char_alist = entry;
98 SCM att_scm =scm_cdr (scm_assq (char_alist, ly_symbol2scm ("attachment")));
100 return ly_scm2offset (att_scm);
104 Open_type_font::~Open_type_font()
106 FT_Done_Face (face_);
111 Open_type_font::get_indexed_char (int signed_idx) const
113 FT_UInt idx = signed_idx;
114 FT_Load_Glyph (face_,
118 FT_Glyph_Metrics m = face_->glyph->metrics;
119 int hb = m.horiBearingX;
120 int vb = m.horiBearingY;
121 Box b (Interval (-hb, m.width - hb),
122 Interval (-vb, m.height - vb));
124 Real point_constant = 1 PT;
127 b.scale (design_size() * Real (point_constant) / face_->units_per_EM);
132 Open_type_font::name_to_index (String nm) const
134 char * nm_str = (char * )nm.to_str0 ();
135 int idx = FT_Get_Name_Index (face_, nm_str);
145 Open_type_font::design_size () const