]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-column.cc
Fix 787 for real this time (works for more than 2 scripts stacked)
[lilypond.git] / lily / script-column.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2009 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 LY_DEFINE (ly_grob_script_priority_less, "ly:grob-script-priority-less",
45            2, 0, 0, (SCM a, SCM b),
46            "Compare two grobs by script priority.  For internal use.")
47 {
48   Grob *i1 = unsmob_grob (a);
49   Grob *i2 = unsmob_grob (b);
50
51   SCM p1 = i1->get_property ("script-priority");
52   SCM p2 = i2->get_property ("script-priority");
53
54   return scm_to_int (p1) < scm_to_int (p2) ? SCM_BOOL_T : SCM_BOOL_F;
55 }
56
57 MAKE_SCHEME_CALLBACK (Script_column, row_before_line_breaking, 1);
58 SCM
59 Script_column::row_before_line_breaking (SCM smob)
60 {
61   Grob *me = unsmob_grob (smob);
62   vector<Grob*> horizontal_grobs;
63   extract_grob_set (me, "scripts", scripts);
64
65   Grob_scripts_map head_scripts_map;
66   vector<Grob *> affect_all_grobs;
67   for (vsize i = 0; i < scripts.size (); i++)
68     {
69       Grob *sc = scripts[i];
70
71       /*
72         Don't want to consider scripts horizontally next to notes.
73       */
74       if (Accidental_placement::has_interface (sc)
75           || Arpeggio::has_interface (sc))
76         {
77           affect_all_grobs.push_back (sc);
78         }
79       else if (sc->get_property_data ("Y-offset") !=
80                Side_position_interface::y_aligned_side_proc)
81         {
82           head_scripts_map[sc->get_parent (Y_AXIS)].push_back (sc);
83         }
84     }
85
86   for (Grob_scripts_map::const_iterator i (head_scripts_map.begin ());
87        i != head_scripts_map.end ();
88        i++)
89     {
90       vector<Grob*> grobs  = (*i).second;
91
92       // this isn't right in all cases, but in general a safe assumption.
93       concat (grobs, affect_all_grobs);
94       order_grobs (grobs);
95     }
96
97   return SCM_UNSPECIFIED;
98 }
99
100
101 MAKE_SCHEME_CALLBACK (Script_column, before_line_breaking, 1);
102 SCM
103 Script_column::before_line_breaking (SCM smob)
104 {
105   Grob *me = unsmob_grob (smob);
106   vector<Grob*> staff_sided;
107
108   extract_grob_set (me, "scripts", scripts);
109   for (vsize i = 0; i < scripts.size (); i++)
110     {
111       Grob *sc = scripts[i];
112       /*
113         Don't want to consider scripts horizontally next to notes.
114       */
115       if (sc->get_property_data ("X-offset") !=
116           Side_position_interface::x_aligned_side_proc)
117         staff_sided.push_back (sc);
118     }
119
120   order_grobs (staff_sided);
121   return SCM_UNSPECIFIED;
122 }
123
124 void
125 Script_column::order_grobs (vector<Grob*> grobs)
126 {
127   Drul_array<SCM> scripts_drul (SCM_EOL, SCM_EOL);
128   for (vsize i = 0; i < grobs.size (); i++)
129     {
130       Grob *g = grobs[i];
131       Direction d = get_grob_direction (g);
132
133       scripts_drul[d] = scm_cons (g->self_scm (), scripts_drul[d]);
134     }
135
136   Direction d = DOWN;
137   do
138     {
139       SCM ss = scm_reverse_x (scripts_drul[d], SCM_EOL);
140       ss = scm_stable_sort_x (ss, ly_grob_script_priority_less_proc);
141
142       Grob *last = 0;
143       Grob *first = unsmob_grob (scm_car (ss));
144       SCM default_outside_staff = first->get_property("outside-staff-priority");
145       for (SCM s = ss; scm_is_pair (s); s = scm_cdr (s))
146         {
147           Grob *g = unsmob_grob (scm_car (s));
148           if (last)
149             {
150               SCM last_outside_staff = last->get_property ("outside-staff-priority");
151               if (scm_is_number (last_outside_staff))
152                 {
153                   /*
154                     we allow the outside-staff-priority ordering to override the
155                     script-priority ordering; we must set new
156                     outside-staff-priority if outside-staff-priority is
157                     missing or equal to last
158                   */
159                   SCM g_outside_staff = g->get_property ("outside-staff-priority");
160                   if (!scm_is_number (g_outside_staff))
161                       g->set_property ("outside-staff-priority",
162                                      scm_from_double (scm_to_double (last_outside_staff) + 0.1));
163                   else if (fabs (scm_to_double (g_outside_staff) -
164                              scm_to_double(default_outside_staff)) < 0.001)
165                     {
166                       SCM last_script = last->get_property ("script-priority");
167                       SCM g_script = g->get_property("script-priority");
168                       g->set_property (
169                           "outside-staff-priority",
170                           scm_from_double (scm_to_double (last_outside_staff) +
171                             scm_to_double (g_script) -
172                             scm_to_double (last_script)));
173                     }
174                   default_outside_staff = g_outside_staff;
175                 }
176               else
177                 Side_position_interface::add_support (g, last);
178             }
179           last = g;
180         }
181     }
182   while (flip (&d) != DOWN);
183 }
184
185 ADD_INTERFACE (Script_column,
186                "An interface that sorts scripts according to their"
187                " @code{script-priority}.",
188
189                /* properties */
190                ""
191                );