]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
* scm/framework-ps.scm (output-variables): separately scale the
[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 #include <math.h>
10
11 #include "dimensions.hh"
12 #include "all-font-metrics.hh"
13 #include "output-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_smob, 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, "
23            "containing grob properties).\n")
24 {
25   Output_def *paper = unsmob_output_def (paper_smob);
26   SCM_ASSERT_TYPE (paper, paper_smob, SCM_ARG1,
27                    __FUNCTION__, "paper definition");
28   
29   Font_metric *fm = select_font (paper, chain);
30   return fm->self_scm ();
31 }
32
33 LY_DEFINE (ly_paper_get_number, "ly:paper-get-number", 2, 0, 0,
34            (SCM layout_smob, SCM name),
35            "Return the layout variable @var{name}.")
36 {
37   Output_def *layout = unsmob_output_def (layout_smob);
38   SCM_ASSERT_TYPE (layout, layout_smob, SCM_ARG1,
39                    __FUNCTION__, "layout definition");
40   return scm_make_real (layout->get_dimension (name));
41 }
42
43 bool
44 wild_compare (SCM field_val, SCM val)
45 {
46   return (val == SCM_BOOL_F
47           || field_val == ly_symbol2scm ("*")
48           || field_val == val);
49 }
50
51 Font_metric *
52 get_font_by_design_size (Output_def *layout, Real requested,
53                          SCM font_vector,
54                          SCM font_encoding, SCM input_encoding)
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     i = n - 1;
74   else if (i > 0)
75     {
76       if ((requested / last_size) < (size / requested))
77         {
78           i--;
79           size = last_size;
80         }
81     }
82
83   Font_metric *fm = unsmob_metrics (scm_force (SCM_VECTOR_REF (font_vector,
84                                                                i)));
85   return find_scaled_font (layout, fm, requested / size,
86                            font_encoding, input_encoding);
87 }
88
89 Font_metric *
90 get_font_by_mag_step (Output_def *layout, Real requested_step,
91                       SCM font_vector, Real default_size,
92                       SCM font_encoding, SCM input_encoding)
93 {
94   return get_font_by_design_size (layout, default_size
95                                   * pow (2.0, requested_step / 6.0),
96                                   font_vector, font_encoding, input_encoding);
97 }
98
99 SCM
100 properties_to_font_size_family (SCM fonts, SCM alist_chain)
101 {
102   return scm_call_2 (ly_lily_module_constant ("lookup-font"), fonts, alist_chain);
103 }
104
105 Font_metric *
106 select_encoded_font (Output_def *layout, SCM chain, SCM input_encoding)
107 {
108   SCM name = ly_chain_assoc (ly_symbol2scm ("font-name"), chain);
109
110   if (!scm_is_pair (name) || !scm_is_string (scm_cdr (name)))
111     {
112       SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
113       name = properties_to_font_size_family (fonts, chain);
114     }
115   else
116     name = scm_cdr (name);
117
118   if (scm_is_string (name))
119     {
120       SCM mag = ly_chain_assoc (ly_symbol2scm ("font-magnification"), chain);
121       Real rmag = (scm_is_pair (mag)
122                    ? robust_scm2double (scm_cdr (mag), 1.0)
123                    : 1);
124       Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
125                 
126       SCM font_encoding
127         = scm_cdr (ly_chain_assoc (ly_symbol2scm ("font-encoding"), chain));
128       return find_scaled_font (layout, fm, rmag, font_encoding, input_encoding);
129     }
130   else if (scm_instance_p (name))
131     {
132       SCM base_size  = scm_slot_ref (name, ly_symbol2scm ("default-size"));
133       SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
134
135       SCM font_size = ly_chain_assoc (ly_symbol2scm ("font-size"), chain);
136       Real req = 0;
137       if (scm_is_pair (font_size))
138         req = scm_to_double (scm_cdr (font_size));
139
140       SCM font_encoding
141         = scm_cdr (ly_chain_assoc (ly_symbol2scm ("font-encoding"), chain));
142
143       return get_font_by_mag_step (layout, req, vec,
144                                    scm_to_double (base_size) * point_constant,
145                                    font_encoding, input_encoding);
146     }
147
148   assert (0);
149   return 0;
150 }
151
152 Font_metric *
153 select_font (Output_def *layout, SCM chain)
154 {
155   return select_encoded_font (layout, chain, SCM_EOL);
156 }