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