]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column.cc
1cb1452c852dba5eef1ad055387bbab354df5aa4
[lilypond.git] / lily / paper-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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_musical (Grob *me)
128 {
129   SCM m = me->get_property ("shortest-starter-duration");
130   Moment s (0);
131   if (unsmob_moment (m))
132     s = *unsmob_moment (m);
133   return s != Moment (0);
134 }
135
136 bool
137 Paper_column::is_used (Grob *me)
138 {
139   extract_grob_set (me, "elements", elts);
140   if (elts.size ())
141     return true;
142
143   extract_grob_set (me, "bounded-by-me", bbm);
144   if (bbm.size ())
145     return true;
146
147   if (Paper_column::is_breakable (me))
148     return true;
149
150   if (to_boolean (me->get_property ("used")))
151     return true;
152
153   if (scm_is_pair (me->get_property ("labels")))
154     return true;
155
156   return false;
157 }
158
159 bool
160 Paper_column::is_breakable (Grob *me)
161 {
162   return scm_is_symbol (me->get_property ("line-break-permission"));
163 }
164
165 Real
166 Paper_column::minimum_distance (Grob *left, Grob *right)
167 {
168   Drul_array<Grob *> cols (left, right);
169   Drul_array<Skyline> skys = Drul_array<Skyline> (Skyline (RIGHT), Skyline (LEFT));
170
171   for (LEFT_and_RIGHT (d))
172     {
173       Skyline_pair *sp = Skyline_pair::unsmob (cols[d]->get_property ("horizontal-skylines"));
174       if (sp)
175         skys[d] = (*sp)[-d];
176     }
177
178   skys[RIGHT].merge (Separation_item::conditional_skyline (right, left));
179
180   return max (0.0, skys[LEFT].distance (skys[RIGHT]));
181 }
182
183 Interval
184 Paper_column::break_align_width (Grob *me, SCM align_sym)
185 {
186   Grob *p = me->get_parent (X_AXIS);
187
188   if (is_musical (me))
189     {
190       me->programming_error ("tried to get break-align-width of a musical column");
191       return Interval (0, 0) + me->relative_coordinate (p, X_AXIS);
192     }
193
194   Grob *align = 0;
195   if (align_sym == ly_symbol2scm ("staff-bar")
196       || align_sym == ly_symbol2scm ("break-alignment"))
197     align
198       = Pointer_group_interface::find_grob (me, ly_symbol2scm ("elements"),
199                                             (align_sym == ly_symbol2scm ("staff-bar")
200                                              ? Bar_line::non_empty_barline
201                                              : Break_alignment_interface::has_interface));
202   else
203     {
204       extract_grob_set (me, "elements", elts);
205       for (vsize i = 0; i < elts.size (); i++)
206         {
207           if (elts[i]->get_property ("break-align-symbol") == align_sym)
208             {
209               align = elts[i];
210               break;
211             }
212         }
213     }
214
215   if (!align)
216     return Interval (0, 0) + me->relative_coordinate (p, X_AXIS);
217
218   return align->extent (p, X_AXIS);
219 }
220
221 /*
222   Loop through elements of a PaperColumn, find all grobs implementing specified
223   interface and return their combined extent.
224 */
225 Interval
226 Paper_column::get_interface_extent (Grob *column, SCM iface, Axis a)
227 {
228   Interval extent = Interval (0, 0);
229   extract_grob_set (column, "elements", elts);
230
231   for (vsize i = 0; i < elts.size (); i++)
232     if (elts[i]->internal_has_interface (iface))
233       extent.unite (robust_relative_extent (elts[i], elts[i], a));
234
235   return extent;
236 }
237
238 /*
239   Print a:
240   - vertical line,
241   - the rank number,
242   - rank moment,
243   - blue arrow representing ideal distance,
244   - red arrow representing minimum distance
245   to aid debugging.  To turn this on, simply add
246   \override Score.PaperColumn #'stencil = #ly:paper-column::print
247   \override Score.NonMusicalPaperColumn #'stencil = #ly:paper-column::print
248   to your score.
249   Also, as of 2013-10-16 there's a switch in Frescobaldi that turns this on.
250 */
251 MAKE_SCHEME_CALLBACK (Paper_column, print, 1);
252 SCM
253 Paper_column::print (SCM p)
254 {
255   Paper_column *me = dynamic_cast<Paper_column *> (unsmob_grob (p));
256
257   string r = ::to_string (Paper_column::get_rank (me));
258
259   Moment *mom = unsmob_moment (me->get_property ("when"));
260   string when = mom ? mom->to_string () : "?/?";
261
262   Font_metric *musfont = Font_interface::get_default_font (me);
263   SCM properties = Font_interface::text_font_alist_chain (me);
264   SCM scm_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
265                                                   properties,
266                                                   ly_string2scm (r));
267   SCM when_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
268                                                    properties,
269                                                    ly_string2scm (when));
270   Stencil t = *unsmob_stencil (scm_mol);
271   t.scale (1.2, 1.4);
272   t.add_at_edge (Y_AXIS, DOWN, *unsmob_stencil (when_mol), 0.1);
273   t.align_to (X_AXIS, LEFT);
274   // compensate for font serifs and half letter-distance
275   t.translate (Offset (-0.1, 0));
276   t.align_to (Y_AXIS, DOWN);
277
278   Stencil l = Lookup::filled_box (Box (Interval (0, 0.02),
279                                        Interval (-8, -1)));
280
281   Real small_pad = 0.15;
282   Real big_pad = 0.35;
283
284   // number of printed arrows from *both* loops
285   int j = 0;
286
287   for (SCM s = me->get_object ("ideal-distances");
288        scm_is_pair (s); s = scm_cdr (s))
289     {
290       Spring *sp = unsmob_spring (scm_caar (s));
291       if (!unsmob_grob (scm_cdar (s))
292           || !unsmob_grob (scm_cdar (s))->get_system ())
293         continue;
294
295       j++;
296
297       Stencil arrowhead (musfont->find_by_name ("arrowheads.open.01"));
298       // initial scaling; it will also scale with font-size.
299       arrowhead.scale (1, 1.66);
300       Real head_len = arrowhead.extent (X_AXIS).length ();
301
302       SCM stil = Text_interface::interpret_markup (me->layout ()->self_scm (),
303                                                    properties,
304                                                    ly_string2scm (String_convert::form_string ("%5.2lf", sp->distance ())));
305       Stencil *number_stc = unsmob_stencil (stil);
306       number_stc->scale (1, 1.1);
307       Real num_height = number_stc->extent (Y_AXIS).length ();
308       Real num_len = number_stc->extent (X_AXIS).length ();
309       number_stc->align_to (Y_AXIS, DOWN);
310
311       // arrow's y-coord relative to the top of l stencil:
312       Real y = -2.5;
313       y -= j * (num_height + small_pad + big_pad);
314       // horizontally center number on the arrow, excluding arrowhead.
315       Offset num_off = Offset ((sp->distance () - num_len - head_len) / 2,
316                                y + small_pad);
317
318       vector<Offset> pts;
319       pts.push_back (Offset (0, y));
320
321       Offset p2 (sp->distance (), y);
322       pts.push_back (p2);
323
324       Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
325       id_stencil.add_stencil (arrowhead.translated (p2));
326       id_stencil.add_stencil (number_stc->translated (num_off));
327       // use a lighter shade of blue so it will remain legible on black background.
328       id_stencil = id_stencil.in_color (0.2, 0.4, 1);
329       l.add_stencil (id_stencil);
330     }
331
332   for (SCM s = me->get_object ("minimum-distances");
333        scm_is_pair (s); s = scm_cdr (s))
334     {
335       Real dist = scm_to_double (scm_cdar (s));
336       Grob *other = unsmob_grob (scm_caar (s));
337       if (!other || other->get_system () != me->get_system ())
338         continue;
339
340       j++;
341
342       Stencil arrowhead (musfont->find_by_name ("arrowheads.open.01"));
343       // initial scaling; it will also scale with font-size.
344       arrowhead.scale (1, 1.66);
345       Real head_len = arrowhead.extent (X_AXIS).length ();
346
347       SCM stil = Text_interface::interpret_markup (me->layout ()->self_scm (),
348                                                    properties,
349                                                    ly_string2scm (String_convert::form_string ("%5.2lf", dist)));
350       Stencil *number_stc = unsmob_stencil (stil);
351       number_stc->scale (1, 1.1);
352       Real num_height = number_stc->extent (Y_AXIS).length ();
353       Real num_len = number_stc->extent (X_AXIS).length ();
354       number_stc->align_to (Y_AXIS, UP);
355
356       // arrow's y-coord relative to the top of l stencil:
357       Real y = -3;
358       y -= j * (num_height + small_pad + big_pad);
359       // horizontally center number on the arrow, excluding arrowhead.
360       Offset num_off = Offset ((dist - num_len - head_len) / 2,
361                                y - small_pad);
362
363       vector<Offset> pts;
364       pts.push_back (Offset (0, y));
365
366       Offset p2 (dist, y);
367       pts.push_back (p2);
368
369       Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
370       id_stencil.add_stencil (arrowhead.translated (p2));
371       id_stencil.add_stencil (number_stc->translated (num_off));
372       // use a lighter shade of red so it will remain legible on black background.
373       id_stencil = id_stencil.in_color (1, 0.25, 0.25);
374       l.add_stencil (id_stencil);
375     }
376   t.add_stencil (l);
377   return t.smobbed_copy ();
378 }
379
380 /*
381   This is all too hairy. We use bounded-by-me to make sure that some
382   columns are kept "alive". Unfortunately, when spanners are suicided,
383   this falls apart again, because suicided spanners are still in
384   bounded-by-me
385
386   THIS IS BROKEN KLUDGE. WE SHOULD INVENT SOMETHING BETTER.
387 */
388 MAKE_SCHEME_CALLBACK (Paper_column, before_line_breaking, 1);
389 SCM
390 Paper_column::before_line_breaking (SCM grob)
391 {
392   Grob *me = unsmob_grob (grob);
393
394   SCM bbm = me->get_object ("bounded-by-me");
395   Grob_array *ga = unsmob_grob_array (bbm);
396   if (!ga)
397     return SCM_UNSPECIFIED;
398
399   vector<Grob *> &array (ga->array_reference ());
400
401   for (vsize i = array.size (); i--;)
402     {
403       Grob *g = array[i];
404
405       if (!g || !g->is_live ())
406         /* UGH . potentially quadratic. */
407         array.erase (array.begin () + i);
408     }
409
410   return SCM_UNSPECIFIED;
411 }
412
413 /* FIXME: This is a hack that we use to identify columns that used to
414    contain note-heads but whose note-heads were moved by one of the ligature
415    engravers. Once the ligature engravers are fixed to behave nicely, this
416    function can be removed.
417 */
418 bool
419 Paper_column::is_extraneous_column_from_ligature (Grob *me)
420 {
421   if (!is_musical (me))
422     return false;
423
424   // If all the note-heads that I think are my children actually belong
425   // to another column, then I am extraneous.
426   extract_grob_set (me, "elements", elts);
427   bool has_notehead = false;
428   for (vsize i = 0; i < elts.size (); i++)
429     {
430       if (Rhythmic_head::has_interface (elts[i]))
431         {
432           has_notehead = true;
433           if (dynamic_cast<Item *> (elts[i])->get_column () == me)
434             return false;
435         }
436     }
437   return has_notehead;
438 }
439
440 ADD_INTERFACE (Paper_column,
441                "@code{Paper_column} objects form the top-most X@tie{}parents"
442                " for items.  There are two types of columns: musical and"
443                " non-musical, to which musical and non-musical objects are"
444                " attached respectively.  The spacing engine determines the"
445                " X@tie{}positions of these objects.\n"
446                "\n"
447                "They are numbered, the first (leftmost) is column@tie{}0."
448                "  Numbering happens before line breaking, and columns are not"
449                " renumbered after line breaking.  Since many columns go"
450                " unused, you should only use the rank field to get ordering"
451                " information.  Two adjacent columns may have non-adjacent"
452                " numbers.",
453
454                /* properties */
455                "between-cols "
456                "bounded-by-me "
457                "full-measure-extra-space "
458                "grace-spacing "
459                "labels "
460                "line-break-system-details "
461                "line-break-penalty "
462                "line-break-permission "
463                "maybe-loose "
464                "page-break-penalty "
465                "page-break-permission "
466                "page-turn-penalty "
467                "page-turn-permission "
468                "rhythmic-location "
469                "shortest-playing-duration "
470                "shortest-starter-duration "
471                "spacing "
472                "used "
473                "when ");
474