]> git.donarmstrong.com Git - lilypond.git/blob - lily/pure-from-neighbor-engraver.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / pure-from-neighbor-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2011--2015 Mike Solomon <mike@mikesolomon.org>
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 <map>
21 #include <algorithm>
22
23 #include "grob.hh"
24 #include "item.hh"
25 #include "pointer-group-interface.hh"
26 #include "pure-from-neighbor-interface.hh"
27 #include "engraver.hh"
28
29 #include "translator.icc"
30
31 using std::vector;
32
33 class Pure_from_neighbor_engraver : public Engraver
34 {
35   vector<Grob *> pure_relevants_;
36   vector<Grob *> need_pure_heights_from_neighbors_;
37
38 public:
39   TRANSLATOR_DECLARATIONS (Pure_from_neighbor_engraver);
40 protected:
41   DECLARE_ACKNOWLEDGER (pure_from_neighbor);
42   DECLARE_ACKNOWLEDGER (item);
43   void finalize ();
44 };
45
46 Pure_from_neighbor_engraver::Pure_from_neighbor_engraver ()
47 {
48 }
49
50 void
51 Pure_from_neighbor_engraver::acknowledge_item (Grob_info i)
52 {
53   if (!has_interface<Pure_from_neighbor_interface> (i.item ()))
54     pure_relevants_.push_back (i.item ());
55 }
56
57 bool
58 in_same_column (Grob *g1, Grob *g2)
59 {
60   return (g1->spanned_rank_interval ()[LEFT]
61           == g2->spanned_rank_interval ()[LEFT])
62          && (g1->spanned_rank_interval ()[RIGHT]
63              == g2->spanned_rank_interval ()[RIGHT])
64          && (g1->spanned_rank_interval ()[LEFT]
65              == g1->spanned_rank_interval ()[RIGHT]);
66 }
67
68 void
69 Pure_from_neighbor_engraver::acknowledge_pure_from_neighbor (Grob_info i)
70 {
71   need_pure_heights_from_neighbors_.push_back (i.item ());
72 }
73
74 void
75 Pure_from_neighbor_engraver::finalize ()
76 {
77   if (!need_pure_heights_from_neighbors_.size ())
78     return;
79
80   vector_sort (need_pure_heights_from_neighbors_, Grob::less);
81   vector_sort (pure_relevants_, Grob::less);
82
83   /*
84     first, clump need_pure_heights_from_neighbors into
85     vectors of grobs that have the same column.
86   */
87
88   vsize l = 0;
89   vector<vector<Grob *> > need_pure_heights_from_neighbors;
90   do
91     {
92       vector<Grob *> temp;
93       temp.push_back (need_pure_heights_from_neighbors_[l]);
94       for (;
95            (l < need_pure_heights_from_neighbors_.size () - 1
96             && ((need_pure_heights_from_neighbors_[l]
97                  ->spanned_rank_interval ()[LEFT])
98                 == (need_pure_heights_from_neighbors_[l + 1]
99                     ->spanned_rank_interval ()[LEFT])));
100            l++)
101         temp.push_back (need_pure_heights_from_neighbors_[l + 1]);
102       need_pure_heights_from_neighbors.push_back (temp);
103       l++;
104     }
105   while (l < need_pure_heights_from_neighbors_.size ());
106
107   /*
108     then, loop through the pure_relevants_ list, adding the items
109     to the elements of need_pure_heights_from_neighbors_ on either side.
110   */
111
112   int pos[2] = { -1, 0};
113   for (vsize i = 0; i < pure_relevants_.size (); i++)
114     {
115       while (pos[1] < (int) need_pure_heights_from_neighbors.size ()
116              && (pure_relevants_[i]->spanned_rank_interval ()[LEFT]
117                  > (need_pure_heights_from_neighbors[pos[1]][0]
118                     ->spanned_rank_interval ()[LEFT])))
119         {
120           pos[0] = pos[1];
121           pos[1]++;
122         }
123       for (int j = 0; j < 2; j++)
124         if (pos[j] >= 0 && pos[j]
125             < (int) need_pure_heights_from_neighbors.size ())
126           for (vsize k = 0;
127                k < need_pure_heights_from_neighbors[pos[j]].size ();
128                k++)
129             if (!in_same_column (need_pure_heights_from_neighbors[pos[j]][k],
130                                  pure_relevants_[i]))
131               Pointer_group_interface::add_grob
132               (need_pure_heights_from_neighbors[pos[j]][k],
133                ly_symbol2scm ("neighbors"),
134                pure_relevants_[i]);
135     }
136
137   need_pure_heights_from_neighbors_.clear ();
138   pure_relevants_.clear ();
139 }
140
141 ADD_ACKNOWLEDGER (Pure_from_neighbor_engraver, item);
142 ADD_ACKNOWLEDGER (Pure_from_neighbor_engraver, pure_from_neighbor);
143 ADD_TRANSLATOR (Pure_from_neighbor_engraver,
144                 /* doc */
145                 "Coordinates items that get their pure heights from their neighbors.",
146
147                 /* create */
148                 "",
149
150                 /* read */
151                 "",
152
153                 /* write */
154                 ""
155                );