2 tfm.cc -- implement Tex_font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Jan Nieuwenhuizen <janneke@gnu.org>
8 some code shamelessly copied from GNU fontutils-0.6/tfm/tfm_input.c
13 #include "tfm-reader.hh"
14 #include "string-convert.hh"
16 #include "dimensions.hh"
18 static SCM tex_dimension_hash_tab;
19 static Tex_font_char_metric dummy_static_char_metric;
24 lookup_tex_text_dimension (Font_metric *font,
29 SCM limit = ly_lily_module_constant ("TEX_STRING_HASHLIMIT");
30 String key_str = ly_scm2string (font->font_file_name());
31 int hash_code = scm_to_int (scm_hash (text, limit));
32 key_str = to_string (hash_code) + key_str;
34 SCM val = scm_hash_ref (tex_dimension_hash_tab,
35 scm_makfrom0str (key_str.to_str0 ()),
38 if (scm_is_pair (val))
40 b[X_AXIS][LEFT] = 0.0 PT;
41 b[X_AXIS][RIGHT] = scm_to_double (scm_car (val)) PT;
43 b[Y_AXIS][UP] = scm_to_double (scm_car (val)) PT;
45 b[Y_AXIS][DOWN] = scm_to_double (scm_car (val)) PT;
52 Tex_font_char_metric::Tex_font_char_metric ()
59 italic_correction_ = 0;
63 italic_correction_fix_ = 0;
67 Tex_font_char_metric::dimensions () const
78 Real point_constant = 1 PT;
80 return Box (Interval (0, width_*point_constant ),
81 Interval ((d <? height_)*point_constant,
82 (d >? height_)*point_constant));
85 Tex_font_metric::Tex_font_metric ()
87 encoding_table_ = SCM_EOL;
91 Tex_font_metric::derived_mark () const
93 scm_gc_mark (encoding_table_);
96 Tex_font_char_metric const *
97 Tex_font_metric::find_ascii (int ascii, bool warn) const
99 if (ascii >= 0 && ascii < ascii_to_metric_idx_.size ()
100 && ascii_to_metric_idx_[ascii] >= 0)
101 return & char_metrics_[ascii_to_metric_idx_ [ascii]];
103 warning (_f ("can't find ascii character: %d", ascii));
104 return &dummy_static_char_metric;
107 /* UGH: glyphs need not be consecutive in TFM. */
109 Tex_font_metric::count () const
111 for (int i = ascii_to_metric_idx_.size (); i--;)
112 if (ascii_to_metric_idx_[i] != -1)
118 Tex_font_metric::get_ascii_char (int a) const
120 Box b = find_ascii (a)->dimensions ();
125 Tex_font_metric::make_tfm (String file_name)
127 Tex_font_metric *tfm = new Tex_font_metric;
128 Tex_font_metric_reader reader (file_name);
130 tfm->info_ = reader.info_;
131 tfm->header_ = reader.header_;
132 tfm->char_metrics_ = reader.char_metrics_;
133 tfm->ascii_to_metric_idx_ = reader.ascii_to_metric_idx_;
135 = scm_call_1 (ly_lily_module_constant ("get-coding-table"),
136 scm_makfrom0str (tfm->coding_scheme ().to_str0 ()));
138 return tfm->self_scm ();
142 Tex_font_metric::design_size () const
144 return info_.design_size;
148 Tex_font_metric::coding_scheme () const
150 String scm = info_.coding_scheme;
152 for(int i = 0; i < scm.length (); i++)
160 Tex_font_metric::name_to_index (String s) const
162 SCM sym = ly_symbol2scm (s.to_str0 ());
164 SCM idx = scm_hash_ref (encoding_table_, sym, SCM_BOOL_F);
165 if (ly_c_char_p (idx))
167 return (unsigned char) ly_scm2char (idx);
174 LY_DEFINE(ly_load_text_dimensions, "ly:load-text-dimensions",
176 (SCM dimension_alist),
177 "Load dimensions from TeX in a (KEY . (W H D)) format alist")
179 if (!tex_dimension_hash_tab)
181 tex_dimension_hash_tab =
182 scm_gc_protect_object (scm_make_hash_table (scm_from_int (113)));
185 for (SCM s = dimension_alist;
189 SCM key = scm_caar (s);
190 SCM val = scm_cdar (s);
192 if (scm_hash_ref (tex_dimension_hash_tab, key, SCM_BOOL_F)
195 scm_hash_set_x (tex_dimension_hash_tab, key, val);
199 return SCM_UNSPECIFIED;