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