]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-loose-columns.cc
* scm/define-grob-properties.scm (all-user-grob-properties): add
[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--2006 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
17 #include "moment.hh"
18
19 /* Find the loose columns in POSNS, and drape them around the columns
20    specified in BETWEEN-COLS.  */
21 void
22 set_loose_columns (System *which, Column_x_positions const *posns)
23 {
24   int loose_col_count = posns->loose_cols_.size ();
25   if (!loose_col_count)
26     return;
27
28   Real default_padding = 1.0;
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         right = right->find_prebroken_piece (LEFT);
71
72       clique.push_back (right);
73
74       Grob *common = right->common_refpoint (left, X_AXIS);
75       Item *finished_right_column = clique.back ();
76
77       for (vsize j = clique.size () - 2; j > 0; j--)
78         {
79           int count = 0;
80           Real total_space = 0.0;
81           Real total_fixed = 0.0;
82
83           extract_grob_set (col, "spacing-wishes", wishes);
84           for (vsize i = 0; i < wishes.size (); i++)
85             {
86               Grob *spacing = wishes[i];
87               Real space = 0.0;
88               Real fixed = 0.0;
89
90               if (Staff_spacing::has_interface (spacing))
91                 Staff_spacing::get_spacing_params (spacing, &space, &fixed);
92               else if (Note_spacing::has_interface (spacing))
93                 {
94                   Spacing_options options;
95
96                   fixed = robust_relative_extent (col, col, X_AXIS)[RIGHT];
97
98                   Moment dt = Paper_column::when_mom (right) - Paper_column::when_mom (col);
99                   bool expand = false;
100                   space = options.get_duration_space (dt, &expand);
101                   Note_spacing::get_spacing (spacing, right, space, options.increment_,
102                                              &space, &fixed);
103                 }
104               else
105                 continue;
106
107               count++;
108
109               total_space += space;
110               total_fixed += fixed;
111             }
112
113           Real distance_to_next = 0.0;
114           Real right_point = 0.0;
115           if (count)
116             {
117               total_space /= count;
118               total_fixed /= count;
119
120               distance_to_next = total_space;
121               right_point
122                 = finished_right_column->relative_coordinate (common, X_AXIS);
123             }
124           else
125             {
126               Interval my_extent = robust_relative_extent (col, col, X_AXIS);
127               distance_to_next = my_extent[RIGHT] + default_padding;
128               right_point = robust_relative_extent (finished_right_column, common, X_AXIS)[LEFT];
129             }
130
131           Real my_offset = right_point - distance_to_next;
132
133           col->system_ = which;
134           col->translate_axis (my_offset - col->relative_coordinate (common, X_AXIS), X_AXIS);
135
136           finished_right_column = col;
137         }
138     }
139 }
140