2 font-select.cc -- implement property -> font_metric routines.
4 source file of the GNU LilyPond music typesetter
6 (c) 2003--2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
13 #include "all-font-metrics.hh"
14 #include "output-def.hh"
15 #include "font-interface.hh"
18 LY_DEFINE (ly_paper_get_font, "ly:paper-get-font", 2, 0, 0,
19 (SCM paper_smob, SCM chain),
21 "Return a font metric satisfying the font-qualifiers "
22 "in the alist chain @var{chain}.\n"
23 "(An alist chain is a list of alists, "
24 "containing grob properties).\n")
26 Output_def *paper = unsmob_output_def (paper_smob);
27 SCM_ASSERT_TYPE (paper, paper_smob, SCM_ARG1,
28 __FUNCTION__, "paper definition");
30 Font_metric *fm = select_font (paper, chain);
31 return fm->self_scm ();
34 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
35 (SCM layout_smob, SCM name),
36 "Return the layout variable @var{name}.")
38 Output_def *layout = unsmob_output_def (layout_smob);
39 SCM_ASSERT_TYPE (layout, layout_smob, SCM_ARG1,
40 __FUNCTION__, "layout definition");
41 return scm_make_real (layout->get_dimension (name));
45 wild_compare (SCM field_val, SCM val)
47 return (val == SCM_BOOL_F
48 || field_val == ly_symbol2scm ("*")
53 TODO: this triggers a great number of font-loads (feta11 upto
54 parmesan23). We could make a Delayed_load_font_metric for which the
55 design size is specced in advance.
58 get_font_by_design_size (Output_def *layout, Real requested,
59 SCM font_vector, SCM input_encoding)
61 int n = SCM_VECTOR_LENGTH (font_vector);
63 Real last_size = -1e6;
68 SCM entry = SCM_VECTOR_REF (font_vector, i);
69 Font_metric *fm = unsmob_metrics (scm_force (entry));
70 size = fm->design_size ();
81 if ((requested / last_size) < (size / requested))
88 Font_metric *fm = unsmob_metrics (scm_force (SCM_VECTOR_REF (font_vector,
90 return find_scaled_font (layout, fm, requested / size, input_encoding);
95 get_font_by_mag_step (Output_def *layout, Real requested_step,
96 SCM font_vector, Real default_size,
99 return get_font_by_design_size (layout, default_size
100 * pow (2.0, requested_step / 6.0),
101 font_vector, input_encoding);
105 properties_to_font_size_family (SCM fonts, SCM alist_chain)
107 return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
111 select_encoded_font (Output_def *layout, SCM chain, SCM input_encoding)
113 SCM name = ly_assoc_chain (ly_symbol2scm ("font-name"), chain);
115 if (!scm_is_pair (name) || !scm_is_string (scm_cdr (name)))
117 SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
118 name = properties_to_font_size_family (fonts, chain);
121 name = scm_cdr (name);
123 if (scm_is_string (name))
125 SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
126 Real rmag = (scm_is_pair (mag)
127 ? robust_scm2double (scm_cdr (mag), 1.0)
129 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
131 return find_scaled_font (layout, fm, rmag, input_encoding);
133 else if (scm_instance_p (name))
135 SCM base_size = scm_slot_ref (name, ly_symbol2scm ("default-size"));
136 SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
138 SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
140 if (scm_is_pair (font_size))
141 req = scm_to_double (scm_cdr (font_size));
143 return get_font_by_mag_step (layout, req, vec, scm_to_double (base_size),
152 select_font (Output_def *layout, SCM chain)
154 return select_encoded_font (layout, chain, SCM_EOL);