]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-item.cc
release: 1.1.24
[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--1998 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   breakable_b_ =true;
26   default_b_ = false;
27   set_c_position (0);
28 }
29
30 void 
31 Key_item::set_c_position (int c0)
32 {
33   c0_position = c0;
34   // Find the c in the range -4 through 2
35   int from_bottom_pos = c0 + 4; 
36   from_bottom_pos = from_bottom_pos%7;
37   from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
38   c_position  = from_bottom_pos - 4;
39 }
40
41
42 void
43 Key_item::add (int p, int a)
44 {
45   pitch_arr_.push (p);
46   acc_arr_.push (a);
47 }
48
49 void
50 Key_item::add_old (int p, int a)
51 {
52   old_pitch_arr_.push (p);
53   old_acc_arr_.push (a);
54 }
55
56
57 int
58 Key_item::calculate_position(int p, int a) const
59 {
60   if (multi_octave_b_) 
61     {
62       return p + c0_position;
63     }
64   else {
65     if ((a<0 && ((p>FLAT_TOP_PITCH) || (p+c_position>4)) && (p+c_position>1)) 
66         ||
67         (a>0 && ((p>SHARP_TOP_PITCH) || (p+c_position>5)) && (p+c_position>2))) 
68       {
69         p -= 7; /* Typeset below c_position */
70       }
71     return p + c_position;
72   }
73 }
74
75 /*
76   TODO
77   - space the `natural' signs wider
78   - dehair this
79  */
80 Molecule*
81 Key_item::do_brew_molecule_p() const
82 {
83   Molecule*output = new Molecule;
84   Real inter = paper()->internote_f ();
85   
86   int j;
87   if ((break_status_dir_ == LEFT || break_status_dir_ == CENTER)
88       || old_pitch_arr_.size ())
89     {
90       for (int i =0; i < old_pitch_arr_.size(); i++) 
91         {
92           for (j =0; (j < pitch_arr_.size())
93                  && (old_pitch_arr_[i] != pitch_arr_[j]); j++) 
94             ;
95           
96           if (j == pitch_arr_.size()
97               || (old_pitch_arr_[i] == pitch_arr_[j]
98                   && old_acc_arr_[i] != acc_arr_[j]))
99             {
100               Molecule m =lookup_l ()->accidental (0,false);
101               m.translate_axis (calculate_position(old_pitch_arr_[i], old_acc_arr_[i]) * inter, Y_AXIS);
102               output->add_at_edge (X_AXIS, RIGHT, m,0); 
103             }
104         }
105
106       /*
107         Add half a space between  cancellation and key sig.
108
109         As suggested by [Ross], p.148.
110        */
111       Interval x(0, inter);
112       Interval y(0,0);
113
114       output->add_at_edge (X_AXIS, RIGHT, lookup_l()->fill (Box(x,y)),0);
115     }
116  
117   for (int i =0; i < pitch_arr_.size(); i++) 
118     {
119       Molecule m =lookup_l ()->accidental (acc_arr_[i],false);
120       m.translate_axis (calculate_position(pitch_arr_[i], acc_arr_[i]) * inter, Y_AXIS);
121       output->add_at_edge (X_AXIS, RIGHT, m, 0);
122     }
123   if (pitch_arr_.size()) 
124     {
125       Molecule m (lookup_l ()->fill (Box (
126                                           Interval (0, paper()->note_width ()),
127                                           Interval (0,0))));
128       
129       output->add_at_edge (X_AXIS, RIGHT, m,0 );
130     }
131   return output;
132 }
133
134
135
136 void 
137 Key_item::do_pre_processing()
138 {
139   if (default_b_) 
140     {
141       transparent_b_ = (break_status_dir() != RIGHT);
142       set_empty (transparent_b_);
143     }
144 }