]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
* lily/modified-font-metric.cc (text_dimension): try
[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 "pango-font.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
22 SCM
23 get_font_table (Output_def *def)
24 {
25   SCM font_table = def->lookup_variable (ly_symbol2scm ("scaled-fonts"));
26   if (scm_hash_table_p (font_table) != SCM_BOOL_T)
27     {
28       font_table = scm_c_make_hash_table (11);
29       def->set_variable (ly_symbol2scm ("scaled-fonts"), font_table);
30     }
31   return font_table;
32 }
33   
34
35
36 /* TODO: should add nesting for Output_def here too. */
37 Font_metric *
38 find_scaled_font (Output_def *mod, Font_metric *f, Real m,
39                   SCM font_encoding, SCM input_encoding)
40 {
41   if (mod->parent_)
42     return find_scaled_font (mod->parent_, f, m, font_encoding, input_encoding);
43   
44   Real lookup_mag = m / output_scale (mod);
45
46   
47   
48   SCM font_table = get_font_table (mod);
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 (scm_is_pair (met))
54         return unsmob_metrics (scm_cdr (met));
55     }
56   else
57     sizes = SCM_EOL;
58   
59   SCM val = Modified_font_metric::make_scaled_font_metric (f, lookup_mag,
60                                                            font_encoding,
61                                                            input_encoding);
62
63   sizes = scm_acons (scm_make_real (lookup_mag), val, sizes);
64   scm_gc_unprotect_object (val);
65   scm_hashq_set_x (font_table, f->self_scm (), sizes);
66   return unsmob_metrics (val);
67 }
68
69 /* TODO: this is a nasty interface. During formatting,
70    the Output_def should be scaled to the output_scale_
71    specified in the toplevel Output_def.  */
72 Output_def * 
73 scale_output_def (Output_def *o, Real amount)
74 {
75   SCM proc = ly_lily_module_constant ("scale-layout");
76   SCM new_pap = scm_call_2 (proc, o->self_scm (), scm_double2num (amount));
77   scm_gc_protect_object (new_pap);
78
79   return unsmob_output_def (new_pap);
80 }
81
82 LY_DEFINE (ly_paper_fonts, "ly:paper-fonts",
83            1, 0, 0,
84            (SCM bp),
85            "Return fonts scaled up BP")
86 {
87   Output_def *b = unsmob_output_def (bp);
88
89   SCM font_table = b->lookup_variable (ly_symbol2scm ("scaled-fonts"));
90   
91   SCM_ASSERT_TYPE (b, bp, SCM_ARG1, __FUNCTION__, "paper");
92
93   SCM ell = SCM_EOL;
94   if (scm_hash_table_p (font_table) == SCM_BOOL_T)
95     {
96       SCM func = ly_lily_module_constant ("hash-table->alist");
97
98       for (SCM s = scm_call_1 (func, font_table); scm_is_pair (s);
99            s = scm_cdr (s))
100         {
101           SCM entry = scm_car (s);
102           for (SCM t = scm_cdr (entry); scm_is_pair (t); t = scm_cdr (t))
103             {
104               Font_metric *fm = unsmob_metrics (scm_cdar (t));
105
106               if (dynamic_cast<Modified_font_metric*> (fm)
107                   || dynamic_cast<Pango_font*> (fm))
108                 ell = scm_cons (fm->self_scm (), ell);
109             }
110         }
111     }
112   return ell;
113 }