]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font.cc
* scm/font.scm (add-music-fonts): Load aybabtu iso all separate
[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       tab = scm_c_eval_string (contents.to_str0 ());
59       free (buffer);
60     }
61   return tab;
62 }
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   for (charcode = FT_Get_First_Char (face, &gindex); gindex != 0;
73        charcode = FT_Get_Next_Char (face, charcode, &gindex))
74     m[gindex] = charcode;
75   return m;
76 }
77
78 #if 0
79 Glyph_name_to_charcode_map
80 make_glyph_name_to_charcode_map (FT_Face face)
81 {
82   Glyph_name_to_charcode_map m;
83   FT_ULong charcode;
84   FT_UInt gindex;
85   char buffer[1024];
86
87   for (charcode = FT_Get_First_Char (face, &gindex); gindex != 0;
88        charcode = FT_Get_Next_Char (face, charcode, &gindex))
89     {
90       if (FT_Get_Glyph_Name (face, gindex, buffer, sizeof (buffer) - 1))
91         {
92           programming_error ("no glyph name");
93           continue;
94         }
95       m[String (buffer)] = charcode;
96     }
97   return m;
98 }
99 #endif
100
101 Open_type_font::~Open_type_font()
102 {
103   FT_Done_Face (face_);
104 }
105
106 SCM
107 Open_type_font::make_otf (String str)
108 {
109   FT_Face face;
110   int error_code = FT_New_Face(freetype2_library, str.to_str0 (), 0, &face);
111
112   if (error_code == FT_Err_Unknown_File_Format)
113     {
114       error (_f ("Unsupported font format: %s", str.to_str0 ()));
115     }
116   else if (error_code)
117     {
118       error (_f ("Unknown error: %d reading font file: %s", error_code,
119                  str.to_str0 ()));
120     }
121
122   Open_type_font *otf = new Open_type_font (face);
123
124   return otf->self_scm ();
125 }
126
127
128 Open_type_font::Open_type_font (FT_Face face)
129 {
130   face_ = face;
131   lily_character_table_ = SCM_EOL;
132   lily_global_table_ = SCM_EOL;
133   lily_subfonts_ = SCM_EOL;
134   
135   lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
136   lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
137   lily_subfonts_ = load_scheme_table ("LILF", face_);
138   index_to_charcode_map_ = make_index_to_charcode_map (face_);
139   
140   //glyph_name_to_charcode_map_ = make_glyph_name_to_charcode_map (face_);
141 }
142
143 void
144 Open_type_font::derived_mark () const
145 {
146   scm_gc_mark (lily_character_table_);
147   scm_gc_mark (lily_global_table_);
148   scm_gc_mark (lily_subfonts_);
149 }
150
151 Offset
152 Open_type_font::attachment_point (String glyph_name) const
153 {
154   SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
155   SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
156
157   Offset o;
158   if (entry == SCM_BOOL_F)
159     return o;
160
161   SCM char_alist = entry;
162
163   
164   SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
165   
166   return point_constant * ly_scm2offset (att_scm);
167 }
168
169 Box
170 Open_type_font::get_indexed_char (int signed_idx) const
171 {
172   FT_UInt idx = signed_idx;
173   FT_Load_Glyph (face_,
174                  idx,
175                  FT_LOAD_NO_SCALE);
176
177   FT_Glyph_Metrics m = face_->glyph->metrics;
178   int hb = m.horiBearingX;
179   int vb = m.horiBearingY;
180   Box b (Interval (-hb, m.width - hb),
181          Interval (-vb, m.height - vb));
182
183   b.scale (design_size () * Real (point_constant) / face_->units_per_EM);
184   return b;
185 }
186
187 int
188 Open_type_font::name_to_index (String nm) const
189 {
190   char *nm_str = (char*) nm.to_str0 ();
191   if (int idx = FT_Get_Name_Index (face_, nm_str))
192     return idx;
193   return -1;
194 }
195
196 unsigned
197 Open_type_font::index_to_charcode (int i) const
198 {
199   return ((Open_type_font*) this)->index_to_charcode_map_[i];
200 }
201
202 #if 0
203 unsigned
204 Open_type_font::glyph_name_to_charcode (String glyph_name) const
205 {
206   return ((Open_type_font*) this)->glyph_name_to_charcode_map_[glyph_name];
207 }
208 #endif
209
210 Real
211 Open_type_font::design_size () const
212 {
213   SCM entry = scm_hashq_ref (lily_global_table_,
214                              ly_symbol2scm ("staffsize"),
215                              scm_from_int (12));
216   return scm_to_double (entry);
217 }
218
219
220 SCM
221 Open_type_font::sub_fonts () const
222 {
223   return lily_subfonts_;
224 }
225
226 LY_DEFINE (ly_font_sub_fonts, "ly:font-sub-fonts", 1, 0, 0,
227           (SCM font),
228            "Given the font metric @var{font} of an OpenType font, return the "
229            "names of the subfonts within @var{font}.")
230 {
231   Font_metric *fm = unsmob_metrics (font);
232   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
233   return fm->sub_fonts ();
234 }
235