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