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