]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
* lily/all-font-metrics.cc (find_font): Add "lm" to try-AFM-first
[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, Font_metric *f, Real m,
24                   SCM font_encoding, SCM input_encoding)
25 {
26   if (mod->parent_)
27     return find_scaled_font (mod->parent_, f, m, font_encoding, input_encoding);
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
69             = find_scaled_font (mod, unsmob_metrics (scm_car (s)), m,
70                                 font_encoding, input_encoding);
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     val = Modified_font_metric::make_scaled_font_metric (f, lookup_mag,
80                                                          font_encoding,
81                                                          input_encoding);
82
83   sizes = scm_acons (scm_make_real (lookup_mag), val, sizes);
84   scm_gc_unprotect_object (val);
85   scm_hashq_set_x (font_table, f->self_scm (), sizes);
86   return unsmob_metrics (val);
87 }
88
89 /* TODO: this is a nasty interface. During formatting,
90    the Output_def should be scaled to the output_scale_
91    specified in the toplevel Output_def.  */
92 Output_def * 
93 scale_output_def (Output_def *o, Real amount)
94 {
95   SCM proc = ly_scheme_function ("scale-layout");
96   SCM new_pap = scm_call_2 (proc, o->self_scm (), scm_double2num (amount));
97   scm_gc_protect_object (new_pap);
98
99   return unsmob_output_def (new_pap);
100 }
101
102 LY_DEFINE (ly_paper_fonts, "ly:paper-fonts",
103            1, 0, 0,
104            (SCM bp),
105            "Return fonts scaled up BP")
106 {
107   Output_def *b = unsmob_output_def (bp);
108
109   SCM font_table = b->lookup_variable (ly_symbol2scm ("scaled-fonts"));
110   
111   SCM_ASSERT_TYPE (b, bp, SCM_ARG1, __FUNCTION__, "paper");
112
113   SCM ell = SCM_EOL;
114   if (scm_hash_table_p (font_table) == SCM_BOOL_T)
115     {
116       SCM func = ly_scheme_function ("hash-table->alist");
117
118       for (SCM s = scm_call_1 (func, font_table); scm_is_pair (s);
119            s = scm_cdr (s))
120         {
121           SCM entry = scm_car (s);
122           for (SCM t = scm_cdr (entry); scm_is_pair (t); t = scm_cdr (t))
123             {
124               Font_metric *fm = unsmob_metrics (scm_cdar (t));
125
126               if (dynamic_cast<Modified_font_metric*> (fm))
127                 ell = scm_cons (fm->self_scm (), ell);
128             }
129         }
130     }
131   return ell;
132 }