]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[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 "all-font-metrics.hh"
13 #include "paper-def.hh"
14 #include "font-interface.hh"
15 #include "warn.hh"
16
17 LY_DEFINE (ly_paper_get_font, "ly:paper-get-font", 2, 0, 0,
18            (SCM paper, SCM chain),
19
20            "Return a font metric satisfying the font-qualifiers "
21            "in the alist chain @var{chain}.\n"
22            "(An alist chain is a list of alists, containing grob properties).\n")
23 {
24   Paper_def *pap = unsmob_paper (paper);
25   SCM_ASSERT_TYPE (pap, paper, SCM_ARG1, __FUNCTION__, "paper definition");
26   
27   Font_metric *fm = select_font (pap, chain);
28   return fm->self_scm ();
29 }
30
31 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
32            (SCM paper, SCM name),
33            "Return the paper variable @var{name}.")
34 {
35   Paper_def *pap = unsmob_paper (paper);
36   SCM_ASSERT_TYPE (pap, paper, SCM_ARG1, __FUNCTION__, "paper definition");
37   return scm_make_real (pap->get_dimension (name));
38 }
39
40 bool
41 wild_compare (SCM field_val, SCM val)
42 {
43   return (val == SCM_BOOL_F || field_val == ly_symbol2scm ("*") || field_val == val);
44 }
45
46
47 /*
48   TODO: this triggers a great number of font-loads (feta11 upto
49   parmesan23). We could make a Delayed_load_font_metric for which the
50   design size is specced in advance.
51  */
52 Font_metric*
53 get_font_by_design_size (Paper_def* paper, Real requested,
54                          SCM font_vector)
55 {
56   int n = SCM_VECTOR_LENGTH (font_vector);
57   Real size = 1e6;
58   Real last_size = -1e6;
59   int i = 0;
60   
61   for (; i < n; i++)
62     {
63       SCM entry = SCM_VECTOR_REF (font_vector, i);
64       Font_metric *fm = unsmob_metrics (scm_force (entry));
65       size = fm->design_size ();
66       
67       if (size > requested)
68         break ;
69       last_size = size; 
70     }
71
72   if (i == n)
73     {
74       i = n-1;
75     }
76   else if (i > 0)
77     {
78       if ((requested / last_size) < (size / requested))
79         {
80           i -- ;
81           size = last_size;
82         }
83     }
84
85   Font_metric *fm = unsmob_metrics (scm_force (SCM_VECTOR_REF (font_vector, i)));
86   return paper->find_scaled_font (fm, requested / size);
87 }
88
89
90 Font_metric*
91 get_font_by_mag_step (Paper_def* paper, Real requested_step,
92                       SCM font_vector, Real default_size)
93 {
94   return get_font_by_design_size (paper,
95                                   default_size * pow (2.0, requested_step / 6.0),
96                                   font_vector);
97 }
98
99
100
101 SCM
102 properties_to_font_size_family (SCM fonts, SCM alist_chain)
103 {
104   return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
105 }
106
107
108 Font_metric *
109 select_font (Paper_def *paper, SCM chain)
110 {
111   SCM name = ly_assoc_chain (ly_symbol2scm  ("font-name"), chain);
112   
113   if (!ly_pair_p (name) || !ly_string_p (ly_cdr (name)))
114     {
115       SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
116       name = properties_to_font_size_family (fonts, chain);
117     }
118   else
119     name  = ly_cdr (name);
120
121
122   if (ly_string_p (name))
123     {
124       SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
125   
126       Real rmag = ly_pair_p (mag) ? robust_scm2double (ly_cdr (mag), 1.0) : 1;
127
128       Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
129       
130       
131       return paper->find_scaled_font (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_assoc_chain (ly_symbol2scm ("font-size"), chain);
139       Real req = 0.0;
140       if (ly_pair_p (font_size))
141         req = ly_scm2double (ly_cdr (font_size));
142
143       return get_font_by_mag_step (paper, req,
144                                    vec, ly_scm2double (base_size));
145     }
146
147   assert (0);
148
149   return 0;
150 }
151
152