]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-item.cc
1ffc2c696cca0a6d7d975b351bb981b1c210c412
[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 (ly_car (p1),  ly_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 ("pitch-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_assoc (pitch, acs);
44   bool new_pitch = !gh_pair_p (opts);
45   opts= new_pitch ? SCM_EOL : gh_cdr (opts);
46   
47   if (cautionary)
48     opts = gh_cons (ly_symbol2scm ("cautionary"), opts);
49   if (natural)
50     opts = gh_cons (ly_symbol2scm ("natural"), opts);
51   if (tie_break_reminder)
52     {
53       /* Ugh, these 'options' can't have a value, faking... */
54       opts = gh_cons (tie_break_reminder->self_scm (), opts);
55       opts = gh_cons (ly_symbol2scm ("tie-break-reminder"), opts);
56     }
57
58   if (new_pitch)
59     {
60       pitch = gh_cons (pitch, opts);
61       acs = scm_merge_x (acs, gh_cons (pitch, SCM_EOL), pitch_less_proc);
62     }
63   else
64     scm_assoc_set_x (acs, pitch, opts);
65
66   me->set_grob_property ("accidentals", acs);
67 }
68
69 Molecule
70 Local_key_item::parenthesize (Grob*me, Molecule m)
71 {
72   Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-("));
73   Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-)"));
74   m.add_at_edge (X_AXIS, LEFT, Molecule (open), 0);
75   m.add_at_edge (X_AXIS, RIGHT, Molecule (close), 0);
76
77   return m;
78 }
79
80 /* HW says: maybe move to tie.cc
81
82   Note, tie should not kill all accidentals when broken, only the ones
83   that are indicated by a property tie-break-reminder, I guess
84
85   Find if any of the accidentals were created because they're at the rhs of a
86   tie.  If that's the reason they exist, and the tie was NOT broken,
87   put the accidental up for deletion.  Clear molecule cache. */
88 MAKE_SCHEME_CALLBACK (Local_key_item, after_line_breaking, 1);
89 SCM
90 Local_key_item::after_line_breaking (SCM smob)
91 {
92   Grob *me = unsmob_grob (smob);
93
94   SCM accs = me->get_grob_property ("accidentals");
95   for (SCM s = accs;
96         gh_pair_p (s); s = ly_cdr (s))
97     {
98       SCM opts = ly_cdar (s);
99
100       SCM t = scm_memq (ly_symbol2scm ("tie-break-reminder"), opts);
101       if (t != SCM_BOOL_F)
102         {
103           Grob *tie = unsmob_grob (ly_cadr (t));
104           Spanner *sp = dynamic_cast<Spanner*> (tie);
105           if (!sp->original_l_)
106             {
107               /* there should be a better way to delete part of me */
108               scm_set_car_x (s, scm_list_n (ly_caar (s),
109                                          ly_symbol2scm ("deleted"),
110                                          SCM_UNDEFINED));
111               me->set_grob_property ("molecule", SCM_EOL);
112             }
113         }
114     }
115   
116   return SCM_UNSPECIFIED;
117 }
118
119 /*
120   UGH. clean me, revise placement routine (See Ross & Wanske;
121   accidental placement is more complicated than this.
122  */
123
124 MAKE_SCHEME_CALLBACK (Local_key_item,brew_molecule,1);
125 SCM
126 Local_key_item::brew_molecule (SCM smob)
127 {
128   Grob* me = unsmob_grob (smob);
129   
130   Molecule mol;
131
132   Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
133   Molecule octave_mol;
134   bool oct_b = false;
135   int lastoct = -100;
136
137   SCM scm_style = me->get_grob_property ("style");
138   String style;
139   if (gh_symbol_p (scm_style))
140     {
141       style = ly_scm2string (scm_symbol_to_string (scm_style));
142     }
143   else
144     {
145       /*
146         preferably no name for the default style.
147        */
148       style = "";
149     }
150
151   SCM accs = me->get_grob_property ("accidentals");
152   for (SCM s = accs;
153         gh_pair_p (s); s = ly_cdr (s))
154     {
155       Pitch p (*unsmob_pitch (ly_caar (s)));
156       SCM opts = ly_cdar (s);
157       
158       if (scm_memq (ly_symbol2scm ("deleted"), opts) != SCM_BOOL_F)
159         continue;
160       
161       // do one octave
162       if (p.octave_i ()  != lastoct) 
163         {
164           if (oct_b)
165             {
166               Real dy =lastoct*7* note_distance;
167               octave_mol.translate_axis (dy, Y_AXIS);
168               mol.add_molecule (octave_mol);
169               octave_mol = Molecule ();
170             }
171           oct_b = true; 
172         }
173       
174       lastoct = p.octave_i () ;
175
176       SCM c0 =  me->get_grob_property ("c0-position");
177       Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
178         * note_distance;
179
180       Molecule acc (Font_interface::get_default_font (me)->
181                     find_by_name (String ("accidentals-") +
182                                   style +
183                                   to_str (p.alteration_i_)));
184       
185       if (scm_memq (ly_symbol2scm ("natural"), opts) != SCM_BOOL_F)
186         {
187           Molecule prefix = Font_interface::get_default_font (me)->
188               find_by_name (String ("accidentals-") + style + String ("0"));
189           acc.add_at_edge (X_AXIS, LEFT, Molecule (prefix), 0);
190         }
191
192       if (scm_memq (ly_symbol2scm ("cautionary"), opts) != SCM_BOOL_F)
193         acc = parenthesize (me, acc);
194
195       acc.translate_axis (dy, Y_AXIS);
196       octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
197     }
198
199   if (oct_b)
200     {
201       Real dy =lastoct*7*note_distance;
202       octave_mol.translate_axis (dy, Y_AXIS);
203       mol.add_molecule (octave_mol);
204       octave_mol = Molecule ();
205     }
206   
207  if (gh_pair_p (accs))
208     {
209       Drul_array<SCM> pads;
210
211       /*
212         Use a cons?
213        */
214       pads[RIGHT] = me->get_grob_property ("right-padding");
215       pads[LEFT] = me->get_grob_property ("left-padding");
216
217
218       // unused ?
219       Direction d = LEFT;
220       do {
221         if (!gh_number_p (pads[d]))
222           continue;
223
224         Box b (Interval (0, gh_scm2double (pads[d]) * note_distance),
225               Interval (0,0));
226         Molecule m (Lookup::blank (b));
227         mol.add_at_edge (X_AXIS, d, m, 0);
228       } while (flip (&d)!= LEFT);
229     }
230
231   return mol.smobbed_copy ();
232 }
233
234 bool
235 Local_key_item::has_interface (Grob*m)
236 {
237   return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
238 }
239 void
240 Local_key_item::set_interface (Grob*m)
241 {
242   m->set_interface (ly_symbol2scm ("accidentals-interface"));
243 }