]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-def.cc
9c6be3c464fe8fdedaed620100cb15c65b652699
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10 #include "all-font-metrics.hh"
11 #include "string.hh"
12 #include "misc.hh"
13 #include "paper-def.hh"
14 #include "debug.hh"
15 #include "lookup.hh"
16 #include "score-engraver.hh"
17 #include "paper-score.hh"
18 #include "identifier.hh"
19 #include "main.hh"
20 #include "scope.hh"
21 #include "dictionary-iter.hh"
22 #include "file-results.hh" // urg? header_global_p
23 #include "paper-outputter.hh"
24 #include "paper-stream.hh"
25
26
27 Paper_def::Paper_def ()
28 {
29   lookup_p_tab_p_ = new Hash_table<int, Lookup*>;
30   lookup_p_tab_p_->hash_func_ = int_hash;
31 }
32
33
34 Paper_def::~Paper_def ()
35 {
36   for (Hash_table_iter<int, Lookup*> ai(*lookup_p_tab_p_); ai.ok (); ai++)
37     {
38       delete ai.val ();
39     }
40   
41   delete lookup_p_tab_p_;
42 }
43
44 Paper_def::Paper_def (Paper_def const&s)
45   : Music_output_def (s)
46 {
47   shape_int_a_ = s.shape_int_a_;
48   lookup_p_tab_p_ = new Hash_table<int, Lookup*>;
49   lookup_p_tab_p_->hash_func_ = int_hash;
50   
51   for (Hash_table_iter<int, Lookup*> ai(*s.lookup_p_tab_p_); ai.ok (); ai++)
52     {
53       Lookup * l = new Lookup (*ai.val ());
54       set_lookup (ai.key(), l);
55     }
56 }
57
58
59 Real
60 Paper_def::get_var (String s) const
61 {
62   return get_realvar (ly_symbol2scm (s.ch_C()));
63 }
64
65 Real
66 Paper_def::get_realvar (SCM s) const
67 {
68   if (!scope_p_->elem_b (s))
69     error (_f ("unknown paper variable: `%s'", ly_symbol2string (s)));
70   Real * p = scope_p_->elem (s)->access_content_Real (false);
71   if (!p)
72     {
73       error (_("not a real variable"));
74       return 0.0;
75     }
76
77   return *p;
78 }
79
80
81 Interval
82 Paper_def::line_dimensions_int (int n) const
83 {
84   SCM s = default_properties_ [ly_symbol2scm ("margin-shape")];
85   if (!gh_pair_p (s))
86     {
87       Real lw =  get_var ("linewidth");
88       Real ind = n? 0.0:get_var ("indent");
89
90       return Interval (ind, lw);
91     }
92
93  
94   SCM last = SCM_EOL;
95   while (gh_pair_p (s) && n --)
96     {
97       last = s;
98       s = gh_cdr (s);
99     }
100
101   if (s == SCM_EOL)
102     {
103       s = last;
104     }
105
106   SCM pair = gh_car (s);
107   
108   return Interval (gh_scm2double (gh_car (pair)),
109                    gh_scm2double (gh_cdr (pair)));
110 }
111
112 Real
113 Paper_def::length_mom_to_dist (Moment d,Real k) const
114 {
115   return arithmetic_spacing (d,k);
116 }
117
118
119 /**
120   Get the measure wide constant for arithmetic spacing.
121
122   @see
123   John S. Gourlay. ``Spacing a Line of Music,'' Technical Report
124   OSU-CISRC-10/87-TR35, Department of Computer and Information Science,
125   The Ohio State University, 1987.
126
127   */
128 Real
129 Paper_def::arithmetic_constant (Moment d) const
130 {
131   return get_var ("arithmetic_basicspace") - log_2 (Moment (1,8) <? d);
132 }
133
134 Real
135 Paper_def::arithmetic_spacing (Moment d ,Real k) const
136 {
137   return (log_2 (d) + k)* get_var ("arithmetic_multiplier");
138 }
139
140 void
141 Paper_def::set_lookup (int i, Lookup*l)
142 {
143   if (lookup_p_tab_p_->elem_b (i))
144     {
145       delete lookup_p_tab_p_->elem (i);
146     }
147   (*lookup_p_tab_p_)[i] = l;
148 }
149
150
151 Real
152 Paper_def::interbeam_f (int multiplicity_i) const
153 {
154   if (multiplicity_i <= 3)
155     return get_var ("interbeam");
156   else
157     return get_var ("interbeam4");
158 }
159
160
161 void
162 Paper_def::print () const
163 {
164 #ifndef NPRINT
165   Music_output_def::print ();
166   DEBUG_OUT << "Paper {";
167
168   for (Hash_table_iter<int, Lookup*> ai(*lookup_p_tab_p_); ai.ok (); ai++)
169     {
170       DEBUG_OUT << "Lookup: " << ai.key () << " = " << ai.val ()->font_name_ << '\n';
171     }
172
173   DEBUG_OUT << "}\n";
174 #endif
175 }
176
177 Lookup const *
178 Paper_def::lookup_l (int i) const
179 {
180   return (*lookup_p_tab_p_)[i];
181 }
182
183
184
185 int Paper_def::default_count_i_ = 0;
186
187 int
188 Paper_def::get_next_default_count () const
189 {
190   return default_count_i_ ++;
191 }
192
193 void
194 Paper_def::reset_default_count()
195 {
196   default_count_i_ = 0;
197 }
198
199
200 Paper_stream*
201 Paper_def::paper_stream_p () const
202 {
203   String outname = base_output_str ();
204
205   if (outname != "-")
206     outname += String (".") + output_global_ch;
207   progress_indication (_f ("paper output to %s...",
208                            outname == "-" ? String ("<stdout>") : outname));
209                        
210
211   target_str_global_array.push (outname);
212   return new Paper_stream (outname);
213 }
214
215
216 String
217 Paper_def::base_output_str () const
218 {
219   String str = get_default_output ();
220
221   if (str.empty_b ())
222     {
223       str = default_outname_base_global;
224       int def = get_next_default_count ();
225       if (def)
226         str += "-" + to_str (def);
227     }
228   return str;
229 }
230
231