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))
53 #if (KPATHSEA && HAVE_KPSE_FIND_FILE)
56 char * p = kpse_find_file (name.ch_C(), kpse_afm_format, true);
63 path = search_path_.find (name + ".afm");
68 progress_indication ("[" + path);
69 SCM afm = read_afm_file (path);
71 unsmob_metrics (afm)->name_ = sname;
74 progress_indication ("]");
76 afm_p_dict_->set (sname,afm);
79 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (afm_p_dict_->get (sname)));
83 All_font_metrics::find_scaled (String nm, int m)
85 String index = nm + "@" + to_str (m);
86 SCM sname = ly_symbol2scm (index.ch_C ());
89 if (!scaled_p_dict_->elem_b (sname))
91 Font_metric *f = find_font (nm);
92 SCM s = Scaled_font_metric::make_scaled_font_metric (f, m);
93 scaled_p_dict_->set (sname, s);
94 fm = unsmob_metrics (s);
97 fm = unsmob_metrics (scaled_p_dict_->get (sname));
99 return dynamic_cast<Scaled_font_metric*> (fm);
103 All_font_metrics::find_tfm (String name)
105 SCM sname = ly_symbol2scm (name.ch_C ());
106 if (!tfm_p_dict_->elem_b (sname))
110 //#if KPATHSEA && HAVE_KPSE_FIND_TFM -- urg: a macro
111 #if KPATHSEA && HAVE_KPSE_FIND_FILE
114 char * p = kpse_find_tfm (name.ch_C());
119 path = search_path_.find (name + ".tfm");
123 if (verbose_global_b)
124 progress_indication ("[" + path);
125 SCM tfm = Tex_font_metric::make_tfm (path);
126 if (verbose_global_b)
127 progress_indication ("]");
129 Font_metric *fm = unsmob_metrics (tfm);
131 tfm_p_dict_->set (sname, tfm);
135 dynamic_cast<Tex_font_metric*> (unsmob_metrics (tfm_p_dict_->get(sname)));
140 All_font_metrics::find_font (String name)
151 warning (_f ("can't find font: `%s'", name.ch_C ()));
152 warning (_ ("Loading default font"));
154 String def_name = default_font_sz_;
155 SCM l = scm_assoc (ly_str02scm ("default"),
156 scm_eval (ly_symbol2scm ("cmr-alist")));
159 def_name = ly_scm2string (gh_cdr (l));
161 f = find_tfm (def_name);
165 f= find_afm (def_name);
169 error (_f ("can't find default font: `%s'", def_name.ch_C ()));
170 error (_f ("(search path: `%s')", search_path_.str ()));
171 error (_ ("Giving up"));
177 All_font_metrics::font_descriptions () const
181 l[0] = afm_p_dict_->to_alist ();
182 l[1] = tfm_p_dict_->to_alist ();
183 l[2] = scaled_p_dict_->to_alist ();
186 for (int i=0; i < 3; i++)
188 for (SCM s = l[i]; gh_pair_p (s); s = gh_cdr (s))
190 Font_metric * fm = unsmob_metrics (gh_cdar (s));
192 list = gh_cons (fm->description (), list);
201 find_font (String name)
203 return all_fonts_global_p->find_font (name);