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 "dimensions.hh"
17 #include "modified-font-metric.hh"
18 #include "open-type-font.hh"
20 #include "virtual-methods.hh"
23 #include "ly-smobs.icc"
26 Font_metric::design_size () const
28 return 1.0 * point_constant;
32 Font_metric::coding_scheme () const
34 return "FontSpecific";
38 Font_metric::find_by_name (String s) const
40 s.substitute_char ('-', "M");
41 int idx = name_to_index (s);
47 expr = scm_list_3 (ly_symbol2scm ("named-glyph"),
49 scm_makfrom0str (s.to_str0 ())
51 b = get_indexed_char (idx);
58 Font_metric::Font_metric ()
60 description_ = SCM_EOL;
65 Font_metric::Font_metric (Font_metric const &)
70 Font_metric::~Font_metric ()
75 Font_metric::count () const
81 Font_metric::get_ascii_char (int) const
83 return Box (Interval (0, 0), Interval (0, 0));
87 Font_metric::get_indexed_char (int k) const
89 return get_ascii_char (k);
93 Font_metric::name_to_index (String) const
99 Font_metric::get_indexed_wxwy (int) const
101 return Offset (0, 0);
105 Font_metric::derived_mark () const
110 Font_metric::mark_smob (SCM s)
112 Font_metric *m = (Font_metric*) SCM_CELL_WORD_1 (s);
114 return m->description_;
118 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
120 Font_metric *m = unsmob_metrics (s);
121 scm_puts ("#<", port);
122 scm_puts (classname (m), port);
123 scm_puts (" ", port);
124 scm_write (m->description_, port);
125 scm_puts (">", port);
131 IMPLEMENT_SMOBS (Font_metric);
132 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
133 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
136 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
138 (SCM font, SCM name),
139 "Return a Stencil from @var{font} for the glyph named @var{name}. "
140 "@var{font} must be available as an AFM file. If the glyph "
141 "is not available, return @code{#f}.")
143 Font_metric *fm = unsmob_metrics (font);
144 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
145 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
147 Stencil m = fm->find_by_name (ly_scm2string (name));
149 /* TODO: make optional argument for default if not found. */
150 return m.smobbed_copy ();
153 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
155 (SCM font, SCM index),
156 "Retrieve a Stencil for the glyph numbered @var{index} "
159 Font_metric *fm = unsmob_metrics (font);
160 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
161 SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
163 return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
166 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
168 (SCM font, SCM name),
169 "Return the index for @var{name} in @var{font}.")
171 Font_metric *fm = unsmob_metrics (font);
172 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
173 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
175 return scm_from_int (fm->name_to_index (ly_scm2string (name)));
178 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
180 (SCM font, SCM index),
181 "Return the character code for @var{index} @var{font}.")
183 Font_metric *fm = unsmob_metrics (font);
184 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
185 SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index");
187 return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
190 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
192 (SCM font, SCM name),
193 "Return the character code for glyph @var{name} in @var{font}.")
195 Font_metric *fm = unsmob_metrics (font);
196 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
197 SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
199 return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
201 return scm_from_unsigned_integer (fm->glyph_name_to_charcode (ly_scm2string (name)));
205 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
207 (SCM font, SCM text),
208 "Given the font metric in @var{font} and the string @var{text}, "
209 "compute the extents of that text in that font. "
210 "The return value is a pair of number-pairs.")
213 Modified_font_metric*fm = dynamic_cast<Modified_font_metric*>
214 (unsmob_metrics (font));
216 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
217 SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
218 Stencil stc (fm->text_stencil (ly_scm2string (text)));
219 return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
220 ly_interval2scm (stc.extent (Y_AXIS)));
223 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
226 "Given the font metric @var{font}, "
227 "return the corresponding file name.")
229 Font_metric *fm = unsmob_metrics (font);
230 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
231 return fm->font_file_name();
235 Font_metric::font_file_name () const
237 return scm_car (description_);
241 Font_metric::font_name () const
243 String s ("unknown");
249 LY_DEFINE (ly_font_name, "ly:font-name",
252 "Given the font metric @var{font}, "
253 "return the corresponding name.")
255 Font_metric *fm = unsmob_metrics (font);
257 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
258 return scm_makfrom0str (fm->font_name().to_str0 ());
261 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
263 "Given the font metric @var{font}, return the "
264 "magnification, relative to the current outputscale.")
266 Font_metric *fm = unsmob_metrics (font);
267 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
268 return scm_cdr (fm->description_);
271 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
273 "Given the font metric @var{font}, return the "
274 "design size, relative to the current outputscale.")
276 Font_metric *fm = unsmob_metrics (font);
277 SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
278 return scm_make_real (fm->design_size ());
284 Font_metric::index_to_ascii (int i) const
290 Font_metric::index_to_charcode (int i) const
292 return (unsigned) index_to_ascii (i);
296 Font_metric::get_ascii_char_stencil (int code) const
298 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (),
300 Box b = get_ascii_char (code);
301 return Stencil (b, at);
305 Font_metric::get_indexed_char_stencil (int code) const
307 int idx = index_to_ascii (code);
308 SCM at = scm_list_3 (ly_symbol2scm ("char"), self_scm (), scm_int2num (idx));
309 Box b = get_indexed_char (code);
310 return Stencil (b, at);
315 get_encoded_index (Font_metric *m, String input_coding, int code)
317 String font_coding = m->coding_scheme ();
318 if (font_coding == input_coding)
320 SCM s = scm_call_3 (ly_lily_module_constant ("encoded-index"),
321 scm_makfrom0str (input_coding.to_str0 ()),
322 scm_makfrom0str (font_coding.to_str0 ()),
324 return scm_to_int (s);
328 Font_metric::attachment_point (String) const
330 return Offset (0, 0);
334 Font_metric::sub_fonts () const
340 Font_metric::text_stencil (String str) const
342 SCM lst = scm_list_3 (ly_symbol2scm ("text"),
344 scm_makfrom0str (str.to_str0 ()));
346 Box b = text_dimension (str);
347 return Stencil (b, lst);
351 Font_metric::text_dimension (String) const
353 return Box (Interval (0,0), Interval (0,0));