]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
(Invoking lilypond-book):
[lilypond.git] / lily / font-select.cc
1 /*
2   font-select.cc -- implement property -> font_metric routines.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2003--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9
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"
17
18 Font_metric *
19 get_font_by_design_size (Output_def *layout, Real requested,
20                          SCM font_vector)
21 {
22   int n = scm_c_vector_length (font_vector);
23   Real size = 1e6;
24   Real last_size = -1e6;
25   int i = 0;
26
27   SCM pango_description_string = SCM_EOL;
28   SCM last_pango_description_string = SCM_EOL;
29   for (; i < n; i++)
30     {
31       SCM entry = scm_c_vector_ref (font_vector, i);
32
33       if (scm_promise_p (entry) == SCM_BOOL_T)
34         {
35           Font_metric *fm = unsmob_metrics (scm_force (entry));
36           size = fm->design_size ();
37         }
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)))
42         {
43           size = scm_to_double (scm_car (entry));
44           pango_description_string
45             = scm_cdr (entry);
46         }
47 #endif
48
49       if (size > requested)
50         break;
51       last_size = size;
52       last_pango_description_string = pango_description_string;
53     }
54
55   if (i == n)
56     i = n - 1;
57   else if (i > 0)
58     {
59       if ((requested / last_size) < (size / requested))
60         {
61           i--;
62           size = last_size;
63           pango_description_string = last_pango_description_string;
64         }
65     }
66
67   Font_metric *fm = 0;
68   if (scm_is_string (pango_description_string))
69     {
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
77     }
78   else
79     fm = unsmob_metrics (scm_force (scm_c_vector_ref (font_vector, i)));
80
81   return find_scaled_font (layout, fm, requested / size);
82 }
83
84 Font_metric *
85 get_font_by_mag_step (Output_def *layout, Real requested_step,
86                       SCM font_vector, Real default_size)
87 {
88   return get_font_by_design_size (layout, default_size
89                                   * pow (2.0, requested_step / 6.0),
90                                   font_vector);
91 }
92
93 SCM
94 properties_to_font_size_family (SCM fonts, SCM alist_chain)
95 {
96   return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts,
97                      alist_chain);
98 }
99
100 Font_metric *
101 select_encoded_font (Output_def *layout, SCM chain)
102 {
103   SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
104
105   if (!scm_is_string (name))
106     {
107       SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
108       name = properties_to_font_size_family (fonts, chain);
109     }
110
111 #if HAVE_PANGO_FT2
112   if (scm_is_string (name)
113       && is_pango_format_global)
114     return select_pango_font (layout, chain);
115   else
116 #endif
117     if (scm_is_string (name))
118       {
119         Real rmag
120           = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-magnification"), chain, SCM_BOOL_F),
121                                1.0);
122         Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
123
124         return find_scaled_font (layout, fm, rmag);
125       }
126     else if (scm_instance_p (name))
127       {
128         SCM base_size = scm_slot_ref (name, ly_symbol2scm ("default-size"));
129         SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
130
131         Real req = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
132                                       0.0);
133
134         return get_font_by_mag_step (layout, req, vec,
135                                      scm_to_double (base_size));
136       }
137
138   assert (0);
139   return 0;
140 }
141
142 Font_metric *
143 select_font (Output_def *layout, SCM chain)
144 {
145   return select_encoded_font (layout, chain);
146 }