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