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