]> git.donarmstrong.com Git - lilypond.git/blob - lily/afm.cc
patch::: 1.3.86.jcn2
[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 SCM
30 Adobe_font_metric::make_afm (AFM_Font_info *fi)
31 {
32   Adobe_font_metric * fm = new Adobe_font_metric (fi);
33
34   return fm->smobbed_self();    
35 }
36
37
38 AFM_CharMetricInfo const *
39 Adobe_font_metric::find_ascii_metric (int a , bool warn) const
40 {
41   if (ascii_to_metric_idx_[a] >=0)
42     {
43       int code = ascii_to_metric_idx_[a];
44       if (code>=0)
45         {
46           return font_inf_->cmi + code;
47         }
48     }
49   else if (warn )
50     {
51       warning (_f ("can't find character number: %d", a));
52     }
53
54   return 0;
55 }
56
57 AFM_CharMetricInfo const *
58 Adobe_font_metric::find_char_metric (String nm, bool warn) const
59 {
60   map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
61   
62   if (ai == name_to_metric_dict_.end ())
63     {
64       if (warn)
65         {
66           warning (_f ("can't find character called: `%s'", nm.ch_C()));
67         }
68       return 0;
69     }
70   else
71     return font_inf_->cmi + (*ai).second;
72 }
73
74
75 Box
76 Adobe_font_metric::get_char (int code, bool warn) const
77 {
78   AFM_CharMetricInfo const
79     * c =  find_ascii_metric (code,warn);
80   if (c)
81     return afm_bbox_to_box (c->charBBox);                       
82   else
83     return Box (Interval (0,0),Interval(0,0));
84 }
85
86 SCM
87 read_afm_file (String nm)
88 {
89   FILE *f = fopen (nm.ch_C() , "r");
90
91   AFM_Font_info * fi;
92   int ok = AFM_parseFile (f, &fi, ~1);
93
94   if (ok)
95     {
96       error (_("Error parsing AFM file"));
97       exit (2);
98     }
99   fclose (f);
100
101   return Adobe_font_metric::make_afm (fi);
102 }
103
104   
105 Box
106 afm_bbox_to_box (AFM_BBox bb)
107 {
108   return Box (Interval (bb.llx, bb.urx)* (1/1000.0),
109               Interval (bb.lly, bb.ury)* (1/1000.0));
110
111 }
112   
113
114 Adobe_font_metric::~Adobe_font_metric ()
115 {
116   AFM_free (font_inf_);
117 }