]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
release: 1.3.91
[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   if (!afm_p_dict_->elem_b (sname))
48     {
49       String path;
50
51       if (path.empty_b())
52         path = search_path_.find (name  + ".afm");
53
54       if (path.empty_b ())
55         {
56           char  * p = ly_find_afm (name.ch_C());
57           if (p)
58             path = p;
59         }
60
61       if (path.empty_b())
62         return 0;
63       
64       if (verbose_global_b)
65         progress_indication ("[" + path);
66       SCM  afm = read_afm_file (path);
67
68       unsmob_metrics (afm)->name_ = sname;
69
70       if (verbose_global_b)
71         progress_indication ("]");
72
73        afm_p_dict_->set (sname,afm);
74     }
75   
76   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (afm_p_dict_->get (sname)));
77 }
78
79 Scaled_font_metric * 
80 All_font_metrics::find_scaled (String nm, int m)
81 {
82   String index =  nm + "@" + to_str (m);
83   SCM sname = ly_symbol2scm (index.ch_C ());
84
85   Font_metric *fm =0;
86   if (!scaled_p_dict_->elem_b (sname))
87     {
88       Font_metric *f = find_font (nm);
89       SCM s =  Scaled_font_metric::make_scaled_font_metric (f, m);
90       scaled_p_dict_->set (sname, s);
91       fm =  unsmob_metrics (s);
92     }
93   else
94     fm = unsmob_metrics (scaled_p_dict_->get (sname));
95
96   return dynamic_cast<Scaled_font_metric*> (fm);
97 }
98
99 Tex_font_metric *
100 All_font_metrics::find_tfm (String name)
101 {
102   SCM sname = ly_symbol2scm (name.ch_C ());  
103   if (!tfm_p_dict_->elem_b (sname))
104     {
105       String path;
106       
107       if (path.empty_b())
108         {
109           char * p = ly_find_tfm (name.ch_C());
110           if (p)
111             path = p;
112         }
113
114       if (path.empty_b())
115         path = search_path_.find (name  + ".tfm");
116       if (path.empty_b())
117         return 0;
118
119       if (verbose_global_b)
120         progress_indication ("[" + path);
121       SCM tfm = Tex_font_metric::make_tfm (path);
122       if (verbose_global_b)
123         progress_indication ("]");
124
125       Font_metric *fm = unsmob_metrics (tfm);
126       fm->name_ = sname;
127       tfm_p_dict_->set (sname, tfm);
128     }
129     
130   return
131     dynamic_cast<Tex_font_metric*> (unsmob_metrics (tfm_p_dict_->get(sname)));
132 }
133
134
135 Font_metric *
136 All_font_metrics::find_font (String name)
137 {
138   Font_metric * f=0;
139
140   f= find_afm (name);
141   if (f)
142     return f;
143
144   f = find_tfm (name);
145   if (f)
146     return f;
147
148   warning (_f ("can't find font: `%s'", name.ch_C ()));
149   warning (_ ("Loading default font"));
150   
151   String def_name = default_font_sz_;
152   SCM l = scm_assoc (ly_str02scm ("default"),
153                      scm_eval2 (ly_symbol2scm ("cmr-alist"), SCM_EOL));
154   
155   if (l != SCM_BOOL_F)
156     def_name = ly_scm2string (gh_cdr (l));
157
158   f= find_afm (def_name);
159   if (f)
160     return f;
161
162   f =  find_tfm (def_name);
163   if (f)
164     return f;
165
166   error (_f ("can't find default font: `%s'", def_name.ch_C ()));
167   error (_f ("(search path: `%s')", search_path_.str ()));
168   error (_ ("Giving up"));
169
170   return 0;
171 }
172
173 SCM
174 All_font_metrics::font_descriptions () const
175 {
176   SCM l[] = {0,0,0};
177
178   l[0] = afm_p_dict_->to_alist ();
179   l[1] = tfm_p_dict_->to_alist ();
180   l[2] = scaled_p_dict_->to_alist ();  
181
182   SCM list = SCM_EOL;
183   for (int i=0; i < 3; i++)
184     {
185       for (SCM s = l[i];  gh_pair_p (s); s = gh_cdr (s))
186         {
187           Font_metric * fm = unsmob_metrics (gh_cdar (s));
188
189           list = gh_cons (fm->description (), list);
190         }
191     }
192   return list;
193 }
194
195
196
197 Font_metric*
198 find_font (String name)
199 {
200   return   all_fonts_global_p->find_font (name);
201 }