]> git.donarmstrong.com Git - lilypond.git/blob - lily/afm.cc
95f138090abc29bace0eda1fa066ff1ab8f0ae75
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "afm.hh"
10
11 #include <cstring>
12 using namespace std;
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 = max (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   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 e = AFM_parseFile (f, &fi, ~0);
141
142   if (e)
143     error (_f ("parsing AFM file: `%s'", nm.to_str0 ()));
144   fclose (f);
145
146   return Adobe_font_metric::make_afm (fi, cs, ds);
147 }
148
149 Box
150 afm_bbox_to_box (AFM_BBox bb)
151 {
152 #if 1 /* Fine for feta; ec-fonts-mftraced do not have AFM.  */
153   return Box (Interval (bb.llx, bb.urx) * 0.001 PT,
154               Interval (bb.lly, bb.ury) * 0.001 PT);
155 #else /* FIXME: about right for lmodern.  */
156   return Box (Interval (bb.llx, bb.urx) * 1.14 * 0.001 PT,
157               Interval (bb.lly, bb.ury) * 1.14 * 0.001 PT);
158 #endif
159 }
160
161 Offset
162 Adobe_font_metric::get_indexed_wxwy (int k) const
163 {
164   AFM_CharMetricInfo const *mi = font_info_->cmi + k;
165 #if 1 /* Fine for feta; ec-fonts-mftraced do not have AFM.  */
166   return Offset (mi->wx, mi->wy) * 0.001 PT;
167 #else /* FIXME: about right for lmodern.  */
168   return Offset (mi->wx, mi->wy) * 1.14 * 0.001 PT;
169 #endif
170 }
171
172 Real
173 Adobe_font_metric::design_size () const
174 {
175   return design_size_;
176 }
177
178 String
179 Adobe_font_metric::font_name () const
180 {
181   return font_info_->gfi->fontName;
182 }