]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
* lily/font-metric.cc (get_encoded_index): New function.
[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   ugr: copied from find_tfm.
44  */
45 Adobe_font_metric *
46 All_font_metrics::find_afm (String name)
47 {
48   SCM sname = ly_symbol2scm (name.to_str0 ());
49   SCM name_string = scm_makfrom0str (name.to_str0 ());
50   SCM val;
51   if (!afm_p_dict_->try_retrieve (sname, &val))
52     {
53       String filename;
54
55       if (filename.is_empty ())
56         filename = search_path_.find (name  + ".afm");
57
58       if (filename.is_empty ())
59         {
60           String p = kpathsea_find_afm (name.to_str0 ());
61           if (p.length ())
62             filename = p;
63         }
64
65       if (filename.is_empty ())
66         return 0;
67       
68       if (verbose_global_b)
69         progress_indication ("[" + filename);
70       val = read_afm_file (filename);
71       unsmob_metrics (val)->filename_ = filename;
72       
73       unsmob_metrics (val)->description_ = gh_cons (name_string,
74                                                     gh_double2scm (1.0));
75
76       if (verbose_global_b)
77         progress_indication ("]");
78
79       afm_p_dict_->set (sname, val);
80       scm_gc_unprotect_object (val);
81
82       Adobe_font_metric *afm
83         = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
84
85       /* Only check checksums if there is one.  We take the risk that
86          some file has valid checksum 0 */
87       if (afm->checksum_)
88         {
89           Tex_font_metric * tfm = find_tfm (name);
90           
91           /* FIXME: better warning message
92              (maybe check upon startup for feta16.afm, feta16.tfm?) */
93           if (tfm && tfm->info_.checksum != afm->checksum_)
94             {
95               // FIXME: broken sentence
96               String s = _f ("checksum mismatch for font file: `%s'",
97                              filename.to_str0 ());
98               s += " " + _f ("does not match: `%s'",
99                              tfm->filename_.to_str0 ());
100               s += "\n";
101               s += " TFM: " + to_string ((int) tfm->info_.checksum);
102               s += " AFM: " + to_string ((int) afm->checksum_);
103               s += "\n";
104               s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
105               s += "\n";
106               s += _ ("Rerun with -V to show font paths.");
107               s += "\n";
108               s += _("A script for removing font-files is delivered with the source-code:");
109               s += "\n";
110               s += "buildscripts/clean-fonts.sh";
111               error (s);
112             }
113         }
114     }
115   
116   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
117 }
118
119
120 Tex_font_metric*
121 All_font_metrics::find_tfm (String name)
122 {
123   SCM sname = ly_symbol2scm (name.to_str0 ());
124   SCM name_string = scm_makfrom0str (name.to_str0 ());
125   SCM val;
126   if (!tfm_p_dict_->try_retrieve (sname, &val))
127     {
128       String filename;
129       
130       if (filename.is_empty ())
131         {
132           String p = kpathsea_find_tfm (name.to_str0 ());
133           if (p.length ())
134             filename = p;
135         }
136
137       if (filename.is_empty ())
138         filename = search_path_.find (name  + ".tfm");
139       if (filename.is_empty ())
140         return 0;
141
142       if (verbose_global_b)
143         progress_indication ("[" + filename);
144       
145       val = Tex_font_metric::make_tfm (filename);
146
147       if (verbose_global_b)
148         progress_indication ("]");
149
150       unsmob_metrics (val)->filename_ = filename;
151       unsmob_metrics (val)->description_ = gh_cons (name_string,
152                                                     gh_double2scm (1.0));
153       tfm_p_dict_->set (sname, val);
154       scm_gc_unprotect_object (val);
155     }
156
157   return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
158 }
159
160 Font_metric*
161 All_font_metrics::find_font (String name)
162 {
163   if ((name.left_string (4) == "feta") ||
164       (name.left_string (8) == "parmesan"))
165     {
166       Font_metric *f = find_afm (name);
167       if (f)
168         return f;
169       else
170       f =find_tfm (name);
171       if (f)
172         return f ;
173     }
174   else
175     {
176       Font_metric * f = find_tfm (name);
177       if (f)
178         return f;
179       else
180       f = find_afm (name);
181       if (f)
182         return f;
183     }
184
185   warning (_f ("can't find font: `%s'", name.to_str0 ()));
186   warning (_ ("Loading default font"));
187   
188   String def_name = default_font_str0_;
189
190   /*
191     we're in emergency recovery mode here anyway, so don't try to do
192     anything smart that runs the risk of failing.  */
193   Font_metric*  f= find_afm (def_name);
194   if (f)
195     return f;
196
197   f =  find_tfm (def_name);
198   if (f)
199     return f;
200
201   error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
202   error (_f ("(search path: `%s')", search_path_.to_string ()));
203   error (_ ("Giving up"));
204
205   return 0;
206 }
207
208 All_font_metrics *all_fonts_global;
209
210
211 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
212            (SCM name),
213            "Load the font @var{name}. ")
214 {
215   SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
216
217   Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
218
219   return fm->self_scm ();
220 }
221