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