]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
release: 1.1.18
[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 "misc.hh"
12 #include "paper-def.hh"
13 #include "debug.hh"
14 #include "lookup.hh"
15 #include "score-engraver.hh"
16 #include "p-score.hh"
17 #include "identifier.hh"
18 #include "main.hh"
19 #include "scope.hh"
20 #include "dictionary-iter.hh"
21 #include "file-results.hh" // urg? header_global_p
22 #include "paper-outputter.hh"
23 #include "paper-stream.hh"
24
25 Paper_def::Paper_def ()
26 {
27   lookup_p_tab_p_ = new Hash_table<int, Lookup*>;
28 }
29
30
31 Paper_def::~Paper_def ()
32 {
33   for (Hash_table_iter<int, Lookup*> ai(*lookup_p_tab_p_); ai.ok (); ai++)
34     {
35       delete ai.val ();
36     }
37   
38   delete lookup_p_tab_p_;
39 }
40
41 Paper_def::Paper_def (Paper_def const&s)
42   : Music_output_def (s)
43 {
44   lookup_p_tab_p_ = new Hash_table<int, Lookup*>;
45   for (Hash_table_iter<int, Lookup*> ai(*s.lookup_p_tab_p_); ai.ok (); ai++)
46     {
47       Lookup * l = new Lookup (*ai.val ());
48       l->paper_l_ = this;
49       set_lookup (ai.key(), l);
50     }
51 }
52
53 Real
54 Paper_def::get_var (String s) const
55 {
56   if (!scope_p_->elem_b (s))
57     error (_f ("unknown paper variable: `%s\'", s));
58   Real * p = scope_p_->elem (s)->access_content_Real (false);
59   if (!p)
60     {
61       error (_ ("not a real variable"));
62       return 0.0;
63     }
64
65   return *p;
66 }
67
68 Interval
69 Paper_def::line_dimensions_int (int n) const
70 {
71   if (!shape_int_a_.size ())
72     if (n)
73       return Interval (0, linewidth_f ());
74     else
75       return Interval (get_var ("indent"), linewidth_f ());
76
77   if (n >= shape_int_a_.size ())
78     n = shape_int_a_.size () -1;
79
80   return shape_int_a_[n];
81 }
82
83 Real
84 Paper_def::beam_thickness_f () const
85 {
86   return get_var ("beam_thickness");
87 }
88
89 Real
90 Paper_def::linewidth_f () const
91 {
92   return get_var ("linewidth");
93 }
94
95 Real
96 Paper_def::duration_to_dist (Moment d,Real k) const
97 {
98   return arithmetic_spacing (d,k);
99 }
100
101
102 /**
103   Get the measure wide constant for arithmetic spacing.
104
105   @see
106   John S. Gourlay. ``Spacing a Line of Music,'' Technical Report
107   OSU-CISRC-10/87-TR35, Department of Computer and Information Science,
108   The Ohio State University, 1987.
109
110   */
111 Real
112 Paper_def::arithmetic_constant (Moment d) const
113 {
114   return get_var ("arithmetic_basicspace") - log_2 (Moment (1,8) <? d);
115 }
116
117 Real
118 Paper_def::arithmetic_spacing (Moment d ,Real k) const
119 {
120   return (log_2 (d) + k)* get_var ("arithmetic_multiplier");
121 }
122
123 void
124 Paper_def::set_lookup (int i, Lookup*l)
125 {
126   if (lookup_p_tab_p_->elem_b (i))
127     {
128       delete lookup_p_tab_p_->elem (i);
129     }
130   l ->paper_l_ = this;
131   (*lookup_p_tab_p_)[i] = l;
132 }
133
134 Real
135 Paper_def::interline_f () const
136 {
137   return get_var ("interline");
138 }
139
140 Real
141 Paper_def::rule_thickness () const
142 {
143   return get_var ("rulethickness");
144 }
145
146 Real
147 Paper_def::staffline_f () const
148 {
149   return get_var ("rulethickness");
150 }
151
152 Real
153 Paper_def::staffheight_f () const
154 {
155   return get_var ("staffheight");
156 }
157
158 Real
159 Paper_def::interbeam_f (int multiplicity_i) const
160 {
161   if (multiplicity_i <= 3)
162     return get_var ("interbeam");
163   else
164     return get_var ("interbeam4");
165 }
166
167 Real
168 Paper_def::internote_f () const
169 {
170   return get_var ("interline") /2.0 ;
171 }
172
173 Real
174 Paper_def::note_width () const
175 {
176   return get_var ("notewidth");
177 }
178
179 void
180 Paper_def::print () const
181 {
182 #ifndef NPRINT
183   Music_output_def::print ();
184   DOUT << "Paper {";
185
186   for (Hash_table_iter<int, Lookup*> ai(*lookup_p_tab_p_); ai.ok (); ai++)
187     {
188       DOUT << "Lookup: " << ai.key () ;
189       ai.val ()->print ();
190     }
191
192   DOUT << "}\n";
193 #endif
194 }
195
196 Lookup const *
197 Paper_def::lookup_l (int i) const
198 {
199   return (*lookup_p_tab_p_)[i];
200 }
201
202
203
204 int Paper_def::default_count_i_ = 0;
205
206 int
207 Paper_def::get_next_default_count () const
208 {
209   return default_count_i_ ++;
210 }
211
212 void
213 Paper_def::reset_default_count()
214 {
215   default_count_i_ = 0;
216 }
217
218 extern char const* lily_version_number_sz ();
219
220 Paper_outputter*
221 Paper_def::paper_outputter_p (Paper_stream* os_p, Header* header_l, String origin_str) const
222 {
223   Paper_outputter* p = new Paper_outputter (os_p);
224
225   // for now; breaks -fscm output
226   p->output_comment (_ ("outputting Score, defined at: "));
227   p->output_comment (origin_str);
228
229   p->output_version();
230   if (header_global_p)
231     p->output_scope (header_global_p, "mudela");
232   if (header_l)
233     p->output_scope (header_l, "mudela");
234   if (scope_p_)
235     p->output_scope (scope_p_, "mudelapaper");
236   
237   if (output_global_ch == String("tex"))
238     {
239       *p->outstream_l_ << *scope_p_->elem ("texsetting")->access_content_String (false);
240     }
241   
242
243   SCM scm = gh_list (ly_symbol ("experimental-on"), SCM_UNDEFINED);
244   p->output_scheme (scm);
245   scm = gh_list (ly_symbol ("header-end"), SCM_UNDEFINED);
246   p->output_scheme (scm);
247
248   return p;
249 }
250
251 Paper_stream*
252 Paper_def::paper_stream_p () const
253 {
254   String outname = base_output_str ();
255
256   if (outname != "-")
257     outname += String (".") + output_global_ch;
258   *mlog << _f ("Paper output to %s...", 
259                outname == "-" ? String ("<stdout>") : outname) << endl;
260
261   target_str_global_array.push (outname);
262   return new Paper_stream (outname);
263 }
264
265
266 String
267 Paper_def::base_output_str () const
268 {
269   String str = get_default_output ();
270
271   if (str.empty_b ())
272     {
273       str = default_outname_base_global;
274       int def = get_next_default_count ();
275       if (def)
276         str += "-" + to_str (def);
277     }
278   return str;
279 }
280