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