]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
(print): remove neutral position.
[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
10 #include <math.h>
11
12 #include "paper-def.hh"
13 #include "font-interface.hh"
14 #include "warn.hh"
15
16 LY_DEFINE (ly_paper_get_font, "ly:paper-get-font", 2, 0, 0,
17            (SCM paper, SCM chain),
18
19            "Return a font metric satisfying the font-qualifiers "
20            "in the alist chain @var{chain}.\n"
21            "(An alist chain is a list of alists, containing grob properties).\n")
22 {
23   Paper_def *pap = unsmob_paper (paper);
24   SCM_ASSERT_TYPE (pap, paper, SCM_ARG1, __FUNCTION__, "paper definition");
25   
26   Font_metric *fm = select_font (pap, chain);
27   return fm->self_scm ();
28 }
29
30 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
31            (SCM paper, SCM name),
32            "Return the paper variable @var{name}.")
33 {
34   Paper_def *pap = unsmob_paper (paper);
35   SCM_ASSERT_TYPE (pap, paper, SCM_ARG1, __FUNCTION__, "paper definition");
36   return gh_double2scm (pap->get_realvar (name));
37 }
38
39 bool
40 wild_compare (SCM field_val, SCM val)
41 {
42   return (val == SCM_BOOL_F || field_val == ly_symbol2scm ("*") || field_val == val);
43 }
44 Font_metric*
45 get_font_by_design_size (Paper_def* paper, Real requested,
46                          SCM font_vector)
47 {
48   int n = SCM_VECTOR_LENGTH (font_vector);
49   Real size = 1e6;
50   Real last_size = -1e6;
51   int i = 0;
52   
53   for (; i < n; i++)
54     {
55       size = gh_scm2double (gh_car (SCM_VECTOR_REF (font_vector, i)));
56       if (size > requested)
57         break ;
58       last_size = size; 
59     }
60
61   if (i == n)
62     {
63       i = n-1;
64     }
65   else if (i > 0)
66     {
67       if ((requested / last_size) < (size / requested))
68         {
69           i -- ;
70           size = last_size;
71         }
72     }
73   
74   return paper->find_font (gh_cdr (SCM_VECTOR_REF (font_vector, i)),
75                            requested / size);
76 }
77
78
79 Font_metric*
80 get_font_by_mag_step (Paper_def* paper, Real requested_step,
81                       SCM font_vector, Real default_size)
82 {
83   return get_font_by_design_size (paper,
84                                   default_size * pow (2.0, requested_step / 6.0),
85                                   font_vector);
86 }
87
88
89
90 /*
91   We can probably get more efficiency points if we preprocess FONTS
92   to make lookup easier.
93  */
94 SCM
95 properties_to_font_size_family (SCM fonts, SCM alist_chain)
96 {
97   SCM shape = SCM_BOOL_F;
98   SCM family = SCM_BOOL_F;
99   SCM series = SCM_BOOL_F;
100   
101   shape = ly_assoc_chain (ly_symbol2scm ("font-shape"), alist_chain);
102   family = ly_assoc_chain (ly_symbol2scm ("font-family"), alist_chain);
103   series = ly_assoc_chain (ly_symbol2scm ("font-series"), alist_chain);
104
105   if (gh_pair_p (shape))
106     shape = ly_cdr (shape);
107   if (gh_pair_p (family))
108     family = ly_cdr (family);
109   if (gh_pair_p (series))
110     series = ly_cdr (series);
111
112
113   for (SCM s = fonts ; gh_pair_p (s); s = ly_cdr (s))
114     {
115       SCM qlist = ly_caar (s);
116
117       if (!wild_compare (SCM_VECTOR_REF (qlist, 0), series))
118         continue;
119       if (!wild_compare (SCM_VECTOR_REF (qlist, 1), shape))
120         continue;
121       if (!wild_compare (SCM_VECTOR_REF (qlist, 2), family))
122         continue;
123   
124       SCM qname = ly_cdar (s);
125       return qname;
126     }
127
128   warning (_f ("cannot find font for: (%s %s %s)",
129                ly_symbol2string (series).to_str0 (),
130                ly_symbol2string (shape).to_str0 (),
131                ly_symbol2string (family).to_str0 ()));
132   
133   scm_write (scm_list_n (shape, series , family, 
134                          SCM_UNDEFINED), scm_current_error_port ());
135   scm_flush (scm_current_error_port ());
136  
137   return scm_makfrom0str ("cmr10");
138 }
139
140
141 Font_metric *
142 select_font (Paper_def *paper, SCM chain)
143 {
144   SCM name = ly_assoc_chain (ly_symbol2scm  ("font-name"), chain);
145   
146   if (!gh_pair_p (name) || !gh_string_p (gh_cdr (name)))
147     {
148       SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
149       name = properties_to_font_size_family (fonts, chain);
150     }
151   else
152     name  = gh_cdr (name);
153
154
155   if (gh_string_p (name))
156     {
157       SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
158   
159       Real rmag = gh_pair_p (mag) ? robust_scm2double (gh_cdr (mag), 1.0) : 1;
160   
161       return paper->find_font (name, rmag);
162     }
163   else if (gh_pair_p (name)) // (DEFAULT . FONT-VEC) pair
164     {
165       SCM vec = gh_cdr (name);
166       SCM base_size = gh_car (name);
167       
168       SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
169       Real req = 0.0;
170       if (gh_pair_p (font_size))
171         req = gh_scm2double (ly_cdr (font_size));
172
173       return get_font_by_mag_step (paper, req,
174                                    vec, gh_scm2double (base_size));
175     }
176
177   assert (0);
178
179   return 0;
180 }
181
182