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, FT_ULong *length)
21 FT_ULong tag = FT_MAKE_TAG(tag_str[0], tag_str[1], tag_str[2], tag_str[3]);
23 int error_code = FT_Load_Sfnt_Table (face, tag, 0, NULL, length);
26 FT_Byte*buffer = (FT_Byte*) malloc (*length);
28 error ("Not enough memory");
30 error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length );
33 error (_f ("Could not load %s font table", tag_str));
43 load_scheme_table (char const *tag_str, FT_Face face)
46 FT_Byte* buffer =load_table ("LILC", face, &length);
51 String contents ((Byte const*)buffer, length);
52 contents = "(quote (" + contents + "))";
54 SCM alist = scm_c_eval_string (contents.to_str0());
55 tab = alist_to_hashq (alist);
62 Open_type_font::make_otf (String str)
64 Open_type_font * otf = new Open_type_font;
65 int error_code = FT_New_Face(freetype2_library, str.to_str0(),
68 if (error_code == FT_Err_Unknown_File_Format)
70 error("Unsupported font format");
74 error ("Unknown error reading font file.");
79 otf->lily_character_table_ = load_scheme_table ("LILC", otf->face_);
80 otf->lily_global_table_ = load_scheme_table ("LILY", otf->face_);
82 return otf->self_scm ();
85 Open_type_font::Open_type_font()
87 lily_character_table_ = SCM_EOL;
88 lily_global_table_ = SCM_EOL;
92 Open_type_font::derived_mark () const
94 scm_gc_mark (lily_character_table_);
95 scm_gc_mark (lily_global_table_);
99 Open_type_font::attachment_point (String glyph_name) const
101 SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
102 SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
105 if (entry == SCM_BOOL_F)
108 SCM char_alist = entry;
110 SCM att_scm =scm_cdr (scm_assq (char_alist, ly_symbol2scm ("attachment")));
112 return ly_scm2offset (att_scm);
116 Open_type_font::~Open_type_font()
118 FT_Done_Face (face_);
123 Open_type_font::get_indexed_char (int signed_idx) const
125 FT_UInt idx = signed_idx;
126 FT_Load_Glyph (face_,
130 FT_Glyph_Metrics m = face_->glyph->metrics;
131 int hb = m.horiBearingX;
132 int vb = m.horiBearingY;
133 Box b (Interval (-hb, m.width - hb),
134 Interval (-vb, m.height - vb));
136 Real point_constant = 1 PT;
139 b.scale (design_size() * Real (point_constant) / face_->units_per_EM);
144 Open_type_font::name_to_index (String nm) const
146 char * nm_str = (char * )nm.to_str0 ();
147 int idx = FT_Get_Name_Index (face_, nm_str);
157 Open_type_font::design_size () const