]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font.cc
* lily/include/pango-font.hh: new file.
[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 #include "modified-font-metric.hh"
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 Open_type_font::~Open_type_font()
79 {
80   FT_Done_Face (face_);
81 }
82
83 SCM
84 Open_type_font::make_otf (String str)
85 {
86   FT_Face face;
87   int error_code = FT_New_Face(freetype2_library, str.to_str0 (), 0, &face);
88
89   if (error_code == FT_Err_Unknown_File_Format)
90     {
91       error (_f ("Unsupported font format: %s", str.to_str0 ()));
92     }
93   else if (error_code)
94     {
95       error (_f ("Unknown error: %d reading font file: %s", error_code,
96                  str.to_str0 ()));
97     }
98
99   Open_type_font *otf = new Open_type_font (face);
100
101   return otf->self_scm ();
102 }
103
104 Open_type_font::Open_type_font (FT_Face face)
105 {
106   face_ = face;
107   lily_character_table_ = SCM_EOL;
108   lily_global_table_ = SCM_EOL;
109   lily_subfonts_ = SCM_EOL;
110   
111   lily_character_table_ = alist_to_hashq (load_scheme_table ("LILC", face_));
112   lily_global_table_ = alist_to_hashq (load_scheme_table ("LILY", face_));
113   lily_subfonts_ = load_scheme_table ("LILF", face_);
114   index_to_charcode_map_ = make_index_to_charcode_map (face_);
115 }
116
117 void
118 Open_type_font::derived_mark () const
119 {
120   scm_gc_mark (lily_character_table_);
121   scm_gc_mark (lily_global_table_);
122   scm_gc_mark (lily_subfonts_);
123 }
124
125 Offset
126 Open_type_font::attachment_point (String glyph_name) const
127 {
128   SCM sym = ly_symbol2scm (glyph_name.to_str0 ());
129   SCM entry = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
130
131   Offset o;
132   if (entry == SCM_BOOL_F)
133     return o;
134
135   SCM char_alist = entry;
136   SCM att_scm = scm_cdr (scm_assq (ly_symbol2scm ("attachment"), char_alist));
137   
138   return point_constant * ly_scm2offset (att_scm);
139 }
140
141 Box
142 Open_type_font::get_indexed_char (int signed_idx) const
143 {
144   if (SCM_HASHTABLE_P (lily_character_table_))
145     {
146       const int len =256;
147       char name[len];
148       int code = FT_Get_Glyph_Name (face_, signed_idx, name, len);
149       if (code)
150         warning (_f ("FT_Get_Glyph_Name() returned error: %d", code));
151         
152       SCM sym = ly_symbol2scm (name);
153       SCM alist = scm_hashq_ref (lily_character_table_, sym, SCM_BOOL_F);
154       
155       if (alist != SCM_BOOL_F)
156         {
157           SCM bbox = scm_cdr (scm_assq (ly_symbol2scm ("bbox"), alist));
158       
159           Box b;
160           b[X_AXIS][LEFT] = scm_to_double (scm_car (bbox));
161           bbox = scm_cdr (bbox);
162           b[Y_AXIS][LEFT] = scm_to_double (scm_car (bbox));
163           bbox = scm_cdr (bbox);
164           b[X_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
165           bbox = scm_cdr (bbox);
166           b[Y_AXIS][RIGHT] = scm_to_double (scm_car (bbox));
167           bbox = scm_cdr (bbox);
168
169           b.scale (point_constant);
170           return b;
171         }
172     }
173   
174   FT_UInt idx = signed_idx;
175   FT_Load_Glyph (face_,
176                  idx,
177                  FT_LOAD_NO_SCALE);
178
179   FT_Glyph_Metrics m = face_->glyph->metrics;
180   int hb = m.horiBearingX;
181   int vb = m.horiBearingY;
182   Box b (Interval (-hb, m.width - hb),
183          Interval (-vb, m.height - vb));
184
185   b.scale (design_size () * Real (point_constant) / face_->units_per_EM);
186   return b;
187 }
188
189 int
190 Open_type_font::name_to_index (String nm) const
191 {
192   char *nm_str = (char*) nm.to_str0 ();
193   if (int idx = FT_Get_Name_Index (face_, nm_str))
194     return idx;
195   return -1;
196 }
197
198 unsigned
199 Open_type_font::index_to_charcode (int i) const
200 {
201   return ((Open_type_font*) this)->index_to_charcode_map_[i];
202 }
203
204 int
205 Open_type_font::count () const
206 {
207   return ((Open_type_font*) this)->index_to_charcode_map_.size ();
208 }
209
210 Real
211 Open_type_font::design_size () const
212 {
213   SCM entry = scm_hashq_ref (lily_global_table_,
214                              ly_symbol2scm ("design_size"),
215
216                              /*
217                                Hmm. Design size is arbitrary for
218                                non-design-size fonts. I vote for 1 -
219                                which will trip errors more
220                                quickly. --hwn.
221                               */
222                              scm_from_int (1));
223   return scm_to_double (entry);
224 }
225
226
227 SCM
228 Open_type_font::sub_fonts () const
229 {
230   return lily_subfonts_;
231 }
232
233 LY_DEFINE (ly_font_sub_fonts, "ly:font-sub-fonts", 1, 0, 0,
234           (SCM font),
235            "Given the font metric @var{font} of an OpenType font, return the "
236            "names of the subfonts within @var{font}.")
237 {
238   Font_metric *fm = unsmob_metrics (font);
239   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
240   return fm->sub_fonts ();
241 }
242
243 LY_DEFINE (ly_otf_font_glyph_info, "ly:otf-font-glyph-info", 2, 0, 0,
244           (SCM font, SCM glyph),
245            "Given the font metric @var{font} of an OpenType font, return the "
246            "information about named glyph @var{glyph} (a string)")
247 {
248   Modified_font_metric * fm
249     = dynamic_cast<Modified_font_metric*> (unsmob_metrics (font));
250   Open_type_font * otf = dynamic_cast<Open_type_font*> (fm->original_font ());
251   SCM_ASSERT_TYPE (otf, font, SCM_ARG1, __FUNCTION__, "OTF font-metric");
252   SCM_ASSERT_TYPE (scm_is_string (glyph), glyph, SCM_ARG1,
253                    __FUNCTION__, "string");
254
255   SCM sym = scm_string_to_symbol (glyph);
256   return scm_hashq_ref (otf->get_char_table (), sym, SCM_EOL);
257 }
258
259 SCM
260 Open_type_font::get_char_table () const
261 {
262   return lily_character_table_;
263 }
264
265 SCM
266 Open_type_font::get_subfonts () const
267 {
268   return lily_subfonts_;
269 }
270
271 SCM
272 Open_type_font::get_global_table () const
273 {
274   return lily_global_table_;
275 }
276
277 String
278 Open_type_font::font_name () const
279 {
280   return FT_Get_Postscript_Name (face_);
281 }
282