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>
12 #include "all-font-metrics.hh"
13 #include "paper-def.hh"
14 #include "font-interface.hh"
17 LY_DEFINE (ly_paper_get_font, "ly:paper-get-font", 2, 0, 0,
18 (SCM paper, SCM chain),
20 "Return a font metric satisfying the font-qualifiers "
21 "in the alist chain @var{chain}.\n"
22 "(An alist chain is a list of alists, containing grob properties).\n")
24 Paper_def *pap = unsmob_paper (paper);
25 SCM_ASSERT_TYPE (pap, paper, SCM_ARG1, __FUNCTION__, "paper definition");
27 Font_metric *fm = select_font (pap, chain);
28 return fm->self_scm ();
31 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
32 (SCM paper, SCM name),
33 "Return the paper variable @var{name}.")
35 Paper_def *pap = unsmob_paper (paper);
36 SCM_ASSERT_TYPE (pap, paper, SCM_ARG1, __FUNCTION__, "paper definition");
37 return scm_make_real (pap->get_dimension (name));
41 wild_compare (SCM field_val, SCM val)
43 return (val == SCM_BOOL_F || field_val == ly_symbol2scm ("*") || field_val == val);
48 TODO: this triggers a great number of font-loads (feta11 upto
49 parmesan23). We could make a Delayed_load_font_metric for which the
50 design size is specced in advance.
53 get_font_by_design_size (Paper_def* paper, Real requested,
54 SCM font_vector, SCM input_encoding_name)
56 int n = SCM_VECTOR_LENGTH (font_vector);
58 Real last_size = -1e6;
63 SCM entry = SCM_VECTOR_REF (font_vector, i);
64 Font_metric *fm = unsmob_metrics (scm_force (entry));
65 size = fm->design_size ();
78 if ((requested / last_size) < (size / requested))
85 Font_metric *fm = unsmob_metrics (scm_force (SCM_VECTOR_REF (font_vector, i)));
86 return paper->find_scaled_font (fm, requested / size, input_encoding_name);
91 get_font_by_mag_step (Paper_def* paper, Real requested_step,
92 SCM font_vector, Real default_size, SCM input_encoding_name)
94 return get_font_by_design_size (paper,
95 default_size * pow (2.0, requested_step / 6.0),
96 font_vector, input_encoding_name);
102 properties_to_font_size_family (SCM fonts, SCM alist_chain)
104 return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
109 select_encoded_font (Paper_def *paper, SCM chain, SCM encoding_name)
111 SCM name = ly_assoc_chain (ly_symbol2scm ("font-name"), chain);
113 if (!is_pair (name) || !is_string (ly_cdr (name)))
115 SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
116 name = properties_to_font_size_family (fonts, chain);
119 name = ly_cdr (name);
121 if (is_string (name))
123 SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
125 Real rmag = is_pair (mag) ? robust_scm2double (ly_cdr (mag), 1.0) : 1;
127 Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
130 return paper->find_scaled_font (fm, rmag, encoding_name);
132 else if (scm_instance_p (name))
134 SCM base_size = scm_slot_ref (name, ly_symbol2scm ("default-size"));
135 SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
137 SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
139 if (is_pair (font_size))
140 req = ly_scm2double (ly_cdr (font_size));
142 return get_font_by_mag_step (paper, req,
143 vec, ly_scm2double (base_size), encoding_name);
154 select_font (Paper_def *paper, SCM chain)
156 return select_encoded_font (paper, chain, SCM_EOL);