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