]> git.donarmstrong.com Git - lilypond.git/blob - lily/pure-from-neighbor-engraver.cc
506127c50dd25b95e553d589ef27eeaf556e3068
[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 Mike Solomon <mike@apollinemike.com>
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   SCM pure_relevant_p = ly_lily_module_constant ("pure-relevant?");
50   if (!Pure_from_neighbor_interface::has_interface (i.item ())
51       && to_boolean (scm_call_1 (pure_relevant_p, i.item ()->self_scm ())))
52     pure_relevants_.push_back (i.item ());
53 }
54
55 void
56 Pure_from_neighbor_engraver::acknowledge_pure_from_neighbor (Grob_info i)
57 {
58   need_pure_heights_from_neighbors_.push_back (i.item ());
59 }
60
61 void
62 Pure_from_neighbor_engraver::finalize ()
63 {
64   if (!need_pure_heights_from_neighbors_.size ())
65     return;
66
67   vector_sort (need_pure_heights_from_neighbors_, Grob::less);
68   vector_sort (pure_relevants_, Grob::less);
69
70   /*
71     first, clump need_pure_heights_from_neighbors into
72     vectors of grobs that have the same column.
73   */
74
75   vsize l = 0;
76   vector<vector<Grob *> > need_pure_heights_from_neighbors;
77   do
78     {
79       vector<Grob *> temp;
80       temp.push_back (need_pure_heights_from_neighbors_[l]);
81       for (;
82            (l < need_pure_heights_from_neighbors_.size () - 1
83             && (need_pure_heights_from_neighbors_[l]->spanned_rank_interval ()[LEFT]
84                 == need_pure_heights_from_neighbors_[l + 1]->spanned_rank_interval ()[LEFT]));
85            l++)
86         temp.push_back (need_pure_heights_from_neighbors_[l + 1]);
87       need_pure_heights_from_neighbors.push_back (temp);
88       l++;
89     }
90   while (l < need_pure_heights_from_neighbors_.size ());
91
92   /*
93     then, loop through the pure_relevants_ list, adding the items
94     to the elements of need_pure_heights_from_neighbors_ on either side.
95   */
96
97   int pos[2] = {-1, 0};
98   for (vsize i = 0; i < pure_relevants_.size (); i++)
99     {
100       if (pos[1] < (int) need_pure_heights_from_neighbors.size ()
101           && (pure_relevants_[i]->spanned_rank_interval ()[LEFT]
102               > need_pure_heights_from_neighbors[pos[1]][0]->spanned_rank_interval ()[LEFT]))
103         {
104           pos[0] = pos[1];
105           pos[1]++;
106         }
107       for (int j = 0; j < 2; j++)
108         if (pos[j] >= 0 && pos[j] < (int) need_pure_heights_from_neighbors.size ())
109           for (vsize k = 0; k < need_pure_heights_from_neighbors[pos[j]].size (); k++)
110             Pointer_group_interface::add_grob (need_pure_heights_from_neighbors[pos[j]][k], ly_symbol2scm ("neighbors"), pure_relevants_[i]);
111     }
112
113   need_pure_heights_from_neighbors_.clear ();
114   pure_relevants_.clear ();
115 }
116
117 #include "translator.icc"
118
119 ADD_ACKNOWLEDGER (Pure_from_neighbor_engraver, item);
120 ADD_ACKNOWLEDGER (Pure_from_neighbor_engraver, pure_from_neighbor);
121 ADD_TRANSLATOR (Pure_from_neighbor_engraver,
122                 /* doc */
123                 "Coordinates items that get their pure heights from their neighbors.",
124
125                 /* create */
126                 "",
127
128                 /* read */
129                 "",
130
131                 /* write */
132                 ""
133                );