]> git.donarmstrong.com Git - lilypond.git/blob - lily/mensural-ligature.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / mensural-ligature.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2002--2015 Juergen Reuter <reuter@ipd.uka.de>,
5   Pal Benko <benkop@freestart.hu>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "mensural-ligature.hh"
22
23 #include "directional-element-interface.hh"
24 #include "font-interface.hh"
25 #include "international.hh"
26 #include "item.hh"
27 #include "lookup.hh"
28 #include "note-head.hh"
29 #include "output-def.hh"
30 #include "staff-symbol-referencer.hh"
31 #include "warn.hh"
32
33 using std::string;
34
35 /*
36   draws one half a flexa, i.e. a portion corresponding to a single note.
37   this way coloration of the two notes building up the flexa can be
38   handled independently.
39
40  * TODO: divide this function into mensural and neo-mensural style.
41  *
42  * TODO: move this function to class Lookup?
43  */
44 Stencil
45 brew_flexa (Grob *me,
46             bool solid,
47             Real width,
48             Real thickness,
49             bool begin)
50 {
51   Real staff_space = Staff_symbol_referencer::staff_space (me);
52
53   /*
54     The thickness of the horizontal lines of the flexa shape
55     should be equal to that of the horizontal lines of the
56     neomensural brevis note head (see mf/parmesan-heads.mf).
57   */
58   Real const horizontal_line_thickness = staff_space * 0.35;
59
60   // URGH!  vertical_line_thickness is adjustable (via thickness
61   // property), while horizontal_line_thickness is constant.
62   // Maybe both should be adjustable independently?
63
64   Real height = staff_space - horizontal_line_thickness;
65   Stencil stencil;
66   Real const interval
67     = robust_scm2double (me->get_property ("flexa-interval"), 0.0);
68   Real slope = (interval / 2.0 * staff_space) / width;
69
70   // Compensate optical illusion regarding vertical position of left
71   // and right endings due to slope.
72   Real ypos_correction = -0.1 * staff_space * sign (slope);
73   Real slope_correction = 0.2 * staff_space * sign (slope);
74   Real corrected_slope = slope + slope_correction / width;
75   Real blotdiameter
76     = me->layout ()->get_dimension (ly_symbol2scm ("blot-diameter"));
77   width += 2 * blotdiameter;
78
79   if (solid) // colorated flexae
80     {
81       stencil = Lookup::beam (corrected_slope, width * 0.5, staff_space, blotdiameter);
82     }
83   else // outline
84     {
85       stencil = Lookup::beam (corrected_slope, thickness, height, blotdiameter);
86       if (!begin)
87         {
88           stencil.translate_axis (width * 0.5 - thickness, X_AXIS);
89           stencil.translate_axis (corrected_slope * (width * 0.5 - thickness),
90                                   Y_AXIS);
91         }
92
93       Stencil bottom_edge
94         = Lookup::beam (corrected_slope, width * 0.5, horizontal_line_thickness,
95                         blotdiameter);
96       bottom_edge.translate_axis (-0.5 * height, Y_AXIS);
97       stencil.add_stencil (bottom_edge);
98
99       Stencil top_edge
100         = Lookup::beam (corrected_slope, width * 0.5, horizontal_line_thickness,
101                         blotdiameter);
102       top_edge.translate_axis (+0.5 * height, Y_AXIS);
103       stencil.add_stencil (top_edge);
104     }
105
106   if (begin)
107     stencil.translate_axis (ypos_correction, Y_AXIS);
108   else
109     {
110       stencil.translate_axis (0.5 * thickness - blotdiameter, X_AXIS);
111
112       stencil.translate_axis (interval / -4.0 * staff_space, Y_AXIS);
113     }
114
115   return stencil;
116 }
117
118 Stencil
119 internal_brew_primitive (Grob *me)
120 {
121   SCM primitive_scm = me->get_property ("primitive");
122   if (scm_is_null (primitive_scm))
123     {
124       programming_error ("Mensural_ligature:"
125                          " undefined primitive -> ignoring grob");
126       return Lookup::blank (Box (Interval (0, 0), Interval (0, 0)));
127     }
128   int primitive = scm_to_int (primitive_scm);
129
130   Real thickness = 0.0;
131   Real width = 0.0;
132   Real flexa_width = 0.0;
133   Real staff_space = Staff_symbol_referencer::staff_space (me);
134
135   SCM style = me->get_property ("style");
136   bool const black
137     = scm_is_eq (style, ly_symbol2scm ("blackpetrucci"));
138   bool const semi
139     = scm_is_eq (style, ly_symbol2scm ("semipetrucci"));
140
141   if (primitive & MLP_ANY)
142     {
143       thickness = robust_scm2double (me->get_property ("thickness"), .13);
144       width = robust_scm2double (me->get_property ("head-width"), staff_space);
145     }
146   if (primitive & MLP_FLEXA)
147     flexa_width = robust_scm2double (me->get_property ("flexa-width"), 2.0)
148                   * staff_space;
149
150   Stencil out;
151   int const note_shape = primitive & MLP_ANY;
152   int duration_log = 0;
153   Font_metric *fm = Font_interface::get_default_font (me);
154   string prefix = "noteheads.";
155   string index;
156   string suffix;
157   string color = "";
158   if (black)
159     color = "black";
160   if (semi)
161     color = "semi";
162
163   switch (note_shape)
164     {
165     case MLP_NONE:
166       return Lookup::blank (Box (Interval (0, 0), Interval (0, 0)));
167     case MLP_MAXIMA:
168       duration_log--;
169     case MLP_LONGA:
170       duration_log--;
171     case MLP_BREVIS:
172       duration_log--;
173       suffix = ::to_string (duration_log) + color
174                + (duration_log < -1 ? "lig" : "") + "mensural";
175       index = prefix + "s";
176       out = fm->find_by_name (index + "r" + suffix);
177       if (!out.is_empty ()
178           && !Staff_symbol_referencer::on_line
179           (me,
180            robust_scm2int (me->get_property ("staff-position"), 0)))
181         index += "r";
182       out = fm->find_by_name (index + suffix);
183       break;
184     case MLP_FLEXA_BEGIN:
185     case MLP_FLEXA_END:
186       out = brew_flexa (me, black, flexa_width, thickness,
187                         note_shape == MLP_FLEXA_BEGIN);
188       break;
189     default:
190       programming_error ("Mensural_ligature:"
191                          " unexpected case fall-through");
192       return Lookup::blank (Box (Interval (0, 0), Interval (0, 0)));
193     }
194
195   /*
196     we use thickness because the stem end of the glyph
197     "noteheads.sM2ligmensural" is round.
198   */
199   Real blotdiameter = thickness;
200   /*
201     instead of 2.5 the length of a longa stem should be used
202     Font_interface::get_default_font (???)->find_by_name
203     ("noteheads.sM2ligmensural").extent (Y_AXIS).length () * 0.5
204   */
205   Real stem_length = 2.5 * staff_space;
206
207   if (primitive & MLP_STEM)
208     {
209       // assume MLP_UP
210       Real y_bottom = 0.0, y_top = stem_length;
211
212       if (primitive & MLP_DOWN)
213         {
214           y_bottom = -y_top;
215           y_top = 0.0;
216         }
217
218       Interval x_extent (0, thickness);
219       Interval y_extent (y_bottom, y_top);
220       Box join_box (x_extent, y_extent);
221
222       Stencil join = Lookup::round_filled_box (join_box, blotdiameter);
223       out.add_stencil (join);
224     }
225
226   if (to_boolean (me->get_property ("add-join")))
227     {
228       int join_right = scm_to_int (me->get_property ("delta-position"));
229       if (join_right)
230         {
231           Real y_top = join_right * 0.5 * staff_space;
232           Real y_bottom = 0.0;
233
234           if (y_top < 0.0)
235             {
236               y_bottom = y_top;
237               y_top = 0.0;
238
239               /*
240                 if the previous note is longa-shaped,
241                 the joining line may hide the stem, so made it longer
242                 to serve as stem as well
243               */
244               if (primitive & MLP_LONGA)
245                 y_bottom -= stem_length + 0.25 * blotdiameter;
246             }
247
248           Interval x_extent (width - thickness, width);
249           Interval y_extent (y_bottom, y_top);
250           Box join_box (x_extent, y_extent);
251           Stencil join = Lookup::round_filled_box (join_box, blotdiameter);
252
253           out.add_stencil (join);
254         }
255       else
256         programming_error ("Mensural_ligature: (join_right == 0)");
257     }
258
259 #if 0 /* what happend with the ledger lines? */
260   int pos = Staff_symbol_referencer::get_rounded_position (me);
261   if (primitive & MLP_FLEXA)
262     {
263       pos += delta_pitch;
264       add_ledger_lines (me, &out, pos, 0.5 * delta_pitch, ledger_take_space);
265     }
266 #endif
267
268   return out;
269 }
270
271 MAKE_SCHEME_CALLBACK (Mensural_ligature, brew_ligature_primitive, 1);
272 SCM
273 Mensural_ligature::brew_ligature_primitive (SCM smob)
274 {
275   Grob *me = unsmob<Grob> (smob);
276   return internal_brew_primitive (me).smobbed_copy ();
277 }
278
279 MAKE_SCHEME_CALLBACK (Mensural_ligature, print, 1);
280 SCM
281 Mensural_ligature::print (SCM)
282 {
283   return SCM_EOL;
284 }
285
286 ADD_INTERFACE (Mensural_ligature,
287                "A mensural ligature.",
288
289                /* properties */
290                "delta-position "
291                "ligature-flexa "
292                "head-width "
293                "add-join "
294                "flexa-interval "
295                "primitive "
296                "thickness "
297               );