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