]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-item.cc
release: 1.3.53
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8   keyplacement by Mats Bengtsson
9 */
10
11 #include "group-interface.hh" 
12 #include "key-item.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
15 #include "lookup.hh"
16 #include "staff-symbol-referencer.hh"
17
18 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
19 const int SHARP_TOP_PITCH=4; /*  ais and bis typeset in lower octave */
20
21 Key_item::Key_item ()
22 {
23   set_elt_property ("breakable", SCM_BOOL_T);
24   set_elt_property ("c0-position", gh_int2scm (0));
25
26   set_elt_property ("old-accidentals", SCM_EOL);
27   set_elt_property ("new-accidentals", SCM_EOL);
28 }
29
30 void
31 Key_item::add (int p, int a)
32 {
33   SCM pair = gh_cons (gh_int2scm (p),gh_int2scm (a));
34   Group_interface (this, "new-accidentals").add_thing (pair);
35 }
36
37 void
38 Key_item::add_old (int p, int a)
39 {
40   SCM pair = gh_cons (gh_int2scm (p),gh_int2scm (a));  
41   Group_interface (this, "old-accidentals").add_thing (pair);
42 }
43
44
45 int
46 Key_item::calculate_position(SCM pair) const
47 {
48   int p = gh_scm2int (gh_car (pair));
49   int a = gh_scm2int (gh_cdr (pair));  
50   
51   if (to_boolean (get_elt_property ("multi-octave")))
52     {
53       return p + gh_scm2int (get_elt_property ("c0-position"));
54     }
55   else {
56     // Find the c in the range -4 through 2
57     int from_bottom_pos = gh_scm2int (get_elt_property ("c0-position")) + 4;
58     from_bottom_pos = from_bottom_pos%7;
59     from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
60     int c0 = from_bottom_pos - 4;
61
62     
63     if ((a<0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1)) 
64         ||
65         (a>0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2))) 
66       {
67         p -= 7; /* Typeset below c_position */
68       }
69     /* Provide for the four cases in which there's a glitch 
70        it's a hack, but probably not worth  
71        the effort of finding a nicer solution.
72        --dl. */
73     if (c0==2 && a>0 && p==3)
74       p -= 7;
75     if (c0==-3 && a>0 && p==-1)
76       p += 7;
77     if (c0==-4 && a<0 && p==-1)
78       p += 7;
79     if (c0==-2 && a<0 && p==-3)
80       p += 7;
81     
82     return p + c0;
83   }
84 }
85
86 /*
87   TODO
88   - space the `natural' signs wider
89
90  */
91
92 Molecule 
93 Key_item::do_brew_molecule() const
94 {
95   Molecule mol;
96
97   Staff_symbol_referencer_interface si (this);
98   Real inter = si.staff_space ()/2.0;
99   
100   SCM newas = get_elt_property ("new-accidentals");  
101
102   /*
103     SCM lists are stacks, so we work from right to left, ending with
104     the cancellation signature.
105   */
106   for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
107     {
108       int a = gh_scm2int (gh_cdar (s));
109       Molecule m = lookup_l ()->afm_find ("accidentals-" + to_str (a));
110       m.translate_axis (calculate_position(gh_car (s)) * inter, Y_AXIS);
111       mol.add_at_edge (X_AXIS, LEFT, m, 0);
112     }
113   
114   if (break_status_dir () != RIGHT)
115     {
116       SCM old = get_elt_property ("old-accidentals");
117       /*
118         Add half a space between  cancellation and key sig.
119
120         As suggested by [Ross], p.148.
121        */
122       Interval x(0, inter);
123       Interval y(0,0);
124
125       mol.add_at_edge (X_AXIS, LEFT, lookup_l()->blank (Box(x,y)),0);
126       
127       for (; gh_pair_p (old); old = gh_cdr (old))
128         {
129           SCM found = SCM_EOL;
130
131           /*
132             find correspondences in pitches 
133            */
134           for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
135             if (gh_caar(s) == gh_caar (old))
136               found  = gh_car (s);
137                 
138           if (found == SCM_EOL || gh_cdr (found) != gh_cdar (old))
139             {
140               Molecule m =lookup_l ()->afm_find ("accidentals-0");
141
142               m.translate_axis (calculate_position(gh_car(old)) * inter, Y_AXIS);
143               mol.add_at_edge (X_AXIS, LEFT, m,0);      
144             }
145         }
146
147
148     }
149  
150
151   return mol;
152 }
153
154
155
156