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