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)
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;
31 Some TeX afm files contain code = -1. We don't know why, let's
36 ascii_to_metric_idx_[c->code] = i;
37 name_to_metric_dict_[c->name] = i;
43 Adobe_font_metric::make_afm (AFM_Font_info *fi, unsigned int checksum)
45 Adobe_font_metric * fm = new Adobe_font_metric (fi);
46 fm->checksum_ = checksum;
47 return fm->self_scm ();
51 AFM_CharMetricInfo const *
52 Adobe_font_metric::find_ascii_metric (int a) const
54 if (ascii_to_metric_idx_[a] >=0)
56 int code = ascii_to_metric_idx_[a];
59 return font_inf_->cmi + code;
68 AFM_CharMetricInfo const *
69 Adobe_font_metric::find_char_metric (String nm) const
71 int idx = name_to_index (nm);
73 return font_inf_->cmi+ idx;
79 Adobe_font_metric::name_to_index (String nm)const
81 std::map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
83 if (ai == name_to_metric_dict_.end ())
90 Adobe_font_metric::count () const
92 return font_inf_->numOfChars ;
96 Adobe_font_metric::get_ascii_char (int code) const
98 AFM_CharMetricInfo const
99 * c = find_ascii_metric (code);
100 Box b (Interval (0,0),Interval (0,0));
102 b = afm_bbox_to_box (c->charBBox);
109 Adobe_font_metric::get_indexed_char (int code) const
112 return afm_bbox_to_box (font_inf_->cmi[code].charBBox);
114 return Box (Interval (0,0),Interval (0,0));
118 read_afm_file (String nm)
120 FILE *f = fopen (nm.to_str0 () , "r");
122 char *check_key = "Comment TfmCheckSum";
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)
133 sscanf (s + strlen (check_key), "%ud", &cs);
141 int ok = AFM_parseFile (f, &fi, ~1);
145 error (_f ("Error parsing AFM file: `%s'", nm.to_str0 ()));
150 return Adobe_font_metric::make_afm (fi, cs);
155 actually, AFMs will be printers point, usually, but our .py script dumps
159 afm_bbox_to_box (AFM_BBox bb)
161 return Box (Interval (bb.llx, bb.urx)* (1/1000.0) PT,
162 Interval (bb.lly, bb.ury)* (1/1000.0) PT);
167 Adobe_font_metric::get_indexed_wxwy (int k)const
169 AFM_CharMetricInfo const *mi = font_inf_->cmi+ k;
170 return 1/1000.0 PT * Offset (mi->wx, mi->wy);
175 Adobe_font_metric::~Adobe_font_metric ()
177 AFM_free (font_inf_);
181 return a stencil, without fontification
184 Adobe_font_metric::find_by_name (String s) const
186 AFM_CharMetricInfo const *cm = find_char_metric (s);
191 Why don't we return empty?
199 SCM at = (scm_list_n (ly_symbol2scm ("char"),
200 scm_int2num (cm->code),
203 // at= fontify_atom ((Font_metric*)this, at);
204 Box b = afm_bbox_to_box (cm->charBBox);
206 return Stencil (b, at);