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