]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
release: 1.3.13
[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       
37       *mlog << "[" << path;
38       Adobe_font_metric * afm_p = read_afm_file (path);
39
40       afm_p->name_str_ = name;
41       
42       *mlog << "]" << flush ;
43
44       afm_p_dict_[sname] = afm_p->self_scm_;
45       scm_unprotect_object (afm_p->self_scm_);
46     }
47   
48   return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (afm_p_dict_[sname]));
49 }
50
51 Scaled_font_metric * 
52 All_font_metrics::find_scaled (String nm, int m)
53 {
54   Scaled_font_metric * s=0;
55   String index =  nm + "@" + to_str (m);
56   SCM sname = ly_symbol2scm (index.ch_C ());
57
58   Font_metric *fm =0;
59   if (!scaled_p_dict_.elem_b (sname))
60     {
61       Font_metric *f = find_font (nm);
62       s = new Scaled_font_metric (f, m);
63       scaled_p_dict_[sname] = s->self_scm_;
64       fm =  s;
65       scm_unprotect_object (s->self_scm_);
66     }
67   else
68     fm = unsmob_metrics (scaled_p_dict_[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
84       *mlog << "[" << path;
85       Tex_font_metric   * tfm_p = Tex_font_metric_reader::read_file (path);
86       tfm_p->name_str_ = name;
87
88       *mlog << "]" << flush ;
89
90       tfm_p_dict_[sname] = tfm_p->self_scm_;
91       scm_unprotect_object (tfm_p->self_scm_);      
92     }
93     
94   return
95     dynamic_cast<Tex_font_metric*> (unsmob_metrics (tfm_p_dict_[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   f =  find_tfm (default_font_sz_);
115   if (f)
116     return f;
117   error (_f ("Can't find default font: `%s'", default_font_sz_));
118   error (_f ("(search path: `%s'", search_path_.str ()));
119   error (_ ("Giving up"));
120
121   return 0;
122 }
123
124 SCM
125 All_font_metrics::font_descriptions () const
126 {
127   SCM l[] = {0,0,0};
128
129   l[0] = afm_p_dict_.to_alist ();
130   l[1] = tfm_p_dict_.to_alist ();
131   l[2] = scaled_p_dict_.to_alist ();  
132
133   SCM list = SCM_EOL;
134   for (int i=0; i < 3; i++)
135     {
136       for (SCM s = l[i];  gh_pair_p (s); s = gh_cdr (s))
137         {
138           Font_metric * fm = unsmob_metrics (gh_cdar (s));
139
140           list = gh_cons (fm->description (), list);
141         }
142     }
143   return list;
144 }
145
146 Font_metric *
147 unsmob_metrics( SCM s)
148 {
149   if (SMOB_IS_TYPE_B(Font_metric, s))
150     return SMOB_TO_TYPE(Font_metric, s);
151   else
152     return 0;
153 }
154
155 #include "ly-smobs.icc"
156 IMPLEMENT_SMOBS(Font_metric);