]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
new file, move from
[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 "output-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   Output_def *pap = unsmob_output_def (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   Output_def *pap = unsmob_output_def (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 (Output_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 dynamic_cast<Book_output_def*> (paper->parent_)
88     // ugh.
89     ->find_scaled_font (fm, requested / size, input_encoding_name);
90
91 }
92
93
94 Font_metric*
95 get_font_by_mag_step (Output_def* paper, Real requested_step,
96                       SCM font_vector, Real default_size, SCM input_encoding_name)
97 {
98   return get_font_by_design_size (paper,
99                                   default_size * pow (2.0, requested_step / 6.0),
100                                   font_vector, input_encoding_name);
101 }
102
103
104
105 SCM
106 properties_to_font_size_family (SCM fonts, SCM alist_chain)
107 {
108   return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
109 }
110
111
112 Font_metric *
113 select_encoded_font (Output_def *paper, SCM chain, SCM encoding_name)
114 {
115   SCM name = ly_assoc_chain (ly_symbol2scm  ("font-name"), chain);
116   
117   if (!ly_c_pair_p (name) || !ly_c_string_p (ly_cdr (name)))
118     {
119       SCM fonts = paper->lookup_variable (ly_symbol2scm ("fonts"));
120       name = properties_to_font_size_family (fonts, chain);
121     }
122   else
123     name  = ly_cdr (name);
124
125   if (ly_c_string_p (name))
126     {
127       SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
128   
129       Real rmag = ly_c_pair_p (mag) ? robust_scm2double (ly_cdr (mag), 1.0) : 1;
130
131       Font_metric * fm = all_fonts_global->find_font (ly_scm2string (name));
132       
133       
134       return dynamic_cast<Book_output_def*> (paper->parent_)
135         ->find_scaled_font (fm, rmag, encoding_name);
136     }
137   else if (scm_instance_p (name))
138     {
139       SCM base_size  = scm_slot_ref (name, ly_symbol2scm ("default-size"));
140       SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
141       
142       SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
143       Real req = 0.0;
144       if (ly_c_pair_p (font_size))
145         req = ly_scm2double (ly_cdr (font_size));
146
147       return get_font_by_mag_step (paper, req,
148                                    vec, ly_scm2double (base_size), encoding_name);
149     }
150
151   assert (0);
152
153   return 0;
154 }
155
156 Font_metric *
157 select_font (Output_def *paper, SCM chain)
158 {
159   return select_encoded_font (paper, chain, SCM_EOL);
160 }