]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
patch::: 1.3.99.jcn2
[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   scaled_p_dict_ = new Scheme_hash_table;
32   
33   search_path_.parse_path (path);
34 }
35
36 All_font_metrics::~All_font_metrics ()
37 {
38   scm_unprotect_object (afm_p_dict_->self_scm ());
39   scm_unprotect_object (tfm_p_dict_->self_scm ());
40   scm_unprotect_object (scaled_p_dict_->self_scm ());
41 }
42
43 Adobe_font_metric *
44 All_font_metrics::find_afm (String name)
45 {
46   SCM sname = ly_symbol2scm (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
71       unsmob_metrics (val)->name_ = sname;
72
73       if (verbose_global_b)
74         progress_indication ("]");
75
76       afm_p_dict_->set (sname,val);
77
78       scm_unprotect_object (val);      
79     }
80   
81   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
82 }
83
84 Scaled_font_metric * 
85 All_font_metrics::find_scaled (String nm, Real m)
86 {
87   String index =  nm + "@" + to_str (m);
88   SCM sname = ly_symbol2scm (index.ch_C ());
89
90   SCM val;
91
92   if (!scaled_p_dict_->try_retrieve  (sname, &val))
93     {
94       Font_metric *f = find_font (nm);
95       val =  Scaled_font_metric::make_scaled_font_metric (f, m);
96       scaled_p_dict_->set (sname, val);
97
98       scm_unprotect_object (val);
99     }
100
101   return dynamic_cast<Scaled_font_metric*> (unsmob_metrics (val));
102 }
103
104 Tex_font_metric *
105 All_font_metrics::find_tfm (String name)
106 {
107   SCM sname = ly_symbol2scm (name.ch_C ());
108
109   SCM val;
110   if (!tfm_p_dict_->try_retrieve (sname, &val))
111     {
112       String path;
113       
114       if (path.empty_b())
115         {
116           char * p = ly_find_tfm (name.ch_C());
117           if (p)
118             path = p;
119         }
120
121       if (path.empty_b())
122         path = search_path_.find (name  + ".tfm");
123       if (path.empty_b())
124         return 0;
125
126       if (verbose_global_b)
127         progress_indication ("[" + path);
128       val = Tex_font_metric::make_tfm (path);
129       if (verbose_global_b)
130         progress_indication ("]");
131
132       unsmob_metrics (val)->name_ = sname;
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=0;
147
148   f= find_afm (name);
149   if (f)
150     return f;
151
152   f = find_tfm (name);
153   if (f)
154     return f;
155
156   warning (_f ("can't find font: `%s'", name.ch_C ()));
157   warning (_ ("Loading default font"));
158   
159   String def_name = default_font_sz_;
160   SCM l = scm_assoc (ly_str02scm ("default"),
161                      scm_eval2 (ly_symbol2scm ("cmr-alist"), SCM_EOL));
162   
163   if (l != SCM_BOOL_F)
164     def_name = ly_scm2string (gh_cdr (l));
165
166   f= find_afm (def_name);
167   if (f)
168     return f;
169
170   f =  find_tfm (def_name);
171   if (f)
172     return f;
173
174   error (_f ("can't find default font: `%s'", def_name.ch_C ()));
175   error (_f ("(search path: `%s')", search_path_.str ()));
176   error (_ ("Giving up"));
177
178   return 0;
179 }
180
181 SCM
182 All_font_metrics::font_descriptions () const
183 {
184   SCM l[] = {0,0,0};
185
186   l[0] = afm_p_dict_->to_alist ();
187   l[1] = tfm_p_dict_->to_alist ();
188   l[2] = scaled_p_dict_->to_alist ();  
189
190   SCM list = SCM_EOL;
191   for (int i=0; i < 3; i++)
192     {
193       for (SCM s = l[i];  gh_pair_p (s); s = gh_cdr (s))
194         {
195           Font_metric * fm = unsmob_metrics (gh_cdar (s));
196
197           list = gh_cons (fm->description (), list);
198         }
199     }
200   return list;
201 }
202
203
204
205 Font_metric*
206 find_font (String name)
207 {
208   return all_fonts_global_p->find_font (name);
209 }