]> git.donarmstrong.com Git - lilypond.git/blob - src/stem.cc
release: 0.0.19
[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, Real 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     assert(!output);
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     if (n->balltype == 1)
58         return;
59     int p = n->position;
60     if ( p < minnote)
61         minnote = p;
62     if ( p> maxnote)
63         maxnote = p;
64     heads.add(n);
65 }
66
67
68 void
69 Stem::set_default_dir()
70 {
71     Real mean = (minnote+maxnote)/2;
72     dir = (mean > staff_center) ? -1: 1;
73 }
74
75 void
76 Stem::set_default_stemlen()
77 {
78     if (!dir)
79         set_default_dir();
80
81     int stafftop = 2*staff_center;
82     stemlen = STEMLEN  + (maxnote - minnote);
83     
84     // uhh... how about non 5-line staffs?      
85     if (maxnote < -2 && dir == 1){
86         int t = staff_center - staff_center/2; 
87         stemlen = t - minnote +2;
88     } else if (minnote > stafftop + 2 && dir == -1) {
89         int t = staff_center + staff_center/2;
90         stemlen = maxnote -t +2;
91     }
92
93     assert(stemlen);
94 }
95
96
97 void
98 Stem::set_default_extents()
99 {
100     assert(minnote<=maxnote);
101
102     if (!stemlen)
103         set_default_stemlen();
104
105     set_stemend((dir< 0) ? maxnote-stemlen: minnote +stemlen);
106     if (dir > 0){       
107         stem_xoffset = paper()->note_width();
108     } else
109         stem_xoffset = 0;
110 }
111
112 void
113 Stem::set_noteheads()
114 {
115     heads.sort(Notehead::compare);
116     int parity=0;
117     int lastpos = heads[0]->position;
118     for (int i=1; i < heads.sz(); i ++) {
119         if (ABS(lastpos- heads[i]->position) == 1) {
120             if (parity)
121                 heads[i]->x_dir = (stem_xoffset>0) ? 1:-1;
122             parity = !parity;
123         } else
124             parity = 0;
125         lastpos = heads[i]->position;
126     }
127 }
128
129 void
130 Stem::postprocess()
131 {
132     if (bot == top)
133         set_default_extents();
134     set_noteheads();
135     brew_molecole();
136 }
137
138 Interval
139 Stem::width()const
140 {
141     if (!print_flag || ABS(flag) <= 4)
142         return Interval(0,0);   // TODO!
143     Paperdef*p= paper();
144     Interval r(p->lookup_->flag(flag).dim.x);
145     r+= stem_xoffset;
146     return r;
147 }
148
149 void
150 Stem::brew_molecole()
151 {
152     assert(pstaff_);
153     assert(bot!=top);
154     assert(!output);
155     
156     Paperdef *p =paper();
157
158     Real dy = p->internote();
159     Symbol ss =p->lookup_->stem(bot*dy,top*dy);
160
161     
162     output = new Molecule(Atom(ss));
163
164     if (print_flag&&ABS(flag) > 4){
165         Symbol fl = p->lookup_->flag(flag);
166         Molecule m(fl);
167         if (flag < -4){         
168             output->add_bottom(m);
169         } else if (flag > 4) {
170             output->add_top(m);
171         } else
172             assert(false); 
173     }
174
175     output->translate(Offset(stem_xoffset,0));
176
177 }
178
179 Real
180 Stem::hpos()const
181 {
182     return pcol_->hpos + stem_xoffset;
183 }
184
185
186 void
187 Stem::preprocess()
188 {
189     set_default_extents();      // needed for the flags.
190 }