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