]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-loose-columns.cc
* lily/spacing-determine-loose-columns.cc: new file.
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9
10 #include "system.hh"
11 #include "paper-column.hh"
12 #include "column-x-positions.hh"
13 #include "pointer-group-interface.hh"
14 #include "staff-spacing.hh"
15
16 /* Find the loose columns in POSNS, and drape them around the columns
17    specified in BETWEEN-COLS.  */
18 void
19 set_loose_columns (System *which, Column_x_positions const *posns)
20 {
21   int loose_col_count = posns->loose_cols_.size ();
22   if (!loose_col_count)
23     return; 
24
25   Real default_padding = 1.0;
26   for (int i = 0; i < loose_col_count; i++)
27     {
28       int divide_over = 1;
29       Item *loose = dynamic_cast<Item *> (posns->loose_cols_[i]);
30       Paper_column *col = dynamic_cast<Paper_column *> (loose);
31
32       if (col->get_system ())
33         continue;
34
35       Item *left = 0;
36       Item *right = 0;
37       while (1)
38         {
39           SCM between = loose->get_object ("between-cols");
40           if (!scm_is_pair (between))
41             break;
42
43           Item *le = dynamic_cast<Item *> (unsmob_grob (scm_car (between)));
44           Item *re = dynamic_cast<Item *> (unsmob_grob (scm_cdr (between)));
45
46           if (! (le && re))
47             break;
48
49           if (!left && le)
50             {
51               left = le->get_column ();
52               if (!left->get_system ())
53                 left = left->find_prebroken_piece (RIGHT);
54             }
55
56           divide_over++;
57           loose = right = re->get_column ();
58         }
59
60       if (!right->get_system ())
61         right = right->find_prebroken_piece (LEFT);
62
63       Grob *common = right->common_refpoint (left, X_AXIS);
64
65       int count = 0;
66       Real total_space = 0.0;
67       Real total_fixed = 0.0;
68
69       extract_grob_set (col, "spacing-wishes", wishes);
70       for (int i = 0; i < wishes.size (); i++)
71         {
72           Grob *spacing = wishes[i];
73           if (Staff_spacing::has_interface (spacing))
74             {
75               Real space = 0.0;
76               Real fixed = 0.0;
77               Staff_spacing::get_spacing_params (spacing, &space, &fixed);
78
79               total_fixed += fixed;
80               total_space += space;
81               count++;
82             }
83         }
84
85       Real right_point = 0.0;
86       Real distance_to_next = 0.0;
87       if (count)
88         {
89           total_space /= count;
90           total_fixed /= count;
91
92           distance_to_next = total_space;
93           right_point = right->relative_coordinate (common, X_AXIS);
94         }
95       else
96         {
97           Interval my_extent = col->extent (col, X_AXIS);
98           distance_to_next = my_extent[RIGHT] + default_padding;
99           right_point = right->extent (common, X_AXIS)[LEFT];
100         }
101
102       Real my_offset = right_point - distance_to_next;
103
104       col->system_ = which;
105       col->translate_axis (my_offset - col->relative_coordinate (common, X_AXIS), X_AXIS);
106     }
107 }
108
109