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