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>
13 #include "all-font-metrics.hh"
18 #include "lily-guile.hh"
19 #include "scm-hash.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.
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 ());
51 if (!afm_p_dict_
->try_retrieve (sname
, &val
))
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 ());
65 if (filename
.is_empty ())
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
,
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 */
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'",
98 s
+= " " + _f ("does not match: `%s'",
99 tfm
->filename_
.to_str0 ());
101 s
+= " TFM: " + to_string ((int) tfm
->info_
.checksum
);
102 s
+= " AFM: " + to_string ((int) afm
->checksum_
);
104 s
+= _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
106 s
+= _ ("Rerun with -V to show font paths.");
108 s
+= _("A script for removing font-files is delivered with the source-code:");
110 s
+= "buildscripts/clean-fonts.sh";
116 return dynamic_cast<Adobe_font_metric
*> (unsmob_metrics (val
));
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 ());
126 if (!tfm_p_dict_
->try_retrieve (sname
, &val
))
130 if (filename
.is_empty ())
132 String p
= kpathsea_find_tfm (name
.to_str0 ());
137 if (filename
.is_empty ())
138 filename
= search_path_
.find (name
+ ".tfm");
139 if (filename
.is_empty ())
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
));
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
);
176 Font_metric
* f
= find_tfm (name
);
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
);
197 f
= find_tfm (def_name
);
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"));
208 All_font_metrics
*all_fonts_global
;
211 LY_DEFINE (ly_font_load
, "ly:font-load", 1, 0, 0,
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 ();