]> git.donarmstrong.com Git - lilypond.git/blob - lily/all-font-metrics.cc
release: 1.3.6
[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 "dictionary-iter.hh"
17 #include "lily-guile.hh"
18 #include "tfm-reader.hh"
19
20 const char * default_font_sz_ = "cmr10";
21
22 All_font_metrics::All_font_metrics (String path)
23 {
24   search_path_.parse_path (path);
25 }
26
27 Adobe_font_metric *
28 All_font_metrics::find_afm (String name)
29 {
30   if (!afm_p_dict_.elem_b (name))
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
39         * afm_p = new Adobe_font_metric (read_afm_file (path));
40
41       afm_p->name_str_ = name;
42       
43       *mlog << "]" << flush ;
44
45       afm_p_dict_[name] = afm_p;
46     }
47   return afm_p_dict_[name];  
48 }
49
50 Scaled_font_metric * 
51 All_font_metrics::find_scaled (String nm, int m)
52 {
53   Scaled_font_metric * s=0;
54   String index =  nm + "@" + to_str (m);
55   if (!scaled_p_dict_.elem_b (index))
56     {
57       Font_metric *f = find_font (nm);
58       s = new Scaled_font_metric (f, m);
59       scaled_p_dict_[index] = s;
60       return s;  
61     }
62   else
63     return scaled_p_dict_[index];
64 }
65
66 Tex_font_metric *
67 All_font_metrics::find_tfm (String name)
68 {
69   if (!tfm_p_dict_.elem_b (name))
70     {
71       String path = name  + ".tfm";
72       path = search_path_.find (path);
73       if (path.empty_b ())
74         return 0;
75
76       *mlog << "[" << path;
77       Tex_font_metric   * tfm_p = Tex_font_metric_reader::read_file (path);
78       tfm_p->name_str_ = name;
79
80       *mlog << "]" << flush ;
81
82       tfm_p_dict_[name] = tfm_p;
83     }
84   return tfm_p_dict_[name];  
85 }
86
87
88 Font_metric *
89 All_font_metrics::find_font (String name)
90 {
91   Font_metric * f=0;
92   f = find_tfm (name);
93   if (f)
94     return f;
95
96   f= find_afm (name);
97   if (f)
98     return f;
99
100   warning (_f ("Can't find font: `%s'", name.ch_C ()));
101   warning (_ ("Loading default font"));
102   
103   f =  find_tfm (default_font_sz_);
104   if (f)
105     return f;
106   error (_f ("Can't find default font: `%s'", default_font_sz_));
107   error (_f ("(search path: `%s'", search_path_.str ()));
108   error (_ ("Giving up"));
109 }
110
111 SCM
112 All_font_metrics::font_descriptions () const
113 {
114   SCM l = SCM_EOL;
115   for (Dictionary_iter<Adobe_font_metric*> ai(afm_p_dict_); ai.ok (); ai++)
116     l = gh_cons (ai.val ()->description (), l);
117   for (Dictionary_iter<Tex_font_metric*> ai(tfm_p_dict_); ai.ok (); ai++)
118     l = gh_cons(ai.val ()->description (), l);
119
120   for (Dictionary_iter<Scaled_font_metric*> ai(scaled_p_dict_); ai.ok (); ai++)
121     l = gh_cons (ai.val ()->description (),l);
122   
123   return l;
124 }