lilypond-1.3.124
[lilypond.git] / lily / afm.cc
blobb9224648bec3d5e9612b8bdad1787e130a1dbb81
1 /*
2 afm.cc -- implement Adobe_font_metric
4 source file of the Flower Library
6 (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include "afm.hh"
10 #include "warn.hh"
11 #include "molecule.hh"
13 Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi)
15 checksum_ = 0;
16 font_inf_ = 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;
25 ascii_to_metric_idx_[c->code] = i;
26 name_to_metric_dict_[c->name] = i;
31 SCM
32 Adobe_font_metric::make_afm (AFM_Font_info *fi, unsigned int checksum)
34 Adobe_font_metric * fm = new Adobe_font_metric (fi);
35 fm->checksum_ = checksum;
36 return fm->self_scm();
40 AFM_CharMetricInfo const *
41 Adobe_font_metric::find_ascii_metric (int a , bool warn) const
43 if (ascii_to_metric_idx_[a] >=0)
45 int code = ascii_to_metric_idx_[a];
46 if (code>=0)
48 return font_inf_->cmi + code;
51 else if (warn )
53 warning (_f ("can't find character number: %d", a));
56 return 0;
59 AFM_CharMetricInfo const *
60 Adobe_font_metric::find_char_metric (String nm, bool warn) const
62 map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
64 if (ai == name_to_metric_dict_.end ())
66 if (warn)
68 warning (_f ("can't find character called: `%s'", nm.ch_C()));
70 return 0;
72 else
73 return font_inf_->cmi + (*ai).second;
77 Box
78 Adobe_font_metric::get_char (int code) const
80 AFM_CharMetricInfo const
81 * c = find_ascii_metric (code,false);
82 Box b (Interval (0,0),Interval(0,0));
83 if (c)
84 b = afm_bbox_to_box (c->charBBox);
86 return b;
89 SCM
90 read_afm_file (String nm)
92 FILE *f = fopen (nm.ch_C() , "r");
93 char s[2048];
94 char *check_key = "TfmCheckSum";
95 fgets (s, sizeof (s), f);
97 unsigned int cs = 0;
98 if (strncmp (s, check_key, strlen (check_key)) == 0)
100 sscanf (s + strlen (check_key), "%ud", &cs);
102 else
104 rewind (f);
108 AFM_Font_info * fi;
109 int ok = AFM_parseFile (f, &fi, ~1);
111 if (ok)
113 error (_f ("Error parsing AFM file: %s", nm.ch_C ()));
114 exit (2);
116 fclose (f);
118 return Adobe_font_metric::make_afm (fi, cs);
123 afm_bbox_to_box (AFM_BBox bb)
125 return Box (Interval (bb.llx, bb.urx)* (1/1000.0),
126 Interval (bb.lly, bb.ury)* (1/1000.0));
131 Adobe_font_metric::~Adobe_font_metric ()
133 AFM_free (font_inf_);
137 return a molecule, without fontification
139 Molecule
140 Adobe_font_metric::find_by_name (String s) const
142 AFM_CharMetricInfo const *cm = find_char_metric (s, false);
144 if (!cm)
146 Molecule m;
147 m.set_empty (false);
148 return m;
151 SCM at = (gh_list (ly_symbol2scm ("char"),
152 gh_int2scm (cm->code),
153 SCM_UNDEFINED));
155 // at= fontify_atom ((Font_metric*)this, at);
156 Box b = afm_bbox_to_box (cm->charBBox);
158 return Molecule (b, at);