lilypond-1.3.67
[lilypond.git] / lily / all-font-metrics.cc
blobf8e2c0cee1ae37ed3c061af5ec6d3c97f769c445
1 /*
2 all-font-metrics.cc -- implement All_font_metrics
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
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"
19 const char * default_font_sz_ = "cmr10";
21 All_font_metrics::All_font_metrics (String path)
23 search_path_.parse_path (path);
26 Adobe_font_metric *
27 All_font_metrics::find_afm (String name)
29 SCM sname = ly_symbol2scm (name.ch_C ());
30 if (!afm_p_dict_.elem_b (sname))
32 String path = name + ".afm";
33 path = search_path_.find (path);
34 if (path.empty_b ())
35 return 0;
37 if (verbose_global_b)
38 progress_indication ("[" + path);
39 Adobe_font_metric * afm_p = read_afm_file (path);
41 afm_p->name_ = ly_symbol2scm (name.ch_C ());
43 if (verbose_global_b)
44 progress_indication ("]");
46 afm_p_dict_.set (sname,afm_p->self_scm_);
49 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (afm_p_dict_.get (sname)));
52 Scaled_font_metric *
53 All_font_metrics::find_scaled (String nm, int m)
55 Scaled_font_metric * s=0;
56 String index = nm + "@" + to_str (m);
57 SCM sname = ly_symbol2scm (index.ch_C ());
59 Font_metric *fm =0;
60 if (!scaled_p_dict_.elem_b (sname))
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;
67 else
68 fm = unsmob_metrics (scaled_p_dict_.get (sname));
70 return dynamic_cast<Scaled_font_metric*> (fm);
73 Tex_font_metric *
74 All_font_metrics::find_tfm (String name)
76 SCM sname = ly_symbol2scm (name.ch_C ());
77 if (!tfm_p_dict_.elem_b (sname))
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( ));
88 if (verbose_global_b)
89 progress_indication ("]");
91 tfm_p_dict_.set (sname, tfm_p->self_scm_);
94 return
95 dynamic_cast<Tex_font_metric*> (unsmob_metrics (tfm_p_dict_.get(sname)));
99 Font_metric *
100 All_font_metrics::find_font (String name)
102 Font_metric * f=0;
103 f = find_tfm (name);
104 if (f)
105 return f;
107 f= find_afm (name);
108 if (f)
109 return f;
111 warning (_f ("can't find font: `%s'", name.ch_C ()));
112 warning (_ ("Loading default font"));
114 String def_name = default_font_sz_;
115 SCM l = scm_assoc (ly_str02scm ("default"),
116 scm_eval (ly_symbol2scm ("cmr-alist")));
118 if (l != SCM_BOOL_F)
119 def_name = ly_scm2string (gh_cdr (l));
121 f = find_tfm (def_name);
122 if (f)
123 return f;
125 f= find_afm (def_name);
126 if (f)
127 return f;
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"));
133 return 0;
137 All_font_metrics::font_descriptions () const
139 SCM l[] = {0,0,0};
141 l[0] = afm_p_dict_.to_alist ();
142 l[1] = tfm_p_dict_.to_alist ();
143 l[2] = scaled_p_dict_.to_alist ();
145 SCM list = SCM_EOL;
146 for (int i=0; i < 3; i++)
148 for (SCM s = l[i]; gh_pair_p (s); s = gh_cdr (s))
150 Font_metric * fm = unsmob_metrics (gh_cdar (s));
152 list = gh_cons (fm->description (), list);
155 return list;
160 Font_metric*
161 find_font (String name)
163 return all_fonts_global_p->find_font (name);