]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font.cc
* lily/include/font-metric.hh (index_to_charcode): New function.
[lilypond.git] / lily / open-type-font.cc
1 /*
2   open-type-font.cc --  implement Open_type_font
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include <map>
11 #include <stdio.h>
12
13 #include <freetype/tttables.h>
14
15 #include "warn.hh"
16 #include "open-type-font.hh"
17 #include "dimensions.hh"
18
19
20 const Real point_constant = 1 PT;
21
22 FT_Byte *
23 load_table (char const *tag_str, FT_Face face, FT_ULong *length)
24 {
25   FT_ULong tag = FT_MAKE_TAG(tag_str[0], tag_str[1], tag_str[2], tag_str[3]);
26
27   int error_code = FT_Load_Sfnt_Table (face, tag, 0, NULL, length);
28   if (!error_code)
29     {
30       FT_Byte *buffer = (FT_Byte*) malloc (*length);
31       if (buffer == NULL)
32         error (_f ("Cannot allocate %d bytes", *length));
33
34       error_code = FT_Load_Sfnt_Table (face, tag, 0, buffer, length );
35       if (error_code)
36         {
37           error (_f ("Could not load %s font table", tag_str));
38         }
39
40       return buffer;
41     }
42
43   return 0;
44 }
45
46 SCM
47 load_scheme_table (char const *tag_str, FT_Face face)
48 {
49   FT_ULong length = 0;
50   FT_Byte *buffer = load_table (tag_str, face, &length);
51
52   SCM tab = SCM_EOL;
53   if (buffer)
54     {
55       String contents ((Byte const*)buffer, length);
56       contents = "(quote (" +  contents + "))";
57
58       SCM alist = scm_c_eval_string (contents.to_str0 ());
59       tab = alist_to_hashq (alist);
60       free (buffer);
61     }
62   return tab;
63 }
64         
65 Index_to_charcode_map
66 make_index_to_charcode_map (FT_Face face)
67 {
68   Index_to_charcode_map m;
69   FT_ULong charcode;
70   FT_UInt gindex;
71
72   charcode = FT_Get_First_Char (face, &gindex);
73   while (gindex != 0)
74     {
75       printf ("index -> code: %d %d \n", gindex, charcode);
76       m[gindex] = charcode;
77       charcode = FT_Get_Next_Char (face, charcode, &gindex);
78     }
79   return m;
80 }
81
82 Open_type_font::Open_type_font (FT_Face face)
83 {
84   face_ = face;
85   lily_character_table_ = SCM_EOL;
86   lily_global_table_ = SCM_EOL;
87
88   lily_character_table_ = load_scheme_table ("LILC", face_);
89   lily_global_table_ = load_scheme_table ("LILY", face_);
90   index_to_charcode_map_ = make_index_to_charcode_map (face_);
91 }
92
93 Open_type_font::~Open_type_font()
94 {
95   FT_Done_Face (face_);
96 }
97
98 SCM
99 Open_type_font::make_otf (String str)
100 {
101   FT_Face face;
102   int error_code = FT_New_Face(freetype2_library, str.to_str0 (), 0, &face);
103
104   if (error_code == FT_Err_Unknown_File_Format)
105     {
106       error (_f ("Unsupported font format: %s", str.to_str0 ()));
107     }
108   else if (error_code)
109     {
110       error (_f ("Unknown error: %d reading font file: %s", error_code,
111                  str.to_str0 ()));
112     }
113
114   Open_type_font *otf = new Open_type_font (face);
115
116   return otf->self_scm ();
117 }
118
119 void
120 Open_type_font::derived_mark () const
121 {
122   scm_gc_mark (lily_character_table_);
123   scm_gc_mark (lily_global_table_);
124 }
125
126 Offset
127 Open_type_font::attachment_point (String glyph_name) const
128 {
129   SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
130   SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
131
132   Offset o;
133   if (entry == SCM_BOOL_F)
134     return o;
135
136   SCM char_alist = entry;
137
138   SCM att_scm =scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
139
140   return ly_scm2offset (att_scm);
141 }
142
143 Box
144 Open_type_font::get_indexed_char (int signed_idx) const
145 {
146   FT_UInt idx = signed_idx;
147   FT_Load_Glyph (face_,
148                  idx,
149                  FT_LOAD_NO_SCALE);
150
151   FT_Glyph_Metrics m = face_->glyph->metrics;
152   int hb = m.horiBearingX;
153   int vb = m.horiBearingY;
154   Box b (Interval (-hb, m.width - hb),
155          Interval (-vb, m.height - vb));
156
157   b.scale (design_size () * Real (point_constant) / face_->units_per_EM);
158   return b;
159 }
160
161 int
162 Open_type_font::name_to_index (String nm) const
163 {
164   char *nm_str = (char*) nm.to_str0 ();
165   if (int idx = FT_Get_Name_Index (face_, nm_str))
166     return idx;
167   return -1;
168 }
169
170 unsigned
171 Open_type_font::index_to_charcode (int i) const
172 {
173   return ((Open_type_font*) this)->index_to_charcode_map_[index_to_ascii (i)];
174 }
175
176 Real
177 Open_type_font::design_size () const
178 {
179   return point_constant
180     * scm_to_double (scm_hashq_ref (lily_global_table_,
181                                     ly_symbol2scm ("staffsize"), SCM_BOOL_F));
182 }