]> git.donarmstrong.com Git - lilypond.git/blob - lily/open-type-font.cc
(get_indexed_char): scale metrics by
[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 <stdio.h>
11
12 #include "warn.hh"
13 #include "open-type-font.hh"
14 #include "dimensions.hh"
15
16 SCM
17 Open_type_font::make_otf (String str)
18 {
19   Open_type_font * otf = new Open_type_font;
20   int error_code = FT_New_Face(freetype2_library, str.to_str0(),
21                                0, &(otf->face_));
22   
23   if (error_code == FT_Err_Unknown_File_Format)
24     {
25       error("Unsupported font format");
26     }
27   else if (error_code)
28     {
29       error ("Unknown error reading font file.");
30     }
31
32   return otf->self_scm ();
33 }
34
35 Open_type_font::~Open_type_font()
36 {
37   FT_Done_Face (face_);
38 }
39
40
41 Box
42 Open_type_font::get_indexed_char (int signed_idx) const
43 {
44   FT_UInt idx = signed_idx;
45   FT_Load_Glyph (face_,
46                  idx,
47                  FT_LOAD_NO_SCALE);
48
49   FT_Glyph_Metrics m = face_->glyph->metrics;
50   int hb = m.horiBearingX;
51   int vb = m.horiBearingY;
52   Box b (Interval (-hb, m.width - hb),
53          Interval (-vb, m.height - vb));
54
55   Real point_constant = 1 PT;
56   
57
58   b.scale (design_size() * Real (point_constant) / face_->units_per_EM);
59   return b;
60 }
61
62 int
63 Open_type_font::name_to_index (String nm) const
64 {
65   char * nm_str = (char * )nm.to_str0 ();
66   int idx = FT_Get_Name_Index (face_, nm_str);
67
68   if (idx == 0)
69     return -1;
70   else
71     return idx;
72 }
73
74
75 Real
76 Open_type_font::design_size () const
77 {
78   return 20.0;
79 }