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