* lily/ledger-line-engraver.cc: new file.
[lilypond.git] / lily / all-font-metrics.cc
blob52c4bccae20016b3c646266648842c8026d0cebb
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.hh"
12 #include "main.hh"
13 #include "all-font-metrics.hh"
14 #include "warn.hh"
15 #include "afm.hh"
16 #include "tfm.hh"
17 #include "lily-guile.hh"
18 #include "scm-hash.hh"
19 #include "kpath.hh"
21 static const char *default_font_str0_ = "cmr10";
23 All_font_metrics::All_font_metrics (String path)
25 afm_p_dict_ = new Scheme_hash_table;
26 tfm_p_dict_ = new Scheme_hash_table;
28 search_path_.parse_path (path);
31 All_font_metrics::~All_font_metrics ()
33 scm_gc_unprotect_object (afm_p_dict_->self_scm ());
34 scm_gc_unprotect_object (tfm_p_dict_->self_scm ());
38 TODO: our AFM handling is broken: the units in an AFM file are
39 relative to the design size (1000 units = 1 designsize). Hence we
40 should include design size when generating an AFM metric.
42 ugr: copied from find_tfm.
44 Adobe_font_metric *
45 All_font_metrics::find_afm (String name)
47 SCM sname = ly_symbol2scm (name.to_str0 ());
48 SCM name_string = scm_makfrom0str (name.to_str0 ());
49 SCM val;
50 if (!afm_p_dict_->try_retrieve (sname, &val))
52 String filename;
54 if (filename.is_empty ())
55 filename = search_path_.find (name + ".afm");
57 if (filename.is_empty ())
59 String p = kpathsea_find_afm (name.to_str0 ());
60 if (p.length ())
61 filename = p;
64 if (filename.is_empty ())
65 return 0;
67 if (verbose_global_b)
68 progress_indication ("[" + filename);
69 val = read_afm_file (filename);
70 unsmob_metrics (val)->filename_ = filename;
72 unsmob_metrics (val)->description_ = scm_cons (name_string,
73 scm_make_real (1.0));
75 if (verbose_global_b)
76 progress_indication ("]");
78 afm_p_dict_->set (sname, val);
79 scm_gc_unprotect_object (val);
81 Adobe_font_metric *afm
82 = dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
84 /* Only check checksums if there is one. We take the risk that
85 some file has valid checksum 0 */
86 if (afm->checksum_)
88 Tex_font_metric * tfm = find_tfm (name);
90 /* FIXME: better warning message
91 (maybe check upon startup for feta16.afm, feta16.tfm?) */
92 if (tfm && tfm->info_.checksum != afm->checksum_)
94 // FIXME: broken sentence
95 String s = _f ("checksum mismatch for font file: `%s'",
96 filename.to_str0 ());
97 s += " " + _f ("does not match: `%s'",
98 tfm->filename_.to_str0 ());
99 s += "\n";
100 s += " TFM: " + to_string ((int) tfm->info_.checksum);
101 s += " AFM: " + to_string ((int) afm->checksum_);
102 s += "\n";
103 s += _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
104 s += "\n";
105 s += _ ("Rerun with -V to show font paths.");
106 s += "\n";
107 s += _("A script for removing font-files is delivered with the source-code:");
108 s += "\n";
109 s += "buildscripts/clean-fonts.sh";
110 error (s);
115 return dynamic_cast<Adobe_font_metric*> (unsmob_metrics (val));
119 Tex_font_metric*
120 All_font_metrics::find_tfm (String name)
122 SCM sname = ly_symbol2scm (name.to_str0 ());
123 SCM name_string = scm_makfrom0str (name.to_str0 ());
124 SCM val;
125 if (!tfm_p_dict_->try_retrieve (sname, &val))
127 String filename;
129 if (filename.is_empty ())
131 String p = kpathsea_find_tfm (name.to_str0 ());
132 if (p.length ())
133 filename = p;
136 if (filename.is_empty ())
137 filename = search_path_.find (name + ".tfm");
138 if (filename.is_empty ())
139 return 0;
141 if (verbose_global_b)
142 progress_indication ("[" + filename);
144 val = Tex_font_metric::make_tfm (filename);
146 if (verbose_global_b)
147 progress_indication ("]");
149 unsmob_metrics (val)->filename_ = filename;
150 unsmob_metrics (val)->description_ = scm_cons (name_string,
151 scm_make_real (1.0));
152 tfm_p_dict_->set (sname, val);
153 scm_gc_unprotect_object (val);
156 return dynamic_cast<Tex_font_metric*> (unsmob_metrics (val));
159 Font_metric*
160 All_font_metrics::find_font (String name)
162 if ((name.left_string (4) == "feta") ||
163 (name.left_string (8) == "parmesan"))
165 Font_metric *f = find_afm (name);
166 if (f)
167 return f;
168 else
169 f =find_tfm (name);
170 if (f)
171 return f ;
173 else
175 Font_metric * f = find_tfm (name);
176 if (f)
177 return f;
178 else
179 f = find_afm (name);
180 if (f)
181 return f;
184 warning (_f ("can't find font: `%s'", name.to_str0 ()));
185 warning (_ ("Loading default font"));
187 String def_name = default_font_str0_;
190 we're in emergency recovery mode here anyway, so don't try to do
191 anything smart that runs the risk of failing. */
192 Font_metric* f= find_afm (def_name);
193 if (f)
194 return f;
196 f = find_tfm (def_name);
197 if (f)
198 return f;
200 error (_f ("can't find default font: `%s'", def_name.to_str0 ()));
201 error (_f ("(search path: `%s')", search_path_.to_string ()));
202 error (_ ("Giving up"));
204 return 0;
207 All_font_metrics *all_fonts_global;
210 LY_DEFINE (ly_font_load, "ly:font-load", 1, 0, 0,
211 (SCM name),
212 "Load the font @var{name}. ")
214 SCM_ASSERT_TYPE (ly_c_string_p (name), name, SCM_ARG1, __FUNCTION__, "string");
216 Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
218 return fm->self_scm ();