]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
* lily/font-interface.cc (text_font_alist_chain): rename function,
[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   static SCM proc;
95   if (!proc )
96     proc = scm_c_eval_string ("lookup-font");
97
98   return scm_call_2 (proc, fonts, alist_chain);
99 }
100
101
102 Font_metric *
103 select_font (Paper_def *paper, SCM chain)
104 {
105   SCM name = ly_assoc_chain (ly_symbol2scm  ("font-name"), chain);
106   
107   if (!gh_pair_p (name) || !gh_string_p (gh_cdr (name)))
108     {
109       SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
110       name = properties_to_font_size_family (fonts, chain);
111     }
112   else
113     name  = gh_cdr (name);
114
115
116   if (gh_string_p (name))
117     {
118       SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
119   
120       Real rmag = gh_pair_p (mag) ? robust_scm2double (gh_cdr (mag), 1.0) : 1;
121   
122       return paper->find_font (name, rmag);
123     }
124   else if (gh_pair_p (name)) // (DEFAULT . FONT-VEC) pair
125     {
126       SCM vec = gh_cdr (name);
127       SCM base_size = gh_car (name);
128       
129       SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
130       Real req = 0.0;
131       if (gh_pair_p (font_size))
132         req = gh_scm2double (ly_cdr (font_size));
133
134       return get_font_by_mag_step (paper, req,
135                                    vec, gh_scm2double (base_size));
136     }
137
138   assert (0);
139
140   return 0;
141 }
142
143