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