]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-item.cc
patch::: 1.3.60.mb1: Re: LilyPond 1.3.60
[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 Key_item::Key_item (SCM s)
19   : Item (s)
20 {
21   set_elt_property ("c0-position", gh_int2scm (0));
22 }
23
24
25 /*
26   FIXME: too much hardcoding here.
27  */
28 const int FLAT_TOP_PITCH=2; /* fes,ges,as and bes typeset in lower octave */
29 const int SHARP_TOP_PITCH=4; /*  ais and bis typeset in lower octave */
30
31
32 /*
33   FIXME: key-item should just get a list of (position, acc), and leave
34   the thinking to other parties.
35  */
36 int
37 Key_item::calculate_position(SCM pair) const
38 {
39   SCM note = gh_car (pair);
40   if (gh_pair_p (note))
41     {
42       int o = gh_scm2int (gh_car (note));
43       int p = gh_scm2int (gh_cdr (note));
44       return p + o*7 + gh_scm2int (get_elt_property ("c0-position"));
45     }
46   int p = gh_scm2int (note);
47   int a = gh_scm2int (gh_cdr (pair));  
48   
49   // Find the c in the range -4 through 2
50   int from_bottom_pos = gh_scm2int (get_elt_property ("c0-position")) + 4;
51   from_bottom_pos = from_bottom_pos%7;
52   from_bottom_pos = (from_bottom_pos + 7)%7; // Precaution to get positive.
53   int c0 = from_bottom_pos - 4;
54   
55   
56   if ((a<0 && ((p>FLAT_TOP_PITCH) || (p+c0>4)) && (p+c0>1)) 
57       ||
58       (a>0 && ((p>SHARP_TOP_PITCH) || (p+c0>5)) && (p+c0>2))) 
59     {
60       p -= 7; /* Typeset below c_position */
61     }
62   /* Provide for the four cases in which there's a glitch 
63      it's a hack, but probably not worth  
64      the effort of finding a nicer solution.
65      --dl. */
66   if (c0==2 && a>0 && p==3)
67     p -= 7;
68   if (c0==-3 && a>0 && p==-1)
69     p += 7;
70   if (c0==-4 && a<0 && p==-1)
71     p += 7;
72   if (c0==-2 && a<0 && p==-3)
73     p += 7;
74   
75   return p + c0;
76 }
77
78 MAKE_SCHEME_SCORE_ELEMENT_CALLBACKS(Key_item)
79
80 /*
81   TODO
82   - space the `natural' signs wider
83  */
84 Molecule 
85 Key_item::do_brew_molecule () const
86 {
87   Molecule mol;
88
89   Staff_symbol_referencer_interface si (this);
90   Real inter = si.staff_space ()/2.0;
91   
92   SCM newas = get_elt_property ("new-accidentals");  
93
94   /*
95     SCM lists are stacks, so we work from right to left, ending with
96     the cancellation signature.
97   */
98   for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
99     {
100       int a = gh_scm2int (gh_cdar (s));
101       Molecule m = lookup_l ()->afm_find ("accidentals-" + to_str (a));
102       m.translate_axis (calculate_position(gh_car (s)) * inter, Y_AXIS);
103       mol.add_at_edge (X_AXIS, LEFT, m, 0);
104     }
105   
106   if (break_status_dir () != RIGHT)
107     {
108       SCM old = get_elt_property ("old-accidentals");
109       /*
110         Add half a space between  cancellation and key sig.
111
112         As suggested by [Ross], p.148.
113        */
114       Interval x(0, inter);
115       Interval y(0,0);
116
117       mol.add_at_edge (X_AXIS, LEFT, lookup_l()->blank (Box(x,y)),0);
118       
119       for (; gh_pair_p (old); old = gh_cdr (old))
120         {
121           SCM found = SCM_EOL;
122
123           /*
124             find correspondences in pitches 
125            */
126           for (SCM s = newas; gh_pair_p (s); s = gh_cdr (s))
127             if (gh_caar(s) == gh_caar (old))
128               found  = gh_car (s);
129                 
130           if (found == SCM_EOL || gh_cdr (found) != gh_cdar (old))
131             {
132               Molecule m =lookup_l ()->afm_find ("accidentals-0");
133
134               m.translate_axis (calculate_position(gh_car(old)) * inter, Y_AXIS);
135               mol.add_at_edge (X_AXIS, LEFT, m,0);      
136             }
137         }
138
139
140     }
141  
142
143   return mol;
144 }
145
146
147
148