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