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