* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / all-font-metrics.cc
blobf878338bcb3ec93263559f859db9931cbecc8a52
1 /*
3 all-font-metrics.cc -- implement All_font_metrics
5 source file of the GNU LilyPond music typesetter
7 (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
11 #include "config.h"
12 #include "main.hh"
13 #include "all-font-metrics.hh"
15 #include "warn.hh"
16 #include "afm.hh"
17 #include "tfm.hh"
18 #include "lily-guile.hh"
19 #include "scm-hash.hh"
20 #include "kpath.hh"
22 static const char *default_font_str0_ = "cmr10";
24 All_font_metrics::All_font_metrics (String path)
26 afm_p_dict_ = new Scheme_hash_table;
27 tfm_p_dict_ = new Scheme_hash_table;
29 search_path_.parse_path (path);
32 All_font_metrics::~All_font_metrics ()
34 scm_gc_unprotect_object (afm_p_dict_->self_scm ());
35 scm_gc_unprotect_object (tfm_p_dict_->self_scm ());
39 TODO: our AFM handling is broken: the units in an AFM file are
40 relative to the design size (1000 units = 1 designsize). Hence we
41 should include design size when generating an AFM metric.
43 ugr: copied from find_tfm.
45 Adobe_font_metric *
46 All_font_metrics::find_afm (String name)
48 SCM sname = ly_symbol2scm (name.to_str0 ());
49 SCM name_string = scm_makfrom0str (name.to_str0 ());
50 SCM val;
51 if (!afm_p_dict_->try_retrieve (sname, &val))
53 String filename;
55 if (filename.is_empty ())
56 filename = search_path_.find (name + ".afm");
58 if (filename.is_empty ())
60 String p = kpathsea_find_afm (name.to_str0 ());
61 if (p.length ())
62 filename = p;
65 if (filename.is_empty ())
66 return 0;
68 if (verbose_global_b)
69 progress_indication ("[" + filename);
70 val = read_afm_file (filename);
71 unsmob_metrics (val)->filename_ = filename;
73 unsmob_metrics (val)->description_ = scm_cons (name_string,
74 scm_make_real (1.0));
76 if (verbose_global_b)
77 progress_indication ("]");
79 afm_p_dict_->set (sname, val);
80 scm_gc_unprotect_object (val);
82 Adobe_font_metric *afm
83 = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
85 /* Only check checksums if there is one. We take the risk that
86 some file has valid checksum 0 */
87 if (afm->checksum_)
89 Tex_font_metric * tfm = find_tfm (name);
91 /* FIXME: better warning message
92 (maybe check upon startup for feta16.afm, feta16.tfm?) */
93 if (tfm && tfm->info_.checksum != afm->checksum_)
95 // FIXME: broken sentence
96 String s = _f ("checksum mismatch for font file: `%s'",
97 filename.to_str0 ());
98 s += " " + _f ("does not match: `%s'",
99 tfm->filename_.to_str0 ());
100 s += "\n";
101 s += " TFM: " + to_string ((int) tfm->info_.checksum);
102 s += " AFM: " + to_string ((int) afm->checksum_);
103 s += "\n";
104 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
105 s += "\n";
106 s += _ ("Rerun with -V to show font paths.");
107 s += "\n";
108 s += _("A script for removing font-files is delivered with the source-code:");
109 s += "\n";
110 s += "buildscripts/clean-fonts.sh";
111 error (s);
116 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
120 Tex_font_metric*
121 All_font_metrics::find_tfm (String name)
123 SCM sname = ly_symbol2scm (name.to_str0 ());
124 SCM name_string = scm_makfrom0str (name.to_str0 ());
125 SCM val;
126 if (!tfm_p_dict_->try_retrieve (sname, &val))
128 String filename;
130 if (filename.is_empty ())
132 String p = kpathsea_find_tfm (name.to_str0 ());
133 if (p.length ())
134 filename = p;
137 if (filename.is_empty ())
138 filename = search_path_.find (name + ".tfm");
139 if (filename.is_empty ())
140 return 0;
142 if (verbose_global_b)
143 progress_indication ("[" + filename);
145 val = Tex_font_metric::make_tfm (filename);
147 if (verbose_global_b)
148 progress_indication ("]");
150 unsmob_metrics (val)->filename_ = filename;
151 unsmob_metrics (val)->description_ = scm_cons (name_string,
152 scm_make_real (1.0));
153 tfm_p_dict_->set (sname, val);
154 scm_gc_unprotect_object (val);
157 return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
160 Font_metric*
161 All_font_metrics::find_font (String name)
163 if ((name.left_string (4) == "feta") ||
164 (name.left_string (8) == "parmesan"))
166 Font_metric *f = find_afm (name);
167 if (f)
168 return f;
169 else
170 f =find_tfm (name);
171 if (f)
172 return f ;
174 else
176 Font_metric * f = find_tfm (name);
177 if (f)
178 return f;
179 else
180 f = find_afm (name);
181 if (f)
182 return f;
185 warning (_f ("can't find font: `%s'", name.to_str0 ()));
186 warning (_ ("Loading default font"));
188 String def_name = default_font_str0_;
191 we're in emergency recovery mode here anyway, so don't try to do
192 anything smart that runs the risk of failing. */
193 Font_metric* f= find_afm (def_name);
194 if (f)
195 return f;
197 f = find_tfm (def_name);
198 if (f)
199 return f;
201 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
202 error (_f ("(search path: `%s')", search_path_.to_string ()));
203 error (_ ("Giving up"));
205 return 0;
208 All_font_metrics *all_fonts_global;
211 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
212 (SCM name),
213 "Load the font @var{name}. ")
215 SCM_ASSERT_TYPE (ly_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
217 Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
219 return fm->self_scm ();