]> git.donarmstrong.com Git - lilypond.git/blob - lily/afm.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[lilypond.git] / lily / afm.cc
1 /*   
2   afm.cc --  implement Adobe_font_metric
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "afm.hh"
11
12 #include <cstring>
13
14 #include "warn.hh"
15 #include "libc-extension.hh"
16 #include "dimensions.hh"
17
18 Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi)
19 {
20   checksum_ = 0;
21   font_info_ = fi;
22   design_size_ = 1.0;
23   
24   for (int i = 256 >? fi->numOfChars; i--;)
25     ascii_to_metric_idx_.push (-1);
26   
27   for (int i=0; i < fi->numOfChars; i++)
28     {
29       AFM_CharMetricInfo * c = fi->cmi + i;
30
31       /* Some TeX afm files contain code = -1.  We don't know why,
32         let's ignore it.  */
33       if (c->code >= 0)
34         ascii_to_metric_idx_[c->code] = i;
35       name_to_metric_dict_[c->name] = i;
36     }
37 }
38
39 Adobe_font_metric::~Adobe_font_metric ()
40 {
41   AFM_free (font_info_);
42 }
43
44 SCM
45 Adobe_font_metric::make_afm (AFM_Font_info *fi,
46                              unsigned int checksum,
47                              Real design_size)
48 {
49   Adobe_font_metric *fm = new Adobe_font_metric (fi);
50   fm->checksum_ = checksum;
51   fm->design_size_ = design_size;
52   return fm->self_scm ();    
53 }
54
55 AFM_CharMetricInfo const*
56 Adobe_font_metric::find_ascii_metric (int a) const
57 {
58   if (ascii_to_metric_idx_[a] >=0)
59     {
60       int code = ascii_to_metric_idx_[a];
61       if (code >= 0)
62         return font_info_->cmi + code;
63     }
64   return 0;
65 }
66
67 AFM_CharMetricInfo const*
68 Adobe_font_metric::find_char_metric (String nm) const
69 {
70   int idx = name_to_index (nm);
71   if (idx >= 0)
72     return font_info_->cmi + idx;
73   return 0;
74 }
75
76 int
77 Adobe_font_metric::name_to_index (String name) const
78 {
79   std::map<String,int>::const_iterator ai = name_to_metric_dict_.find (name);
80   if (ai == name_to_metric_dict_.end ())
81     return -1;
82   return (*ai).second; 
83 }
84
85 int
86 Adobe_font_metric::count () const
87 {
88   return font_info_->numOfChars;
89 }
90
91 Box
92 Adobe_font_metric::get_ascii_char (int code) const
93 {
94   AFM_CharMetricInfo const *c = find_ascii_metric (code);
95   Box b (Interval (0, 0), Interval (0, 0));
96   if (c)
97     b = afm_bbox_to_box (c->charBBox);          
98   return b;
99 }
100
101 int
102 Adobe_font_metric::index_to_ascii (int code) const
103 {
104   return font_info_->cmi[code].code;
105 }
106
107 Box
108 Adobe_font_metric::get_indexed_char (int code) const
109 {
110   if (code >= 0)
111     return afm_bbox_to_box (font_info_->cmi[code].charBBox);
112   else
113     return Box (Interval (0, 0), Interval (0, 0));
114 }
115
116 SCM
117 read_afm_file (String nm)
118 {
119   FILE *f = fopen (nm.to_str0 () , "r");
120   char s[2048] = "";
121   char *check_key = "Comment TfmCheckSum";
122   char *size_key = "Comment DesignSize";
123
124   unsigned int cs = 0;
125   Real ds = 1.0;
126   
127   /* Assume check_key in first 10 lines */
128   for (int i = 0; i < 10; i++)
129     {
130       fgets (s, sizeof (s), f);
131       if (strncmp (s, check_key, strlen (check_key)) == 0)
132         sscanf (s + strlen (check_key), "%ud", &cs);
133       else if (strncmp (s, size_key, strlen (size_key)) == 0)
134         sscanf (s + strlen (size_key), "%lf", &ds);
135     }
136   
137   rewind (f);
138     
139   AFM_Font_info *fi;
140   int ok = AFM_parseFile (f, &fi, ~0);
141
142   if (ok)
143     {
144       error (_f ("Error parsing AFM file: `%s'", nm.to_str0 ()));
145       exit (2);
146     }
147   fclose (f);
148
149   return Adobe_font_metric::make_afm (fi, cs, ds);
150 }
151
152
153 /* Actually, AFMs will be printers point, usually, but our .py script dumps
154   real points.  */
155 Box
156 afm_bbox_to_box (AFM_BBox bb)
157 {
158   return Box (Interval (bb.llx, bb.urx)* (1/1000.0) PT,
159               Interval (bb.lly, bb.ury)* (1/1000.0) PT);
160
161 }
162
163 Offset
164 Adobe_font_metric::get_indexed_wxwy (int k) const
165 {
166   AFM_CharMetricInfo const *mi = font_info_->cmi+ k;
167   return 1/1000.0 PT * Offset (mi->wx, mi->wy); 
168 }
169
170 Real
171 Adobe_font_metric::design_size () const
172 {
173   return design_size_;    
174 }
175
176 String
177 Adobe_font_metric::coding_scheme () const
178 {
179   return font_info_->gfi->encodingScheme;
180 }