]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
ead9c06ae5930001a3c42f56daed25ea3accdf53
[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
58 Real
59 Paper_def::get_var (String s) const
60 {
61   return get_realvar (ly_symbol (s));
62 }
63
64 Real
65 Paper_def::get_realvar (SCM s) const
66 {
67   if (!scope_p_->elem_b (s))
68     error (_f ("unknown paper variable: `%s'", symbol_to_string (s)));
69   Real * p = scope_p_->elem (s)->access_content_Real (false);
70   if (!p)
71     {
72       error (_("not a real variable"));
73       return 0.0;
74     }
75
76   return *p;
77 }
78
79 Interval
80 Paper_def::line_dimensions_int (int n) const
81 {
82   if (!shape_int_a_.size ())
83     {
84       Real lw =  get_realvar (linewidth_scm_sym);
85       Real ind = n? 0.0:get_var ("indent");
86
87       return Interval (ind, lw);
88     }
89   
90   if (n >= shape_int_a_.size ())
91     n = shape_int_a_.size () -1;
92
93   return shape_int_a_[n];
94 }
95
96
97 Real
98 Paper_def::length_mom_to_dist (Moment d,Real k) const
99 {
100   return arithmetic_spacing (d,k);
101 }
102
103
104 /**
105   Get the measure wide constant for arithmetic spacing.
106
107   @see
108   John S. Gourlay. ``Spacing a Line of Music,'' Technical Report
109   OSU-CISRC-10/87-TR35, Department of Computer and Information Science,
110   The Ohio State University, 1987.
111
112   */
113 Real
114 Paper_def::arithmetic_constant (Moment d) const
115 {
116   return get_var ("arithmetic_basicspace") - log_2 (Moment (1,8) <? d);
117 }
118
119 Real
120 Paper_def::arithmetic_spacing (Moment d ,Real k) const
121 {
122   return (log_2 (d) + k)* get_var ("arithmetic_multiplier");
123 }
124
125 void
126 Paper_def::set_lookup (int i, Lookup*l)
127 {
128   if (lookup_p_tab_p_->elem_b (i))
129     {
130       delete lookup_p_tab_p_->elem (i);
131     }
132   (*lookup_p_tab_p_)[i] = l;
133 }
134
135
136 Real
137 Paper_def::interbeam_f (int multiplicity_i) const
138 {
139   if (multiplicity_i <= 3)
140     return get_realvar (interbeam_scm_sym);
141   else
142     return get_realvar (interbeam4_scm_sym);
143 }
144
145
146 void
147 Paper_def::print () const
148 {
149 #ifndef NPRINT
150   Music_output_def::print ();
151   DEBUG_OUT << "Paper {";
152
153   for (Hash_table_iter<int, Lookup*> ai(*lookup_p_tab_p_); ai.ok (); ai++)
154     {
155       DEBUG_OUT << "Lookup: " << ai.key () << " = " << ai.val ()->font_name_ << '\n';
156     }
157
158   DEBUG_OUT << "}\n";
159 #endif
160 }
161
162 Lookup const *
163 Paper_def::lookup_l (int i) const
164 {
165   return (*lookup_p_tab_p_)[i];
166 }
167
168
169
170 int Paper_def::default_count_i_ = 0;
171
172 int
173 Paper_def::get_next_default_count () const
174 {
175   return default_count_i_ ++;
176 }
177
178 void
179 Paper_def::reset_default_count()
180 {
181   default_count_i_ = 0;
182 }
183
184 Paper_outputter*
185 Paper_def::paper_outputter_p (Paper_stream* os_p, Scope* header_l, String origin_str) const
186 {
187   Paper_outputter* p = new Paper_outputter (os_p);
188
189   // for now; breaks -fscm output
190   p->output_comment (_ ("Outputting Score, defined at: "));
191   p->output_comment (origin_str);
192
193   p->output_version();
194   if (header_global_p)
195     p->output_scope (header_global_p, "mudela");
196   if (header_l)
197     p->output_scope (header_l, "mudela");
198   if (scope_p_)
199     p->output_scope (scope_p_, "mudelapaper");
200   
201
202   //  *p->outstream_l_  << *scope_p_->elem (String (output_global_ch) + "setting")->access_content_String (false);
203
204   SCM scm = gh_list (ly_symbol ("experimental-on"), SCM_UNDEFINED);
205   p->output_scheme (scm);
206   scm = gh_list (ly_symbol ("header-end"), SCM_UNDEFINED);
207   p->output_scheme (scm);
208
209   return p;
210 }
211
212 Paper_stream*
213 Paper_def::paper_stream_p () const
214 {
215   String outname = base_output_str ();
216
217   if (outname != "-")
218     outname += String (".") + output_global_ch;
219   *mlog << _f ("paper output to %s...", 
220                outname == "-" ? String ("<stdout>") : outname) << endl;
221
222   target_str_global_array.push (outname);
223   return new Paper_stream (outname);
224 }
225
226
227 String
228 Paper_def::base_output_str () const
229 {
230   String str = get_default_output ();
231
232   if (str.empty_b ())
233     {
234       str = default_outname_base_global;
235       int def = get_next_default_count ();
236       if (def)
237         str += "-" + to_str (def);
238     }
239   return str;
240 }
241