lilypond-1.3.28
[lilypond.git] / lily / afm.cc
blob0a675150aa3f15c61b52e76e5c25d7700be2b525
1 /*
2 afm2.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"
12 Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi)
14 font_inf_ = fi;
16 for (int i= 256; i--;)
17 ascii_to_metric_idx_.push (-1);
19 for (int i=0; i < fi->numOfChars; i++)
21 AFM_CharMetricInfo * c = fi->cmi + i;
23 ascii_to_metric_idx_[c->code] = i;
24 name_to_metric_dict_[c->name] = i;
29 AFM_CharMetricInfo const *
30 Adobe_font_metric::find_ascii_metric (int a , bool warn) const
32 if (ascii_to_metric_idx_[a] >=0)
34 int code = ascii_to_metric_idx_[a];
35 if (code>=0)
37 return font_inf_->cmi + code;
40 else if (warn )
42 warning (_f ("Can't find character number: %d", a));
45 return 0;
48 AFM_CharMetricInfo const *
49 Adobe_font_metric::find_char_metric (String nm, bool warn) const
51 if (!name_to_metric_dict_.elem_b (nm))
53 if (warn)
55 warning (_f ("Can't find character called: `%s'", nm.ch_C()));
57 return 0;
59 else
60 return font_inf_->cmi + name_to_metric_dict_ [nm];
64 Box
65 Adobe_font_metric::get_char (int code, bool warn) const
67 AFM_CharMetricInfo const
68 * c = find_ascii_metric (code,warn);
69 if (c)
70 return afm_bbox_to_box (c->charBBox);
71 else
72 return Box (Interval (0,0),Interval(0,0));
75 Adobe_font_metric*
76 read_afm_file (String nm)
78 FILE *f = fopen (nm.ch_C() , "r");
80 AFM_Font_info * fi;
81 int ok = AFM_parseFile (f, &fi, ~1);
83 if (ok)
85 error (_("Error parsing AFM file"));
86 exit (2);
88 fclose (f);
90 return new Adobe_font_metric (fi);
94 Box
95 afm_bbox_to_box (AFM_BBox bb)
97 return Box (Interval (bb.llx, bb.urx)* (1/1000.0),
98 Interval (bb.lly, bb.ury)* (1/1000.0));
103 Adobe_font_metric::~Adobe_font_metric ()
105 AFM_free (font_inf_);