]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-column.cc
Better approximations for cross-staff slurs
[lilypond.git] / lily / script-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--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 "script-column.hh"
21
22 #include "accidental-placement.hh"
23 #include "arpeggio.hh"
24 #include "directional-element-interface.hh"
25 #include "side-position-interface.hh"
26 #include "warn.hh"
27 #include "grob.hh"
28 #include "pointer-group-interface.hh"
29
30 #include <map>
31
32 typedef map<Grob *, vector <Grob *> > Grob_scripts_map;
33
34 void
35 Script_column::add_side_positioned (Grob *me, Grob *script)
36 {
37   SCM p = script->get_property ("script-priority");
38   if (!scm_is_number (p))
39     return;
40
41   Pointer_group_interface::add_grob (me, ly_symbol2scm ("scripts"), script);
42 }
43
44 int
45 pushed_by_slur (Grob *g)
46 {
47   return g->get_property ("avoid-slur") == ly_symbol2scm ("outside")
48          || g->get_property ("avoid-slur") == ly_symbol2scm ("around");
49 }
50
51 LY_DEFINE (ly_grob_script_priority_less, "ly:grob-script-priority-less",
52            2, 0, 0, (SCM a, SCM b),
53            "Compare two grobs by script priority.  For internal use.")
54 {
55   Grob *i1 = unsmob_grob (a);
56   Grob *i2 = unsmob_grob (b);
57
58   /*
59    * avoid-staff of slur trumps script priority. If one grob is
60    * supposed to be printed outside a slur and another grob inside,
61    * we place the inside grob below the outside even if the inside
62    * grob has a higher script-priority.
63    */
64   if (unsmob_grob (i1->get_object ("slur"))
65       && unsmob_grob (i2->get_object ("slur")))
66     {
67       int push1 = pushed_by_slur (i1);
68       int push2 = pushed_by_slur (i2);
69       if (push1 != push2)
70         return push1 < push2 ? SCM_BOOL_T : SCM_BOOL_F;
71     }
72
73   SCM p1 = i1->get_property ("script-priority");
74   SCM p2 = i2->get_property ("script-priority");
75
76   return scm_to_int (p1) < scm_to_int (p2) ? SCM_BOOL_T : SCM_BOOL_F;
77 }
78
79 MAKE_SCHEME_CALLBACK (Script_column, row_before_line_breaking, 1);
80 SCM
81 Script_column::row_before_line_breaking (SCM smob)
82 {
83   Grob *me = unsmob_grob (smob);
84   vector<Grob *> horizontal_grobs;
85   extract_grob_set (me, "scripts", scripts);
86
87   Grob_scripts_map head_scripts_map;
88   vector<Grob *> affect_all_grobs;
89   for (vsize i = 0; i < scripts.size (); i++)
90     {
91       Grob *sc = scripts[i];
92
93       /*
94         Don't want to consider scripts horizontally next to notes.
95       */
96       if (Accidental_placement::has_interface (sc)
97           || Arpeggio::has_interface (sc))
98         {
99           affect_all_grobs.push_back (sc);
100         }
101       else if (sc->get_property_data ("Y-offset")
102                != Side_position_interface::y_aligned_side_proc)
103         {
104           head_scripts_map[sc->get_parent (Y_AXIS)].push_back (sc);
105         }
106     }
107
108   for (Grob_scripts_map::const_iterator i (head_scripts_map.begin ());
109        i != head_scripts_map.end ();
110        i++)
111     {
112       vector<Grob *> grobs = (*i).second;
113
114       // this isn't right in all cases, but in general a safe assumption.
115       concat (grobs, affect_all_grobs);
116       order_grobs (grobs);
117     }
118
119   return SCM_UNSPECIFIED;
120 }
121
122 MAKE_SCHEME_CALLBACK (Script_column, before_line_breaking, 1);
123 SCM
124 Script_column::before_line_breaking (SCM smob)
125 {
126   Grob *me = unsmob_grob (smob);
127   vector<Grob *> staff_sided;
128
129   extract_grob_set (me, "scripts", scripts);
130   for (vsize i = 0; i < scripts.size (); i++)
131     {
132       Grob *sc = scripts[i];
133       /*
134         Don't want to consider scripts horizontally next to notes.
135       */
136       if (sc->get_property_data ("X-offset")
137           != Side_position_interface::x_aligned_side_proc)
138         staff_sided.push_back (sc);
139     }
140
141   order_grobs (staff_sided);
142   return SCM_UNSPECIFIED;
143 }
144
145 void
146 Script_column::order_grobs (vector<Grob *> grobs)
147 {
148   Drul_array<SCM> scripts_drul (SCM_EOL, SCM_EOL);
149   for (vsize i = 0; i < grobs.size (); i++)
150     {
151       Grob *g = grobs[i];
152       Direction d = get_grob_direction (g);
153
154       scripts_drul[d] = scm_cons (g->self_scm (), scripts_drul[d]);
155     }
156
157   for (DOWN_and_UP (d))
158     {
159       SCM ss = scm_reverse_x (scripts_drul[d], SCM_EOL);
160       ss = scm_stable_sort_x (ss, ly_grob_script_priority_less_proc);
161
162       Grob *g = 0;  // current grob in list
163       Grob *last = 0; // previous grob in list
164       SCM initial_outside_staff = SCM_EOL;  // initial outside_staff_priority of current grob
165       SCM last_initial_outside_staff = SCM_EOL;  // initial outside_staff_priority of previous grob
166
167       //  loop over all grobs in script column (already sorted by script_priority)
168       for (SCM s = ss; scm_is_pair (s);
169            s = scm_cdr (s), last = g, last_initial_outside_staff = initial_outside_staff)
170         {
171           g = unsmob_grob (scm_car (s));
172           initial_outside_staff = g->get_property ("outside-staff-priority");
173           if (last)    //not the first grob in the list
174             {
175               SCM last_outside_staff = last->get_property ("outside-staff-priority");
176               /*
177                 if outside_staff_priority is missing for previous grob, just
178                 use it as a support for the current grob
179               */
180               if (!scm_is_number (last_outside_staff))
181                 Side_position_interface::add_support (g, last);
182               /*
183                 if outside_staff_priority is missing or is equal to original
184                 outside_staff_priority of previous grob, set new
185                 outside_staff_priority to just higher than outside_staff_priority
186                 of previous grob in order to preserve ordering.
187               */
188               else if ((!scm_is_number (initial_outside_staff))
189                        || (fabs (scm_to_double (initial_outside_staff)
190                                  - robust_scm2double (last_initial_outside_staff, 0)) < 0.001))
191                 g->set_property ("outside-staff-priority",
192                                  scm_from_double (scm_to_double (last_outside_staff) + 0.1));
193             }
194         }
195     }
196 }
197
198 ADD_INTERFACE (Script_column,
199                "An interface that sorts scripts according to their"
200                " @code{script-priority} and @code{outside-staff-priority}.",
201
202                /* properties */
203                ""
204               );