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