]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
0ebc486c678220463fc2c53ff4d872d4068b2813
[lilypond.git] / lily / all-font-metrics.cc
1 /*   
2   all-font-metrics.cc --  implement All_font_metrics
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "config.h"
11 #include "main.hh"
12 #include "all-font-metrics.hh"
13 #include "debug.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
22
23
24
25 const char * default_font_sz_ = "cmr10";
26
27 All_font_metrics::All_font_metrics (String path)
28 {
29   afm_p_dict_ = new Scheme_hash_table;
30   tfm_p_dict_ = new Scheme_hash_table;
31   
32   search_path_.parse_path (path);
33 }
34
35 All_font_metrics::~All_font_metrics ()
36 {
37   scm_unprotect_object (afm_p_dict_->self_scm ());
38   scm_unprotect_object (tfm_p_dict_->self_scm ());
39 }
40
41 Adobe_font_metric *
42 All_font_metrics::find_afm (String name)
43 {
44   SCM sname = ly_symbol2scm (name.ch_C ());
45
46   SCM name_str = ly_str02scm (name.ch_C ());
47
48   SCM val;
49   
50   if (!afm_p_dict_->try_retrieve  (sname, &val))
51     {
52       String path;
53
54       if (path.empty_b())
55         path = search_path_.find (name  + ".afm");
56
57       if (path.empty_b ())
58         {
59           char  * p = ly_find_afm (name.ch_C());
60           if (p)
61             path = p;
62         }
63
64       if (path.empty_b())
65         return 0;
66       
67       if (verbose_global_b)
68         progress_indication ("[" + path);
69       val = read_afm_file (path);
70       unsmob_metrics (val)->path_ = path;
71       
72       unsmob_metrics (val)->description_ = gh_cons (name_str, gh_double2scm (1.0));
73
74       if (verbose_global_b)
75         progress_indication ("]");
76
77       afm_p_dict_->set (sname,val);
78
79       scm_unprotect_object (val);
80
81
82       Adobe_font_metric *afm
83         = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val) );
84       Tex_font_metric * tfm = find_tfm (name);
85
86       if (tfm->info_.checksum != afm->checksum_)
87         {
88           String s = _f ("checksum mismatch for font file:\n`%s'", path.ch_C ());
89           s += " " + _f ("does not match: `%s'", tfm->path_.ch_C()); // FIXME
90           s += "\n";
91           s += " TFM: " + to_str ((int) tfm->info_.checksum);
92           s += " AFM: " + to_str ((int) afm->checksum_);
93           s += "\n";
94           s += _(" Rebuild all .afm files, and remove all .pk and .tfm files.  Rerun with -V to show font paths.");
95
96           error (s);
97         }
98     }
99   
100   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
101 }
102
103
104 Tex_font_metric *
105 All_font_metrics::find_tfm (String name)
106 {
107   SCM sname = ly_symbol2scm (name.ch_C ());
108   SCM name_str = ly_str02scm (name.ch_C ());
109
110   SCM val;
111   if (!tfm_p_dict_->try_retrieve (sname, &val))
112     {
113       String path;
114       
115       if (path.empty_b())
116         {
117           char * p = ly_find_tfm (name.ch_C());
118           if (p)
119             path = p;
120         }
121
122       if (path.empty_b())
123         path = search_path_.find (name  + ".tfm");
124       if (path.empty_b())
125         return 0;
126
127       
128       if (verbose_global_b)
129         progress_indication ("[" + path);
130       val = Tex_font_metric::make_tfm (path);
131
132       if (verbose_global_b)
133         progress_indication ("]");
134
135       unsmob_metrics (val)->path_ = path;
136       unsmob_metrics (val)->description_ = gh_cons (name_str, gh_double2scm (1.0));
137       tfm_p_dict_->set (sname, val);
138
139       scm_unprotect_object (val);
140     }
141
142   return
143     dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
144 }
145
146
147 Font_metric *
148 All_font_metrics::find_font (String name)
149 {
150   Font_metric * f= find_afm (name);
151   if (f)
152     return f;
153
154   f = find_tfm (name);
155   if (f)
156     return f;
157
158   warning (_f ("can't find font: `%s'", name.ch_C ()));
159   warning (_ ("Loading default font"));
160   
161   String def_name = default_font_sz_;
162   SCM l = scm_assoc (ly_str02scm ("default"),
163                      scm_eval2 (ly_symbol2scm ("cmr-alist"), SCM_EOL));
164   
165   if (l != SCM_BOOL_F)
166     def_name = ly_scm2string (gh_cdr (l));
167
168   f= find_afm (def_name);
169   if (f)
170     return f;
171
172   f =  find_tfm (def_name);
173   if (f)
174     return f;
175
176   error (_f ("can't find default font: `%s'", def_name.ch_C ()));
177   error (_f ("(search path: `%s')", search_path_.str ()));
178   error (_ ("Giving up"));
179
180   return 0;
181 }
182
183