]> git.donarmstrong.com Git - lilypond.git/blob - lily/vaticana-ligature.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / vaticana-ligature.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2003--2014 Juergen Reuter <reuter@ipd.uka.de>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "vaticana-ligature.hh"
21
22 #include "bezier.hh"
23 #include "font-interface.hh"
24 #include "international.hh"
25 #include "item.hh"
26 #include "lookup.hh"
27 #include "note-head.hh"
28 #include "output-def.hh"
29 #include "staff-symbol-referencer.hh"
30 #include "warn.hh"
31
32 Stencil
33 vaticana_brew_cauda (Grob *me,
34                      int pos,
35                      int delta_pitch,
36                      Real thickness,
37                      Real blotdiameter)
38 {
39   bool on_staffline = Staff_symbol_referencer::on_line (me, pos);
40   bool above_staff = pos > Staff_symbol_referencer::staff_span (me)[UP];
41
42   if (delta_pitch > -1)
43     {
44       me->programming_error ("flexa cauda: invalid delta_pitch; assuming -1");
45       delta_pitch = -1;
46     }
47   Real length;
48   if (on_staffline)
49     {
50       if (delta_pitch >= -1)
51         length = 1.30;
52       else if (delta_pitch >= -2)
53         length = 1.35;
54       else
55         length = 1.85;
56     }
57   else
58     {
59       if (delta_pitch >= -1)
60         if (above_staff)
61           length = 1.30;
62         else
63           length = 1.00;
64       else if (delta_pitch >= -2)
65         length = 1.35;
66       else if (delta_pitch >= -3)
67         length = 1.50;
68       else
69         length = 1.85;
70     }
71   Box cauda_box (Interval (0, thickness), Interval (-length, 0));
72   return Lookup::round_filled_box (cauda_box, blotdiameter);
73 }
74
75 /*
76  * TODO: move this function to class Lookup?
77  */
78 Stencil
79 vaticana_brew_flexa (Grob *me,
80                      bool solid,
81                      Real line_thickness)
82 {
83   Real staff_space = Staff_symbol_referencer::staff_space (me);
84   Stencil stencil;
85   Real right_height = 0.6 * staff_space;
86
87   Real interval;
88   SCM flexa_height_scm = me->get_property ("flexa-height");
89   if (flexa_height_scm != SCM_EOL)
90     interval = scm_to_int (flexa_height_scm);
91   else
92     {
93       me->warning ("Vaticana_ligature: "
94                    + _ ("flexa-height undefined; assuming 0"));
95       interval = 0.0;
96     }
97
98   if (interval >= 0.0)
99     me->warning (_ ("ascending vaticana style flexa"));
100
101   Real width = robust_scm2double (me->get_property ("flexa-width"), 2);
102
103   /*
104    * Compensate curve thickness that appears to be smaller in steep
105    * section of bend.
106    */
107   Real left_height
108     = right_height
109       + min (0.12 * abs (interval), 0.3) * staff_space;
110
111   /*
112    * Compensate optical illusion regarding vertical position of left
113    * and right endings due to curved shape.
114    */
115   Real ypos_correction = -0.1 * staff_space * sign (interval);
116   Real interval_correction = 0.2 * staff_space * sign (interval);
117   Real corrected_interval = interval * staff_space + interval_correction;
118
119   /*
120    * middle curve of flexa shape
121    */
122   Bezier curve;
123   curve.control_[0] = Offset (0.00 * width, 0.0);
124   curve.control_[1] = Offset (0.33 * width, corrected_interval / 2.0);
125   curve.control_[2] = Offset (0.66 * width, corrected_interval / 2.0);
126   curve.control_[3] = Offset (1.00 * width, corrected_interval / 2.0);
127
128   Bezier top_curve = curve, bottom_curve = curve;
129   for (int i = 0; i < 4; i++)
130     {
131       Real curve_thickness = 0.33 * ((3 - i) * left_height + i * right_height);
132       top_curve.control_[i] += Offset (0, 0.5 * curve_thickness);
133       bottom_curve.control_[i] -= Offset (0, 0.5 * curve_thickness);
134     }
135
136   if (solid)
137     {
138       Stencil solid_head
139         = Lookup::bezier_sandwich (top_curve, bottom_curve, 0.0);
140       stencil.add_stencil (solid_head);
141     }
142   else // outline
143     {
144       Bezier inner_top_curve = top_curve;
145       inner_top_curve.translate (Offset (0.0, -line_thickness));
146       Stencil top_edge
147         = Lookup::bezier_sandwich (top_curve, inner_top_curve, 0.0);
148       stencil.add_stencil (top_edge);
149
150       Bezier inner_bottom_curve = bottom_curve;
151       inner_bottom_curve.translate (Offset (0.0, +line_thickness));
152       Stencil bottom_edge
153         = Lookup::bezier_sandwich (bottom_curve, inner_bottom_curve, 0.0);
154       stencil.add_stencil (bottom_edge);
155
156       /*
157        * TODO: Use horizontal slope with proper slope value rather
158        * than filled box for left edge, since the filled box stands
159        * out from the flexa shape if the interval is big and the line
160        * thickness small.  The difficulty here is to compute a proper
161        * slope value, as it should roughly be equal with the slope of
162        * the left end of the bezier curve.
163        */
164       Box left_edge_box (Interval (0, line_thickness),
165                          Interval (-0.5 * left_height, +0.5 * left_height));
166       Stencil left_edge = Lookup::filled_box (left_edge_box);
167       stencil.add_stencil (left_edge);
168
169       Box right_edge_box (Interval (-line_thickness, 0),
170                           Interval (-0.5 * right_height, +0.5 * right_height));
171       Stencil right_edge = Lookup::filled_box (right_edge_box);
172       right_edge.translate_axis (width, X_AXIS);
173       right_edge.translate_axis (corrected_interval / 2.0, Y_AXIS);
174       stencil.add_stencil (right_edge);
175     }
176   stencil.translate_axis (ypos_correction, Y_AXIS);
177   return stencil;
178 }
179
180 Stencil
181 vaticana_brew_join (Grob *me, int delta_pitch,
182                     Real join_thickness, Real blotdiameter)
183 {
184   Real staff_space = Staff_symbol_referencer::staff_space (me);
185   if (!delta_pitch)
186     {
187       me->programming_error ("Vaticana_ligature: "
188                              "zero join (delta_pitch == 0)");
189       return Lookup::blank (Box (Interval (0, 0), Interval (0, 0)));
190     }
191   Interval x_extent = Interval (0, join_thickness);
192   Interval y_extent = (delta_pitch > 0)
193                       ? Interval (0, delta_pitch * 0.5 * staff_space) : // ascending join
194                       Interval (delta_pitch * 0.5 * staff_space, 0); // descending join
195   Box join_box (x_extent, y_extent);
196   return Lookup::round_filled_box (join_box, blotdiameter);
197 }
198
199 Stencil
200 vaticana_brew_primitive (Grob *me)
201 {
202   SCM glyph_name_scm = me->get_property ("glyph-name");
203   if (glyph_name_scm == SCM_EOL)
204     {
205       me->programming_error ("Vaticana_ligature: "
206                              "undefined glyph-name -> ignoring grob");
207       return Lookup::blank (Box (Interval (0, 0), Interval (0, 0)));
208     }
209
210   string glyph_name = ly_scm2string (glyph_name_scm);
211
212   Stencil out;
213   Real thickness = robust_scm2double (me->get_property ("thickness"), 1);
214
215   Real line_thickness
216     = thickness * me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
217
218   Real blotdiameter
219     = (me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter")));
220
221   int pos = Staff_symbol_referencer::get_rounded_position (me);
222
223   SCM delta_pitch_scm = me->get_property ("delta-position");
224   int delta_pitch;
225   if (delta_pitch_scm != SCM_EOL)
226     delta_pitch = scm_to_int (delta_pitch_scm);
227   else
228     delta_pitch = 0;
229
230   Real x_offset = robust_scm2double (me->get_property ("x-offset"), 0);
231
232   bool add_stem = to_boolean (me->get_property ("add-stem"));
233   bool add_cauda = to_boolean (me->get_property ("add-cauda"));
234   bool add_join = to_boolean (me->get_property ("add-join"));
235
236   if (glyph_name == "")
237     {
238       /*
239        * This is an empty head.  This typically applies for the right
240        * side of a curved flexa shape, which is already typeset by the
241        * associated left side head.  The only possible thing left to
242        * do is to draw a vertical join to the next head.  (Urgh: need
243        * flexa_width.)
244        */
245       Real staff_space = Staff_symbol_referencer::staff_space (me);
246       Real flexa_width = robust_scm2double (me->get_property ("flexa-width"), 2) * staff_space;
247       out
248         = Lookup::blank (Box (Interval (0, 0.5 * flexa_width), Interval (0, 0)));
249     }
250   else if (glyph_name == "flexa")
251     out = vaticana_brew_flexa (me, true, line_thickness);
252   else
253     {
254       out
255         = Font_interface::get_default_font (me)->
256           find_by_name ("noteheads.s" + glyph_name);
257     }
258   out.translate_axis (x_offset, X_AXIS);
259   Real head_width = out.extent (X_AXIS).length ();
260
261   if (add_cauda)
262     {
263       Stencil cauda
264         = vaticana_brew_cauda (me, pos, delta_pitch,
265                                line_thickness, blotdiameter);
266       out.add_stencil (cauda);
267     }
268
269   if (add_stem)
270     {
271       Stencil stem
272         = vaticana_brew_cauda (me, pos, -1,
273                                line_thickness, blotdiameter);
274       stem.translate_axis (head_width - line_thickness, X_AXIS);
275       out.add_stencil (stem);
276     }
277
278   if (add_join)
279     {
280       Stencil join
281         = vaticana_brew_join (me, delta_pitch, line_thickness, blotdiameter);
282       join.translate_axis (head_width - line_thickness, X_AXIS);
283       out.add_stencil (join);
284     }
285
286   return out;
287 }
288
289 MAKE_SCHEME_CALLBACK (Vaticana_ligature, brew_ligature_primitive, 1);
290 SCM
291 Vaticana_ligature::brew_ligature_primitive (SCM smob)
292 {
293   Grob *me = unsmob_grob (smob);
294   SCM primitive = vaticana_brew_primitive (me).smobbed_copy ();
295   return primitive;
296 }
297
298 MAKE_SCHEME_CALLBACK (Vaticana_ligature, print, 1);
299 SCM
300 Vaticana_ligature::print (SCM)
301 {
302   return SCM_EOL;
303 }
304
305 ADD_INTERFACE (Vaticana_ligature,
306                "A vaticana style Gregorian ligature.",
307
308                /* properties */
309                "glyph-name "
310                "flexa-height "
311                "flexa-width "
312                "thickness "
313                "add-cauda "
314                "add-stem "
315                "add-join "
316                "delta-position "
317                "x-offset "
318               );