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"
17 #include "lily-guile.hh"
18 #include "scm-hash.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.
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 ());
50 if (!afm_p_dict_
->try_retrieve (sname
, &val
))
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 ());
64 if (filename
.is_empty ())
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
,
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 */
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'",
97 s
+= " " + _f ("does not match: `%s'",
98 tfm
->filename_
.to_str0 ());
100 s
+= " TFM: " + to_string ((int) tfm
->info_
.checksum
);
101 s
+= " AFM: " + to_string ((int) afm
->checksum_
);
103 s
+= _ ("Rebuild all .afm files, and remove all .pk and .tfm files.");
105 s
+= _ ("Rerun with -V to show font paths.");
107 s
+= _("A script for removing font-files is delivered with the source-code:");
109 s
+= "buildscripts/clean-fonts.sh";
115 return dynamic_cast<Adobe_font_metric
*> (unsmob_metrics (val
));
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 ());
125 if (!tfm_p_dict_
->try_retrieve (sname
, &val
))
129 if (filename
.is_empty ())
131 String p
= kpathsea_find_tfm (name
.to_str0 ());
136 if (filename
.is_empty ())
137 filename
= search_path_
.find (name
+ ".tfm");
138 if (filename
.is_empty ())
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
));
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
);
175 Font_metric
* f
= find_tfm (name
);
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
);
196 f
= find_tfm (def_name
);
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"));
207 All_font_metrics
*all_fonts_global
;
210 LY_DEFINE (ly_font_load
, "ly:font-load", 1, 0, 0,
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 ();