]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-item.cc
patch::: 1.3.1.hwn1
[lilypond.git] / lily / key-item.cc
1 /*
2   key-item.cc -- implement Key_item
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996, 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   keyplacement by Mats Bengtsson
9 */
10
11 #include "key-item.hh"
12 #include "key.hh"
13 #include "debug.hh"
14 #include "molecule.hh"
15 #include "paper-def.hh"
16 #include "lookup.hh"
17 #include "musical-pitch.hh"
18
19 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
20 const int SHARP_TOP_PITCH=4; /*  ais and bis typeset in lower octave */
21
22 Key_item::Key_item ()
23 {
24   multi_octave_b_ = false;
25   set_elt_property ("breakable", SCM_BOOL_T);
26   set_c_position (0);
27 }
28
29 int
30 Key_item::get_c_position () const
31 {
32   // Find the c in the range -4 through 2
33   int from_bottom_pos = c0_position_ + 4;       
34   from_bottom_pos = from_bottom_pos%7;
35   from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
36   return from_bottom_pos - 4;
37 }
38
39
40 void 
41 Key_item::set_c_position (int c0)
42 {
43   c0_position_ = c0;
44 }
45
46
47 void
48 Key_item::add (int p, int a)
49 {
50   pitch_arr_.push (p);
51   acc_arr_.push (a);
52 }
53
54 void
55 Key_item::add_old (int p, int a)
56 {
57   old_pitch_arr_.push (p);
58   old_acc_arr_.push (a);
59 }
60
61
62 int
63 Key_item::calculate_position(int p, int a) const
64 {
65   if (multi_octave_b_) 
66     {
67       return p + c0_position_;
68     }
69   else {
70     if ((a<0 && ((p>FLAT_TOP_PITCH) || (p+get_c_position ()>4)) && (p+get_c_position ()>1)) 
71         ||
72         (a>0 && ((p>SHARP_TOP_PITCH) || (p+get_c_position ()>5)) && (p+get_c_position ()>2))) 
73       {
74         p -= 7; /* Typeset below c_position */
75       }
76     return p + get_c_position ();
77   }
78 }
79
80 /*
81   TODO
82   - space the `natural' signs wider
83   - dehair this
84  */
85 Molecule*
86 Key_item::do_brew_molecule_p() const
87 {
88   Molecule*output = new Molecule;
89   Real inter = staff_line_leading_f ()/2.0;
90   
91   int j;
92   if ((break_status_dir () == LEFT || break_status_dir () == CENTER)
93       || old_pitch_arr_.size ())
94     {
95       for (int i =0; i < old_pitch_arr_.size(); i++) 
96         {
97           for (j =0; (j < pitch_arr_.size())
98                  && (old_pitch_arr_[i] != pitch_arr_[j]); j++) 
99             ;
100           
101           if (j == pitch_arr_.size()
102               || (old_pitch_arr_[i] == pitch_arr_[j]
103                   && old_acc_arr_[i] != acc_arr_[j]))
104             {
105               Molecule m =lookup_l ()->accidental (0,false);
106               m.translate_axis (calculate_position(old_pitch_arr_[i], old_acc_arr_[i]) * inter, Y_AXIS);
107               output->add_at_edge (X_AXIS, RIGHT, m,0); 
108             }
109         }
110
111       /*
112         Add half a space between  cancellation and key sig.
113
114         As suggested by [Ross], p.148.
115        */
116       Interval x(0, inter);
117       Interval y(0,0);
118
119       output->add_at_edge (X_AXIS, RIGHT, lookup_l()->fill (Box(x,y)),0);
120     }
121  
122   for (int i =0; i < pitch_arr_.size(); i++) 
123     {
124       Molecule m =lookup_l ()->accidental (acc_arr_[i],false);
125       m.translate_axis (calculate_position(pitch_arr_[i], acc_arr_[i]) * inter, Y_AXIS);
126       output->add_at_edge (X_AXIS, RIGHT, m, 0);
127     }
128
129   return output;
130 }
131
132
133
134