]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
* lily/paper-outputter.cc (output_stencil): New method.
[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) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #include <math.h>
11
12 #include "virtual-font-metric.hh"
13 #include "all-font-metrics.hh"
14 #include "string.hh"
15 #include "misc.hh"
16 #include "paper-def.hh"
17 #include "warn.hh"
18 #include "scaled-font-metric.hh"
19 #include "main.hh"
20 #include "scm-hash.hh"
21 #include "paper-outputter.hh"
22 #include "ly-module.hh"
23
24 /*
25   This is an almost empty thing. The only substantial thing this class
26   handles is scaling up and down to real-world dimensions (internally
27   dimensions are against global staff-space.)
28  */
29
30 Paper_def::Paper_def ()
31 {
32   /* Do not remove this statement, scm_make_hash_table may trigger GC.  */
33   scaled_fonts_ = SCM_EOL;
34   scaled_fonts_ = scm_c_make_hash_table (11);
35 }
36
37 Paper_def::Paper_def (Paper_def const&src)
38   : Music_output_def (src)
39 {
40   /* Do not remove this statement, scm_make_hash_table may trigger GC.  */
41   scaled_fonts_ = SCM_EOL;
42   scaled_fonts_ = scm_c_make_hash_table (11);
43 }
44
45 Paper_def::~Paper_def ()
46 {
47 }
48
49 void
50 Paper_def::derived_mark ()
51 {
52   scm_gc_mark (scaled_fonts_);
53 }
54
55 Real
56 Paper_def::get_dimension (SCM s) const
57 {
58   SCM val = lookup_variable (s);
59   SCM scale = lookup_variable (ly_symbol2scm ("outputscale"));
60   
61   Real sc = ly_scm2double (scale);
62   return ly_scm2double (val) / sc;
63 }
64
65 /* FIXME.  This is broken until we have a generic way of
66    putting lists inside the \paper block.  */
67 Interval
68 Paper_def::line_dimensions_int (int n) const
69 {
70   Real lw =  get_dimension (ly_symbol2scm ("linewidth"));
71   Real ind = n? 0.0:get_dimension (ly_symbol2scm ("indent"));
72
73   return Interval (ind, lw);
74 }
75
76 Paper_outputter*
77 Paper_def::get_paper_outputter (String outname) const
78 {
79   progress_indication (_f ("paper output to `%s'...",
80                            outname == "-" ? String ("<stdout>") : outname));
81   return new Paper_outputter (outname);
82 }
83
84 Font_metric*
85 Paper_def::find_scaled_font (Font_metric *f, Real m, SCM input_enc_name)
86 {
87   SCM sizes = scm_hashq_ref (scaled_fonts_, f->self_scm (), SCM_BOOL_F);
88   if (sizes != SCM_BOOL_F)
89     {
90       SCM met = scm_assoc (scm_make_real (m), sizes);
91       if (ly_c_pair_p (met))
92         return unsmob_metrics (ly_cdr (met));
93     }
94   else
95     sizes = SCM_EOL;
96   
97   /* Hmm. We're chaining font - metrics.  Should consider whether to
98      merge virtual-font and scaled_font.  */
99   SCM val = SCM_EOL;
100   if (Virtual_font_metric * vf = dynamic_cast<Virtual_font_metric*> (f))
101     {
102       /* For fontify_atom (), the magnification and name must be known
103          at the same time. That's impossible for
104
105           Scaled (Virtual_font (Font1,Font2))
106
107         so we replace by
108
109           Virtual_font (Scaled (Font1), Scaled (Font2))  */
110       SCM lst = SCM_EOL;
111       SCM *t = &lst;
112       for (SCM s = vf->get_font_list (); ly_c_pair_p (s); s = ly_cdr (s))
113         {
114           Font_metric *scaled = find_scaled_font (unsmob_metrics (ly_car (s)),
115                                                   m, input_enc_name);
116           *t = scm_cons (scaled->self_scm (), SCM_EOL);
117           t = SCM_CDRLOC(*t);
118         }
119
120       vf = new Virtual_font_metric (lst);
121       val = vf->self_scm ();
122     }
123   else
124     {
125       SCM scale_var = ly_module_lookup (scope_, ly_symbol2scm ("outputscale"));
126
127       if (!ly_c_symbol_p (input_enc_name))
128         {
129           SCM var = ly_module_lookup (scope_, ly_symbol2scm ("inputencoding"));
130           input_enc_name = scm_variable_ref (var);
131         }
132       m /= ly_scm2double (scm_variable_ref (scale_var));
133       val = Modified_font_metric::make_scaled_font_metric (input_enc_name,
134                                                            f, m);
135     }
136
137   sizes = scm_acons (scm_make_real (m), val, sizes);
138   scm_gc_unprotect_object (val);
139   scm_hashq_set_x (scaled_fonts_, f->self_scm (), sizes);
140   return unsmob_metrics (val);
141 }
142
143
144 /* Return alist to translate internally used fonts back to real-world
145    coordinates.  */
146 SCM
147 Paper_def::font_descriptions () const
148 {
149   SCM func = ly_scheme_function ("hash-table->alist");
150
151   SCM l = SCM_EOL;
152   for (SCM s = scm_call_1 (func, scaled_fonts_); ly_c_pair_p (s); s = ly_cdr (s))
153     {
154       SCM entry = ly_car (s);
155       for (SCM t = ly_cdr (entry); ly_c_pair_p (t); t  = ly_cdr (t))
156         {
157           Font_metric *fm= unsmob_metrics (ly_cdar (t));
158
159           if (dynamic_cast<Modified_font_metric*> (fm))
160             l = scm_cons (fm->self_scm (), l);
161         }
162     }
163   return l;
164 }
165
166 Paper_def* 
167 unsmob_paper (SCM x)
168 {
169   return dynamic_cast<Paper_def*> (unsmob_music_output_def (x));
170 }
171   
172
173 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
174            1, 0, 0, (SCM def),
175            "Is @var{def} a paper definition?")
176 {
177   Paper_def *op = dynamic_cast<Paper_def*> (unsmob_music_output_def (def));
178
179   bool pap = op;
180   return ly_bool2scm (pap);
181 }