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 gh_double2scm (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,
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);
91 get_font_by_mag_step (Paper_def* paper, Real requested_step,
92 SCM font_vector, Real default_size)
94 return get_font_by_design_size (paper,
95 default_size * pow (2.0, requested_step / 6.0),
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_font (Paper_def *paper, SCM chain)
111 SCM name = ly_assoc_chain (ly_symbol2scm ("font-name"), chain);
113 if (!gh_pair_p (name) || !gh_string_p (gh_cdr (name)))
115 SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
116 name = properties_to_font_size_family (fonts, chain);
119 name = gh_cdr (name);
122 if (gh_string_p (name))
124 SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
126 Real rmag = gh_pair_p (mag) ? robust_scm2double (gh_cdr (mag), 1.0) : 1;
128 Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
131 return paper->find_scaled_font (fm, rmag);
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 (gh_pair_p (font_size))
141 req = gh_scm2double (ly_cdr (font_size));
143 return get_font_by_mag_step (paper, req,
144 vec, gh_scm2double (base_size));