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