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>
12 #include "all-font-metrics.hh"
17 #include "lily-guile.hh"
18 #include "scm-hash.hh"
21 #if HAVE_KPATHSEA_KPATHSEA_H
23 #include <kpathsea/kpathsea.h>
27 const char * default_font_sz_ = "cmr10";
29 All_font_metrics::All_font_metrics (String path)
31 afm_p_dict_ = new Scheme_hash_table;
32 tfm_p_dict_ = new Scheme_hash_table;
33 scaled_p_dict_ = new Scheme_hash_table;
35 search_path_.parse_path (path);
38 All_font_metrics::~All_font_metrics ()
40 scm_unprotect_object (afm_p_dict_->self_scm ());
41 scm_unprotect_object (tfm_p_dict_->self_scm ());
42 scm_unprotect_object (scaled_p_dict_->self_scm ());
46 All_font_metrics::find_afm (String name)
48 SCM sname = ly_symbol2scm (name.ch_C ());
49 if (!afm_p_dict_->elem_b (sname))
54 path = search_path_.find (name + ".afm");
56 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
59 char * p = kpse_find_file (name.ch_C(), kpse_afm_format, true);
69 progress_indication ("[" + path);
70 SCM afm = read_afm_file (path);
72 unsmob_metrics (afm)->name_ = sname;
75 progress_indication ("]");
77 afm_p_dict_->set (sname,afm);
80 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (afm_p_dict_->get (sname)));
84 All_font_metrics::find_scaled (String nm, int m)
86 String index = nm + "@" + to_str (m);
87 SCM sname = ly_symbol2scm (index.ch_C ());
90 if (!scaled_p_dict_->elem_b (sname))
92 Font_metric *f = find_font (nm);
93 SCM s = Scaled_font_metric::make_scaled_font_metric (f, m);
94 scaled_p_dict_->set (sname, s);
95 fm = unsmob_metrics (s);
98 fm = unsmob_metrics (scaled_p_dict_->get (sname));
100 return dynamic_cast<Scaled_font_metric*> (fm);
104 All_font_metrics::find_tfm (String name)
106 SCM sname = ly_symbol2scm (name.ch_C ());
107 if (!tfm_p_dict_->elem_b (sname))
111 //#if KPATHSEA && HAVE_KPSE_FIND_TFM -- urg: a macro
112 #if KPATHSEA && HAVE_KPSE_FIND_FILE
115 char * p = kpse_find_tfm (name.ch_C());
121 path = search_path_.find (name + ".tfm");
125 if (verbose_global_b)
126 progress_indication ("[" + path);
127 SCM tfm = Tex_font_metric::make_tfm (path);
128 if (verbose_global_b)
129 progress_indication ("]");
131 Font_metric *fm = unsmob_metrics (tfm);
133 tfm_p_dict_->set (sname, tfm);
137 dynamic_cast<Tex_font_metric*> (unsmob_metrics (tfm_p_dict_->get(sname)));
142 All_font_metrics::find_font (String name)
154 warning (_f ("can't find font: `%s'", name.ch_C ()));
155 warning (_ ("Loading default font"));
157 String def_name = default_font_sz_;
158 SCM l = scm_assoc (ly_str02scm ("default"),
159 scm_eval2 (ly_symbol2scm ("cmr-alist"), SCM_EOL));
162 def_name = ly_scm2string (gh_cdr (l));
164 f= find_afm (def_name);
168 f = find_tfm (def_name);
172 error (_f ("can't find default font: `%s'", def_name.ch_C ()));
173 error (_f ("(search path: `%s')", search_path_.str ()));
174 error (_ ("Giving up"));
180 All_font_metrics::font_descriptions () const
184 l[0] = afm_p_dict_->to_alist ();
185 l[1] = tfm_p_dict_->to_alist ();
186 l[2] = scaled_p_dict_->to_alist ();
189 for (int i=0; i < 3; i++)
191 for (SCM s = l[i]; gh_pair_p (s); s = gh_cdr (s))
193 Font_metric * fm = unsmob_metrics (gh_cdar (s));
195 list = gh_cons (fm->description (), list);
204 find_font (String name)
206 return all_fonts_global_p->find_font (name);