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