]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
bc49391582238c96b5d4a01379266d0672bdbf04
[lilypond.git] / lily / local-key-item.cc
1 /*
2   local-key-item.cc -- implement Local_key_item, Pitch
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "local-key-item.hh"
9 #include "molecule.hh"
10 #include "staff-symbol-referencer.hh"
11 #include "font-interface.hh"
12 #include "paper-def.hh"
13 #include "musical-request.hh"
14 #include "rhythmic-head.hh"
15 #include "misc.hh"
16 #include "spanner.hh"
17 #include "tie.hh"
18 #include "lookup.hh"
19
20 static SCM
21 pitch_less (SCM p1, SCM p2)
22 {
23   return Pitch::less_p (gh_car (p1),  gh_car (p2));
24 }
25
26 static SCM pitch_less_proc;
27
28 void
29 init_pitch_funcs ()
30 {
31   pitch_less_proc = gh_new_procedure2_0 ("pits-less", &pitch_less);
32 }
33
34 ADD_SCM_INIT_FUNC (lkpitch,init_pitch_funcs);
35
36
37 void
38 Local_key_item::add_pitch (Grob*me, Pitch p, bool cautionary, bool natural,
39                            Grob* tie_break_reminder)
40 {
41   SCM acs = me->get_grob_property ("accidentals");
42   SCM pitch = p.smobbed_copy ();
43   SCM opts = SCM_EOL;
44   if (cautionary)
45     opts = gh_cons (ly_symbol2scm ("cautionary"), opts);
46   if (natural)
47     opts = gh_cons (ly_symbol2scm ("natural"), opts);
48   if (tie_break_reminder)
49     {
50       /* Ugh, these 'options' can't have a value, faking... */
51       opts = gh_cons (tie_break_reminder->self_scm (), opts);
52       opts = gh_cons (ly_symbol2scm ("tie-break-reminder"), opts);
53     }
54
55   pitch = gh_cons (pitch, opts);
56   acs = scm_merge_x (acs, gh_cons (pitch, SCM_EOL), pitch_less_proc);
57
58   me->set_grob_property ("accidentals", acs);
59 }
60
61 Molecule
62 Local_key_item::parenthesize (Grob*me, Molecule m)
63 {
64   Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-("));
65   Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-)"));
66   m.add_at_edge (X_AXIS, LEFT, Molecule (open), 0);
67   m.add_at_edge (X_AXIS, RIGHT, Molecule (close), 0);
68
69   return m;
70 }
71
72 /* HW says: maybe move to tie.cc
73
74   Note, tie should not kill all accidentals when broken, only the ones
75   that are indicated by a property tie-break-reminder, I guess
76
77   Find if any of the accidentals were created because they're at the rhs of a
78   tie.  If that's the reason they exist, and the tie was NOT broken,
79   put the accidental up for deletion.  Clear molecule cache. */
80 MAKE_SCHEME_CALLBACK (Local_key_item, after_line_breaking, 1);
81 SCM
82 Local_key_item::after_line_breaking (SCM smob)
83 {
84   Grob *me = unsmob_grob (smob);
85
86   SCM accs = me->get_grob_property ("accidentals");
87   for (SCM s = accs;
88         gh_pair_p (s); s = gh_cdr (s))
89     {
90       SCM opts = gh_cdar (s);
91
92       SCM t = scm_memq (ly_symbol2scm ("tie-break-reminder"), opts);
93       if (t != SCM_BOOL_F)
94         {
95           Grob *tie = unsmob_grob (gh_cadr (t));
96           Spanner *sp = dynamic_cast<Spanner*> (tie);
97           if (!sp->original_l_)
98             {
99               /* there should be a better way to delete part of me */
100               scm_set_car_x (s, gh_list (gh_caar (s),
101                                          ly_symbol2scm ("deleted"),
102                                          SCM_UNDEFINED));
103               me->set_grob_property ("molecule", SCM_EOL);
104             }
105         }
106     }
107   
108   return SCM_UNSPECIFIED;
109 }
110
111 /*
112   UGH. clean me, revise placement routine (See Ross & Wanske;
113   accidental placement is more complicated than this.
114  */
115
116 MAKE_SCHEME_CALLBACK (Local_key_item,brew_molecule,1);
117 SCM
118 Local_key_item::brew_molecule (SCM smob)
119 {
120   Grob* me = unsmob_grob (smob);
121   
122   Molecule mol;
123
124   Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
125   Molecule octave_mol;
126   bool oct_b = false;
127   int lastoct = -100;
128
129   SCM accs = me->get_grob_property ("accidentals");
130   for (SCM s = accs;
131         gh_pair_p (s); s = gh_cdr (s))
132     {
133       Pitch p (*unsmob_pitch (gh_caar (s)));
134       SCM opts = gh_cdar (s);
135       
136       if (scm_memq (ly_symbol2scm ("deleted"), opts) != SCM_BOOL_F)
137         continue;
138       
139       // do one octave
140       if (p.octave_i ()  != lastoct) 
141         {
142           if (oct_b)
143             {
144               Real dy =lastoct*7* note_distance;
145               octave_mol.translate_axis (dy, Y_AXIS);
146               mol.add_molecule (octave_mol);
147               octave_mol = Molecule ();
148             }
149           oct_b = true; 
150         }
151       
152       lastoct = p.octave_i () ;
153
154       SCM c0 =  me->get_grob_property ("c0-position");
155       Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
156         * note_distance;
157       
158       Molecule acc (Font_interface::get_default_font (me)->find_by_name (String ("accidentals-")
159                                                + to_str (p.alteration_i_)));
160       
161       if (scm_memq (ly_symbol2scm ("natural"), opts) != SCM_BOOL_F)
162         {
163           Molecule prefix = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-0"));
164           acc.add_at_edge (X_AXIS, LEFT, Molecule (prefix), 0);
165         }
166
167       if (scm_memq (ly_symbol2scm ("cautionary"), opts) != SCM_BOOL_F)
168         acc = parenthesize (me, acc);
169
170       acc.translate_axis (dy, Y_AXIS);
171       octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
172     }
173
174   if (oct_b)
175     {
176       Real dy =lastoct*7*note_distance;
177       octave_mol.translate_axis (dy, Y_AXIS);
178       mol.add_molecule (octave_mol);
179       octave_mol = Molecule ();
180     }
181   
182  if (gh_pair_p (accs))
183     {
184       Drul_array<SCM> pads;
185
186       /*
187         Use a cons?
188        */
189       pads[RIGHT] = me->get_grob_property ("right-padding");
190       pads[LEFT] = me->get_grob_property ("left-padding");
191
192
193       // unused ?
194       Direction d = LEFT;
195       do {
196         if (!gh_number_p (pads[d]))
197           continue;
198
199         Box b (Interval (0, gh_scm2double (pads[d]) * note_distance),
200               Interval (0,0));
201         Molecule m (Lookup::blank (b));
202         mol.add_at_edge (X_AXIS, d, m, 0);
203       } while (flip (&d)!= LEFT);
204     }
205
206   return mol.smobbed_copy ();
207 }
208
209 bool
210 Local_key_item::has_interface (Grob*m)
211 {
212   return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
213 }
214 void
215 Local_key_item::set_interface (Grob*m)
216 {
217   m->set_interface (ly_symbol2scm ("accidentals-interface"));
218 }