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"
16 #include "dimensions.hh"
18 Adobe_font_metric::Adobe_font_metric (AFM_Font_info *fi)
24 for (int i = 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 std::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 ok = AFM_parseFile (f, &fi, ~1);
144 error (_f ("Error parsing AFM file: `%s'", nm.to_str0 ()));
149 return Adobe_font_metric::make_afm (fi, cs, ds);
153 /* Actually, AFMs will be printers point, usually, but our .py script dumps
156 afm_bbox_to_box (AFM_BBox bb)
158 return Box (Interval (bb.llx, bb.urx)* (1/1000.0) PT,
159 Interval (bb.lly, bb.ury)* (1/1000.0) PT);
164 Adobe_font_metric::get_indexed_wxwy (int k) const
166 AFM_CharMetricInfo const *mi = font_info_->cmi+ k;
167 return 1/1000.0 PT * Offset (mi->wx, mi->wy);
171 Adobe_font_metric::design_size () const
177 Adobe_font_metric::coding_scheme () const
179 return font_info_->gfi->encodingScheme;