2 afm.cc -- implement Adobe_font_metric
4 source file of the Flower Library
6 (c) 2000--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "molecule.hh"
13 Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi)
18 for (int i= 256; i--;)
19 ascii_to_metric_idx_.push (-1);
21 for (int i=0; i < fi->numOfChars; i++)
23 AFM_CharMetricInfo * c = fi->cmi + i;
26 Some TeX afm files contain code = -1. We don't know why, let's
31 ascii_to_metric_idx_[c->code] = i;
32 name_to_metric_dict_[c->name] = i;
38 Adobe_font_metric::make_afm (AFM_Font_info *fi, unsigned int checksum)
40 Adobe_font_metric * fm = new Adobe_font_metric (fi);
41 fm->checksum_ = checksum;
42 return fm->self_scm ();
46 AFM_CharMetricInfo const *
47 Adobe_font_metric::find_ascii_metric (int a , bool warn) const
49 if (ascii_to_metric_idx_[a] >=0)
51 int code = ascii_to_metric_idx_[a];
54 return font_inf_->cmi + code;
59 warning (_f ("can't find character number: %d", a));
65 AFM_CharMetricInfo const *
66 Adobe_font_metric::find_char_metric (String nm, bool warn) const
68 std::map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
70 if (ai == name_to_metric_dict_.end ())
74 warning (_f ("can't find character called: `%s'", nm.ch_C ()));
79 return font_inf_->cmi + (*ai).second;
83 Adobe_font_metric::count () const
85 return ascii_to_metric_idx_.size ();
89 Adobe_font_metric::get_char (int code) const
91 AFM_CharMetricInfo const
92 * c = find_ascii_metric (code,false);
93 Box b (Interval (0,0),Interval (0,0));
95 b = afm_bbox_to_box (c->charBBox);
101 read_afm_file (String nm)
103 FILE *f = fopen (nm.ch_C () , "r");
105 char *check_key = "TfmCheckSum";
106 fgets (s, sizeof (s), f);
109 if (strncmp (s, check_key, strlen (check_key)) == 0)
111 sscanf (s + strlen (check_key), "%ud", &cs);
120 int ok = AFM_parseFile (f, &fi, ~1);
124 error (_f ("Error parsing AFM file: `%s'", nm.ch_C ()));
129 return Adobe_font_metric::make_afm (fi, cs);
134 afm_bbox_to_box (AFM_BBox bb)
136 return Box (Interval (bb.llx, bb.urx)* (1/1000.0),
137 Interval (bb.lly, bb.ury)* (1/1000.0));
142 Adobe_font_metric::~Adobe_font_metric ()
144 AFM_free (font_inf_);
148 return a molecule, without fontification
151 Adobe_font_metric::find_by_name (String s) const
153 AFM_CharMetricInfo const *cm = find_char_metric (s, false);
162 SCM at = (gh_list (ly_symbol2scm ("char"),
163 gh_int2scm (cm->code),
166 // at= fontify_atom ((Font_metric*)this, at);
167 Box b = afm_bbox_to_box (cm->charBBox);
169 return Molecule (b, at);