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>
12 #include "warn.hh" // error ()
13 #include "libc-extension.hh"
16 #include "dimensions.hh"
18 Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi)
25 for (int i= 256 >? fi->numOfChars; i--;)
26 ascii_to_metric_idx_.push (-1);
28 for (int i=0; i < fi->numOfChars; i++)
30 AFM_CharMetricInfo * c = fi->cmi + i;
33 Some TeX afm files contain code = -1. We don't know why, let's
38 ascii_to_metric_idx_[c->code] = i;
39 name_to_metric_dict_[c->name] = i;
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 ();
56 AFM_CharMetricInfo const *
57 Adobe_font_metric::find_ascii_metric (int a) const
59 if (ascii_to_metric_idx_[a] >=0)
61 int code = ascii_to_metric_idx_[a];
64 return font_inf_->cmi + code;
73 AFM_CharMetricInfo const *
74 Adobe_font_metric::find_char_metric (String nm) const
76 int idx = name_to_index (nm);
78 return font_inf_->cmi+ idx;
84 Adobe_font_metric::name_to_index (String nm)const
86 std::map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
88 if (ai == name_to_metric_dict_.end ())
95 Adobe_font_metric::count () const
97 return font_inf_->numOfChars ;
101 Adobe_font_metric::get_ascii_char (int code) const
103 AFM_CharMetricInfo const
104 * c = find_ascii_metric (code);
105 Box b (Interval (0,0),Interval (0,0));
107 b = afm_bbox_to_box (c->charBBox);
114 Adobe_font_metric::get_indexed_char (int code) const
117 return afm_bbox_to_box (font_inf_->cmi[code].charBBox);
119 return Box (Interval (0,0),Interval (0,0));
123 read_afm_file (String nm)
125 FILE *f = fopen (nm.to_str0 () , "r");
127 char *check_key = "Comment TfmCheckSum";
128 char *size_key = "Comment DesignSize";
133 /* Assume check_key in first 10 lines */
134 for (int i = 0; i < 10; i++)
136 fgets (s, sizeof (s), f);
137 if (strncmp (s, check_key, strlen (check_key)) == 0)
139 sscanf (s + strlen (check_key), "%ud", &cs);
141 else if (strncmp (s, size_key, strlen (size_key)) == 0)
143 sscanf (s + strlen (size_key), "%lf", &ds);
150 int ok = AFM_parseFile (f, &fi, ~1);
154 error (_f ("Error parsing AFM file: `%s'", nm.to_str0 ()));
159 return Adobe_font_metric::make_afm (fi, cs, ds);
164 actually, AFMs will be printers point, usually, but our .py script dumps
168 afm_bbox_to_box (AFM_BBox bb)
170 return Box (Interval (bb.llx, bb.urx)* (1/1000.0) PT,
171 Interval (bb.lly, bb.ury)* (1/1000.0) PT);
176 Adobe_font_metric::get_indexed_wxwy (int k)const
178 AFM_CharMetricInfo const *mi = font_inf_->cmi+ k;
179 return 1/1000.0 PT * Offset (mi->wx, mi->wy);
184 Adobe_font_metric::~Adobe_font_metric ()
186 AFM_free (font_inf_);
190 return a stencil, without fontification
193 Adobe_font_metric::find_by_name (String s) const
195 AFM_CharMetricInfo const *cm = find_char_metric (s);
200 Why don't we return empty?
208 SCM at = (scm_list_n (ly_symbol2scm ("char"),
209 scm_int2num (cm->code),
212 // at= fontify_atom ((Font_metric*)this, at);
213 Box b = afm_bbox_to_box (cm->charBBox);
215 return Stencil (b, at);