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