]> git.donarmstrong.com Git - lilypond.git/blob - lily/afm-reader.cc
release: 1.0.1
[lilypond.git] / lily / afm-reader.cc
1 /*   
2   afm-reader.cc --  implement Adobe_font_metric_file
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "direction.hh"
11 #include "afm.hh"
12 #include "data-file.hh"
13 #include "string-convert.hh"
14
15
16 Box
17 parse_box (Array<String> a)
18 {
19   Box b;
20   int i=0;
21   b[X_AXIS][SMALLER] = a[i++].value_f ();  
22   b[Y_AXIS][SMALLER] = a[i++].value_f ();
23   b[X_AXIS][BIGGER] = a[i++].value_f ();
24   b[Y_AXIS][BIGGER] = a[i++].value_f ();
25   return b;
26 }
27
28 String
29 strip_leading_white (String c)
30 {
31   int i=0;
32   while (c[i] == ' ')
33     i++;
34   c = c.cut_str (i, INT_MAX);
35   return c;
36 }
37
38 Adobe_font_char_metric
39 read_char_metric (String s)
40 {
41   Adobe_font_char_metric char_metric;
42   Array<String> a= String_convert::split_arr (s, ';');
43   for (int i=0; i < a.size (); i++)
44     {
45       String c = strip_leading_white (a[i]);
46
47       Array<String> b = String_convert::split_arr (c, ' ');
48       if (b[0] == "C")
49         char_metric.C_ = b[1].value_i ();
50       if (b[0] == "WX")
51         char_metric.WX_ = b[1].value_f ();
52       if (b[0] == "N")
53         char_metric.N_ = strip_leading_white (b[1]);
54       if (b[0] == "B")
55         char_metric.B_ = parse_box (b.slice (1, b.size()));
56     }
57   return char_metric;
58 }
59
60 void
61 read_char_metrics (Array<Adobe_font_char_metric> &mets, Data_file &input)
62 {
63   while (!input.eof_b ())
64     {
65       input.gobble_leading_white ();
66       String s= input.get_line ();
67       if (s == "EndCharMetrics")
68         return ;
69       mets.push (read_char_metric (s));
70     }
71 }
72
73 #define READSTRING(k)  if (key == #k) { \
74   afm.k ## _ = input.get_line (); continue; }
75 #define READBOX(b) if (key == #b) { \
76   afm.b ## _ = read_box (input); continue; }
77 #define READREAL(r) if (key == #r) { \
78   afm.r ## _ = read_real (input); continue; }
79
80 Real
81 read_real(Data_file &d)
82 {
83   String s = d.get_word ();
84   d.gobble_white ();
85   return s.value_f ();
86 }
87
88
89 Box
90 read_box ( Data_file &d)
91 {
92   Box b;
93   b[X_AXIS][SMALLER] = read_real (d);  
94   b[Y_AXIS][SMALLER] = read_real (d);
95   b[X_AXIS][BIGGER] = read_real (d);
96   b[Y_AXIS][BIGGER] = read_real (d);
97   return b;
98 }
99
100 Adobe_font_metric
101 read_afm (String fn)
102 {
103   Data_file input (fn);
104
105   assert (!input.eof_b ());
106   
107   Adobe_font_metric afm;
108   
109   while (!input.eof_b ())
110     {
111       input.gobble_leading_white ();
112       String w = input.get_word ();
113       if (w == "StartFontMetrics")
114         break;
115       input.get_line ();
116     }
117   
118   while (!input.eof_b ())
119     {
120       input.gobble_leading_white ();
121       String key = input.get_word ();
122       if (key == "Comment")
123         continue;
124
125       READSTRING(FontName);
126       READSTRING(FullName);
127       READSTRING(FamilyName);
128       READSTRING(Weight);
129       READSTRING(Version);
130       READSTRING(Notice);
131       READSTRING(EncodingScheme);
132       READREAL(ItalicAngle);
133       READREAL(UnderlineThickness);
134       READREAL(UnderlinePosition);
135       READBOX(FontBBox);
136       if (key == "StartCharMetrics")
137         {
138           input.get_line ();
139           read_char_metrics (afm.char_metrics_, input);
140         }
141       if (key == "EndFontMetrics")
142         break;
143
144     }
145
146   /*
147     read to EOF
148   */
149   input.gulp ();
150  
151   return afm;
152 }
153
154