]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-loose-columns.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / spacing-loose-columns.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 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 using std::vector;
32
33 /* Find the loose columns in POSNS, and drape them around the columns
34    specified in BETWEEN-COLS.  */
35 void
36 set_loose_columns (System *which, Column_x_positions const *posns)
37 {
38   int loose_col_count = posns->loose_cols_.size ();
39   if (!loose_col_count)
40     return;
41
42   for (int i = 0; i < loose_col_count; i++)
43     dynamic_cast<Paper_column *> (posns->loose_cols_[i])->set_system (which);
44
45   for (int i = 0; i < loose_col_count; i++)
46     {
47       int divide_over = 1;
48       Item *loose = dynamic_cast<Item *> (posns->loose_cols_[i]);
49
50       Item *left = 0;
51       Item *right = 0;
52
53       vector<Item *> clique;
54       while (1)
55         {
56           SCM between = loose->get_object ("between-cols");
57           if (!scm_is_pair (between))
58             break;
59
60           /* If the line was broken at one of the loose columns, split
61              the clique at that column. */
62           if (!loose->get_system ())
63             break;
64
65           Paper_column *le = unsmob<Paper_column> (scm_car (between));
66           Paper_column *re = unsmob<Paper_column> (scm_cdr (between));
67
68           if (! (le && re))
69             break;
70
71           if (!left && le)
72             {
73               left = le->get_column ();
74               if (!left->get_system ())
75                 left = left->find_prebroken_piece (RIGHT);
76
77               clique.push_back (left);
78             }
79
80           clique.push_back (loose);
81
82           divide_over++;
83           loose = right = re->get_column ();
84         }
85
86       if (!right)
87         {
88           programming_error ("Can't attach loose column sensibly."
89                              "  Attaching to end of system.");
90           right = which->get_bound (RIGHT);
91         }
92
93       if (right->get_system ())
94         ; /* do nothing */
95       else if (right->find_prebroken_piece (LEFT)
96                && right->find_prebroken_piece (LEFT)->get_system () == which)
97         right = right->find_prebroken_piece (LEFT);
98       else if (Paper_column::get_rank (which->get_bound (RIGHT)) < Paper_column::get_rank (right))
99         right = which->get_bound (RIGHT);
100       else
101         {
102           clique.back ()->programming_error ("Loose column does not have right side to attach to.");
103           System *base_system = dynamic_cast<System *> (which->original ());
104           int j = Paper_column::get_rank (clique.back ()) + 1;
105           int end_rank = Paper_column::get_rank (which->get_bound (RIGHT));
106           extract_grob_set (base_system, "columns", base_cols);
107           for (; j < end_rank; j++)
108             {
109               if (base_cols[j]->get_system () == which)
110                 right = dynamic_cast<Item *> ((Grob *)base_cols[j]);
111             }
112         }
113
114       Grob *common = right->common_refpoint (left, X_AXIS);
115
116       clique.push_back (right);
117
118       /*
119         We use two vectors to keep track of loose column spacing:
120           clique_spacing keeps track of ideal spaces.
121           clique_tight_spacing keeps track of minimum spaces.
122         Below, a scale factor is applied to the shifting of loose columns that
123         aims to preserve clique_spacing but gets closer to clique_tight_spacing as the
124         space becomes smaller.  This is used because the rods placed for loose columns
125         are tight (meaning they use minimum distances - see set_distances_for_loose_columns).
126         However, other rods may widen this distance, in which case we don't want a crammed score.
127         Thus, we aim for non-crammed, and fall back on crammed as needed.
128       */
129       vector<Real> clique_spacing;
130       vector<Real> clique_tight_spacing;
131       clique_spacing.push_back (0.0);
132       clique_tight_spacing.push_back (0.0);
133       for (vsize j = 1; j + 1 < clique.size (); j++)
134         {
135           Grob *clique_col = clique[j];
136
137           Paper_column *loose_col = dynamic_cast<Paper_column *> (clique[j]);
138           Paper_column *next_col = dynamic_cast<Paper_column *> (clique[j + 1]);
139
140           Grob *spacing = unsmob<Grob> (clique_col->get_object ("spacing"));
141           if (Grob *grace_spacing = unsmob<Grob> (clique_col->get_object ("grace-spacing")))
142             {
143               spacing = grace_spacing;
144             }
145
146           Spacing_options options;
147           if (spacing)
148             options.init_from_grob (spacing);
149           else
150             programming_error ("Column without spacing object");
151
152           Real base_note_space = 0.0;
153           Real tight_note_space = 0.0;
154
155           if (Paper_column::is_musical (next_col)
156               && Paper_column::is_musical (loose_col))
157             {
158               Spring spring = Spacing_spanner::note_spacing (spacing, loose_col,
159                                                              next_col, &options);
160               if (has_interface<Note_spacing> (spacing))
161                 spring = Note_spacing::get_spacing (spacing, next_col,
162                                                     spring, options.increment_);
163
164               base_note_space = spring.distance ();
165               tight_note_space = spring.min_distance ();
166             }
167           else
168             {
169               Spring spring = Spacing_spanner::standard_breakable_column_spacing (spacing,
170                               loose_col, next_col,
171                               &options);
172
173               base_note_space = spring.distance ();
174               tight_note_space = spring.min_distance ();
175             }
176
177           Real loose_col_horizontal_length = loose_col->extent (loose_col, X_AXIS).length ();
178           base_note_space = max (base_note_space, loose_col_horizontal_length);
179           tight_note_space = max (tight_note_space, loose_col_horizontal_length);
180
181           clique_spacing.push_back (base_note_space);
182           clique_tight_spacing.push_back (tight_note_space);
183         }
184
185       Real permissible_distance = clique.back ()->relative_coordinate (common, X_AXIS) - robust_relative_extent (clique[0], common, X_AXIS)[RIGHT];
186       Real right_point = robust_relative_extent (clique.back (), common, X_AXIS)[LEFT];
187       Grob *finished_right_column = clique.back ();
188
189       Real sum_tight_spacing = 0;
190       Real sum_spacing = 0;
191       // currently a magic number - what would be a good grob to hold this property?
192       Real left_padding = 0.15;
193       for (vsize j = 0; j < clique_spacing.size (); j++)
194         {
195           sum_tight_spacing += clique_tight_spacing[j];
196           sum_spacing += clique_spacing[j];
197         }
198       Real scale_factor = max (0.0, min (1.0, (permissible_distance - left_padding - sum_tight_spacing) / (sum_spacing - sum_tight_spacing)));
199       for (vsize j = clique.size () - 2; j > 0; j--)
200         {
201           Paper_column *clique_col = dynamic_cast<Paper_column *> (clique[j]);
202
203           right_point = finished_right_column->relative_coordinate (common, X_AXIS);
204
205           Real distance_to_next = clique_tight_spacing[j] + (clique_spacing[j] - clique_tight_spacing[j]) * scale_factor;
206
207           Real my_offset = right_point - distance_to_next;
208
209           clique_col->translate_axis (my_offset - clique_col->relative_coordinate (common, X_AXIS), X_AXIS);
210
211           finished_right_column = clique_col;
212         }
213     }
214 }
215