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