]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
release: 1.1.0
[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 "assoc-iter.hh"
17 #include "score-engraver.hh"
18 #include "p-score.hh"
19 #include "identifier.hh"
20 #include "main.hh"
21 #include "scope.hh"
22 #include "dictionary-iter.hh"
23 #include "file-results.hh" // urg? header_global_p
24 #include "paper-outputter.hh"
25 #include "paper-stream.hh"
26
27 Paper_def::Paper_def ()
28 {
29   lookup_p_assoc_p_ = new Assoc<int, Lookup*>;
30 }
31
32
33 Paper_def::~Paper_def ()
34 {
35   for (Assoc_iter<int, Lookup*> ai(*lookup_p_assoc_p_); ai.ok (); ai++)
36     {
37       delete ai.val ();
38     }
39   
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
55 Real
56 Paper_def::get_var (String s) const
57 {
58   if (!scope_p_->elem_b (s))
59     error (_f ("unknown paper variable: `%s\'", s));
60   Real * p = scope_p_->elem (s)->access_Real (false);
61   if (!p)
62     {
63       error (_ ("not a real variable"));
64       return 0.0;
65     }
66
67   return *p;
68 }
69
70 Interval
71 Paper_def::line_dimensions_int (int n) const
72 {
73   if (!shape_int_a_.size ())
74     if (n)
75       return Interval (0, linewidth_f ());
76     else
77       return Interval (get_var ("indent"), linewidth_f ());
78
79   if (n >= shape_int_a_.size ())
80     n = shape_int_a_.size () -1;
81
82   return shape_int_a_[n];
83 }
84
85 Real
86 Paper_def::beam_thickness_f () const
87 {
88   return get_var ("beam_thickness");
89 }
90
91 Real
92 Paper_def::linewidth_f () const
93 {
94   return get_var ("linewidth");
95 }
96
97 Real
98 Paper_def::duration_to_dist (Moment d,Real k) const
99 {
100   return arithmetic_spacing (d,k);
101 }
102
103
104 /**
105   Get the measure wide constant for arithmetic.
106
107   @see
108   John S. Gourlay. ``Spacing a Line of Music,'' Technical Report
109   OSU-CISRC-10/87-TR35, Department of Computer and Information Science,
110   The Ohio State University, 1987.
111
112   */
113 Real
114 Paper_def::arithmetic_constant (Moment d) const
115 {
116   return get_var ("arithmetic_basicspace") - log_2 (Moment (1,8) <? d);
117 }
118
119 Real
120 Paper_def::arithmetic_spacing (Moment d ,Real k) const
121 {
122   return (log_2 (d) + k)* get_var ("arithmetic_multiplier");
123 }
124
125 Real
126 Paper_def::geometric_spacing (Moment d) const
127 {
128   Real dur_f = (d) ?pow (get_var ("geometric"), log_2 (d)) : 0;
129   return get_var ("basicspace") + get_var ("unitspace")  * dur_f;
130 }
131
132 void
133 Paper_def::set_lookup (int i, Lookup*l)
134 {
135   if (lookup_p_assoc_p_->elem_b (i))
136     {
137       delete lookup_p_assoc_p_->elem (i);
138     }
139   l ->paper_l_ = this;
140   (*lookup_p_assoc_p_)[i] = l;
141 }
142
143 Real
144 Paper_def::interline_f () const
145 {
146   return get_var ("interline");
147 }
148
149 Real
150 Paper_def::rule_thickness () const
151 {
152   return get_var ("rulethickness");
153 }
154
155 Real
156 Paper_def::staffline_f () const
157 {
158   return get_var ("rulethickness");
159 }
160
161 Real
162 Paper_def::staffheight_f () const
163 {
164   return get_var ("staffheight");
165 }
166
167 Real
168 Paper_def::interbeam_f (int multiplicity_i) const
169 {
170   if (multiplicity_i <= 3)
171     return get_var ("interbeam");
172   else
173     return get_var ("interbeam4");
174 }
175
176 Real
177 Paper_def::internote_f () const
178 {
179   return get_var ("interline") /2.0 ;
180 }
181
182 Real
183 Paper_def::note_width () const
184 {
185   return get_var ("notewidth");
186 }
187
188 void
189 Paper_def::print () const
190 {
191 #ifndef NPRINT
192   Music_output_def::print ();
193   DOUT << "Paper {";
194
195   for (Assoc_iter<int, Lookup*> ai(*lookup_p_assoc_p_); ai.ok (); ai++)
196     {
197       DOUT << "Lookup: " << ai.key () ;
198       ai.val ()->print ();
199     }
200
201   DOUT << "}\n";
202 #endif
203 }
204
205 Lookup const *
206 Paper_def::lookup_l (int i) const
207 {
208   return (*lookup_p_assoc_p_)[i];
209 }
210
211 IMPLEMENT_IS_TYPE_B1 (Paper_def, Music_output_def);
212
213 int Paper_def::default_count_i_ = 0;
214
215 int
216 Paper_def::get_next_default_count () const
217 {
218   return default_count_i_ ++;
219 }
220
221 //urg
222 extern char const* lily_version_number_sz ();
223
224 void
225 output_def (Paper_outputter* p, String key, String val)
226 {
227   SCM args_scm =
228     gh_cons (gh_str02scm (key.ch_l ()), gh_cons (gh_str02scm (val.ch_l ()), SCM_EOL));
229   SCM scm =
230     ly_append (ly_lambda_o (),
231     ly_list1 (ly_append (ly_func_o ("lily-def"), args_scm)));
232   p->output_scheme (scm);
233 }
234
235 void
236 output_header (Paper_outputter* p, Scope *head)
237 {
238   if (!head)
239     return;
240
241   String id_str = "Lily was here";
242   if (no_timestamps_global_b)
243     id_str += ".";
244   else
245     id_str += String (", ") + lily_version_number_sz ();
246   output_def (p, "lily_id_string", id_str);
247   
248   for (Dictionary_iter<Identifier*> i (*head); i.ok (); i++)
249     {
250       if (!i.val ()->access_String_identifier ())
251         continue;
252       
253       String val = *i.val()->access_String_identifier ()->data_p_;
254       output_def (p, i.key (), val);
255     }
256 }
257
258 void
259 Paper_def::output_settings (Paper_outputter* p) const
260 {
261   for (Dictionary_iter<Identifier*> i (*scope_p_); i.ok (); i++)
262     output_def (p, String ("mudelapaper") + i.key (), i.val ()->str ());
263   p->output_string (*scope_p_->elem (String (output_global_ch) + "setting")->access_String ());
264 }
265
266 Paper_outputter*
267 Paper_def::paper_outputter_p (Paper_stream* os_p, Header* header_l, String origin_str) const
268 {
269   Paper_outputter* p = new Paper_outputter (os_p);
270
271   output_header (p, header_global_p);
272   p->output_comment (_ ("outputting Score, defined at: "));
273   p->output_comment (origin_str);
274
275   output_header (p, header_l);
276
277   output_settings (p);
278
279   SCM scm =
280     ly_append (ly_lambda_o (),
281     ly_list1 (ly_append (ly_func_o ("experimental-on"), SCM_EOL)));
282
283   p->output_scheme (scm);
284
285   scm =
286     ly_append (ly_lambda_o (),
287     ly_list1 (ly_append (ly_func_o ("header-end"), SCM_EOL)));
288
289   p->output_scheme (scm);
290
291   return p;
292 }
293
294 Paper_stream*
295 Paper_def::paper_stream_p () const
296 {
297   String outname = base_output_str ();
298
299   if (outname != "-")
300     outname += String (".") + output_global_ch;
301   *mlog << _f ("Paper output to %s...", 
302                outname == "-" ? String ("<stdout>") : outname ) << endl;
303   target_str_global_array.push (outname);
304   return new Paper_stream (outname);
305 }
306
307
308 String
309 Paper_def::base_output_str () const
310 {
311   String str = get_default_output ();
312
313   if (str.empty_b ())
314     {
315       str = default_outname_base_global;
316       int def = get_next_default_count ();
317       if (def)
318         str += "-" + to_str (def);
319     }
320   return str;
321 }
322