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