]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-column.cc
* lily/accidental-engraver.cc: formatting fixes.
[lilypond.git] / lily / note-column.cc
1 /*
2   note-column.cc -- implement Note_column
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "note-column.hh"
10
11 #include <math.h>               // ceil
12
13 #include "axis-group-interface.hh"
14 #include "stem.hh"
15 #include "warn.hh"
16 #include "output-def.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "rest.hh"
19 #include "note-head.hh"
20 #include "accidental-placement.hh"
21 #include "pointer-group-interface.hh"
22
23 /*
24   TODO: figure out if we can prune this class. This is just an
25   annoying layer between (rest)collision & (note-head + stem)
26 */
27
28 bool
29 Note_column::has_rests (Grob *me)
30 {
31   return unsmob_grob (me->get_object ("rest"));
32 }
33
34 int
35 Note_column::shift_compare (Grob *const &p1, Grob *const &p2)
36 {
37   SCM s1 = p1->get_property ("horizontal-shift");
38   SCM s2 = p2->get_property ("horizontal-shift");
39
40   int h1 = (scm_is_number (s1)) ? scm_to_int (s1) : 0;
41   int h2 = (scm_is_number (s2)) ? scm_to_int (s2) : 0;
42   return h1 - h2;
43 }
44
45 Item *
46 Note_column::get_stem (Grob *me)
47 {
48   SCM s = me->get_object ("stem");
49   return unsmob_item (s);
50 }
51
52 Slice
53 Note_column::head_positions_interval (Grob *me)
54 {
55   Slice iv;
56
57   iv.set_empty ();
58
59   extract_grob_set (me, "note-heads", heads);
60   for (int i = 0; i < heads.size(); i++) 
61     {
62       Grob *se = heads[i];
63
64       int j = Staff_symbol_referencer::get_rounded_position (se);
65       iv.unite (Slice (j, j));
66     }
67   return iv;
68 }
69
70 Direction
71 Note_column::dir (Grob *me)
72 {
73   Grob *stem = unsmob_grob (me->get_object ("stem"));
74   if (stem && Stem::has_interface (stem))
75     return Stem::get_direction (stem);
76   else
77     {
78       extract_grob_set (me, "note-heads", heads);
79       if (heads.size ())
80         return (Direction)sign (head_positions_interval (me).center ());
81     }
82   
83   programming_error ("note column without heads and stem");
84   return CENTER;
85 }
86
87 void
88 Note_column::set_stem (Grob *me, Grob *stem)
89 {
90   me->set_object ("stem", stem->self_scm ());
91   me->add_dependency (stem);
92   Axis_group_interface::add_element (me, stem);
93 }
94
95 Grob *
96 Note_column::get_rest (Grob *me)
97 {
98   return unsmob_grob (me->get_object ("rest"));
99 }
100
101 void
102 Note_column::add_head (Grob *me, Grob *h)
103 {
104   bool both = false;
105   if (Rest::has_interface (h))
106     {
107       extract_grob_set (me, "note-heads", heads);
108       if (heads.size ())
109         both = true;
110       else
111         me->set_object ("rest", h->self_scm ());
112     }
113   else if (Note_head::has_interface (h))
114     {
115       if (unsmob_grob (me->get_object ("rest")))
116         both = true;
117       Pointer_group_interface::add_grob (me, ly_symbol2scm ("note-heads"), h);
118     }
119
120   if (both)
121     me->warning (_ ("can't have note heads and rests together on a stem"));
122   else
123     Axis_group_interface::add_element (me, h);
124 }
125
126 /**
127    translate the rest symbols vertically by amount DY, but only if
128    they have no staff-position set.
129 */
130 void
131 Note_column::translate_rests (Grob *me, int dy)
132 {
133   Grob *r = unsmob_grob (me->get_object ("rest"));
134   if (r && !scm_is_number (r->get_property ("staff-position")))
135     {
136       r->translate_axis (dy * Staff_symbol_referencer::staff_space (r) / 2.0, Y_AXIS);
137       Grob *p = r->get_parent (Y_AXIS);
138       p->flush_extent_cache (Y_AXIS);
139     }
140 }
141
142 void
143 Note_column::set_dotcol (Grob *me, Grob *d)
144 {
145   Axis_group_interface::add_element (me, d);
146 }
147
148 Grob *
149 Note_column::first_head (Grob *me)
150 {
151   Grob *st = get_stem (me);
152   return st ? Stem::first_head (st) : 0;
153 }
154
155 /*
156   Return the first Accidentals grob that we find in a note-head.
157 */
158 Grob *
159 Note_column::accidentals (Grob *me)
160 {
161   extract_grob_set (me, "note-heads", heads);
162   Grob *acc = 0;
163   for (int i = 0; i < heads.size(); i++) 
164     {
165       Grob *h = heads[i];
166       acc = h ? unsmob_grob (h->get_object ("accidental-grob")) : 0;
167       if (acc)
168         break;
169     }
170
171   if (!acc)
172     return 0;
173
174   if (Accidental_placement::has_interface (acc->get_parent (X_AXIS)))
175     return acc->get_parent (X_AXIS);
176
177   /* compatibility. */
178   return acc;
179 }
180
181
182 Grob *
183 Note_column::arpeggio (Grob *me)
184 {
185   return unsmob_grob (me->get_object ("arpeggio"));
186 }
187
188 ADD_INTERFACE (Note_column, "note-column-interface",
189                "Stem and noteheads combined",
190                "arpeggio note-heads rest-collision rest horizontal-shift stem accidentals force-hshift");