]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
*** empty log message ***
[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
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_smob, 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, "
24            "containing grob properties).\n")
25 {
26   Output_def *paper = unsmob_output_def (paper_smob);
27   SCM_ASSERT_TYPE (paper, paper_smob, SCM_ARG1,
28                    __FUNCTION__, "paper definition");
29   
30   Font_metric *fm = select_font (paper, chain);
31   return fm->self_scm ();
32 }
33
34 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
35            (SCM paper_smob, SCM name),
36            "Return the paper variable @var{name}.")
37 {
38   Output_def *paper = unsmob_output_def (paper_smob);
39   SCM_ASSERT_TYPE (paper, paper_smob, SCM_ARG1,
40                    __FUNCTION__, "paper definition");
41   return scm_make_real (paper->get_dimension (name));
42 }
43
44 bool
45 wild_compare (SCM field_val, SCM val)
46 {
47   return (val == SCM_BOOL_F
48           || field_val == ly_symbol2scm ("*")
49           || field_val == val);
50 }
51
52 /*
53   TODO: this triggers a great number of font-loads (feta11 upto
54   parmesan23). We could make a Delayed_load_font_metric for which the
55   design size is specced in advance.
56  */
57 Font_metric *
58 get_font_by_design_size (Output_def *paper, Real requested,
59                          SCM font_vector, SCM input_encoding)
60 {
61   int n = SCM_VECTOR_LENGTH (font_vector);
62   Real size = 1e6;
63   Real last_size = -1e6;
64   int i = 0;
65   
66   for (; i < n; i++)
67     {
68       SCM entry = SCM_VECTOR_REF (font_vector, i);
69       Font_metric *fm = unsmob_metrics (scm_force (entry));
70       size = fm->design_size ();
71       
72       if (size > requested)
73         break;
74       last_size = size; 
75     }
76
77   if (i == n)
78     i = n - 1;
79   else if (i > 0)
80     {
81       if ((requested / last_size) < (size / requested))
82         {
83           i--;
84           size = last_size;
85         }
86     }
87
88   Font_metric *fm = unsmob_metrics (scm_force (SCM_VECTOR_REF (font_vector,
89                                                                i)));
90   return find_scaled_font (paper, fm, requested / size, input_encoding);
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,
97                       SCM input_encoding)
98 {
99   return get_font_by_design_size (paper, default_size
100                                   * pow (2.0, requested_step / 6.0),
101                                   font_vector, input_encoding);
102 }
103
104 SCM
105 properties_to_font_size_family (SCM fonts, SCM alist_chain)
106 {
107   return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
108 }
109
110 Font_metric *
111 select_encoded_font (Output_def *paper, SCM chain, SCM input_encoding)
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       Real rmag = (ly_c_pair_p (mag)
127                    ? robust_scm2double (ly_cdr (mag), 1.0)
128                    : 1);
129       Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
130                    
131       return find_scaled_font (paper, fm, rmag, input_encoding);
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;
140       if (ly_c_pair_p (font_size))
141         req = ly_scm2double (ly_cdr (font_size));
142
143       return get_font_by_mag_step (paper, req, vec, ly_scm2double (base_size),
144                                    input_encoding);
145     }
146
147   assert (0);
148   return 0;
149 }
150
151 Font_metric *
152 select_font (Output_def *paper, SCM chain)
153 {
154   return select_encoded_font (paper, chain, SCM_EOL);
155 }