]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
* lily/font-select.cc (get_font_by_design_size): revert
[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
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     {
82       fm = unsmob_metrics (scm_force (scm_c_vector_ref (font_vector, i)));
83     }
84
85   return find_scaled_font (layout, fm, requested / size);
86 }
87
88 Font_metric *
89 get_font_by_mag_step (Output_def *layout, Real requested_step,
90                       SCM font_vector, Real default_size)
91 {
92   return get_font_by_design_size (layout, default_size
93                                   * pow (2.0, requested_step / 6.0),
94                                   font_vector);
95 }
96
97 SCM
98 properties_to_font_size_family (SCM fonts, SCM alist_chain)
99 {
100   return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts, alist_chain);
101 }
102
103 Font_metric *
104 select_encoded_font (Output_def *layout, SCM chain)
105 {
106   SCM name = ly_chain_assoc (ly_symbol2scm ("font-name"), chain);
107
108   if (!scm_is_pair (name) || !scm_is_string (scm_cdr (name)))
109     {
110       SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
111       name = properties_to_font_size_family (fonts, chain);
112     }
113   else
114     name = scm_cdr (name);
115
116 #if HAVE_PANGO_FT2
117   if (scm_is_string (name)
118       && is_pango_format_global)
119     {
120       return select_pango_font (layout, chain);
121     }
122   else
123 #endif
124     if (scm_is_string (name))
125       {
126         SCM mag = ly_chain_assoc (ly_symbol2scm ("font-magnification"), chain);
127         Real rmag = (scm_is_pair (mag)
128                      ? robust_scm2double (scm_cdr (mag), 1.0)
129                      : 1);
130         Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
131                 
132         return find_scaled_font (layout, fm, rmag);
133       }
134     else if (scm_instance_p (name))
135       {
136         SCM base_size  = scm_slot_ref (name, ly_symbol2scm ("default-size"));
137         SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
138
139         SCM font_size = ly_chain_assoc (ly_symbol2scm ("font-size"), chain);
140         Real req = 0;
141         if (scm_is_pair (font_size))
142           req = scm_to_double (scm_cdr (font_size));
143
144         return get_font_by_mag_step (layout, req, vec,
145                                      scm_to_double (base_size));
146       }
147
148   assert (0);
149   return 0;
150 }
151
152 Font_metric *
153 select_font (Output_def *layout, SCM chain)
154 {
155   return select_encoded_font (layout, chain);
156 }