]> git.donarmstrong.com Git - lilypond.git/blob - lily/afm.cc
release: 1.3.66
[lilypond.git] / lily / afm.cc
1 /*   
2   afm.cc --  implement Adobe_font_metric
3   
4   source file of the Flower Library
5   
6   (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9 #include "afm.hh"
10 #include "warn.hh"
11
12 Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi)
13 {
14   font_inf_ = fi;
15
16   for (int i= 256; i--;)
17     ascii_to_metric_idx_.push (-1);
18   
19   for (int i=0; i < fi->numOfChars; i++)
20     {
21       AFM_CharMetricInfo * c = fi->cmi + i;
22
23       ascii_to_metric_idx_[c->code] = i;
24       name_to_metric_dict_[c->name] = i;
25     }
26 }
27   
28
29 AFM_CharMetricInfo const *
30 Adobe_font_metric::find_ascii_metric (int a , bool warn) const
31 {
32   if (ascii_to_metric_idx_[a] >=0)
33     {
34       int code = ascii_to_metric_idx_[a];
35       if (code>=0)
36         {
37           return font_inf_->cmi + code;
38         }
39     }
40   else if (warn )
41     {
42       warning (_f ("can't find character number: %d", a));
43     }
44
45   return 0;
46 }
47
48 AFM_CharMetricInfo const *
49 Adobe_font_metric::find_char_metric (String nm, bool warn) const
50 {
51   map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
52   
53   if (ai == name_to_metric_dict_.end ())
54     {
55       if (warn)
56         {
57           warning (_f ("can't find character called: `%s'", nm.ch_C()));
58         }
59       return 0;
60     }
61   else
62     return font_inf_->cmi + (*ai).second;
63 }
64
65
66 Box
67 Adobe_font_metric::get_char (int code, bool warn) const
68 {
69   AFM_CharMetricInfo const
70     * c =  find_ascii_metric (code,warn);
71   if (c)
72     return afm_bbox_to_box (c->charBBox);                       
73   else
74     return Box (Interval (0,0),Interval(0,0));
75 }
76
77 Adobe_font_metric*
78 read_afm_file (String nm)
79 {
80   FILE *f = fopen (nm.ch_C() , "r");
81
82   AFM_Font_info * fi;
83   int ok = AFM_parseFile (f, &fi, ~1);
84
85   if (ok)
86     {
87       error (_("Error parsing AFM file"));
88       exit (2);
89     }
90   fclose (f);
91
92   return new Adobe_font_metric (fi);
93 }
94
95   
96 Box
97 afm_bbox_to_box (AFM_BBox bb)
98 {
99   return Box (Interval (bb.llx, bb.urx)* (1/1000.0),
100               Interval (bb.lly, bb.ury)* (1/1000.0));
101
102 }
103   
104
105 Adobe_font_metric::~Adobe_font_metric ()
106 {
107   AFM_free (font_inf_);
108 }