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