]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
* lily/tie-column.cc (set_manual_tie_configuration): new function.
[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--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include <cmath>
10 using namespace std;
11
12 #include "dimensions.hh"
13 #include "all-font-metrics.hh"
14 #include "output-def.hh"
15 #include "font-interface.hh"
16 #include "warn.hh"
17 #include "pango-font.hh"
18 #include "main.hh"
19
20 Font_metric *
21 get_font_by_design_size (Output_def *layout, Real requested,
22                          SCM font_vector)
23 {
24   int n = scm_c_vector_length (font_vector);
25   Real size = 1e6;
26   Real last_size = -1e6;
27   int i = 0;
28
29   SCM pango_description_string = SCM_EOL;
30   SCM last_pango_description_string = SCM_EOL;
31   for (; i < n; i++)
32     {
33       SCM entry = scm_c_vector_ref (font_vector, i);
34
35       if (scm_promise_p (entry) == SCM_BOOL_T)
36         {
37           Font_metric *fm = unsmob_metrics (scm_force (entry));
38           size = fm->design_size ();
39         }
40 #if HAVE_PANGO_FT2
41       else if (scm_is_pair (entry)
42                && scm_is_number (scm_car (entry))
43                && scm_is_string (scm_cdr (entry)))
44         {
45           size = scm_to_double (scm_car (entry));
46           pango_description_string
47             = scm_cdr (entry);
48         }
49 #endif
50
51       if (size > requested)
52         break;
53       last_size = size;
54       last_pango_description_string = pango_description_string;
55     }
56
57   if (i == n)
58     i = n - 1;
59   else if (i > 0)
60     {
61       if ((requested / last_size) < (size / requested))
62         {
63           i--;
64           size = last_size;
65           pango_description_string = last_pango_description_string;
66         }
67     }
68
69   Font_metric *fm = 0;
70   if (scm_is_string (pango_description_string))
71     {
72 #if HAVE_PANGO_FT2
73       return find_pango_font (layout,
74                               pango_description_string,
75                               requested / size);
76 #else
77       error ("Trying to retrieve pango font without HAVE_PANGO_FT2.");
78 #endif
79     }
80   else
81     fm = unsmob_metrics (scm_force (scm_c_vector_ref (font_vector, i)));
82
83   return find_scaled_font (layout, fm, requested / size);
84 }
85
86 Font_metric *
87 get_font_by_mag_step (Output_def *layout, Real requested_step,
88                       SCM font_vector, Real default_size)
89 {
90   return get_font_by_design_size (layout, default_size
91                                   * pow (2.0, requested_step / 6.0),
92                                   font_vector);
93 }
94
95 SCM
96 properties_to_font_size_family (SCM fonts, SCM alist_chain)
97 {
98   return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts,
99                      alist_chain);
100 }
101
102 Font_metric *
103 select_encoded_font (Output_def *layout, SCM chain)
104 {
105   SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
106
107   if (!scm_is_string (name))
108     {
109       SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
110       name = properties_to_font_size_family (fonts, chain);
111     }
112
113 #if HAVE_PANGO_FT2
114   if (scm_is_string (name)
115       && is_pango_format_global)
116     return select_pango_font (layout, chain);
117   else
118 #endif
119     if (scm_is_string (name))
120       {
121         Real rmag
122           = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-magnification"), chain, SCM_BOOL_F),
123                                1.0);
124         Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
125
126         return find_scaled_font (layout, fm, rmag);
127       }
128     else if (scm_instance_p (name))
129       {
130         SCM base_size = scm_slot_ref (name, ly_symbol2scm ("default-size"));
131         SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
132
133         Real req = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
134                                       0.0);
135
136         return get_font_by_mag_step (layout, req, vec,
137                                      scm_to_double (base_size));
138       }
139
140   assert (0);
141   return 0;
142 }
143
144 Font_metric *
145 select_font (Output_def *layout, SCM chain)
146 {
147   return select_encoded_font (layout, chain);
148 }