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