]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-loose-columns.cc
a59fa7d0f9350734baa6717ab25017ed25ec59cf
[lilypond.git] / lily / spacing-loose-columns.cc
1 /*
2   spacing-loose-columns.cc -- implement loose column spacing.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "system.hh"
10 #include "paper-column.hh"
11 #include "column-x-positions.hh"
12 #include "pointer-group-interface.hh"
13 #include "staff-spacing.hh"
14 #include "note-spacing.hh"
15 #include "spacing-spanner.hh"
16 #include "warn.hh"
17 #include "moment.hh"
18 #include "spacing-options.hh"
19
20 /* Find the loose columns in POSNS, and drape them around the columns
21    specified in BETWEEN-COLS.  */
22 void
23 set_loose_columns (System *which, Column_x_positions const *posns)
24 {
25   int loose_col_count = posns->loose_cols_.size ();
26   if (!loose_col_count)
27     return;
28
29   for (int i = 0; i < loose_col_count; i++)
30     {
31       int divide_over = 1;
32       Item *loose = dynamic_cast<Item *> (posns->loose_cols_[i]);
33       Paper_column *col = dynamic_cast<Paper_column *> (loose);
34
35       if (col->get_system ())
36         continue;
37
38       Item *left = 0;
39       Item *right = 0;
40
41       vector<Item*> clique;
42       while (1)
43         {
44           SCM between = loose->get_object ("between-cols");
45           if (!scm_is_pair (between))
46             break;
47
48           Item *le = dynamic_cast<Item *> (unsmob_grob (scm_car (between)));
49           Item *re = dynamic_cast<Item *> (unsmob_grob (scm_cdr (between)));
50
51           if (! (le && re))
52             break;
53
54           if (!left && le)
55             {
56               left = le->get_column ();
57               if (!left->get_system ())
58                 left = left->find_prebroken_piece (RIGHT);
59
60               clique.push_back (left);
61             }
62
63           clique.push_back (loose);
64
65           divide_over++;
66           loose = right = re->get_column ();
67         }
68
69       if (right->get_system ())
70         ; /* do nothing */
71       else if (right->find_prebroken_piece (LEFT)
72                && right->find_prebroken_piece (LEFT)->get_system () == which)
73         right = right->find_prebroken_piece (LEFT);
74       else if (Paper_column::get_rank (which->get_bound (RIGHT)) < Paper_column::get_rank (right))
75         
76         right = which->get_bound (RIGHT);
77       else
78         {
79           clique.back ()->programming_error ("Loose column does not have right side to attach to.");
80           System *base_system = dynamic_cast<System*> (which->original ());
81           int j = Paper_column::get_rank (clique.back ()) + 1;
82           int end_rank = Paper_column::get_rank (which->get_bound (RIGHT));
83           extract_grob_set (base_system, "columns", base_cols);
84           for (; j < end_rank; j++)
85             {
86               if (base_cols[j]->get_system () == which)
87                 right = dynamic_cast<Item*> ((Grob*)base_cols[j]);
88             }
89         }
90       
91
92       if (!right)
93         {
94           programming_error ("Can't attach loose column sensibly. Attaching to end of system.");
95           right = which->get_bound (RIGHT);
96         }
97       Grob *common = right->common_refpoint (left, X_AXIS);
98
99       clique.push_back (right);
100
101       vector<Real> clique_spacing;
102       clique_spacing.push_back (0.0);
103       for (vsize j = 1; j + 1 < clique.size (); j ++)
104         {
105           Grob *clique_col = clique[j];
106
107           Paper_column *loose_col = dynamic_cast<Paper_column *> (clique[j]);
108           Paper_column *next_col = dynamic_cast<Paper_column *> (clique[j + 1]);
109
110           Grob *spacing = unsmob_grob (clique_col->get_object ("spacing"));
111           if (Grob *grace_spacing = unsmob_grob (clique_col->get_object ("grace-spacing")))
112             {
113               spacing = grace_spacing;
114             }
115           
116           Spacing_options options;
117           if (spacing)
118             options.init_from_grob (spacing);
119           else
120             programming_error ("Column without spacing object");
121
122           Real base_note_space = 0.0;
123
124           if (Paper_column::is_musical (next_col)
125               && Paper_column::is_musical (loose_col))
126             base_note_space = Spacing_spanner::note_spacing (spacing, loose_col, next_col,
127                                                              &options);
128           else
129             {
130               Spring spring = Spacing_spanner::standard_breakable_column_spacing (spacing,
131                                                                                   loose_col, next_col,
132                                                                                   &options);
133
134               base_note_space = spring.distance ();
135             }
136
137           base_note_space = max (base_note_space,
138                                  robust_relative_extent (loose_col, loose_col, X_AXIS)[RIGHT]
139                                  - robust_relative_extent (next_col, next_col, X_AXIS)[LEFT]);
140           
141           clique_spacing.push_back (base_note_space);
142         }
143
144       Real default_padding = 1.0;
145       clique_spacing.push_back (default_padding);
146
147       Real right_point = robust_relative_extent (clique.back (), common, X_AXIS)[LEFT];
148       
149           
150       Grob *finished_right_column = clique.back ();
151       
152       for (vsize j = clique.size () - 2; j > 0; j--)
153         {
154           Paper_column *clique_col = dynamic_cast<Paper_column *> (clique[j]);
155           
156           right_point = finished_right_column->relative_coordinate (common, X_AXIS);
157
158           Real distance_to_next = clique_spacing[j];
159           
160           Real my_offset = right_point - distance_to_next;
161
162           clique_col->set_system (which);
163           clique_col->translate_axis (my_offset - clique_col->relative_coordinate (common, X_AXIS), X_AXIS);      
164
165           finished_right_column = clique_col;
166         }
167  
168     }
169 }
170