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