]> git.donarmstrong.com Git - lilypond.git/blob - lily/arpeggio.cc
80858426bc6eb43bc7f12e2f77a83aec4351d8b3
[lilypond.git] / lily / arpeggio.cc
1 /*
2   arpeggio.cc -- implement Arpeggio
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "arpeggio.hh"
10
11 #include "bezier.hh"
12 #include "font-interface.hh"
13 #include "grob.hh"
14 #include "lookup.hh"
15 #include "output-def.hh"
16 #include "pointer-group-interface.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "staff-symbol.hh"
19 #include "stem.hh"
20 #include "warn.hh"
21
22 Grob *
23 Arpeggio::get_common_y (Grob *me)
24 {
25   Grob *common = me;
26
27   extract_grob_set (me, "stems", stems);
28   for (vsize i = 0; i < stems.size (); i++)
29     {
30       Grob *stem = stems[i];
31       common = common->common_refpoint (Staff_symbol_referencer::get_staff_symbol (stem),
32                                         Y_AXIS);
33     }
34
35   return common;
36 }
37
38 MAKE_SCHEME_CALLBACK(Arpeggio, calc_positions, 1);
39 SCM
40 Arpeggio::calc_positions (SCM grob)
41 {
42   Grob *me = unsmob_grob (grob);
43   Grob *common = get_common_y (me);
44   
45   /*
46     TODO:
47
48     Using stems here is not very convenient; should store noteheads
49     instead, and also put them into the support. Now we will mess up
50     in vicinity of a collision.
51   */
52   Interval heads;
53   Real my_y = me->relative_coordinate (common, Y_AXIS);
54
55   extract_grob_set (me, "stems", stems);
56   for (vsize i = 0; i < stems.size (); i++)
57     {
58       Grob *stem = stems[i];
59       Grob *ss = Staff_symbol_referencer::get_staff_symbol (stem);
60       Interval iv = Stem::head_positions (stem);
61       iv *= Staff_symbol::staff_space (ss) / 2.0;
62
63       heads.unite (iv + ss->relative_coordinate (common, Y_AXIS)
64                    - my_y);
65     }
66
67   heads *= 1/Staff_symbol_referencer::staff_space(me);
68
69   return ly_interval2scm (heads);
70 }
71
72 MAKE_SCHEME_CALLBACK (Arpeggio, print, 1);
73 SCM
74 Arpeggio::print (SCM smob)
75 {
76   Grob *me = unsmob_grob (smob);
77   Interval heads = robust_scm2interval (me->get_property ("positions"),
78                                         Interval())
79     * Staff_symbol_referencer::staff_space (me);
80   
81   if (heads.is_empty () || heads.length () < 0.5)
82     {
83       if (!to_boolean (me->get_property ("transparent")))
84         {
85           me->warning ("no heads for arpeggio found?");
86           me->suicide ();
87         }
88       return SCM_EOL;
89     }
90
91   SCM ad = me->get_property ("arpeggio-direction");
92   Direction dir = CENTER;
93   if (is_direction (ad))
94     dir = to_dir (ad);
95
96   Stencil mol;
97   Font_metric *fm = Font_interface::get_default_font (me);
98   Stencil squiggle = fm->find_by_name ("scripts.arpeggio");
99
100   Stencil arrow;
101   if (dir)
102     {
103       arrow = fm->find_by_name ("scripts.arpeggio.arrow." + to_string (dir));
104       heads[dir] -= dir * arrow.extent (Y_AXIS).length ();
105     }
106
107   for (Real y = heads[LEFT]; y < heads[RIGHT];
108        y += squiggle.extent (Y_AXIS).length ())
109     mol.add_at_edge (Y_AXIS, UP, squiggle, 0.0);
110
111   mol.translate_axis (heads[LEFT], Y_AXIS);
112   if (dir)
113     mol.add_at_edge (Y_AXIS, dir, arrow, 0);
114
115   return mol.smobbed_copy ();
116 }
117
118 /* Draws a vertical bracket to the left of a chord
119    Chris Jackson <chris@fluffhouse.org.uk> */
120
121 MAKE_SCHEME_CALLBACK (Arpeggio, brew_chord_bracket, 1);
122 SCM
123 Arpeggio::brew_chord_bracket (SCM smob)
124 {
125   Grob *me = unsmob_grob (smob);
126   Interval heads = robust_scm2interval (me->get_property ("positions"),
127                                         Interval())
128     * Staff_symbol_referencer::staff_space (me);
129
130   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
131   Real sp = 1.5 * Staff_symbol_referencer::staff_space (me);
132   Real dy = heads.length () + sp;
133   Real x = 0.7;
134
135   Stencil mol (Lookup::bracket (Y_AXIS, Interval (0, dy), lt, x, lt));
136   mol.translate_axis (heads[LEFT] - sp / 2.0, Y_AXIS);
137   return mol.smobbed_copy ();
138 }
139
140 MAKE_SCHEME_CALLBACK (Arpeggio, brew_chord_slur, 1);
141 SCM
142 Arpeggio::brew_chord_slur (SCM smob)
143 {
144   Grob *me = unsmob_grob (smob);
145   Interval heads = robust_scm2interval (me->get_property ("positions"),
146                                         Interval())
147     * Staff_symbol_referencer::staff_space (me);
148
149   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
150   Real dy = heads.length ();
151
152   Real height_limit = 1.5;
153   Real ratio = .33;
154   Bezier curve = slur_shape (dy, height_limit, ratio);
155   curve.rotate (M_PI / 2);
156
157   Stencil mol (Lookup::slur (curve, lt, lt));
158   mol.translate_axis (heads[LEFT], Y_AXIS);
159   return mol.smobbed_copy ();
160 }
161
162 /*
163   We have to do a callback, because print () triggers a
164   vertical alignment if it is cross-staff.
165 */
166 MAKE_SCHEME_CALLBACK (Arpeggio, width, 1);
167 SCM
168 Arpeggio::width (SCM smob)
169 {
170   Grob *me = unsmob_grob (smob);
171   Stencil arpeggio = Font_interface::get_default_font (me)->find_by_name ("scripts.arpeggio");
172
173   return ly_interval2scm (arpeggio.extent (X_AXIS));
174 }
175
176 MAKE_SCHEME_CALLBACK (Arpeggio, height, 1);
177 SCM
178 Arpeggio::height (SCM smob)
179 {
180   return Grob::stencil_height (smob);
181 }
182
183 MAKE_SCHEME_CALLBACK (Arpeggio, pure_height, 3);
184 SCM
185 Arpeggio::pure_height (SCM smob, SCM, SCM)
186 {
187   Grob *me = unsmob_grob (smob);
188   if (to_boolean (me->get_property ("cross-staff")))
189     return ly_interval2scm (Interval ());
190
191   return height (smob);
192 }
193
194 ADD_INTERFACE (Arpeggio,
195                "Functions and settings for drawing an arpeggio symbol (a"
196                " wavy line left to noteheads.",
197
198                /* properties */
199                "arpeggio-direction "
200                "positions "
201                "script-priority " // TODO: make around-note-interface
202                "stems "
203                );
204