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