]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
The grand \paper -> \layout, \bookpaper -> \paper renaming.
[lilypond.git] / lily / paper-def.cc
1 /* 
2   paper-def.cc -- implement Paper_def
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "dimensions.hh"
10 #include "font-metric.hh"
11 #include "ly-module.hh"
12 #include "output-def.hh"
13 #include "modified-font-metric.hh"
14 #include "virtual-font-metric.hh"
15
16 Real
17 output_scale (Output_def *od)
18 {
19   return scm_to_double (od->lookup_variable (ly_symbol2scm ("outputscale")));
20 }
21
22 /* TODO: should add nesting for Output_def here too. */
23 Font_metric *
24 find_scaled_font (Output_def *mod,
25                   Font_metric *f, Real m, SCM input_enc_name)
26 {
27   if (mod->parent_)
28     return find_scaled_font (mod->parent_, f, m, input_enc_name);
29   
30   Real lookup_mag = m;
31   if (!dynamic_cast<Virtual_font_metric*> (f))
32     lookup_mag /= output_scale (mod);
33
34   SCM font_table = mod->lookup_variable (ly_symbol2scm ("scaled-fonts"));
35   if (scm_hash_table_p (font_table) != SCM_BOOL_T)
36     {
37       font_table = scm_c_make_hash_table (11);
38       mod->set_variable (ly_symbol2scm ("scaled-fonts"), font_table);
39     }
40
41   
42   SCM sizes = scm_hashq_ref (font_table, f->self_scm (), SCM_BOOL_F);
43   if (sizes != SCM_BOOL_F)
44     {
45       SCM met = scm_assoc (scm_make_real (lookup_mag), sizes);
46       if (scm_is_pair (met))
47         return unsmob_metrics (scm_cdr (met));
48     }
49   else
50     sizes = SCM_EOL;
51   
52   /* Hmm. We're chaining font - metrics.  Should consider whether to
53      merge virtual-font and scaled_font.  */
54   SCM val = SCM_EOL;
55   if (Virtual_font_metric * vf = dynamic_cast<Virtual_font_metric*> (f))
56     {
57       /* For fontify_atom (), the magnification and name must be known
58          at the same time. That's impossible for
59          
60          Scaled (Virtual_font (Font1,Font2))
61          
62          so we replace by
63          
64          Virtual_font (Scaled (Font1), Scaled (Font2))  */
65       SCM lst = SCM_EOL;
66       SCM *t = &lst;
67       for (SCM s = vf->get_font_list (); scm_is_pair (s); s = scm_cdr (s))
68         {
69           Font_metric *scaled = find_scaled_font (mod,
70                                                   unsmob_metrics (scm_car (s)),
71                                                   m, input_enc_name);
72           *t = scm_cons (scaled->self_scm (), SCM_EOL);
73           t = SCM_CDRLOC (*t);
74         }
75
76       vf = new Virtual_font_metric (lst);
77       val = vf->self_scm ();
78     }
79   else
80     {
81       val = Modified_font_metric::make_scaled_font_metric (input_enc_name,
82                                                            f, lookup_mag);
83     }
84
85   sizes = scm_acons (scm_make_real (lookup_mag), val, sizes);
86   scm_gc_unprotect_object (val);
87   scm_hashq_set_x (font_table, f->self_scm (), sizes);
88   return unsmob_metrics (val);
89 }
90
91 /* TODO: this is a nasty interface. During formatting,
92    the Output_def should be scaled to the output_scale_
93    specified in the toplevel Output_def.  */
94 Output_def * 
95 scale_output_def (Output_def *o, Real amount)
96 {
97   SCM proc = ly_scheme_function ("scale-layout");
98   SCM new_pap = scm_call_2 (proc, o->self_scm (), scm_double2num (amount));
99   scm_gc_protect_object (new_pap);
100
101   return unsmob_output_def (new_pap);
102 }
103
104 LY_DEFINE (ly_paper_fonts, "ly:paper-fonts",
105            1, 0, 0,
106            (SCM bp),
107            "Return fonts scaled up BP")
108 {
109   Output_def *b = unsmob_output_def (bp);
110
111   SCM font_table = b->lookup_variable (ly_symbol2scm ("scaled-fonts"));
112   
113   SCM_ASSERT_TYPE (b, bp, SCM_ARG1, __FUNCTION__, "paper");
114
115   SCM ell = SCM_EOL;
116   if (scm_hash_table_p (font_table) == SCM_BOOL_T)
117     {
118       SCM func = ly_scheme_function ("hash-table->alist");
119
120       for (SCM s = scm_call_1 (func, font_table); scm_is_pair (s);
121            s = scm_cdr (s))
122         {
123           SCM entry = scm_car (s);
124           for (SCM t = scm_cdr (entry); scm_is_pair (t); t = scm_cdr (t))
125             {
126               Font_metric *fm = unsmob_metrics (scm_cdar (t));
127
128               if (dynamic_cast<Modified_font_metric*> (fm))
129                 ell = scm_cons (fm->self_scm (), ell);
130             }
131         }
132     }
133   return ell;
134 }