2 font-metric.cc -- implement Font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
11 #include "font-metric.hh"
16 #include "modified-font-metric.hh"
17 #include "virtual-methods.hh"
21 #include "ly-smobs.icc"
24 Font_metric::design_size () const
30 Font_metric::coding_scheme () const
32 return "FontSpecific";
36 Font_metric::find_by_name (String s) const
38 s.substitute_char ('-', "M");
39 int idx = name_to_index (s);
45 expr = scm_list_3 (ly_symbol2scm ("char"),
47 scm_int2num (index_to_ascii (idx)));
48 b = get_indexed_char (idx);
55 Font_metric::Font_metric ()
57 description_ = SCM_EOL;
62 Font_metric::Font_metric (Font_metric const &)
67 Font_metric::~Font_metric ()
72 Font_metric::count () const
78 Font_metric::get_ascii_char (int) const
80 return Box (Interval (0, 0), Interval (0, 0));
84 Font_metric::get_indexed_char (int k) const
86 return get_ascii_char (k);
90 Font_metric::name_to_index (String) const
96 Font_metric::get_indexed_wxwy (int) const
102 Font_metric::derived_mark () const
107 Font_metric::mark_smob (SCM s)
109 Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
111 return m->description_;
115 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
117 Font_metric *m = unsmob_metrics (s);
118 scm_puts ("#<", port);
119 scm_puts (classname (m), port);
120 scm_puts (" ", port);
121 scm_write (m->description_, port);
122 scm_puts (">", port);
128 IMPLEMENT_SMOBS (Font_metric);
129 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
130 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
133 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
135 (SCM font, SCM name),
136 "Return a Stencil from @var{font} for the glyph named @var{name}. "
137 "@var{font} must be available as an AFM file. If the glyph "
138 "is not available, return @code{#f}.")
140 Font_metric *fm = unsmob_metrics (font);
141 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
142 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
144 Stencil m = fm->find_by_name (ly_scm2string (name));
146 /* TODO: make optional argument for default if not found. */
147 return m.smobbed_copy ();
150 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
152 (SCM font, SCM index),
153 "Retrieve a Stencil for the glyph numbered @var{index} "
156 Font_metric *fm = unsmob_metrics (font);
157 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
158 SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
160 return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
163 LY_DEFINE (ly_text_dimension,"ly:text-dimension",
165 (SCM font, SCM text),
166 "Given the font metric in @var{font} and the string @var{text}, "
167 "compute the extents of that text in that font. "
168 "The return value is a pair of number-pairs.")
171 Modified_font_metric*fm = dynamic_cast<Modified_font_metric*>
172 (unsmob_metrics (font));
173 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
174 SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
176 b = fm->text_dimension (ly_scm2string (text));
178 return scm_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
181 LY_DEFINE (ly_font_file_name,"ly:font-file-name",
184 "Given the font metric @var{font}, "
185 "return the corresponding file name.")
187 Font_metric *fm = unsmob_metrics (font);
188 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
189 return scm_car (fm->description_);
195 LY_DEFINE (ly_font_name,"ly:font-name",
198 "Given the font metric @var{font}, "
199 "return the corresponding name.")
201 Font_metric *fm = unsmob_metrics (font);
203 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
206 if (Modified_font_metric* mfm = dynamic_cast<Modified_font_metric*> (fm))
207 return ly_font_name (mfm->original_font ()->self_scm ());
208 else if (Adobe_font_metric* afm = dynamic_cast<Adobe_font_metric*> (fm))
210 return scm_makfrom0str (afm->font_info_->gfi->fontName);
218 LY_DEFINE (ly_font_magnification,"ly:font-magnification", 1 , 0, 0,
220 "Given the font metric @var{font}, return the "
221 "magnification, relative to the current outputscale.")
223 Font_metric *fm = unsmob_metrics (font);
224 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
225 return scm_cdr (fm->description_);
228 LY_DEFINE (ly_font_design_size,"ly:font-design-size", 1 , 0, 0,
230 "Given the font metric @var{font}, return the "
231 "design size, relative to the current outputscale.")
233 Font_metric *fm = unsmob_metrics (font);
234 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
235 return scm_make_real (fm->design_size ());
241 Font_metric::index_to_ascii (int i) const
247 Font_metric::get_ascii_char_stencil (int code) const
249 SCM at = scm_list_3 (ly_symbol2scm ("char"),
252 Box b = get_ascii_char (code);
253 return Stencil (b, at);
257 Font_metric::get_indexed_char_stencil (int code) const
259 int idx = index_to_ascii (code);
260 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (idx));
261 Box b = get_indexed_char (code);
262 return Stencil (b, at);
267 get_encoded_index (Font_metric *m, String input_coding, int code)
269 String font_coding = m->coding_scheme ();
270 if (font_coding == input_coding)
272 SCM s = scm_call_3 (ly_scheme_function ("encoded-index"),
273 scm_makfrom0str (input_coding.to_str0 ()),
274 scm_makfrom0str (font_coding.to_str0 ()),
276 return scm_to_int (s);