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