Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / paper-def.cc
blob0e6362a4d4d689b1b7faadb8dad25409350bddfb
1 /*
2 paper-def.cc -- implement Paper_def
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
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"
15 Real
16 output_scale (Output_def *od)
18 return scm_to_double (od->lookup_variable (ly_symbol2scm ("output-scale")));
21 SCM
22 get_font_table (Output_def *def)
24 SCM font_table = def->lookup_variable (ly_symbol2scm ("scaled-fonts"));
25 if (scm_hash_table_p (font_table) != SCM_BOOL_T)
27 font_table = scm_c_make_hash_table (11);
28 def->set_variable (ly_symbol2scm ("scaled-fonts"), font_table);
30 return font_table;
33 SCM
34 get_pango_font_table (Output_def *def)
36 SCM font_table = def->lookup_variable (ly_symbol2scm ("pango-fonts"));
37 if (scm_hash_table_p (font_table) != SCM_BOOL_T)
39 font_table = scm_c_make_hash_table (11);
40 def->set_variable (ly_symbol2scm ("pango-fonts"), font_table);
42 return font_table;
45 /* TODO: should add nesting for Output_def here too. */
46 Font_metric *
47 find_scaled_font (Output_def *mod, Font_metric *f, Real m)
49 if (mod->parent_)
50 return find_scaled_font (mod->parent_, f, m);
52 Real lookup_mag = m / output_scale (mod);
54 SCM font_table = get_font_table (mod);
55 SCM sizes = scm_hashq_ref (font_table, f->self_scm (), SCM_EOL);
56 SCM handle = scm_assoc (scm_from_double (lookup_mag), sizes);
57 if (scm_is_pair (handle))
58 return unsmob_metrics (scm_cdr (handle));
60 SCM val = Modified_font_metric::make_scaled_font_metric (f, lookup_mag);
62 sizes = scm_acons (scm_from_double (lookup_mag), val, sizes);
63 unsmob_metrics (val)->unprotect ();
64 scm_hashq_set_x (font_table, f->self_scm (), sizes);
65 return unsmob_metrics (val);
68 Font_metric *
69 find_pango_font (Output_def *layout, SCM descr, Real factor)
71 if (layout->parent_)
72 return find_pango_font (layout->parent_, descr, factor);
74 SCM table = get_pango_font_table (layout);
75 SCM sizes = scm_hash_ref (table, descr, SCM_EOL);
76 SCM size_key = scm_from_double (factor);
77 SCM handle = scm_assoc (size_key, sizes);
78 if (scm_is_pair (handle))
79 return unsmob_metrics (scm_cdr (handle));
81 PangoFontDescription *description
82 = pango_font_description_from_string (scm_i_string_chars (descr));
84 pango_font_description_set_size (description,
85 gint (factor *
86 pango_font_description_get_size (description)));
89 Font_metric *fm = all_fonts_global->find_pango_font (description,
90 output_scale (layout));
92 pango_font_description_free (description);
94 sizes = scm_acons (size_key, fm->self_scm (), sizes);
95 scm_hash_set_x (table, descr, sizes);
97 return fm;
100 /* TODO: this is a nasty interface. During formatting,
101 the Output_def should be scaled to the output_scale_
102 specified in the toplevel Output_def. */
103 Output_def *
104 scale_output_def (Output_def *o, Real amount)
106 SCM proc = ly_lily_module_constant ("scale-layout");
107 SCM new_pap = scm_call_2 (proc, o->self_scm (), scm_double2num (amount));
109 o = unsmob_output_def (new_pap);
110 o->protect ();
111 return o;