]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
*** empty log message ***
[lilypond.git] / lily / all-font-metrics.cc
1 /*
2   
3   all-font-metrics.cc --  implement All_font_metrics
4   
5   source file of the GNU LilyPond music typesetter
6   
7   (c) 1999--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8   
9  */
10
11 #include "config.h"
12 #include "main.hh"
13 #include "all-font-metrics.hh"
14
15 #include "warn.hh"
16 #include "afm.hh"
17 #include "tfm.hh"
18 #include "lily-guile.hh"
19 #include "scm-hash.hh"
20 #include "kpath.hh"
21
22 static const char * default_font_str0_ = "cmr10";
23
24 All_font_metrics::All_font_metrics (String path)
25 {
26   afm_p_dict_ = new Scheme_hash_table;
27   tfm_p_dict_ = new Scheme_hash_table;
28   
29   search_path_.parse_path (path);
30 }
31
32 All_font_metrics::~All_font_metrics ()
33 {
34   scm_gc_unprotect_object (afm_p_dict_->self_scm ());
35   scm_gc_unprotect_object (tfm_p_dict_->self_scm ());
36 }
37
38 /*
39   TODO: our AFM handling is broken: the units in an AFM file are
40   relative to the design size (1000 units = 1 designsize). Hence we
41   should include design size when generating an AFM metric. 
42  */
43 Adobe_font_metric *
44 All_font_metrics::find_afm (String name)
45 {
46   SCM sname = ly_symbol2scm (name.to_str0 ());
47
48   SCM name_string = scm_makfrom0str (name.to_str0 ());
49
50   SCM val;
51   
52   if (!afm_p_dict_->try_retrieve (sname, &val))
53     {
54       String path;
55
56       if (path.empty_b ())
57         path = search_path_.find (name  + ".afm");
58
59       if (path.empty_b ())
60         {
61           String p = kpathsea_find_afm (name.to_str0 ());
62           if (p.length ())
63             path = p;
64         }
65
66       if (path.empty_b ())
67         return 0;
68       
69       if (verbose_global_b)
70         progress_indication ("[" + path);
71       val = read_afm_file (path);
72       unsmob_metrics (val)->path_ = path;
73       
74       unsmob_metrics (val)->description_ = gh_cons (name_string, gh_double2scm (1.0));
75
76       if (verbose_global_b)
77         progress_indication ("]");
78
79       afm_p_dict_->set (sname,val);
80
81       scm_gc_unprotect_object (val);
82
83
84       Adobe_font_metric *afm
85         = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
86
87       /*
88         only check checksums if there is one.  We take the risk that
89         some file has valid checksum 0
90       */
91       if (afm->checksum_)
92         {
93           Tex_font_metric * tfm = find_tfm (name);
94           
95           /* FIXME: better warning message
96              (maybe check upon startup for feta16.afm, feta16.tfm?)
97           */
98           if (tfm && tfm->info_.checksum != afm->checksum_)
99             {
100               String s = _f ("checksum mismatch for font file: `%s'",
101                              path.to_str0 ());
102               s += " " + _f ("does not match: `%s'", tfm->path_.to_str0 ()); // FIXME
103               s += "\n";
104               s += " TFM: " + to_string ((int) tfm->info_.checksum);
105               s += " AFM: " + to_string ((int) afm->checksum_);
106               s += "\n";
107               s += _ (" Rebuild all .afm files, and remove all .pk and .tfm files.\nRerun with -V to show font paths.\n");
108               s += _("A script for removing font-files is delivered with the source-code,\n"
109                      "in buildscripts/clean-fonts.sh");
110               error (s);
111             }
112         }
113     }
114   
115   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
116 }
117
118
119 Tex_font_metric *
120 All_font_metrics::find_tfm (String name)
121 {
122   SCM sname = ly_symbol2scm (name.to_str0 ());
123   SCM name_string = scm_makfrom0str (name.to_str0 ());
124
125   SCM val;
126   if (!tfm_p_dict_->try_retrieve (sname, &val))
127     {
128       String path;
129       
130       if (path.empty_b ())
131         {
132           String p = kpathsea_find_tfm (name.to_str0 ());
133           if (p.length ())
134             path = p;
135         }
136
137       if (path.empty_b ())
138         path = search_path_.find (name  + ".tfm");
139       if (path.empty_b ())
140         return 0;
141
142       if (verbose_global_b)
143         progress_indication ("[" + path);
144       
145       val = Tex_font_metric::make_tfm (path);
146
147       if (verbose_global_b)
148         progress_indication ("]");
149
150       unsmob_metrics (val)->path_ = path;
151       unsmob_metrics (val)->description_ = gh_cons (name_string, gh_double2scm (1.0));
152       tfm_p_dict_->set (sname, val);
153
154       scm_gc_unprotect_object (val);
155     }
156
157   return
158     dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
159 }
160
161
162
163 Font_metric *
164 All_font_metrics::find_font (String name)
165 {
166   if (name.left_string (4) == "feta") 
167     {
168       Font_metric * f = find_afm (name);
169       if (f)
170         return f;
171       else
172       f =find_tfm (name);
173       if (f)
174         return f ;
175     }
176   else
177     {
178       Font_metric * f = find_tfm (name);
179       if (f)
180         return f;
181       else
182       f =find_afm (name);
183       if (f)
184         return f ;
185     }
186
187   warning (_f ("can't find font: `%s'", name.to_str0 ()));
188   warning (_ ("Loading default font"));
189   
190   String def_name = default_font_str0_;
191
192   /*
193     we're in emergency recovery mode here anyway, so don't try to do
194     anything smart that runs the risk of failing.  */
195   Font_metric*  f= find_afm (def_name);
196   if (f)
197     return f;
198
199   f =  find_tfm (def_name);
200   if (f)
201     return f;
202
203   error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
204   error (_f ("(search path: `%s')", search_path_.to_string ()));
205   error (_ ("Giving up"));
206
207   return 0;
208 }
209
210