]> git.donarmstrong.com Git - lilypond.git/blob - lily/break-align-engraver.cc
1013ac6e7be4b9a2b164f76dfd7e6c62f1a87145
[lilypond.git] / lily / break-align-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2014 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 #include "engraver.hh"
20 #include "protected-scm.hh"
21 #include "break-align-interface.hh"
22 #include "align-interface.hh"
23 #include "axis-group-interface.hh"
24 #include "context.hh"
25 #include "translator-group.hh"
26 #include "item.hh"
27
28 #include "translator.icc"
29
30 class Break_align_engraver : public Engraver
31 {
32   Item *align_;
33   SCM column_alist_;
34   Item *left_edge_;
35
36   void add_to_group (SCM, Item *);
37   void create_alignment (Grob_info);
38 protected:
39   void stop_translation_timestep ();
40   virtual void derived_mark () const;
41 public:
42   TRANSLATOR_DECLARATIONS (Break_align_engraver);
43   DECLARE_ACKNOWLEDGER (break_aligned);
44   DECLARE_ACKNOWLEDGER (break_alignable);
45 };
46
47 void
48 Break_align_engraver::stop_translation_timestep ()
49 {
50   column_alist_ = SCM_EOL;
51
52   align_ = 0;
53   left_edge_ = 0;
54 }
55
56 Break_align_engraver::Break_align_engraver ()
57 {
58   column_alist_ = SCM_EOL;
59   left_edge_ = 0;
60   align_ = 0;
61 }
62
63 void
64 Break_align_engraver::derived_mark () const
65 {
66   scm_gc_mark (column_alist_);
67 }
68
69 void
70 Break_align_engraver::acknowledge_break_alignable (Grob_info inf)
71 {
72   /*
73     Special case for MetronomeMark: filter out items which will be aligned
74     on note heads rather than prefatory material
75   */
76   if (!Item::is_non_musical (inf.item ()))
77     return;
78
79   if (!align_)
80     create_alignment (inf);
81
82   Grob *g = inf.grob ();
83
84   if (!g->get_parent (X_AXIS))
85     g->set_parent (align_, X_AXIS);
86 }
87
88 void
89 Break_align_engraver::acknowledge_break_aligned (Grob_info inf)
90 {
91   if (Item *item = dynamic_cast<Item *> (inf.grob ()))
92     {
93       /*
94         Removed check for item->empty (X_AXIS). --hwn 20/1/04
95       */
96       if (item->get_parent (X_AXIS))
97         return;
98
99       if (!Item::is_non_musical (item))
100         return;
101
102       SCM align_name = item->get_property ("break-align-symbol");
103       if (!scm_is_symbol (align_name))
104         return;
105
106       if (!align_)
107         create_alignment (inf);
108
109       add_to_group (align_name, item);
110     }
111 }
112
113 void
114 Break_align_engraver::create_alignment (Grob_info inf)
115 {
116   align_ = make_item ("BreakAlignment", SCM_EOL);
117
118   Context *origin = inf.origin_contexts (this)[0];
119
120   Translator_group *tg = origin->implementation ();
121   Engraver *random_source = dynamic_cast<Engraver *> (unsmob_translator (scm_car (tg->get_simple_trans_list ())));
122   if (!random_source)
123     random_source = this;
124
125   /*
126     Make left edge appear to come from same context as clef/bar-line etc.
127   */
128   left_edge_ = random_source->make_item ("LeftEdge", SCM_EOL);
129   add_to_group (left_edge_->get_property ("break-align-symbol"),
130                 left_edge_);
131 }
132
133 void
134 Break_align_engraver::add_to_group (SCM align_name, Item *item)
135 {
136   SCM s = scm_assoc (align_name, column_alist_);
137   Item *group = 0;
138
139   if (s != SCM_BOOL_F)
140     {
141       Grob *e = unsmob_grob (scm_cdr (s));
142       group = dynamic_cast<Item *> (e);
143     }
144   else
145     {
146       group = make_item ("BreakAlignGroup", item->self_scm ());
147
148       group->set_property ("break-align-symbol", align_name);
149       group->set_parent (align_, Y_AXIS);
150
151       column_alist_ = scm_assoc_set_x (column_alist_, align_name, group->self_scm ());
152
153       Break_alignment_interface::add_element (align_, group);
154     }
155   Axis_group_interface::add_element (group, item);
156 }
157
158 ADD_ACKNOWLEDGER (Break_align_engraver, break_aligned);
159 ADD_ACKNOWLEDGER (Break_align_engraver, break_alignable);
160 ADD_TRANSLATOR (Break_align_engraver,
161                 /* doc */
162                 "Align grobs with corresponding @code{break-align-symbols}"
163                 " into groups, and order the groups according to"
164                 " @code{breakAlignOrder}.  The left edge of the alignment gets"
165                 " a separate group, with a symbol @code{left-edge}.",
166
167                 /* create */
168                 "BreakAlignment "
169                 "BreakAlignGroup "
170                 "LeftEdge ",
171
172                 /* read */
173                 "",
174
175                 /* write */
176                 ""
177                );