]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column.cc
Makes line breaking options more accessible to grobs.
[lilypond.git] / lily / paper-column.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 "paper-column.hh"
21
22 #include "axis-group-interface.hh"
23 #include "bar-line.hh"
24 #include "break-align-interface.hh"
25 #include "font-interface.hh"
26 #include "grob-array.hh"
27 #include "lookup.hh"
28 #include "lookup.hh"
29 #include "moment.hh"
30 #include "output-def.hh"
31 #include "paper-score.hh"
32 #include "pointer-group-interface.hh"
33 #include "rhythmic-head.hh"
34 #include "separation-item.hh"
35 #include "skyline-pair.hh"
36 #include "spaceable-grob.hh"
37 #include "spring.hh"
38 #include "string-convert.hh"
39 #include "system.hh"
40 #include "text-interface.hh"
41 #include "warn.hh"
42
43 Grob *
44 Paper_column::clone () const
45 {
46   return new Paper_column (*this);
47 }
48
49 void
50 Paper_column::do_break_processing ()
51 {
52   Item::do_break_processing ();
53 }
54
55 int
56 Paper_column::get_rank (Grob const *me)
57 {
58   return dynamic_cast<Paper_column const *> (me)->rank_;
59 }
60
61 void
62 Paper_column::set_rank (int rank)
63 {
64   rank_ = rank;
65 }
66
67 System *
68 Paper_column::get_system () const
69 {
70   return system_;
71 }
72
73 void
74 Paper_column::set_system (System *s)
75 {
76   system_ = s;
77 }
78
79 Paper_column *
80 Paper_column::get_column () const
81 {
82   return (Paper_column *) (this);
83 }
84
85 Paper_column::Paper_column (SCM l)
86   : Item (l)
87 {
88   system_ = 0;
89   rank_ = -1;
90 }
91
92 Paper_column::Paper_column (Paper_column const &src)
93   : Item (src)
94 {
95   system_ = 0;
96   rank_ = src.rank_;
97 }
98
99 int
100 Paper_column::compare (Grob *const &a,
101                        Grob *const &b)
102 {
103   return sign (dynamic_cast<Paper_column *> (a)->rank_
104                - dynamic_cast<Paper_column *> (b)->rank_);
105 }
106
107 bool
108 Paper_column::less_than (Grob *const &a,
109                          Grob *const &b)
110 {
111   Paper_column *pa = dynamic_cast<Paper_column *> (a);
112   Paper_column *pb = dynamic_cast<Paper_column *> (b);
113
114   return pa->rank_ < pb->rank_;
115 }
116
117 Moment
118 Paper_column::when_mom (Grob *me)
119 {
120   SCM m = me->get_property ("when");
121   if (Moment *when = unsmob_moment (m))
122     return *when;
123   return Moment (0);
124 }
125
126 bool
127 Paper_column::is_loose (Grob *g)
128 {
129   return (scm_is_pair (g->get_object ("between-cols")));
130 }
131
132 bool
133 Paper_column::is_musical (Grob *me)
134 {
135   SCM m = me->get_property ("shortest-starter-duration");
136   Moment s (0);
137   if (unsmob_moment (m))
138     s = *unsmob_moment (m);
139   return s != Moment (0);
140 }
141
142 bool
143 Paper_column::is_used (Grob *me)
144 {
145   extract_grob_set (me, "elements", elts);
146   if (elts.size ())
147     return true;
148
149   extract_grob_set (me, "bounded-by-me", bbm);
150   if (bbm.size ())
151     return true;
152
153   if (Paper_column::is_breakable (me))
154     return true;
155
156   if (to_boolean (me->get_property ("used")))
157     return true;
158
159   if (scm_is_pair (me->get_property ("labels")))
160     return true;
161
162   return false;
163 }
164
165 bool
166 Paper_column::is_breakable (Grob *me)
167 {
168   return scm_is_symbol (me->get_property ("line-break-permission"));
169 }
170
171 Real
172 Paper_column::minimum_distance (Grob *left, Grob *right)
173 {
174   Drul_array<Grob *> cols (left, right);
175   Drul_array<Skyline> skys = Drul_array<Skyline> (Skyline (RIGHT), Skyline (LEFT));
176
177   Direction d = LEFT;
178   do
179     {
180       Skyline_pair *sp = Skyline_pair::unsmob (cols[d]->get_property ("horizontal-skylines"));
181       if (sp)
182         skys[d] = (*sp)[-d];
183     }
184   while (flip (&d) != LEFT);
185
186   skys[RIGHT].merge (Separation_item::conditional_skyline (right, left));
187
188   return max (0.0, skys[LEFT].distance (skys[RIGHT]));
189 }
190
191 Interval
192 Paper_column::break_align_width (Grob *me, SCM align_sym)
193 {
194   Grob *p = me->get_parent (X_AXIS);
195
196   if (is_musical (me))
197     {
198       me->programming_error ("tried to get break-align-width of a musical column");
199       return Interval (0, 0) + me->relative_coordinate (p, X_AXIS);
200     }
201
202   Grob *align = 0;
203   if (align_sym == ly_symbol2scm ("staff-bar")
204       || align_sym == ly_symbol2scm ("break-alignment"))
205     align
206       = Pointer_group_interface::find_grob (me, ly_symbol2scm ("elements"),
207                                             (align_sym == ly_symbol2scm ("staff-bar")
208                                              ? Bar_line::non_empty_barline
209                                              : Break_alignment_interface::has_interface));
210   else
211     {
212       extract_grob_set (me, "elements", elts);
213       for (vsize i = 0; i < elts.size (); i++)
214         {
215           if (elts[i]->get_property ("break-align-symbol") == align_sym)
216             {
217               align = elts[i];
218               break;
219             }
220         }
221     }
222
223   if (!align)
224     return Interval (0, 0) + me->relative_coordinate (p, X_AXIS);
225
226   return align->extent (p, X_AXIS);
227 }
228
229 /*
230   Print a vertical line and  the rank number, to aid debugging.
231 */
232 MAKE_SCHEME_CALLBACK (Paper_column, print, 1);
233 SCM
234 Paper_column::print (SCM p)
235 {
236   Paper_column *me = dynamic_cast<Paper_column *> (unsmob_grob (p));
237
238   string r = to_string (Paper_column::get_rank (me));
239
240   Moment *mom = unsmob_moment (me->get_property ("when"));
241   string when = mom ? mom->to_string () : "?/?";
242
243   Font_metric *musfont = Font_interface::get_default_font (me);
244   SCM properties = Font_interface::text_font_alist_chain (me);
245
246   SCM scm_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
247                                                   properties,
248                                                   ly_string2scm (r));
249   SCM when_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
250                                                    properties,
251                                                    ly_string2scm (when));
252   Stencil t = *unsmob_stencil (scm_mol);
253   t.add_at_edge (Y_AXIS, DOWN, *unsmob_stencil (when_mol), 0.1);
254   t.align_to (X_AXIS, CENTER);
255   t.align_to (Y_AXIS, DOWN);
256
257   Stencil l = Lookup::filled_box (Box (Interval (-0.01, 0.01),
258                                        Interval (-2, -1)));
259
260   SCM small_letters = scm_cons (scm_acons (ly_symbol2scm ("font-size"),
261                                            scm_from_int (-6), SCM_EOL),
262                                 properties);
263
264   int j = 0;
265   for (SCM s = me->get_object ("ideal-distances");
266        scm_is_pair (s); s = scm_cdr (s))
267     {
268       Spring *sp = unsmob_spring (scm_caar (s));
269       if (!unsmob_grob (scm_cdar (s))
270           || !unsmob_grob (scm_cdar (s))->get_system ())
271         continue;
272
273       j++;
274       Real y = -j * 1 - 3;
275       vector<Offset> pts;
276       pts.push_back (Offset (0, y));
277
278       Offset p2 (sp->distance (), y);
279       pts.push_back (p2);
280
281       Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
282       Stencil head (musfont->find_by_name ("arrowheads.open.01"));
283
284       SCM distance_stc = Text_interface::interpret_markup (me->layout ()->self_scm (),
285                                                            small_letters,
286                                                            ly_string2scm (String_convert::form_string ("%5.2lf", sp->distance ())));
287
288       id_stencil.add_stencil (unsmob_stencil (distance_stc)->translated (Offset (sp->distance () / 3, y + 1)));
289       id_stencil.add_stencil (head.translated (p2));
290       id_stencil = id_stencil.in_color (0, 0, 1);
291       l.add_stencil (id_stencil);
292     }
293
294   for (SCM s = me->get_object ("minimum-distances");
295        scm_is_pair (s); s = scm_cdr (s))
296     {
297       Real dist = scm_to_double (scm_cdar (s));
298       Grob *other = unsmob_grob (scm_caar (s));
299       if (!other || other->get_system () != me->get_system ())
300         continue;
301
302       j++;
303
304       Real y = -j * 1.0 - 3.5;
305       vector<Offset> pts;
306       pts.push_back (Offset (0, y));
307
308       Offset p2 (dist, y);
309       pts.push_back (p2);
310
311       Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
312       Stencil head (musfont->find_by_name ("arrowheads.open.0M1"));
313       head.translate_axis (y, Y_AXIS);
314       id_stencil.add_stencil (head);
315
316       SCM distance_stc = Text_interface::interpret_markup (me->layout ()->self_scm (),
317                                                            small_letters,
318                                                            ly_string2scm (String_convert::form_string ("%5.2lf",
319                                                                dist)));
320
321       id_stencil.add_stencil (unsmob_stencil (distance_stc)->translated (Offset (dist / 3, y - 1)));
322
323       id_stencil = id_stencil.in_color (1, 0, 0);
324       l.add_stencil (id_stencil);
325     }
326   t.add_stencil (l);
327   return t.smobbed_copy ();
328 }
329
330 /*
331   This is all too hairy. We use bounded-by-me to make sure that some
332   columns are kept "alive". Unfortunately, when spanners are suicided,
333   this falls apart again, because suicided spanners are still in
334   bounded-by-me
335
336   THIS IS BROKEN KLUDGE. WE SHOULD INVENT SOMETHING BETTER.
337 */
338 MAKE_SCHEME_CALLBACK (Paper_column, before_line_breaking, 1);
339 SCM
340 Paper_column::before_line_breaking (SCM grob)
341 {
342   Grob *me = unsmob_grob (grob);
343
344   SCM bbm = me->get_object ("bounded-by-me");
345   Grob_array *ga = unsmob_grob_array (bbm);
346   if (!ga)
347     return SCM_UNSPECIFIED;
348
349   vector<Grob *> &array (ga->array_reference ());
350
351   for (vsize i = array.size (); i--;)
352     {
353       Grob *g = array[i];
354
355       if (!g || !g->is_live ())
356         /* UGH . potentially quadratic. */
357         array.erase (array.begin () + i);
358     }
359
360   return SCM_UNSPECIFIED;
361 }
362
363 /* FIXME: This is a hack that we use to identify columns that used to
364    contain note-heads but whose note-heads were moved by one of the ligature
365    engravers. Once the ligature engravers are fixed to behave nicely, this
366    function can be removed.
367 */
368 bool
369 Paper_column::is_extraneous_column_from_ligature (Grob *me)
370 {
371   if (!is_musical (me))
372     return false;
373
374   // If all the note-heads that I think are my children actually belong
375   // to another column, then I am extraneous.
376   extract_grob_set (me, "elements", elts);
377   bool has_notehead = false;
378   for (vsize i = 0; i < elts.size (); i++)
379     {
380       if (Rhythmic_head::has_interface (elts[i]))
381         {
382           has_notehead = true;
383           if (dynamic_cast<Item *> (elts[i])->get_column () == me)
384             return false;
385         }
386     }
387   return has_notehead;
388 }
389
390 ADD_INTERFACE (Paper_column,
391                "@code{Paper_column} objects form the top-most X@tie{}parents"
392                " for items.  There are two types of columns: musical and"
393                " non-musical, to which musical and non-musical objects are"
394                " attached respectively.  The spacing engine determines the"
395                " X@tie{}positions of these objects.\n"
396                "\n"
397                "They are numbered, the first (leftmost) is column@tie{}0."
398                "  Numbering happens before line breaking, and columns are not"
399                " renumbered after line breaking.  Since many columns go"
400                " unused, you should only use the rank field to get ordering"
401                " information.  Two adjacent columns may have non-adjacent"
402                " numbers.",
403
404                /* properties */
405                "between-cols "
406                "bounded-by-me "
407                "full-measure-extra-space "
408                "grace-spacing "
409                "labels "
410                "line-break-system-details "
411                "line-break-penalty "
412                "line-break-permission "
413                "maybe-loose "
414                "page-break-penalty "
415                "page-break-permission "
416                "page-turn-penalty "
417                "page-turn-permission "
418                "rhythmic-location "
419                "shortest-playing-duration "
420                "shortest-starter-duration "
421                "spacing "
422                "used "
423                "when ");
424