]> git.donarmstrong.com Git - lilypond.git/blob - lily/item.cc
Doc-fr: updates input.itely
[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::find_prebroken_piece (Direction d) const
135 {
136   Item *me = (Item *) (this);
137   if (!d)
138     return me;
139   return dynamic_cast<Item *> (broken_to_drul_[d]);
140 }
141
142 Direction
143 Item::break_status_dir () const
144 {
145   if (original ())
146     {
147       Item *i = dynamic_cast<Item *> (original ());
148
149       return (i->broken_to_drul_[LEFT] == this) ? LEFT : RIGHT;
150     }
151   else
152     return CENTER;
153 }
154
155 void
156 Item::handle_prebroken_dependencies ()
157 {
158   Grob::handle_prebroken_dependencies ();
159
160   /*
161     Can't do this earlier, because try_visibility_lambda () might set
162     the elt property transparent, which would then be copied.
163   */
164   if (!Item::break_visible (this))
165     suicide ();
166 }
167
168 bool
169 Item::break_visible (Grob *g)
170 {
171   Item *it = dynamic_cast<Item *> (g);
172   SCM vis = g->get_property ("break-visibility");
173   if (scm_is_vector (vis))
174     return to_boolean (scm_c_vector_ref (vis, it->break_status_dir () + 1));
175   return true;
176 }
177
178 bool
179 Item::pure_is_visible (int start, int end) const
180 {
181   SCM vis = get_property ("break-visibility");
182   if (scm_is_vector (vis))
183     {
184       int pos = 1;
185       int pc_rank = Paper_column::get_rank (get_column ());
186       if (pc_rank == start)
187         pos = 2;
188       else if (pc_rank == end)
189         pos = 0;
190       return to_boolean (scm_vector_ref (vis, scm_from_int (pos)));
191     }
192   return true;
193 }
194
195 Interval_t<int>
196 Item::spanned_rank_interval () const
197 {
198   int c = get_column ()->get_rank ();
199   return Interval_t<int> (c, c);
200 }
201
202 Interval_t<Moment>
203 spanned_time_interval (Item *l, Item *r)
204 {
205   Drul_array<Item *> bounds (l, r);
206   Interval_t<Moment> iv;
207
208   Direction d = LEFT;
209   do
210     {
211       if (bounds[d] && bounds[d]->get_column ())
212         iv[d] = robust_scm2moment (bounds[d]->get_column ()->get_property ("when"),
213                                    iv[d]);
214     }
215   while (flip (&d) != LEFT);
216
217   do
218     {
219       if (!bounds[d] || !bounds[d]->get_column ())
220         iv[d] = iv[-d];
221     }
222   while (flip (&d) != LEFT);
223
224   return iv;
225 }
226
227 void
228 Item::derived_mark () const
229 {
230   if (broken_to_drul_[LEFT])
231     scm_gc_mark (broken_to_drul_[LEFT]->self_scm ());
232   if (broken_to_drul_[RIGHT])
233     scm_gc_mark (broken_to_drul_[RIGHT]->self_scm ());
234 }
235
236 Item *
237 unsmob_item (SCM s)
238 {
239   return dynamic_cast<Item *> (unsmob_grob (s));
240 }
241
242 Interval
243 Item::pure_height (Grob *g, int start, int end)
244 {
245   if (cached_pure_height_valid_)
246     return cached_pure_height_ + pure_relative_y_coordinate (g, start, end);
247
248   cached_pure_height_ = Grob::pure_height (this, start, end);
249   cached_pure_height_valid_ = true;
250   return cached_pure_height_ + pure_relative_y_coordinate (g, start, end);
251 }
252
253 ADD_INTERFACE (Item,
254                "Grobs can be distinguished in their role in the horizontal"
255                " spacing.  Many grobs define constraints on the spacing by"
256                " their sizes, for example, note heads, clefs, stems, and all"
257                " other symbols with a fixed shape.  These grobs form a"
258                " subtype called @code{Item}.\n"
259                "\n"
260                "Some items need special treatment for line breaking.  For"
261                " example, a clef is normally only printed at the start of a"
262                " line (i.e., after a line break).   To model this,"
263                " @q{breakable} items (clef, key signature, bar lines, etc.)"
264                " are copied twice.  Then we have three versions of each"
265                " breakable item: one version if there is no line break, one"
266                " version that is printed before the line break (at the end of"
267                " a system), and one version that is printed after the line"
268                " break.\n"
269                "\n"
270                "Whether these versions are visible and take up space is"
271                " determined by the outcome of the @code{break-visibility}"
272                " grob property, which is a function taking a direction"
273                " (@code{-1}, @code{0} or@tie{}@code{1}) as an argument.  It"
274                " returns a cons of booleans, signifying whether this grob"
275                " should be transparent and have no extent.\n"
276                "\n"
277                "The following variables for @code{break-visibility} are"
278                " predefined:\n"
279                "@example\n"
280                "           grob will show:   before  no     after\n"
281                "                             break   break  break\n"
282                "  all-invisible              no      no     no\n"
283                "  begin-of-line-visible      no      no     yes\n"
284                "  end-of-line-visible        yes     no     no\n"
285                "  all-visible                yes     yes    yes\n"
286                "  begin-of-line-invisible    yes     yes    no\n"
287                "  end-of-line-invisible      no      yes    yes\n"
288                "  center-invisible           yes      no    yes\n"
289                "@end example",
290
291                /* properties */
292                "break-visibility "
293                "extra-spacing-height "
294                "extra-spacing-width "
295                "non-musical "
296               );