]> git.donarmstrong.com Git - lilypond.git/blob - lily/item.cc
Merge remote branch 'origin' into release/unstable
[lilypond.git] / lily / item.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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 "item.hh"
21
22 #include "axis-group-interface.hh"
23 #include "paper-score.hh"
24 #include "warn.hh"
25 #include "paper-column.hh"
26 #include "lily-guile.hh"
27 #include "system.hh"
28 #include "pointer-group-interface.hh"
29
30 #include "moment.hh"
31
32 Grob *
33 Item::clone () const
34 {
35   return new Item (*this);
36 }
37
38 Item::Item (SCM s)
39   : Grob (s)
40 {
41   broken_to_drul_[LEFT] = broken_to_drul_[RIGHT] = 0;
42   cached_pure_height_valid_ = false;
43 }
44
45 /**
46    Item copy ctor.  Copy nothing: everything should be a elt property
47    or a special purpose pointer (such as broken_to_drul_[]) */
48 Item::Item (Item const &s)
49   : Grob (s)
50 {
51   broken_to_drul_[LEFT] = broken_to_drul_[RIGHT] = 0;
52   cached_pure_height_valid_ = false;
53 }
54
55 bool
56 Item::is_non_musical (Grob *me)
57 {
58   if (me->original ())
59     return false;
60
61   Item *i = dynamic_cast<Item *> (me->get_parent (X_AXIS));
62   return i ? Item::is_non_musical (i) : to_boolean (me->get_property ("non-musical"));
63 }
64
65 Paper_column *
66 Item::get_column () const
67 {
68   Item *parent = dynamic_cast<Item *> (get_parent (X_AXIS));
69   return parent ? parent->get_column () : 0;
70 }
71
72 System *
73 Item::get_system () const
74 {
75   Grob *g = get_parent (X_AXIS);
76   return g ? g->get_system () : 0;
77 }
78
79 void
80 Item::copy_breakable_items ()
81 {
82   Drul_array<Item *> new_copies;
83   Direction i = LEFT;
84   do
85     {
86       Grob *dolly = clone ();
87       Item *item = dynamic_cast<Item *> (dolly);
88       get_root_system (this)->typeset_grob (item);
89       new_copies[i] = item;
90     }
91   while (flip (&i) != LEFT);
92
93   broken_to_drul_ = new_copies;
94 }
95
96 bool
97 Item::is_broken () const
98 {
99   return broken_to_drul_[LEFT] || broken_to_drul_[RIGHT];
100 }
101
102 /*
103   Generate items for begin and end-of line.
104 */
105 void
106 Item::discretionary_processing ()
107 {
108   if (is_broken ())
109     return;
110
111   if (Item::is_non_musical (this))
112     copy_breakable_items ();
113 }
114
115 Grob *
116 Item::find_broken_piece (System *l) const
117 {
118   if (get_system () == l)
119     return (Item *) (this);
120
121   Direction d = LEFT;
122   do
123     {
124       Grob *s = broken_to_drul_[d];
125       if (s && s->get_system () == l)
126         return s;
127     }
128   while (flip (&d) != LEFT);
129
130   return 0;
131 }
132
133 Item *
134 Item::maybe_find_prebroken_piece (Item *g, Direction d)
135 {
136   Item *ret = g->find_prebroken_piece (d);
137   if (ret)
138     return ret;
139   return g;
140 }
141
142 Item *
143 Item::find_prebroken_piece (Direction d) const
144 {
145   Item *me = (Item *) (this);
146   if (!d)
147     return me;
148   return dynamic_cast<Item *> (broken_to_drul_[d]);
149 }
150
151 Direction
152 Item::break_status_dir () const
153 {
154   if (original ())
155     {
156       Item *i = dynamic_cast<Item *> (original ());
157
158       return (i->broken_to_drul_[LEFT] == this) ? LEFT : RIGHT;
159     }
160   else
161     return CENTER;
162 }
163
164 void
165 Item::handle_prebroken_dependencies ()
166 {
167   Grob::handle_prebroken_dependencies ();
168
169   /*
170     Can't do this earlier, because try_visibility_lambda () might set
171     the elt property transparent, which would then be copied.
172   */
173   if (!Item::break_visible (this))
174     suicide ();
175 }
176
177 bool
178 Item::break_visible (Grob *g)
179 {
180   Item *it = dynamic_cast<Item *> (g);
181   SCM vis = g->get_property ("break-visibility");
182   if (scm_is_vector (vis))
183     return to_boolean (scm_c_vector_ref (vis, it->break_status_dir () + 1));
184   return true;
185 }
186
187 bool
188 Item::pure_is_visible (int start, int end) const
189 {
190   SCM vis = get_property ("break-visibility");
191   if (scm_is_vector (vis))
192     {
193       int pos = 1;
194       int pc_rank = Paper_column::get_rank (get_column ());
195       if (pc_rank == start)
196         pos = 2;
197       else if (pc_rank == end)
198         pos = 0;
199       return to_boolean (scm_vector_ref (vis, scm_from_int (pos)));
200     }
201   return true;
202 }
203
204 Interval_t<int>
205 Item::spanned_rank_interval () const
206 {
207   int c = get_column ()->get_rank ();
208   return Interval_t<int> (c, c);
209 }
210
211 Interval_t<Moment>
212 spanned_time_interval (Item *l, Item *r)
213 {
214   Drul_array<Item *> bounds (l, r);
215   Interval_t<Moment> iv;
216
217   Direction d = LEFT;
218   do
219     {
220       if (bounds[d] && bounds[d]->get_column ())
221         iv[d] = robust_scm2moment (bounds[d]->get_column ()->get_property ("when"),
222                                    iv[d]);
223     }
224   while (flip (&d) != LEFT);
225
226   do
227     {
228       if (!bounds[d] || !bounds[d]->get_column ())
229         iv[d] = iv[-d];
230     }
231   while (flip (&d) != LEFT);
232
233   return iv;
234 }
235
236 void
237 Item::derived_mark () const
238 {
239   if (broken_to_drul_[LEFT])
240     scm_gc_mark (broken_to_drul_[LEFT]->self_scm ());
241   if (broken_to_drul_[RIGHT])
242     scm_gc_mark (broken_to_drul_[RIGHT]->self_scm ());
243 }
244
245 Item *
246 unsmob_item (SCM s)
247 {
248   return dynamic_cast<Item *> (unsmob_grob (s));
249 }
250
251 Interval
252 Item::pure_height (Grob *g, int start, int end)
253 {
254   if (cached_pure_height_valid_)
255     return cached_pure_height_ + pure_relative_y_coordinate (g, start, end);
256
257   cache_pure_height (Grob::pure_height (this, start, end));
258   return cached_pure_height_ + pure_relative_y_coordinate (g, start, end);
259 }
260
261 void
262 Item::cache_pure_height (Interval height)
263 {
264   cached_pure_height_ = height;
265   cached_pure_height_valid_ = true;
266 }
267
268 ADD_INTERFACE (Item,
269                "Grobs can be distinguished in their role in the horizontal"
270                " spacing.  Many grobs define constraints on the spacing by"
271                " their sizes, for example, note heads, clefs, stems, and all"
272                " other symbols with a fixed shape.  These grobs form a"
273                " subtype called @code{Item}.\n"
274                "\n"
275                "Some items need special treatment for line breaking.  For"
276                " example, a clef is normally only printed at the start of a"
277                " line (i.e., after a line break).   To model this,"
278                " @q{breakable} items (clef, key signature, bar lines, etc.)"
279                " are copied twice.  Then we have three versions of each"
280                " breakable item: one version if there is no line break, one"
281                " version that is printed before the line break (at the end of"
282                " a system), and one version that is printed after the line"
283                " break.\n"
284                "\n"
285                "Whether these versions are visible and take up space is"
286                " determined by the outcome of the @code{break-visibility}"
287                " grob property, which is a function taking a direction"
288                " (@w{@code{-1}}, @code{0} or@tie{}@code{1}) as an argument.  It"
289                " returns a cons of booleans, signifying whether this grob"
290                " should be transparent and have no extent.\n"
291                "\n"
292                "The following variables for @code{break-visibility} are"
293                " predefined:\n"
294                "@example\n"
295                "           grob will show:   before  no     after\n"
296                "                             break   break  break\n"
297                "  all-invisible              no      no     no\n"
298                "  begin-of-line-visible      no      no     yes\n"
299                "  end-of-line-visible        yes     no     no\n"
300                "  all-visible                yes     yes    yes\n"
301                "  begin-of-line-invisible    yes     yes    no\n"
302                "  end-of-line-invisible      no      yes    yes\n"
303                "  center-invisible           yes      no    yes\n"
304                "@end example",
305
306                /* properties */
307                "break-visibility "
308                "extra-spacing-height "
309                "extra-spacing-width "
310                "non-musical "
311               );