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