]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
* po/nl.po: Update.
[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--2004 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.is_empty ())
57         path = search_path_.find (name  + ".afm");
58
59       if (path.is_empty ())
60         {
61           String p = kpathsea_find_afm (name.to_str0 ());
62           if (p.length ())
63             path = p;
64         }
65
66       if (path.is_empty ())
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.");
108               s += "\n";
109               s += _ ("Rerun with -V to show font paths.");
110               s += "\n";
111               s += _("A script for removing font-files is delivered with the source-code:");
112               s += "\n";
113               s += "buildscripts/clean-fonts.sh";
114               error (s);
115             }
116         }
117     }
118   
119   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
120 }
121
122
123 Tex_font_metric *
124 All_font_metrics::find_tfm (String name)
125 {
126   SCM sname = ly_symbol2scm (name.to_str0 ());
127   SCM name_string = scm_makfrom0str (name.to_str0 ());
128
129   SCM val;
130   if (!tfm_p_dict_->try_retrieve (sname, &val))
131     {
132       String path;
133       
134       if (path.is_empty ())
135         {
136           String p = kpathsea_find_tfm (name.to_str0 ());
137           if (p.length ())
138             path = p;
139         }
140
141       if (path.is_empty ())
142         path = search_path_.find (name  + ".tfm");
143       if (path.is_empty ())
144         return 0;
145
146       if (verbose_global_b)
147         progress_indication ("[" + path);
148       
149       val = Tex_font_metric::make_tfm (path);
150
151       if (verbose_global_b)
152         progress_indication ("]");
153
154       unsmob_metrics (val)->path_ = path;
155       unsmob_metrics (val)->description_ = gh_cons (name_string, gh_double2scm (1.0));
156       tfm_p_dict_->set (sname, val);
157
158       scm_gc_unprotect_object (val);
159     }
160
161   return
162     dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
163 }
164
165
166
167 Font_metric *
168 All_font_metrics::find_font (String name)
169 {
170   if ((name.left_string (4) == "feta") ||
171       (name.left_string (8) == "parmesan"))
172     {
173       Font_metric * f = find_afm (name);
174       if (f)
175         return f;
176       else
177       f =find_tfm (name);
178       if (f)
179         return f ;
180     }
181   else
182     {
183       Font_metric * f = find_tfm (name);
184       if (f)
185         return f;
186       else
187       f =find_afm (name);
188       if (f)
189         return f ;
190     }
191
192   warning (_f ("can't find font: `%s'", name.to_str0 ()));
193   warning (_ ("Loading default font"));
194   
195   String def_name = default_font_str0_;
196
197   /*
198     we're in emergency recovery mode here anyway, so don't try to do
199     anything smart that runs the risk of failing.  */
200   Font_metric*  f= find_afm (def_name);
201   if (f)
202     return f;
203
204   f =  find_tfm (def_name);
205   if (f)
206     return f;
207
208   error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
209   error (_f ("(search path: `%s')", search_path_.to_string ()));
210   error (_ ("Giving up"));
211
212   return 0;
213 }
214
215