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