]> git.donarmstrong.com Git - lilypond.git/blob - src/stem.cc
df250aa8d37d1e43cdb554009b7c44609d3b28a2
[lilypond.git] / src / stem.cc
1 #include "stem.hh"
2 #include "dimen.hh" 
3 #include "debug.hh"
4 #include "paper.hh"
5 #include "notehead.hh"
6 #include "lookup.hh"
7 #include "molecule.hh"
8 #include "pcol.hh"
9 #include "misc.hh"
10
11 const int STEMLEN=7;
12
13 Stem::Stem(int c, Moment len)
14 {
15     note_length = len;
16     beams_left = 0;
17     beams_right = 0;
18     minnote = 1000;             // invalid values
19     maxnote = -1000;
20     bot = top = 0;
21     flag = 4;
22     dir =0;
23     staff_center=c;
24     stemlen=0;
25     print_flag=true;
26     stem_xoffset=0;
27 }
28
29 void
30 Stem::print() const
31 {
32 #ifndef NPRINT
33     mtor << "{\n";
34     mtor << "flag "<< flag << " print_flag " << print_flag
35          << "min,max [" << minnote << ", " << maxnote << "]";
36         
37
38     Item::print();
39     mtor << "}\n";
40 #endif
41 }
42 void
43 Stem::set_stemend(Real se)
44 {
45
46     // todo: margins
47     assert((dir > 0 && se >= maxnote) || (se <= minnote && dir <0));
48     
49     top = (dir < 0) ? maxnote           : se;
50     bot = (dir < 0) ? se  : minnote;
51     flag = dir*abs(flag);
52 }
53
54 void
55 Stem::add(Notehead *n)
56 {
57     assert(status < PRECALCED);
58     
59     if (n->balltype == 1)
60         return;
61     int p = n->position;
62     if ( p < minnote)
63         minnote = p;
64     if ( p> maxnote)
65         maxnote = p;
66     heads.add(n);
67     n->dependencies.add(this);
68 }
69
70
71 void
72 Stem::set_default_dir()
73 {
74     Real mean = (minnote+maxnote)/2;
75     dir = (mean > staff_center) ? -1: 1;
76 }
77
78 void
79 Stem::set_default_stemlen()
80 {
81     if (!dir)
82         set_default_dir();
83
84     int stafftop = 2*staff_center;
85     stemlen = STEMLEN  + (maxnote - minnote);
86     
87     // uhh... how about non 5-line staffs?      
88     if (maxnote < -2 && dir == 1){
89         int t = staff_center - staff_center/2; 
90         stemlen = t - minnote +2;
91     } else if (minnote > stafftop + 2 && dir == -1) {
92         int t = staff_center + staff_center/2;
93         stemlen = maxnote -t +2;
94     }
95
96     assert(stemlen);
97 }
98
99
100 void
101 Stem::set_default_extents()
102 {
103     assert(minnote<=maxnote);
104
105     if (!stemlen)
106         set_default_stemlen();
107
108     set_stemend((dir< 0) ? maxnote-stemlen: minnote +stemlen);
109     if (dir > 0){       
110         stem_xoffset = paper()->note_width();
111     } else
112         stem_xoffset = 0;
113 }
114
115 void
116 Stem::set_noteheads()
117 {
118     heads.sort(Notehead::compare);
119     int parity=1;
120     int lastpos = heads[0]->position;
121     for (int i=1; i < heads.sz(); i ++) {
122         int dy =abs(lastpos- heads[i]->position);
123         
124         if (dy <= 1) {
125             if (parity)
126                 heads[i]->x_dir = (stem_xoffset>0) ? 1:-1;
127             parity = !parity;
128         } else
129             parity = 0;
130         lastpos = heads[i]->position;
131     }
132 }
133
134 void
135 Stem::do_pre_processing()
136 {
137     if (bot == top)
138         set_default_extents();
139     set_noteheads();
140 }
141
142
143 Interval
144 Stem::width()const
145 {
146     if (!print_flag || abs(flag) <= 4)
147         return Interval(0,0);   // TODO!
148     Paperdef*p= paper();
149     Interval r(p->lookup_->flag(flag).dim.x);
150     r+= stem_xoffset;
151     return r;
152 }
153
154 Molecule*
155 Stem::brew_molecule()const return out;
156 {
157     assert(pstaff_);
158     assert(bot!=top);
159  
160     
161     Paperdef *p =paper();
162
163     Real dy = p->internote();
164     Symbol ss =p->lookup_->stem(bot*dy,top*dy);
165
166     
167     out = new Molecule(Atom(ss));
168
169     if (print_flag&&abs(flag) > 4){
170         Symbol fl = p->lookup_->flag(flag);
171         Molecule m(fl);
172         if (flag < -4){         
173             out->add_bottom(m);
174         } else if (flag > 4) {
175             out->add_top(m);
176         } else
177             assert(false); 
178     }
179
180     out->translate(Offset(stem_xoffset,0));
181 }
182
183 Real
184 Stem::hpos()const
185 {
186     return pcol_->hpos + stem_xoffset;
187 }
188
189