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>
14 #include "libc-extension.hh"
15 #include "dimensions.hh"
17 Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi)
23 for (int i = max (256, fi->numOfChars); i--;)
24 ascii_to_metric_idx_.push (-1);
26 for (int i = 0; i < fi->numOfChars; i++)
28 AFM_CharMetricInfo *c = fi->cmi + i;
30 /* Some TeX afm files contain code = -1. We don't know why,
33 ascii_to_metric_idx_[c->code] = i;
34 name_to_metric_dict_[c->name] = i;
38 Adobe_font_metric::~Adobe_font_metric ()
40 AFM_free (font_info_);
44 Adobe_font_metric::make_afm (AFM_Font_info *fi,
45 unsigned int checksum,
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 ();
54 AFM_CharMetricInfo const *
55 Adobe_font_metric::find_ascii_metric (int a) const
57 if (ascii_to_metric_idx_[a] >= 0)
59 int code = ascii_to_metric_idx_[a];
61 return font_info_->cmi + code;
66 AFM_CharMetricInfo const *
67 Adobe_font_metric::find_char_metric (String nm) const
69 int idx = name_to_index (nm);
71 return font_info_->cmi + idx;
76 Adobe_font_metric::name_to_index (String name) const
78 std::map<String, int>::const_iterator ai = name_to_metric_dict_.find (name);
79 if (ai == name_to_metric_dict_.end ())
85 Adobe_font_metric::count () const
87 return font_info_->numOfChars;
91 Adobe_font_metric::get_ascii_char (int code) const
93 AFM_CharMetricInfo const *c = find_ascii_metric (code);
94 Box b (Interval (0, 0), Interval (0, 0));
96 b = afm_bbox_to_box (c->charBBox);
101 Adobe_font_metric::index_to_ascii (int code) const
103 return font_info_->cmi[code].code;
107 Adobe_font_metric::get_indexed_char (int code) const
110 return afm_bbox_to_box (font_info_->cmi[code].charBBox);
112 return Box (Interval (0, 0), Interval (0, 0));
116 read_afm_file (String nm)
118 FILE *f = fopen (nm.to_str0 (), "r");
120 char *check_key = "Comment TfmCheckSum";
121 char *size_key = "Comment DesignSize";
126 /* Assume check_key in first 10 lines */
127 for (int i = 0; i < 10; i++)
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);
139 int e = AFM_parseFile (f, &fi, ~0);
142 error (_f ("parsing AFM file: `%s'", nm.to_str0 ()));
145 return Adobe_font_metric::make_afm (fi, cs, ds);
149 afm_bbox_to_box (AFM_BBox bb)
151 #if 1 /* Fine for feta; ec-fonts-mftraced do not have AFM. */
152 return Box (Interval (bb.llx, bb.urx) * 0.001 PT,
153 Interval (bb.lly, bb.ury) * 0.001 PT);
154 #else /* FIXME: about right for lmodern. */
155 return Box (Interval (bb.llx, bb.urx) * 1.14 * 0.001 PT,
156 Interval (bb.lly, bb.ury) * 1.14 * 0.001 PT);
161 Adobe_font_metric::get_indexed_wxwy (int k) const
163 AFM_CharMetricInfo const *mi = font_info_->cmi + k;
164 #if 1 /* Fine for feta; ec-fonts-mftraced do not have AFM. */
165 return Offset (mi->wx, mi->wy) * 0.001 PT;
166 #else /* FIXME: about right for lmodern. */
167 return Offset (mi->wx, mi->wy) * 1.14 * 0.001 PT;
172 Adobe_font_metric::design_size () const
178 Adobe_font_metric::font_name () const
180 return font_info_->gfi->fontName;