]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
release: 1.3.74
[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 "main.hh"
11 #include "all-font-metrics.hh"
12 #include "debug.hh"
13 #include "warn.hh"
14 #include "afm.hh"
15 #include "tfm.hh"
16 #include "lily-guile.hh"
17 #include "scm-hash.hh"
18
19
20
21
22 extern "C" {
23 #include <kpathsea/kpathsea.h>
24 }
25
26 const char * default_font_sz_ = "cmr10";
27
28 All_font_metrics::All_font_metrics (String path)
29 {
30   afm_p_dict_ = new Scheme_hash_table;
31   tfm_p_dict_ = new Scheme_hash_table;
32   scaled_p_dict_ = new Scheme_hash_table;
33   
34   search_path_.parse_path (path);
35 }
36
37 All_font_metrics::~All_font_metrics ()
38 {
39   scm_unprotect_object (afm_p_dict_->self_scm ());
40   scm_unprotect_object (tfm_p_dict_->self_scm ());
41   scm_unprotect_object (scaled_p_dict_->self_scm ());
42 }
43
44 Adobe_font_metric *
45 All_font_metrics::find_afm (String name)
46 {
47   SCM sname = ly_symbol2scm (name.ch_C ());
48   if (!afm_p_dict_->elem_b (sname))
49     {
50       String path;
51       if (path.empty_b ())
52         {
53           char  * p = kpse_find_file(name.ch_C(), kpse_afm_format, true);
54           if (p)
55             path = p;
56         }
57       
58       if (path.empty_b())
59         path = search_path_.find (name  + ".afm");
60       if (path.empty_b())
61         return 0;
62       
63       if (verbose_global_b)
64         progress_indication ("[" + path);
65       SCM  afm = read_afm_file (path);
66
67       unsmob_metrics (afm)->name_ = sname;
68
69       if (verbose_global_b)
70         progress_indication ("]");
71
72        afm_p_dict_->set (sname,afm);
73     }
74   
75   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (afm_p_dict_->get (sname)));
76 }
77
78 Scaled_font_metric * 
79 All_font_metrics::find_scaled (String nm, int m)
80 {
81   String index =  nm + "@" + to_str (m);
82   SCM sname = ly_symbol2scm (index.ch_C ());
83
84   Font_metric *fm =0;
85   if (!scaled_p_dict_->elem_b (sname))
86     {
87       Font_metric *f = find_font (nm);
88       SCM s =  Scaled_font_metric::make_scaled_font_metric (f, m);
89       scaled_p_dict_->set (sname, s);
90       fm =  unsmob_metrics (s);
91     }
92   else
93     fm = unsmob_metrics (scaled_p_dict_->get (sname));
94
95   return dynamic_cast<Scaled_font_metric*> (fm);
96 }
97
98 Tex_font_metric *
99 All_font_metrics::find_tfm (String name)
100 {
101   SCM sname = ly_symbol2scm (name.ch_C ());  
102   if (!tfm_p_dict_->elem_b (sname))
103     {
104       String path;
105       
106       if (path.empty_b())
107         {
108           char * p = kpse_find_tfm(name.ch_C());
109           path = p;
110         }
111       if (path.empty_b())
112         path = search_path_.find (name  + ".tfm");
113       if (path.empty_b())
114         return 0;
115
116       if (verbose_global_b)
117         progress_indication ("[" + path);
118       SCM tfm = Tex_font_metric::make_tfm (path);
119       if (verbose_global_b)
120         progress_indication ("]");
121
122       Font_metric *fm = unsmob_metrics (tfm);
123       fm->name_ = sname;
124       tfm_p_dict_->set (sname, tfm);
125     }
126     
127   return
128     dynamic_cast<Tex_font_metric*> (unsmob_metrics (tfm_p_dict_->get(sname)));
129 }
130
131
132 Font_metric *
133 All_font_metrics::find_font (String name)
134 {
135   Font_metric * f=0;
136   f = find_tfm (name);
137   if (f)
138     return f;
139
140   f= find_afm (name);
141   if (f)
142     return f;
143
144   warning (_f ("can't find font: `%s'", name.ch_C ()));
145   warning (_ ("Loading default font"));
146   
147   String def_name = default_font_sz_;
148   SCM l = scm_assoc (ly_str02scm ("default"),
149                      scm_eval (ly_symbol2scm ("cmr-alist")));
150   
151   if (l != SCM_BOOL_F)
152     def_name = ly_scm2string (gh_cdr (l));
153
154   f =  find_tfm (def_name);
155   if (f)
156     return f;
157
158   f= find_afm (def_name);
159   if (f)
160     return f;
161
162   error (_f ("can't find default font: `%s'", def_name.ch_C ()));
163   error (_f ("(search path: `%s')", search_path_.str ()));
164   error (_ ("Giving up"));
165
166   return 0;
167 }
168
169 SCM
170 All_font_metrics::font_descriptions () const
171 {
172   SCM l[] = {0,0,0};
173
174   l[0] = afm_p_dict_->to_alist ();
175   l[1] = tfm_p_dict_->to_alist ();
176   l[2] = scaled_p_dict_->to_alist ();  
177
178   SCM list = SCM_EOL;
179   for (int i=0; i < 3; i++)
180     {
181       for (SCM s = l[i];  gh_pair_p (s); s = gh_cdr (s))
182         {
183           Font_metric * fm = unsmob_metrics (gh_cdar (s));
184
185           list = gh_cons (fm->description (), list);
186         }
187     }
188   return list;
189 }
190
191
192
193 Font_metric*
194 find_font (String name)
195 {
196   return   all_fonts_global_p->find_font (name);
197 }