]> git.donarmstrong.com Git - lilypond.git/blob - lily/afm.cc
release: 1.3.100
[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 #include "molecule.hh"
12
13 Adobe_font_metric::Adobe_font_metric (AFM_Font_info * fi)
14 {
15   font_inf_ = fi;
16
17   for (int i= 256; i--;)
18     ascii_to_metric_idx_.push (-1);
19   
20   for (int i=0; i < fi->numOfChars; i++)
21     {
22       AFM_CharMetricInfo * c = fi->cmi + i;
23
24       ascii_to_metric_idx_[c->code] = i;
25       name_to_metric_dict_[c->name] = i;
26     }
27 }
28
29
30 SCM
31 Adobe_font_metric::make_afm (AFM_Font_info *fi)
32 {
33   Adobe_font_metric * fm = new Adobe_font_metric (fi);
34
35   return fm->self_scm();    
36 }
37
38
39 AFM_CharMetricInfo const *
40 Adobe_font_metric::find_ascii_metric (int a , bool warn) const
41 {
42   if (ascii_to_metric_idx_[a] >=0)
43     {
44       int code = ascii_to_metric_idx_[a];
45       if (code>=0)
46         {
47           return font_inf_->cmi + code;
48         }
49     }
50   else if (warn )
51     {
52       warning (_f ("can't find character number: %d", a));
53     }
54
55   return 0;
56 }
57
58 AFM_CharMetricInfo const *
59 Adobe_font_metric::find_char_metric (String nm, bool warn) const
60 {
61   map<String,int>::const_iterator ai = name_to_metric_dict_.find (nm);
62   
63   if (ai == name_to_metric_dict_.end ())
64     {
65       if (warn)
66         {
67           warning (_f ("can't find character called: `%s'", nm.ch_C()));
68         }
69       return 0;
70     }
71   else
72     return font_inf_->cmi + (*ai).second;
73 }
74
75
76 Box
77 Adobe_font_metric::get_char (int code) const
78 {
79   AFM_CharMetricInfo const
80     * c =  find_ascii_metric (code,false);
81   Box b (Interval (0,0),Interval(0,0));
82   if (c)
83     b = afm_bbox_to_box (c->charBBox);                  
84
85   return b;
86 }
87
88 SCM
89 read_afm_file (String nm)
90 {
91   FILE *f = fopen (nm.ch_C() , "r");
92
93   AFM_Font_info * fi;
94   int ok = AFM_parseFile (f, &fi, ~1);
95
96   if (ok)
97     {
98       error (_("Error parsing AFM file"));
99       exit (2);
100     }
101   fclose (f);
102
103   return Adobe_font_metric::make_afm (fi);
104 }
105
106   
107 Box
108 afm_bbox_to_box (AFM_BBox bb)
109 {
110   return Box (Interval (bb.llx, bb.urx)* (1/1000.0),
111               Interval (bb.lly, bb.ury)* (1/1000.0));
112
113 }
114   
115
116 Adobe_font_metric::~Adobe_font_metric ()
117 {
118   AFM_free (font_inf_);
119 }
120
121 /*
122   return a molecule, without fontification 
123  */
124 Molecule
125 Adobe_font_metric::find_by_name (String s) const
126 {
127   AFM_CharMetricInfo const *cm = find_char_metric (s, false);
128
129   if (!cm)
130     {
131       Molecule m;
132       m.set_empty (false);
133       return m;
134     }
135   
136   SCM at =  (gh_list (ly_symbol2scm ("char"),
137                       gh_int2scm (cm->code),
138                       SCM_UNDEFINED));
139   
140   //  at= fontify_atom ((Font_metric*)this, at);
141   Box b = afm_bbox_to_box (cm->charBBox);
142
143   return Molecule (b, at);
144 }