]> git.donarmstrong.com Git - lilypond.git/blob - lily/spanner.cc
release: 1.2.5
[lilypond.git] / lily / spanner.cc
1 /*
2   spanner.cc -- implement Spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "dimension-cache.hh"
9 #include "debug.hh"
10 #include "spanner.hh"
11 #include "paper-column.hh"
12 #include "paper-score.hh"
13 #include "molecule.hh"
14 #include "paper-outputter.hh"
15 #include "score-column.hh"
16 #include "line-of-score.hh"
17
18 void
19 Spanner::do_print() const
20 {
21 #ifndef NPRINT
22   DOUT << "Between " << classname (spanned_drul_[LEFT])
23        << " and " << classname (spanned_drul_[RIGHT]) << '\n';
24
25   if (broken_b ())
26     DOUT << "Broken in " << to_str (broken_into_l_arr_.size ()) << " pieces";
27 #endif
28 }
29
30 void
31 Spanner::break_into_pieces ()
32 {
33   if (broken_b ())
34     return; 
35          
36   Item * left = spanned_drul_[LEFT];
37   Item * right = spanned_drul_[RIGHT];
38
39   if  (left == right)
40     {
41       warning (_ ("left spanpoint is right spanpoint\n"));
42       return;
43     }
44   
45   Link_array<Item> break_points = pscore_l_->broken_col_range (left,right);
46
47   break_points.insert (left,0);
48   break_points.push (right);
49
50   for (int i=1; i < break_points.size(); i++) 
51     {
52       Breaking_information info;
53       info.bounds_[LEFT] = break_points[i-1];
54       info.bounds_[RIGHT] = break_points[i];
55       Direction d = LEFT;
56       do
57         {
58           Item *&pc_l = info.bounds_[d] ;
59           if (!pc_l->line_l())
60             pc_l =  pc_l->find_prebroken_piece(- d);
61           
62           assert (pc_l);
63           if (!info.line_l_)
64             info.line_l_ = pc_l-> line_l ();
65           else
66             assert( info.line_l_ = pc_l->line_l ());
67           
68         }
69       while ((flip(&d))!= LEFT);
70
71       Spanner *span_p = dynamic_cast<Spanner*>(clone ());
72       span_p->set_bounds(LEFT,info.bounds_[LEFT]);
73       span_p->set_bounds(RIGHT,info.bounds_[RIGHT]);
74       pscore_l_->typeset_element (span_p);
75
76       info.broken_spanner_l_ = span_p;
77       span_p->handle_broken_dependencies();
78
79       broken_into_l_arr_.push (span_p);
80     }
81 }
82
83 void
84 Spanner::set_my_columns()
85 {
86   Direction i = (Direction)1;
87   do 
88     {
89       if (!spanned_drul_[i]->line_l())
90         set_bounds(i,spanned_drul_[i]->find_prebroken_piece((Direction)-i));
91     } 
92   while (flip(&i) != 1);
93 }       
94
95 void
96 Spanner::set_bounds(Direction d, Item*i)
97 {
98   spanned_drul_[d] =i;
99   if (i)
100     {
101       i->used_b_ = true;
102     }
103
104   if (d== LEFT)
105     {
106       set_parent ( i, X_AXIS);
107     }
108   
109   if  (spanned_drul_[Direction(-d)] == spanned_drul_[d]
110        && i)
111     warning (_f ("Spanner `%s\' with equal left and right spanpoints", classname (this)));
112 }
113
114 void
115 Spanner::do_break_processing()
116 {
117   if (!line_l())
118     break_into_pieces ();
119   else 
120     handle_broken_dependencies();
121 }
122
123 Spanner::Spanner ()
124 {
125   spanned_drul_[LEFT]=0;
126   spanned_drul_[RIGHT]=0;
127 }
128
129 Spanner::Spanner (Spanner const &s)
130   : Score_element (s)
131 {
132   spanned_drul_[LEFT] = spanned_drul_[RIGHT] =0;
133 }
134
135
136 Interval
137 Spanner::do_width() const
138 {  
139   Real l = spanned_drul_[LEFT]->relative_coordinate (0, X_AXIS);
140   Real r = spanned_drul_[RIGHT]->relative_coordinate (0, X_AXIS);
141
142   if (r< l)
143     warning ("Spanner with negative length");
144         
145   return Interval (0, r-l);
146 }
147
148 Line_of_score *
149 Spanner::line_l() const
150 {
151   if (!spanned_drul_[LEFT] || !spanned_drul_[RIGHT])
152     return 0;
153   if (spanned_drul_[LEFT]->line_l() != spanned_drul_[RIGHT]->line_l())
154     return 0;
155   return spanned_drul_[LEFT]->line_l();
156 }
157
158
159 Spanner*
160 Spanner::find_broken_piece (Line_of_score*l) const
161 {
162   for (int i=0; i < broken_into_l_arr_.size (); i++)
163     {
164       if (broken_into_l_arr_[i]->line_l () == l)
165         return broken_into_l_arr_[i];
166     }
167
168   return 0;                                
169 }
170
171 bool
172 Spanner::broken_b() const
173 {
174   return broken_into_l_arr_.size();
175 }
176
177 Array<Rod>
178 Spanner::get_rods () const
179 {
180   Array<Rod> r;
181   return r;
182 }
183
184 Array<Spring>
185 Spanner::get_springs () const
186 {
187   Array<Spring> s;
188   return s;    
189 }
190
191 void
192 Spanner::do_space_processing ()
193 {
194   Array<Rod> rs (get_rods ());
195   for (int i=0; i < rs.size (); i++)
196     {
197       rs[i].add_to_cols ();
198     }
199
200   Array<Spring> ss (get_springs ());
201   for (int i=0; i < ss.size (); i++)
202     {
203       ss[i].add_to_cols ();
204     }
205 }
206
207 /*
208   UGH.
209  */
210 void
211 Spanner::handle_broken_dependents ()
212 {
213   Spanner *unbrok = dynamic_cast<Spanner*> (original_l_);
214   if (!unbrok || parent_l(Y_AXIS))
215     return;
216   
217   Spanner *refpoint = dynamic_cast<Spanner*> (unbrok->parent_l (Y_AXIS));
218   
219   if (refpoint)
220     {
221       Spanner * broken_refpoint = refpoint->find_broken_piece (line_l ());
222       if (broken_refpoint)
223         set_parent ( broken_refpoint,Y_AXIS);
224       else
225         programming_error ("Spanner y -refpoint lost.");
226     }
227 }
228
229 // If this is a broken spanner, return the amount the left end is to
230 // be shifted horizontally so that the spanner starts after the
231 // initial clef and key on the staves. This is necessary for ties,
232 // slurs, crescendo and decrescendo signs, for example.
233 Real
234 Spanner::get_broken_left_end_align () const
235 {
236   int i;
237   Line_of_score *l;
238   Score_column *sc = dynamic_cast<Score_column*> (spanned_drul_[LEFT]->column_l());
239
240   // Relevant only if left span point is first column in line
241   if(sc != NULL && sc->line_l ()->cols_.find_i (sc) == 0)
242     {
243       // We could possibly return the right edge of the whole Score_column here,
244       // but we do a full search for the Break_align_item.
245       for(i = 0; i < sc->elem_l_arr_.size (); i++)
246         {
247           if(0 == strcmp (classname (sc->elem_l_arr_[i]), "Break_align_item"))
248             {
249               return sc->elem_l_arr_[i]->extent (X_AXIS) [RIGHT];
250             }
251         }
252     }
253
254   return 0.0;
255 }