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