]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
a633e685f2d37fb2626b1310264cb724ddcc482a
[lilypond.git] / lily / font-select.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2003--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "dimensions.hh"
21 #include "all-font-metrics.hh"
22 #include "output-def.hh"
23 #include "font-interface.hh"
24 #include "warn.hh"
25 #include "pango-font.hh"
26 #include "main.hh"
27
28 Font_metric *
29 get_font_by_design_size (Output_def *layout, Real requested,
30                          SCM font_vector)
31 {
32   int n = scm_c_vector_length (font_vector);
33   Real size = 1e6;
34   Real last_size = -1e6;
35   int i = 0;
36
37   SCM pango_description_string = SCM_EOL;
38   SCM last_pango_description_string = SCM_EOL;
39   for (; i < n; i++)
40     {
41       SCM entry = scm_c_vector_ref (font_vector, i);
42
43       if (scm_promise_p (entry) == SCM_BOOL_T)
44         {
45           Font_metric *fm = unsmob_metrics (scm_force (entry));
46           size = fm->design_size ();
47         }
48 #if HAVE_PANGO_FT2
49       else if (scm_is_pair (entry)
50                && scm_is_number (scm_car (entry))
51                && scm_is_string (scm_cdr (entry)))
52         {
53           size = scm_to_double (scm_car (entry));
54           pango_description_string
55             = scm_cdr (entry);
56         }
57 #endif
58
59       if (size > requested)
60         break;
61       last_size = size;
62       last_pango_description_string = pango_description_string;
63     }
64
65   if (i == n)
66     i = n - 1;
67   else if (i > 0)
68     {
69       if ((requested / last_size) < (size / requested))
70         {
71           i--;
72           size = last_size;
73           pango_description_string = last_pango_description_string;
74         }
75     }
76
77   Font_metric *fm = 0;
78   if (scm_is_string (pango_description_string))
79     {
80 #if HAVE_PANGO_FT2
81       return find_pango_font (layout,
82                               pango_description_string,
83                               requested / size);
84 #else
85       error ("Trying to retrieve pango font without HAVE_PANGO_FT2.");
86 #endif
87     }
88   else
89     fm = unsmob_metrics (scm_force (scm_c_vector_ref (font_vector, i)));
90
91   return find_scaled_font (layout, fm, requested / size);
92 }
93
94 Font_metric *
95 get_font_by_mag_step (Output_def *layout, Real requested_step,
96                       SCM font_vector, Real default_size)
97 {
98   return get_font_by_design_size (layout, default_size
99                                   * pow (2.0, requested_step / 6.0),
100                                   font_vector);
101 }
102
103 SCM
104 properties_to_font_size_family (SCM fonts, SCM alist_chain)
105 {
106   return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts,
107                      alist_chain);
108 }
109
110 Font_metric *
111 select_encoded_font (Output_def *layout, SCM chain)
112 {
113   SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
114
115   if (!scm_is_string (name))
116     {
117       SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
118       name = properties_to_font_size_family (fonts, chain);
119     }
120
121 #if HAVE_PANGO_FT2
122   if (scm_is_string (name))
123     return select_pango_font (layout, chain);
124   else
125 #endif
126     if (scm_is_true (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 }