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