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