]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-loose-columns.cc
Update source file headers. Fixes using standard GNU package conventions.
[lilypond.git] / lily / spacing-loose-columns.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "system.hh"
21 #include "paper-column.hh"
22 #include "column-x-positions.hh"
23 #include "pointer-group-interface.hh"
24 #include "staff-spacing.hh"
25 #include "note-spacing.hh"
26 #include "spacing-spanner.hh"
27 #include "warn.hh"
28 #include "moment.hh"
29 #include "spacing-options.hh"
30
31 /* Find the loose columns in POSNS, and drape them around the columns
32    specified in BETWEEN-COLS.  */
33 void
34 set_loose_columns (System *which, Column_x_positions const *posns)
35 {
36   int loose_col_count = posns->loose_cols_.size ();
37   if (!loose_col_count)
38     return;
39
40   for (int i = 0; i < loose_col_count; i++)
41     {
42       int divide_over = 1;
43       Item *loose = dynamic_cast<Item *> (posns->loose_cols_[i]);
44       Paper_column *col = dynamic_cast<Paper_column *> (loose);
45
46       if (col->get_system ())
47         continue;
48
49       Item *left = 0;
50       Item *right = 0;
51
52       vector<Item*> clique;
53       while (1)
54         {
55           SCM between = loose->get_object ("between-cols");
56           if (!scm_is_pair (between))
57             break;
58
59           Item *le = dynamic_cast<Item *> (unsmob_grob (scm_car (between)));
60           Item *re = dynamic_cast<Item *> (unsmob_grob (scm_cdr (between)));
61
62           if (! (le && re))
63             break;
64
65           if (!left && le)
66             {
67               left = le->get_column ();
68               if (!left->get_system ())
69                 left = left->find_prebroken_piece (RIGHT);
70
71               clique.push_back (left);
72             }
73
74           clique.push_back (loose);
75
76           divide_over++;
77           loose = right = re->get_column ();
78         }
79
80       if (right->get_system ())
81         ; /* do nothing */
82       else if (right->find_prebroken_piece (LEFT)
83                && right->find_prebroken_piece (LEFT)->get_system () == which)
84         right = right->find_prebroken_piece (LEFT);
85       else if (Paper_column::get_rank (which->get_bound (RIGHT)) < Paper_column::get_rank (right))
86         
87         right = which->get_bound (RIGHT);
88       else
89         {
90           clique.back ()->programming_error ("Loose column does not have right side to attach to.");
91           System *base_system = dynamic_cast<System*> (which->original ());
92           int j = Paper_column::get_rank (clique.back ()) + 1;
93           int end_rank = Paper_column::get_rank (which->get_bound (RIGHT));
94           extract_grob_set (base_system, "columns", base_cols);
95           for (; j < end_rank; j++)
96             {
97               if (base_cols[j]->get_system () == which)
98                 right = dynamic_cast<Item*> ((Grob*)base_cols[j]);
99             }
100         }
101       
102
103       if (!right)
104         {
105           programming_error ("Can't attach loose column sensibly. Attaching to end of system.");
106           right = which->get_bound (RIGHT);
107         }
108       Grob *common = right->common_refpoint (left, X_AXIS);
109
110       clique.push_back (right);
111
112       vector<Real> clique_spacing;
113       clique_spacing.push_back (0.0);
114       for (vsize j = 1; j + 1 < clique.size (); j ++)
115         {
116           Grob *clique_col = clique[j];
117
118           Paper_column *loose_col = dynamic_cast<Paper_column *> (clique[j]);
119           Paper_column *next_col = dynamic_cast<Paper_column *> (clique[j + 1]);
120
121           Grob *spacing = unsmob_grob (clique_col->get_object ("spacing"));
122           if (Grob *grace_spacing = unsmob_grob (clique_col->get_object ("grace-spacing")))
123             {
124               spacing = grace_spacing;
125             }
126           
127           Spacing_options options;
128           if (spacing)
129             options.init_from_grob (spacing);
130           else
131             programming_error ("Column without spacing object");
132
133           Real base_note_space = 0.0;
134
135           if (Paper_column::is_musical (next_col)
136               && Paper_column::is_musical (loose_col))
137             base_note_space = Spacing_spanner::note_spacing (spacing, loose_col, next_col,
138                                                              &options);
139           else
140             {
141               Spring spring = Spacing_spanner::standard_breakable_column_spacing (spacing,
142                                                                                   loose_col, next_col,
143                                                                                   &options);
144
145               base_note_space = spring.distance ();
146             }
147
148           base_note_space = max (base_note_space,
149                                  robust_relative_extent (loose_col, loose_col, X_AXIS)[RIGHT]
150                                  - robust_relative_extent (next_col, next_col, X_AXIS)[LEFT]);
151           
152           clique_spacing.push_back (base_note_space);
153         }
154
155       Real default_padding = 1.0;
156       clique_spacing.push_back (default_padding);
157
158       Real right_point = robust_relative_extent (clique.back (), common, X_AXIS)[LEFT];
159       
160           
161       Grob *finished_right_column = clique.back ();
162       
163       for (vsize j = clique.size () - 2; j > 0; j--)
164         {
165           Paper_column *clique_col = dynamic_cast<Paper_column *> (clique[j]);
166           
167           right_point = finished_right_column->relative_coordinate (common, X_AXIS);
168
169           Real distance_to_next = clique_spacing[j];
170           
171           Real my_offset = right_point - distance_to_next;
172
173           clique_col->set_system (which);
174           clique_col->translate_axis (my_offset - clique_col->relative_coordinate (common, X_AXIS), X_AXIS);      
175
176           finished_right_column = clique_col;
177         }
178  
179     }
180 }
181