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>
11 #include "dimensions.hh"
12 #include "all-font-metrics.hh"
13 #include "output-def.hh"
14 #include "font-interface.hh"
16 #include "pango-font.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 get_font_by_design_size (Output_def *layout, Real requested,
56 int n = SCM_VECTOR_LENGTH (font_vector);
58 Real last_size = -1e6;
61 String pango_description_string;
64 SCM entry = SCM_VECTOR_REF (font_vector, i);
66 if (scm_promise_p (entry) == SCM_BOOL_T)
68 Font_metric *fm = unsmob_metrics (scm_force (entry));
69 size = fm->design_size ();
72 else if (scm_is_pair (entry)
73 && scm_is_number (scm_car (entry))
74 && scm_is_string (scm_cdr (entry)))
76 size = scm_to_double (scm_car (entry));
77 pango_description_string
78 = ly_scm2string (scm_cdr (entry));
91 if ((requested / last_size) < (size / requested))
99 if (pango_description_string != "")
102 PangoFontDescription *description
103 = pango_font_description_from_string (pango_description_string.to_str0 ());
104 fm = all_fonts_global->find_pango_font (description);
106 error ("Trying to retrieve pango font without HAVE_PANGO_FT2.");
111 fm = unsmob_metrics (scm_force (SCM_VECTOR_REF (font_vector, i)));
114 return find_scaled_font (layout, fm, requested / size);
118 get_font_by_mag_step (Output_def *layout, Real requested_step,
119 SCM font_vector, Real default_size)
121 return get_font_by_design_size (layout, default_size
122 * pow (2.0, requested_step / 6.0),
127 properties_to_font_size_family (SCM fonts, SCM alist_chain)
129 return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts, alist_chain);
133 select_encoded_font (Output_def *layout, SCM chain)
135 SCM name = ly_chain_assoc (ly_symbol2scm ("font-name"), chain);
137 if (!scm_is_pair (name) || !scm_is_string (scm_cdr (name)))
139 SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
140 name = properties_to_font_size_family (fonts, chain);
143 name = scm_cdr (name);
145 if (scm_is_string (name))
147 SCM mag = ly_chain_assoc (ly_symbol2scm ("font-magnification"), chain);
148 Real rmag = (scm_is_pair (mag)
149 ? robust_scm2double (scm_cdr (mag), 1.0)
151 Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
153 return find_scaled_font (layout, fm, rmag);
155 else if (scm_instance_p (name))
157 SCM base_size = scm_slot_ref (name, ly_symbol2scm ("default-size"));
158 SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
160 SCM font_size = ly_chain_assoc (ly_symbol2scm ("font-size"), chain);
162 if (scm_is_pair (font_size))
163 req = scm_to_double (scm_cdr (font_size));
165 return get_font_by_mag_step (layout, req, vec,
166 scm_to_double (base_size));
174 select_font (Output_def *layout, SCM chain)
176 return select_encoded_font (layout, chain);