2 afm.cc -- implement Adobe_font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 2000--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
13 #include "libc-extension.hh"
15 #include "dimensions.hh"
17 Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi)
23 for (int i = 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 ok = AFM_parseFile (f, &fi, ~0);
143 error (_f ("Error parsing AFM file: `%s'", nm.to_str0 ()));
148 return Adobe_font_metric::make_afm (fi, cs, ds);
152 /* Actually, AFMs will be printers point, usually, but our .py script dumps
155 afm_bbox_to_box (AFM_BBox bb)
157 return Box (Interval (bb.llx, bb.urx)* (1/1000.0) PT,
158 Interval (bb.lly, bb.ury)* (1/1000.0) PT);
163 Adobe_font_metric::get_indexed_wxwy (int k) const
165 AFM_CharMetricInfo const *mi = font_info_->cmi+ k;
166 return 1/1000.0 PT * Offset (mi->wx, mi->wy);
170 Adobe_font_metric::design_size () const
176 Adobe_font_metric::coding_scheme () const
178 return font_info_->gfi->encodingScheme;