]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
patch::: 1.3.88.hwn1.jcn2: 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
20
21 #if HAVE_KPATHSEA_KPATHSEA_H
22 extern "C" {
23 #include <kpathsea/kpathsea.h>
24 }
25 #endif
26
27 const char * default_font_sz_ = "cmr10";
28
29 All_font_metrics::All_font_metrics (String path)
30 {
31   afm_p_dict_ = new Scheme_hash_table;
32   tfm_p_dict_ = new Scheme_hash_table;
33   scaled_p_dict_ = new Scheme_hash_table;
34   
35   search_path_.parse_path (path);
36 }
37
38 All_font_metrics::~All_font_metrics ()
39 {
40   scm_unprotect_object (afm_p_dict_->self_scm ());
41   scm_unprotect_object (tfm_p_dict_->self_scm ());
42   scm_unprotect_object (scaled_p_dict_->self_scm ());
43 }
44
45 Adobe_font_metric *
46 All_font_metrics::find_afm (String name)
47 {
48   SCM sname = ly_symbol2scm (name.ch_C ());
49   if (!afm_p_dict_->elem_b (sname))
50     {
51       String path;
52
53       if (path.empty_b())
54         path = search_path_.find (name  + ".afm");
55
56 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
57       if (path.empty_b ())
58         {
59           char  * p = kpse_find_file (name.ch_C(), kpse_afm_format, true);
60           if (p)
61             path = p;
62         }
63 #endif
64       
65       if (path.empty_b())
66         return 0;
67       
68       if (verbose_global_b)
69         progress_indication ("[" + path);
70       SCM  afm = read_afm_file (path);
71
72       unsmob_metrics (afm)->name_ = sname;
73
74       if (verbose_global_b)
75         progress_indication ("]");
76
77        afm_p_dict_->set (sname,afm);
78     }
79   
80   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (afm_p_dict_->get (sname)));
81 }
82
83 Scaled_font_metric * 
84 All_font_metrics::find_scaled (String nm, int m)
85 {
86   String index =  nm + "@" + to_str (m);
87   SCM sname = ly_symbol2scm (index.ch_C ());
88
89   Font_metric *fm =0;
90   if (!scaled_p_dict_->elem_b (sname))
91     {
92       Font_metric *f = find_font (nm);
93       SCM s =  Scaled_font_metric::make_scaled_font_metric (f, m);
94       scaled_p_dict_->set (sname, s);
95       fm =  unsmob_metrics (s);
96     }
97   else
98     fm = unsmob_metrics (scaled_p_dict_->get (sname));
99
100   return dynamic_cast<Scaled_font_metric*> (fm);
101 }
102
103 Tex_font_metric *
104 All_font_metrics::find_tfm (String name)
105 {
106   SCM sname = ly_symbol2scm (name.ch_C ());  
107   if (!tfm_p_dict_->elem_b (sname))
108     {
109       String path;
110       
111 //#if KPATHSEA && HAVE_KPSE_FIND_TFM  -- urg: a macro
112 #if KPATHSEA && HAVE_KPSE_FIND_FILE
113       if (path.empty_b())
114         {
115           char * p = kpse_find_tfm (name.ch_C());
116           if (p)
117             path = p;
118         }
119 #endif
120       if (path.empty_b())
121         path = search_path_.find (name  + ".tfm");
122       if (path.empty_b())
123         return 0;
124
125       if (verbose_global_b)
126         progress_indication ("[" + path);
127       SCM tfm = Tex_font_metric::make_tfm (path);
128       if (verbose_global_b)
129         progress_indication ("]");
130
131       Font_metric *fm = unsmob_metrics (tfm);
132       fm->name_ = sname;
133       tfm_p_dict_->set (sname, tfm);
134     }
135     
136   return
137     dynamic_cast<Tex_font_metric*> (unsmob_metrics (tfm_p_dict_->get(sname)));
138 }
139
140
141 Font_metric *
142 All_font_metrics::find_font (String name)
143 {
144   Font_metric * f=0;
145
146   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 SCM
180 All_font_metrics::font_descriptions () const
181 {
182   SCM l[] = {0,0,0};
183
184   l[0] = afm_p_dict_->to_alist ();
185   l[1] = tfm_p_dict_->to_alist ();
186   l[2] = scaled_p_dict_->to_alist ();  
187
188   SCM list = SCM_EOL;
189   for (int i=0; i < 3; i++)
190     {
191       for (SCM s = l[i];  gh_pair_p (s); s = gh_cdr (s))
192         {
193           Font_metric * fm = unsmob_metrics (gh_cdar (s));
194
195           list = gh_cons (fm->description (), list);
196         }
197     }
198   return list;
199 }
200
201
202
203 Font_metric*
204 find_font (String name)
205 {
206   return   all_fonts_global_p->find_font (name);
207 }