2 afm.cc -- implement Adobe_font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
15 #include "libc-extension.hh"
16 #include "dimensions.hh"
18 Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi)
24 for (int i = max (256, fi->numOfChars); i--;)
25 ascii_to_metric_idx_.push (-1);
27 for (int i = 0; i < fi->numOfChars; i++)
29 AFM_CharMetricInfo *c = fi->cmi + i;
31 /* Some TeX afm files contain code = -1. We don't know why,
34 ascii_to_metric_idx_[c->code] = i;
35 name_to_metric_dict_[c->name] = i;
39 Adobe_font_metric::~Adobe_font_metric ()
41 AFM_free (font_info_);
45 Adobe_font_metric::make_afm (AFM_Font_info *fi,
46 unsigned int checksum,
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 ();
55 AFM_CharMetricInfo const *
56 Adobe_font_metric::find_ascii_metric (int a) const
58 if (ascii_to_metric_idx_[a] >= 0)
60 int code = ascii_to_metric_idx_[a];
62 return font_info_->cmi + code;
67 AFM_CharMetricInfo const *
68 Adobe_font_metric::find_char_metric (String nm) const
70 int idx = name_to_index (nm);
72 return font_info_->cmi + idx;
77 Adobe_font_metric::name_to_index (String name) const
79 map<String, int>::const_iterator ai = name_to_metric_dict_.find (name);
80 if (ai == name_to_metric_dict_.end ())
86 Adobe_font_metric::count () const
88 return font_info_->numOfChars;
92 Adobe_font_metric::get_ascii_char (int code) const
94 AFM_CharMetricInfo const *c = find_ascii_metric (code);
95 Box b (Interval (0, 0), Interval (0, 0));
97 b = afm_bbox_to_box (c->charBBox);
102 Adobe_font_metric::index_to_ascii (int code) const
104 return font_info_->cmi[code].code;
108 Adobe_font_metric::get_indexed_char (int code) const
111 return afm_bbox_to_box (font_info_->cmi[code].charBBox);
113 return Box (Interval (0, 0), Interval (0, 0));
117 read_afm_file (String nm)
119 FILE *f = fopen (nm.to_str0 (), "r");
121 char *check_key = "Comment TfmCheckSum";
122 char *size_key = "Comment DesignSize";
127 /* Assume check_key in first 10 lines */
128 for (int i = 0; i < 10; i++)
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);
140 int e = AFM_parseFile (f, &fi, ~0);
143 error (_f ("parsing AFM file: `%s'", nm.to_str0 ()));
146 return Adobe_font_metric::make_afm (fi, cs, ds);
150 afm_bbox_to_box (AFM_BBox bb)
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);
162 Adobe_font_metric::get_indexed_wxwy (int k) const
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;
173 Adobe_font_metric::design_size () const
179 Adobe_font_metric::font_name () const
181 return font_info_->gfi->fontName;