]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
* lily/parser.yy (assignment_id): allow LYRICS_STRING as
[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--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "dimensions.hh"
10 #include "output-def.hh"
11 #include "modified-font-metric.hh"
12 #include "pango-font.hh"
13 #include "all-font-metrics.hh"
14
15
16 Real
17 output_scale (Output_def *od)
18 {
19   return scm_to_double (od->lookup_variable (ly_symbol2scm ("outputscale")));
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 SCM
35 get_pango_font_table (Output_def *def)
36 {
37   SCM font_table = def->lookup_variable (ly_symbol2scm ("pango-fonts"));
38   if (scm_hash_table_p (font_table) != SCM_BOOL_T)
39     {
40       font_table = scm_c_make_hash_table (11);
41       def->set_variable (ly_symbol2scm ("pango-fonts"), font_table);
42     }
43   return font_table;
44 }
45
46 /* TODO: should add nesting for Output_def here too. */
47 Font_metric *
48 find_scaled_font (Output_def *mod, Font_metric *f, Real m)
49 {
50   if (mod->parent_)
51     return find_scaled_font (mod->parent_, f, m);
52   
53   Real lookup_mag = m / output_scale (mod);
54   
55   SCM font_table = get_font_table (mod);
56   SCM sizes = scm_hashq_ref (font_table, f->self_scm (), SCM_EOL);
57   SCM handle = scm_assoc (scm_make_real (lookup_mag), sizes);
58   if (scm_is_pair (handle))
59     return unsmob_metrics (scm_cdr (handle));
60   
61   SCM val = Modified_font_metric::make_scaled_font_metric (f, lookup_mag);
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 Font_metric*
70 find_pango_font (Output_def *layout,  SCM descr, Real factor)
71 {
72   if (layout->parent_)
73     return find_pango_font (layout->parent_, descr, factor);
74   
75   SCM table = get_pango_font_table (layout);
76   SCM sizes =  scm_hash_ref (table, descr, SCM_EOL);
77   SCM size_key = scm_from_double (factor);
78   SCM handle = scm_assoc (size_key, sizes);
79   if (scm_is_pair (handle))
80     {
81       return unsmob_metrics (scm_cdr (handle));
82     }
83
84   PangoFontDescription *description
85     = pango_font_description_from_string (scm_i_string_chars (descr));
86   Font_metric *fm = all_fonts_global->find_pango_font (description,
87                                                        factor,
88                                                        output_scale (layout));
89
90   sizes = scm_acons (size_key,  fm->self_scm(), sizes);
91   scm_hash_set_x (table, descr, sizes);
92
93   return fm;
94 }
95
96 /* TODO: this is a nasty interface. During formatting,
97    the Output_def should be scaled to the output_scale_
98    specified in the toplevel Output_def.  */
99 Output_def * 
100 scale_output_def (Output_def *o, Real amount)
101 {
102   SCM proc = ly_lily_module_constant ("scale-layout");
103   SCM new_pap = scm_call_2 (proc, o->self_scm (), scm_double2num (amount));
104   scm_gc_protect_object (new_pap);
105
106   return unsmob_output_def (new_pap);
107 }
108