Docs: Add new procedure for barNumberVisibility
[lilypond/mpolesky.git] / lily / font-select.cc
blob2de54e395581d652a43128e9b4eeb9f6711769fe
1 /*
2 font-select.cc -- implement property -> font_metric routines.
4 source file of the GNU LilyPond music typesetter
6 (c) 2003--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
10 #include "dimensions.hh"
11 #include "all-font-metrics.hh"
12 #include "output-def.hh"
13 #include "font-interface.hh"
14 #include "warn.hh"
15 #include "pango-font.hh"
16 #include "main.hh"
18 Font_metric *
19 get_font_by_design_size (Output_def *layout, Real requested,
20 SCM font_vector)
22 int n = scm_c_vector_length (font_vector);
23 Real size = 1e6;
24 Real last_size = -1e6;
25 int i = 0;
27 SCM pango_description_string = SCM_EOL;
28 SCM last_pango_description_string = SCM_EOL;
29 for (; i < n; i++)
31 SCM entry = scm_c_vector_ref (font_vector, i);
33 if (scm_promise_p (entry) == SCM_BOOL_T)
35 Font_metric *fm = unsmob_metrics (scm_force (entry));
36 size = fm->design_size ();
38 #if HAVE_PANGO_FT2
39 else if (scm_is_pair (entry)
40 && scm_is_number (scm_car (entry))
41 && scm_is_string (scm_cdr (entry)))
43 size = scm_to_double (scm_car (entry));
44 pango_description_string
45 = scm_cdr (entry);
47 #endif
49 if (size > requested)
50 break;
51 last_size = size;
52 last_pango_description_string = pango_description_string;
55 if (i == n)
56 i = n - 1;
57 else if (i > 0)
59 if ((requested / last_size) < (size / requested))
61 i--;
62 size = last_size;
63 pango_description_string = last_pango_description_string;
67 Font_metric *fm = 0;
68 if (scm_is_string (pango_description_string))
70 #if HAVE_PANGO_FT2
71 return find_pango_font (layout,
72 pango_description_string,
73 requested / size);
74 #else
75 error ("Trying to retrieve pango font without HAVE_PANGO_FT2.");
76 #endif
78 else
79 fm = unsmob_metrics (scm_force (scm_c_vector_ref (font_vector, i)));
81 return find_scaled_font (layout, fm, requested / size);
84 Font_metric *
85 get_font_by_mag_step (Output_def *layout, Real requested_step,
86 SCM font_vector, Real default_size)
88 return get_font_by_design_size (layout, default_size
89 * pow (2.0, requested_step / 6.0),
90 font_vector);
93 SCM
94 properties_to_font_size_family (SCM fonts, SCM alist_chain)
96 return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts,
97 alist_chain);
100 Font_metric *
101 select_encoded_font (Output_def *layout, SCM chain)
103 SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
105 if (!scm_is_string (name))
107 SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
108 name = properties_to_font_size_family (fonts, chain);
111 #if HAVE_PANGO_FT2
112 if (scm_is_string (name))
113 return select_pango_font (layout, chain);
114 else
115 #endif
116 if (scm_instance_p (name))
118 SCM base_size = scm_slot_ref (name, ly_symbol2scm ("default-size"));
119 SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
121 Real req = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
122 0.0);
124 return get_font_by_mag_step (layout, req, vec,
125 scm_to_double (base_size));
128 assert (0);
129 return 0;
132 Font_metric *
133 select_font (Output_def *layout, SCM chain)
135 return select_encoded_font (layout, chain);