]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
* scm/lily.scm (tex-output-expression): new function, eval within
[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
45 Font_metric*
46 get_font_by_design_size (Paper_def* paper, Real requested,
47                          SCM font_vector)
48 {
49   int n = SCM_VECTOR_LENGTH (font_vector);
50   Real size = 1e6;
51   Real last_size = -1e6;
52   int i = 0;
53   
54   for (; i < n; i++)
55     {
56       size = gh_scm2double (gh_car (SCM_VECTOR_REF (font_vector, i)));
57       if (size > requested)
58         break ;
59       last_size = size; 
60     }
61
62   if (i == n)
63     {
64       i = n-1;
65     }
66   else if (i > 0)
67     {
68       if ((requested / last_size) < (size / requested))
69         {
70           i -- ;
71           size = last_size;
72         }
73     }
74   
75   return paper->find_font (gh_cdr (SCM_VECTOR_REF (font_vector, i)),
76                            requested / size);
77 }
78
79
80 Font_metric*
81 get_font_by_mag_step (Paper_def* paper, Real requested_step,
82                       SCM font_vector, Real default_size)
83 {
84   return get_font_by_design_size (paper,
85                                   default_size * pow (2.0, requested_step / 6.0),
86                                   font_vector);
87 }
88
89
90
91 SCM
92 properties_to_font_size_family (SCM fonts, SCM alist_chain)
93 {
94   return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
95 }
96
97
98 Font_metric *
99 select_font (Paper_def *paper, SCM chain)
100 {
101   SCM name = ly_assoc_chain (ly_symbol2scm  ("font-name"), chain);
102   
103   if (!gh_pair_p (name) || !gh_string_p (gh_cdr (name)))
104     {
105       SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
106       name = properties_to_font_size_family (fonts, chain);
107     }
108   else
109     name  = gh_cdr (name);
110
111
112   if (gh_string_p (name))
113     {
114       SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
115   
116       Real rmag = gh_pair_p (mag) ? robust_scm2double (gh_cdr (mag), 1.0) : 1;
117   
118       return paper->find_font (name, rmag);
119     }
120   else if (gh_pair_p (name)) // (DEFAULT . FONT-VEC) pair
121     {
122       SCM vec = gh_cdr (name);
123       SCM base_size = gh_car (name);
124       
125       SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
126       Real req = 0.0;
127       if (gh_pair_p (font_size))
128         req = gh_scm2double (ly_cdr (font_size));
129
130       return get_font_by_mag_step (paper, req,
131                                    vec, gh_scm2double (base_size));
132     }
133
134   assert (0);
135
136   return 0;
137 }
138
139