]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-select.cc
* scm/output-ps.scm (new-text): new function. Use glyphshow to
[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 <cmath>
11
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 /*
52   TODO: this triggers a great number of font-loads (feta11 upto
53   parmesan23). We could make a Delayed_load_font_metric for which the
54   design size is specced in advance.
55  */
56 Font_metric *
57 get_font_by_design_size (Output_def *layout, Real requested,
58                          SCM font_vector, 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, input_encoding);
90 }
91
92 Font_metric*
93 get_font_by_mag_step (Output_def *layout, Real requested_step,
94                       SCM font_vector, Real default_size,
95                       SCM input_encoding)
96 {
97   return get_font_by_design_size (layout, default_size
98                                   * pow (2.0, requested_step / 6.0),
99                                   font_vector, input_encoding);
100 }
101
102 SCM
103 properties_to_font_size_family (SCM fonts, SCM alist_chain)
104 {
105   return scm_call_2 (ly_scheme_function ("lookup-font"), fonts, alist_chain);
106 }
107
108 Font_metric *
109 select_encoded_font (Output_def *layout, SCM chain, SCM input_encoding)
110 {
111   SCM name = ly_assoc_chain (ly_symbol2scm  ("font-name"), chain);
112   
113   if (!scm_is_pair (name) || !scm_is_string (scm_cdr (name)))
114     {
115       SCM fonts = layout->lookup_variable (ly_symbol2scm ("fonts"));
116       name = properties_to_font_size_family (fonts, chain);
117     }
118   else
119     name = scm_cdr (name);
120
121   if (scm_is_string (name))
122     {
123       SCM mag = ly_assoc_chain (ly_symbol2scm ("font-magnification"), chain);
124       Real rmag = (scm_is_pair (mag)
125                    ? robust_scm2double (scm_cdr (mag), 1.0)
126                    : 1);
127       Font_metric *fm = all_fonts_global->find_font (ly_scm2string (name));
128                    
129       return find_scaled_font (layout, fm, rmag, input_encoding);
130     }
131   else if (scm_instance_p (name))
132     {
133       SCM base_size  = scm_slot_ref (name, ly_symbol2scm ("default-size"));
134       SCM vec = scm_slot_ref (name, ly_symbol2scm ("size-vector"));
135       
136       SCM font_size = ly_assoc_chain (ly_symbol2scm ("font-size"), chain);
137       Real req = 0;
138       if (scm_is_pair (font_size))
139         req = scm_to_double (scm_cdr (font_size));
140
141       return get_font_by_mag_step (layout, req, vec, scm_to_double (base_size),
142                                    input_encoding);
143     }
144
145   assert (0);
146   return 0;
147 }
148
149 Font_metric *
150 select_font (Output_def *layout, SCM chain)
151 {
152   return select_encoded_font (layout, chain, SCM_EOL);
153 }