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