]> git.donarmstrong.com Git - lilypond.git/blob - lily/afm-reader.cc
release: 1.1.18
[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 Adobe_font_metric::read_char_metrics (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       Adobe_font_char_metric afm_char =read_char_metric (s);
70       char_metrics_.push (afm_char);
71       int i = char_metrics_.size ()-1;
72       ascii_to_metric_idx_ [afm_char.C_] = i;
73       name_to_metric_dict_ [afm_char.N_] = i;
74     }
75 }
76
77 #define READSTRING(k)  if (key == #k) { \
78   afm.k ## _ = input.get_line (); continue; }
79 #define READBOX(b) if (key == #b) { \
80   afm.b ## _ = read_box (input); continue; }
81 #define READREAL(r) if (key == #r) { \
82   afm.r ## _ = read_real (input); continue; }
83
84 Real
85 read_real(Data_file &d)
86 {
87   String s = d.get_word ();
88   d.gobble_white ();
89   return s.value_f ();
90 }
91
92
93 Box
94 read_box ( Data_file &d)
95 {
96   Box b;
97   b[X_AXIS][SMALLER] = read_real (d);  
98   b[Y_AXIS][SMALLER] = read_real (d);
99   b[X_AXIS][BIGGER] = read_real (d);
100   b[Y_AXIS][BIGGER] = read_real (d);
101   return b;
102 }
103
104 Adobe_font_metric
105 read_afm_file (String fn)
106 {
107   Data_file input (fn);
108
109   assert (!input.eof_b ());
110   
111   Adobe_font_metric afm;
112
113   for (int i=0; i < 256; i++)
114     {
115       afm.ascii_to_metric_idx_.push (-1);
116     }
117
118   while (!input.eof_b ())
119     {
120       input.gobble_leading_white ();
121       String w = input.get_word ();
122       if (w == "StartFontMetrics")
123         break;
124       input.get_line ();
125     }
126   
127   while (!input.eof_b ())
128     {
129       input.gobble_leading_white ();
130       String key = input.get_word ();
131       if (key == "Comment")
132         continue;
133
134       READSTRING(FontName);
135       READSTRING(FullName);
136       READSTRING(FamilyName);
137       READSTRING(Weight);
138       READSTRING(Version);
139       READSTRING(Notice);
140       READSTRING(EncodingScheme);
141       READREAL(ItalicAngle);
142       READREAL(UnderlineThickness);
143       READREAL(UnderlinePosition);
144       READBOX(FontBBox);
145       if (key == "StartCharMetrics")
146         {
147           input.get_line ();
148           afm.read_char_metrics (input);
149         }
150       if (key == "EndFontMetrics")
151         break;
152
153     }
154
155   /*
156     read to EOF
157   */
158   input.gulp ();
159  
160   return afm;
161 }
162
163