]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-spacing.cc
* lily/system.cc (do_derived_mark): don't mark from object_alist_
[lilypond.git] / lily / staff-spacing.cc
1 /*
2   staff-spacing.cc -- implement Staff_spacing
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2005  Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "staff-spacing.hh"
10
11 #include <cstdio>
12
13 #include "paper-column.hh"
14 #include "separation-item.hh"
15 #include "warn.hh"
16 #include "bar-line.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "note-column.hh"
19 #include "stem.hh"
20 #include "accidental-placement.hh"
21 #include "pointer-group-interface.hh"
22
23 /*
24   Insert some more space for the next note, in case it has a stem in
25   the wrong direction
26 */
27 Real
28 Staff_spacing::next_note_correction (Grob *me,
29                                      Grob *g,
30                                      Interval bar_size)
31 {
32   if (!g || !Note_column::has_interface (g))
33     return 0.0;
34
35   Item *col = dynamic_cast<Item *> (g)->get_column ();
36   Real max_corr = max (0., (- g->extent (col, X_AXIS)[LEFT]));
37
38   /*
39     Duh. If this gets out of hand, we should invent something more generic.
40   */
41   if (Grob *a = Note_column::accidentals (g))
42     {
43       Interval v;
44       if (Accidental_placement::has_interface (a))
45         {
46           v = Accidental_placement::get_relevant_accidental_extent (a, col, me);
47         }
48       else
49         v = a->extent (col, X_AXIS);
50
51       max_corr = max (max_corr, (- v[LEFT]));
52     }
53   if (Grob *a = unsmob_grob (g->get_object ("arpeggio")))
54     {
55       max_corr = max (max_corr, - a->extent (col, X_AXIS)[LEFT]);
56     }
57
58   /*
59     Let's decrease the space a little if the problem is not located
60     after a barline.
61   */
62   if (bar_size.is_empty ())
63     max_corr *= 0.75;
64
65   if (!bar_size.is_empty ())
66     if (Grob *stem = Note_column::get_stem (g))
67       {
68         Direction d = Stem::get_direction (stem);
69         if (d == DOWN)
70           {
71             Real stem_start = Stem::head_positions (stem) [DOWN];
72             Real stem_end = Stem::stem_end_position (stem);
73             Interval stem_posns (min (stem_start, stem_end),
74                                  max (stem_end, stem_start));
75
76             stem_posns.intersect (bar_size);
77
78             Real corr = min (abs (stem_posns.length () / 7.0), 1.0);
79             corr
80               *= robust_scm2double (me->get_property ("stem-spacing-correction"), 1);
81
82             if (d != DOWN)
83               corr = 0.0;
84             max_corr = max (max_corr, corr);
85           }
86       }
87   return max_corr;
88 }
89
90 /*
91   Y-positions that are covered by BAR_GROB, in the case that it is a
92   barline.  */
93 Interval
94 Staff_spacing::bar_y_positions (Grob *bar_grob)
95 {
96   Interval bar_size;
97   bar_size.set_empty ();
98   if (Bar_line::has_interface (bar_grob))
99     {
100       SCM glyph = bar_grob->get_property ("glyph");
101
102       String glyph_string = scm_is_string (glyph) ? ly_scm2string (glyph) : "";
103       if (glyph_string.left_string (1) == "|" || glyph_string.left_string (1) == ".")
104         {
105           SCM sz = Bar_line::get_staff_bar_size (bar_grob->self_scm ());
106           bar_size = Interval (-1, 1);
107           bar_size *= robust_scm2double (sz, 1)
108             / Staff_symbol_referencer::staff_space (bar_grob);
109         }
110     }
111   return bar_size;
112 }
113
114 /*
115   Do corrections for the following notes.
116
117   This is slightly convoluted, since the staffspacing grob gets
118   pointers to the separation-items, not the note-columns or
119   note-spacings.
120 */
121 Real
122 Staff_spacing::next_notes_correction (Grob *me, Grob *last_grob)
123 {
124   Interval bar_size = bar_y_positions (last_grob);
125   Real max_corr = 0.0;
126
127
128   extract_grob_set (me, "right-items", right_items);
129   for (int i = right_items.size (); i--;)
130     {
131       Grob *g = right_items[i];
132
133       max_corr = max (max_corr, next_note_correction (me, g, bar_size));
134       extract_grob_set (g, "elements", elts);
135       for (int j  = elts.size(); j--;)
136         max_corr = max (max_corr, next_note_correction (me, elts[j], bar_size));
137     }
138
139   return max_corr;
140 }
141
142 void
143 Staff_spacing::get_spacing_params (Grob *me, Real *space, Real *fixed)
144 {
145   *space = 1.0;
146   *fixed = 1.0;
147
148   Grob *separation_item = 0;
149   Item *me_item = dynamic_cast<Item *> (me);
150
151   extract_grob_set (me, "left-items", items);
152   for (int i = items.size(); i--;)
153     {
154       Grob *cand = items[i];
155       if (cand && Separation_item::has_interface (cand))
156         separation_item = cand;
157     }
158
159   //  printf ("doing col %d\n" , Paper_column::get_rank (left_col));
160
161   if (!separation_item)
162     {
163       programming_error ("no sep item");
164       return;
165     }
166
167   Interval last_ext;
168   Grob *last_grob = Separation_item::extremal_break_aligned_grob (separation_item, RIGHT,
169                                                                   &last_ext);
170   if (!last_grob)
171     {
172       /*
173         TODO:
174
175         Should  insert a adjustable space here? For excercises, you might want to
176         use a staff without a clef in the beginning.
177       */
178
179       /*
180         we used to have a warning here, but it generates a lot of
181         spurious error messages.
182       */
183       return;
184     }
185
186   *fixed = last_ext[RIGHT];
187   *space = *fixed + 1.0;
188
189   SCM alist = last_grob->get_property ("space-alist");
190   if (!scm_list_p (alist))
191     return;
192
193   SCM space_def = scm_sloppy_assq (ly_symbol2scm ("first-note"), alist);
194   if (me_item->break_status_dir () == CENTER)
195     {
196       SCM nndef = scm_sloppy_assq (ly_symbol2scm ("next-note"), alist);
197       if (scm_is_pair (nndef))
198         space_def = nndef;
199     }
200
201   if (!scm_is_pair (space_def))
202     {
203       programming_error ("unknown prefatory spacing");
204       return;
205     }
206
207   space_def = scm_cdr (space_def);
208   Real distance = scm_to_double (scm_cdr (space_def));
209   SCM type = scm_car (space_def);
210
211   *fixed = last_ext[RIGHT];
212   if (type == ly_symbol2scm ("fixed-space"))
213     {
214       *fixed += distance;
215       *space = *fixed;
216     }
217   else if (type == ly_symbol2scm ("extra-space"))
218     {
219       *space = *fixed + distance;
220     }
221   else if (type == ly_symbol2scm ("semi-fixed-space"))
222     {
223       *fixed += distance / 2;
224       *space = *fixed + distance / 2;
225     }
226   else if (type == ly_symbol2scm ("minimum-space"))
227     {
228       *space = last_ext[LEFT] + max (last_ext.length (), distance);
229     }
230   else if (type == ly_symbol2scm ("minimum-fixed-space"))
231     {
232       *space = last_ext[LEFT] + max (last_ext.length (), distance);
233       *fixed = *space;
234     }
235
236   *space += next_notes_correction (me, last_grob);
237 }
238
239 ADD_INTERFACE (Staff_spacing, "staff-spacing-interface",
240                "This object calculates spacing details from a "
241                " breakable symbol (left) to another object. For example, it takes care "
242                " of  optical spacing from  a bar lines to a note.",
243                "stem-spacing-correction left-items right-items");