]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
*** empty log message ***
[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   String pango_description_string;
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             = ly_scm2string (scm_cdr (entry));
47         }
48 #endif
49       
50       if (size > requested)
51         break;
52       last_size = size;
53     }
54
55   if (i == n)
56     i = n - 1;
57   else if (i > 0)
58     {
59       if ((requested / last_size) < (size / requested))
60         {
61           i--;
62           size = last_size;
63         }
64     }
65   
66   Font_metric *fm = 0;
67   if (pango_description_string != "")
68     {
69 #if HAVE_PANGO_FT2
70       PangoFontDescription *description
71         = pango_font_description_from_string (pango_description_string.to_str0 ());
72       return all_fonts_global->find_pango_font (description,
73                                                 requested / size,
74                                                 output_scale (layout));
75 #else
76       error ("Trying to retrieve pango font without HAVE_PANGO_FT2."); 
77 #endif
78     }
79   else
80     {
81       fm = unsmob_metrics (scm_force (scm_c_vector_ref (font_vector, i)));
82     }
83
84   return find_scaled_font (layout, fm, requested / size);
85 }
86
87 Font_metric *
88 get_font_by_mag_step (Output_def *layout, Real requested_step,
89                       SCM font_vector, Real default_size)
90 {
91   return get_font_by_design_size (layout, default_size
92                                   * pow (2.0, requested_step / 6.0),
93                                   font_vector);
94 }
95
96 SCM
97 properties_to_font_size_family (SCM fonts, SCM alist_chain)
98 {
99   return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts, alist_chain);
100 }
101
102 Font_metric *
103 select_encoded_font (Output_def *layout, SCM chain)
104 {
105   SCM name = ly_chain_assoc (ly_symbol2scm ("font-name"), chain);
106
107   if (!scm_is_pair (name) || !scm_is_string (scm_cdr (name)))
108     {
109       SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
110       name = properties_to_font_size_family (fonts, chain);
111     }
112   else
113     name = scm_cdr (name);
114
115 #if HAVE_PANGO_FT2
116   if (scm_is_string (name)
117       && is_pango_format_global)
118     {
119       return select_pango_font (layout, chain);
120     }
121   else
122 #endif
123     if (scm_is_string (name))
124       {
125         SCM mag = ly_chain_assoc (ly_symbol2scm ("font-magnification"), chain);
126         Real rmag = (scm_is_pair (mag)
127                      ? robust_scm2double (scm_cdr (mag), 1.0)
128                      : 1);
129         Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
130                 
131         return find_scaled_font (layout, fm, rmag);
132       }
133     else if (scm_instance_p (name))
134       {
135         SCM base_size  = scm_slot_ref (name, ly_symbol2scm ("default-size"));
136         SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
137
138         SCM font_size = ly_chain_assoc (ly_symbol2scm ("font-size"), chain);
139         Real req = 0;
140         if (scm_is_pair (font_size))
141           req = scm_to_double (scm_cdr (font_size));
142
143         return get_font_by_mag_step (layout, req, vec,
144                                      scm_to_double (base_size));
145       }
146
147   assert (0);
148   return 0;
149 }
150
151 Font_metric *
152 select_font (Output_def *layout, SCM chain)
153 {
154   return select_encoded_font (layout, chain);
155 }