]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
aafc2319289b20daf1a1a56ba4ace96b4e4ead21
[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--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10 #include "string.hh"
11 #include "assoc.hh"
12 #include "misc.hh"
13 #include "paper-def.hh"
14 #include "debug.hh"
15 #include "lookup.hh"
16 #include "dimension.hh"
17 #include "assoc-iter.hh"
18 #include "score-engraver.hh"
19 #include "p-score.hh"
20 #include "identifier.hh"
21 #include "main.hh"
22 #include "scope.hh"
23 #include "assoc.hh"
24 #include "assoc-iter.hh"
25
26 Paper_def::Paper_def ()
27 {
28   lookup_p_assoc_p_ = new Assoc<int, Lookup*>;
29   scope_p_ = new Scope;
30 }
31
32 Paper_def::~Paper_def ()
33 {
34   for (Assoc_iter<int, Lookup*> ai(*lookup_p_assoc_p_); ai.ok (); ai++)
35     {
36       delete ai.val ();
37     }
38   
39   delete scope_p_;
40   delete lookup_p_assoc_p_;
41 }
42
43 Paper_def::Paper_def (Paper_def const&s)
44   : Music_output_def (s)
45 {
46   lookup_p_assoc_p_ = new Assoc<int, Lookup*>;
47   for (Assoc_iter<int, Lookup*> ai(*s.lookup_p_assoc_p_); ai.ok (); ai++)
48     {
49       Lookup * l=new Lookup (*ai.val ());
50       l->paper_l_ = this;
51       set_lookup (ai.key(), l);
52     }
53   
54   scope_p_ = new Scope (*s.scope_p_);
55 }
56
57 Real
58 Paper_def::get_var (String s) const
59 {
60   if (!scope_p_->elt_b (s))
61     error (_f ("unknown paper variable: `%s\'", s));
62   Real * p = scope_p_->elem (s)->access_Real (false);
63   if (!p)
64     {
65       error (_ ("not a real variable"));
66       return 0.0;
67     }
68
69   return *p;
70 }
71
72 Interval
73 Paper_def::line_dimensions_int (int n) const
74 {
75   if (!shape_int_a_.size ())
76     if (n)
77       return Interval (0, linewidth_f ());
78     else
79       return Interval (get_var ("indent"), linewidth_f ());
80
81   if (n >= shape_int_a_.size ())
82     n = shape_int_a_.size () -1;
83
84   return shape_int_a_[n];
85 }
86
87 Real
88 Paper_def::beam_thickness_f () const
89 {
90   return get_var ("beam_thickness");
91 }
92
93 Real
94 Paper_def::linewidth_f () const
95 {
96   return get_var ("linewidth");
97 }
98
99 Real
100 Paper_def::duration_to_dist (Moment d,Real k) const
101 {
102   if (get_var ("geometric"))
103     return geometric_spacing (d);
104   return arithmetic_spacing (d,k);
105 }
106
107
108 /**
109   Get the measure wide constant for arithmetic.
110
111   @see
112   John S. Gourlay. ``Spacing a Line of Music,'' Technical Report
113   OSU-CISRC-10/87-TR35, Department of Computer and Information Science,
114   The Ohio State University, 1987.
115
116   */
117 Real
118 Paper_def::arithmetic_constant (Moment d) const
119 {
120   return get_var ("arithmetic_basicspace") - log_2 (Moment (1,8) <? d);
121 }
122
123 Real
124 Paper_def::arithmetic_spacing (Moment d ,Real k) const
125 {
126   return (log_2 (d) + k)* get_var ("arithmetic_multiplier");
127 }
128
129 Real
130 Paper_def::geometric_spacing (Moment d) const
131 {
132   Real dur_f = (d) ?pow (get_var ("geometric"), log_2 (d)) : 0;
133   return get_var ("basicspace") + get_var ("unitspace")  * dur_f;
134 }
135
136 void
137 Paper_def::set_lookup (int i, Lookup*l)
138 {
139   if (lookup_p_assoc_p_->elt_b (i))
140     {
141       delete lookup_p_assoc_p_->elem (i);
142     }
143   l ->paper_l_ = this;
144   (*lookup_p_assoc_p_)[i] = l;
145 }
146
147 Real
148 Paper_def::interline_f () const
149 {
150   return get_var ("interline");
151 }
152
153 Real
154 Paper_def::rule_thickness () const
155 {
156   return get_var ("rulethickness");
157 }
158
159 Real
160 Paper_def::staffline_f () const
161 {
162   return get_var ("rulethickness");
163 }
164
165 Real
166 Paper_def::staffheight_f () const
167 {
168   return get_var ("staffheight");
169 }
170
171 Real
172 Paper_def::interbeam_f (int multiplicity_i) const
173 {
174   if (multiplicity_i <= 3)
175     return get_var ("interbeam");
176   else
177     return get_var ("interbeam4");
178 }
179
180 Real
181 Paper_def::internote_f () const
182 {
183   return get_var ("internote");
184 }
185
186 Real
187 Paper_def::note_width () const
188 {
189   return get_var ("notewidth");
190 }
191
192 void
193 Paper_def::print () const
194 {
195 #ifndef NPRINT
196   Music_output_def::print ();
197   DOUT << "Paper {";
198
199   for (Assoc_iter<int, Lookup*> ai(*lookup_p_assoc_p_); ai.ok (); ai++)
200     {
201       DOUT << "Lookup: " << ai.key () ;
202       ai.val ()->print ();
203     }
204
205   for (Assoc_iter<String,Identifier*> i (*scope_p_); i.ok (); i++)
206     {
207       DOUT << i.key () << "= ";
208       DOUT << i.val ()->str () << '\n';
209     }
210   DOUT << "}\n";
211 #endif
212 }
213
214 Lookup const *
215 Paper_def::lookup_l (int i) const
216 {
217   return (*lookup_p_assoc_p_)[i];
218 }
219
220 IMPLEMENT_IS_TYPE_B1 (Paper_def, Music_output_def);
221
222 String
223 Paper_def::TeX_output_settings_str () const
224 {
225   String s ("\n ");
226   for (Assoc_iter<String,Identifier*> i (*scope_p_); i.ok (); i++)
227     s += String ("\\def\\mudelapaper") + i.key () 
228       + "{" + i.val ()->str () + "}\n";
229   s +=  *scope_p_->elem ("texsetting")->access_String ();
230   return s;
231 }
232
233 int Paper_def::default_count_i_ = 0;
234
235 int
236 Paper_def::get_next_default_count () const
237 {
238   return default_count_i_ ++;
239 }
240