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