2 porrectus.cc -- implement Porrectus
4 Copyright (C) 2001 Juergen Reuter
6 written for the GNU LilyPond music typesetter
8 TODO: --> see porrectus-engraver.cc
11 #include "staff-symbol-referencer.hh"
12 #include "porrectus.hh"
14 #include "molecule.hh"
18 #include "dimensions.hh"
19 #include "direction.hh"
21 #include "font-interface.hh"
22 #include "paper-def.hh"
23 #include "math.h" // rint
26 Porrectus::set_left_head (Grob *me, Item *left_head)
30 me->set_grob_property ("left-head", left_head->self_scm());
34 programming_error (_ ("(left_head == 0)"));
35 me->set_grob_property ("left-head", SCM_EOL);
40 Porrectus::get_left_head (Grob *me)
42 SCM left_head_scm = me->get_grob_property ("left-head");
43 if (left_head_scm == SCM_EOL)
45 programming_error (_ ("undefined left_head"));
50 Item *left_head = dynamic_cast<Item*> (unsmob_grob (left_head_scm));
56 Porrectus::set_right_head (Grob *me, Item *right_head)
60 me->set_grob_property ("right-head", right_head->self_scm());
64 programming_error (_ ("(right_head == 0)"));
65 me->set_grob_property ("right-head", SCM_EOL);
70 Porrectus::get_right_head (Grob *me)
72 SCM right_head_scm = me->get_grob_property ("right-head");
73 if (right_head_scm == SCM_EOL)
75 programming_error (_ ("undefined right_head"));
80 Item *right_head = dynamic_cast<Item*> (unsmob_grob (right_head_scm));
85 // Uugh. The following two functions are almost duplicated code from
86 // custos.cc, which itself is similar to code in note-head.cc. Maybe
87 // this should be moved to staff-symbol-referencer.cc?
89 Porrectus::create_ledger_line (Interval x_extent, Grob *me)
92 Molecule slice = Font_interface::get_default_font (me)->find_by_name ("noteheads-ledgerending");
93 Interval slice_x_extent = slice.extent (X_AXIS);
94 Interval slice_y_extent = slice.extent (Y_AXIS);
96 // Create left ending of ledger line.
97 Molecule left_ending = slice;
98 left_ending.translate_axis (x_extent[LEFT] - slice_x_extent[LEFT], X_AXIS);
99 if (x_extent.length () > slice_x_extent.length ())
100 line.add_molecule (left_ending);
102 // Create right ending of ledger line.
103 Molecule right_ending = slice;
104 right_ending.translate_axis (x_extent[RIGHT] - slice_x_extent[RIGHT],
106 line.add_molecule (right_ending);
108 // Fill out space between left and right ending of ledger line by
109 // lining up a series of slices in a row between them.
110 Molecule fill_out_slice = left_ending;
111 Real thick = slice_y_extent.length ();
112 Real delta_x = slice_x_extent.length () - thick;
113 Real xpos = x_extent [LEFT] + 2*delta_x + thick/2; // TODO: check: thick*2?
114 while (xpos <= x_extent[RIGHT])
116 fill_out_slice.translate_axis (delta_x, X_AXIS);
117 line.add_molecule (fill_out_slice);
125 Porrectus::create_streepjes (Grob *me,
130 Real inter_f = Staff_symbol_referencer::staff_space (me)/2;
131 int streepjes_i = abs (pos) < interspaces
133 : (abs (pos) - interspaces) /2;
134 Molecule molecule = Molecule();
137 Direction dir = (Direction)sign (pos);
138 Molecule ledger_line (create_ledger_line (extent, me));
139 ledger_line.set_empty (true);
140 Real offs = (Staff_symbol_referencer::on_staffline (me, pos))
143 for (int i = 0; i < streepjes_i; i++)
145 Molecule streep (ledger_line);
146 streep.translate_axis (-dir * inter_f * i * 2 + offs,
148 molecule.add_molecule (streep);
154 MAKE_SCHEME_CALLBACK (Porrectus,brew_molecule,1);
156 Porrectus::brew_molecule (SCM smob)
158 Item *me = (Item *)unsmob_grob (smob);
160 Item *left_head = get_left_head (me);
161 Item *right_head = get_right_head (me);
162 if (!left_head || !right_head)
164 warning (_ ("junking lonely porrectus"));
169 SCM scm_style = me->get_grob_property ("style");
171 if ((gh_symbol_p (scm_style)) && (scm_style != SCM_EOL))
172 style = ly_scm2string (scm_symbol_to_string (scm_style));
174 warning (_ ("porrectus style undefined; using mensural"));
178 bool solid = to_boolean (me->get_grob_property ("solid"));
179 bool add_stem = to_boolean (me->get_grob_property ("add-stem"));
181 SCM stem_direction_scm = me->get_grob_property ("stem-direction");
182 Direction stem_direction =
183 gh_number_p (stem_direction_scm) ? to_dir (stem_direction_scm) : DOWN;
185 stem_direction = DOWN;
187 bool auto_properties = to_boolean (me->get_grob_property ("auto-properties"));
189 // determine add_stem and stem_direction automatically from durations
191 if (String::compare_i (style, "mensural") != 0)
192 warning (String("auto-property should be used for\r\n") +
193 String("mensural style porrectus only; trying anyway"));
196 gh_scm2int (left_head->get_grob_property ("duration-log"));
198 gh_scm2int (right_head->get_grob_property ("duration-log"));
200 if ((left_duration == -1) && (right_duration == -1))
203 // cum proprietate et sine perfectione (c.s.)
205 stem_direction = DOWN;
207 else if ((left_duration == -2) && (right_duration == -1))
210 // sine proprietate et sine perfectione (s.s.)
213 else if ((left_duration == 0) && (right_duration == 0))
215 // semibrevis -- semibrevis:
216 // cum opposita proprietate (c.o.p.)
222 warning (String("auto-property: failed determining porrectus\r\n") +
223 String("properties due to improper durations; ") +
224 String("using user-supplied properties"));
228 Real left_position_f = Staff_symbol_referencer::position_f (left_head);
229 Real right_position_f = Staff_symbol_referencer::position_f (right_head);
230 Real interval = right_position_f - left_position_f;
234 SCM line_thickness_scm = me->get_grob_property ("line-thickness");
236 if (gh_number_p (line_thickness_scm))
238 line_thickness = gh_scm2double (line_thickness_scm);
242 line_thickness = 1.0;
245 line_thickness * me->paper_l ()->get_var ("stafflinethickness");
247 SCM porrectus_width_scm = me->get_grob_property ("porrectus-width");
248 Real porrectus_width;
249 if (gh_number_p (porrectus_width_scm))
251 porrectus_width = gh_scm2double (porrectus_width_scm);
255 porrectus_width = 2.4;
257 Real width = porrectus_width * Staff_symbol_referencer::staff_space (me);
259 if (String::compare_i (style, "vaticana") == 0)
260 molecule = brew_vaticana_molecule (me, interval,
261 solid, width, thickness,
262 add_stem, stem_direction);
263 else if (String::compare_i (style, "mensural") == 0)
264 molecule = brew_mensural_molecule (me, interval,
265 solid, width, thickness,
266 add_stem, stem_direction);
270 Real space = Staff_symbol_referencer::staff_space (me);
271 Real head_extent = molecule.extent (X_AXIS).length ();
272 Interval extent (-0.2 * head_extent, 1.2 * head_extent);
273 int interspaces = Staff_symbol_referencer::line_count (me)-1;
275 molecule.translate_axis (left_position_f * space/2, Y_AXIS);
277 Molecule left_head_streepjes =
278 create_streepjes (me, (int)rint (left_position_f), interspaces, extent);
279 left_head_streepjes.translate_axis (left_position_f * space/2, Y_AXIS);
280 molecule.add_molecule (left_head_streepjes);
282 Molecule right_head_streepjes =
283 create_streepjes (me, (int)rint (right_position_f), interspaces, extent);
284 right_head_streepjes.translate_axis (right_position_f * space/2, Y_AXIS);
285 molecule.add_molecule (right_head_streepjes);
287 return molecule.smobbed_copy();
291 Porrectus::brew_vaticana_molecule (Item *me,
297 Direction stem_direction)
301 warning (_ ("ascending vaticana style porrectus"));
304 Real space = Staff_symbol_referencer::staff_space (me);
305 Molecule molecule = Molecule ();
306 Real right_height = 0.6 * space;
308 // Compensate thickness that appears to be smaller in steep section
310 Real left_height = right_height + min (0.12 * abs(interval), 0.3) * space;
314 bool consider_interval =
315 stem_direction * interval > 0.0;
317 Interval stem_box_x (0, thickness);
320 if (consider_interval)
322 Real y_length = max (abs(interval)/2.0*space +
323 (right_height-left_height),
325 stem_box_y = Interval (0, y_length);
328 stem_box_y = Interval (0, space);
331 (stem_direction == UP) ?
333 -0.5*left_height - stem_box_y.length();
335 Box stem_box (stem_box_x, stem_box_y);
336 Molecule stem = Lookup::filledbox (stem_box);
337 stem.translate_axis (y_correction, Y_AXIS);
338 molecule.add_molecule(stem);
341 // Compensate optical illusion regarding vertical position of left
342 // and right endings due to curved shape.
343 Real ypos_correction = -0.1*space * sign(interval);
344 Real interval_correction = 0.2*space * sign(interval);
345 Real corrected_interval = interval*space + interval_correction;
347 // middle curve of vaticana style porrectus
349 curve.control_[0] = Offset (0.00 * width, 0.0);
350 curve.control_[1] = Offset (0.33 * width, corrected_interval / 2.0);
351 curve.control_[2] = Offset (0.66 * width, corrected_interval / 2.0);
352 curve.control_[3] = Offset (1.00 * width, corrected_interval / 2.0);
354 Bezier top_curve = curve, bottom_curve = curve;
355 for (int i = 0; i < 4; i++)
357 Real thickness = 0.33 * ((3 - i)*left_height + i*right_height);
358 top_curve.control_[i] += Offset (0, +0.5*thickness);
359 bottom_curve.control_[i] += Offset (0, -0.5*thickness);
364 Molecule solid_head =
365 brew_bezier_sandwich (top_curve, bottom_curve);
366 molecule.add_molecule (solid_head);
370 Bezier inner_top_curve = top_curve;
371 inner_top_curve.translate (Offset (0.0, -thickness));
373 brew_bezier_sandwich (top_curve, inner_top_curve);
374 molecule.add_molecule(top_edge);
376 Bezier inner_bottom_curve = bottom_curve;
377 inner_bottom_curve.translate (Offset (0.0, +thickness));
378 Molecule bottom_edge =
379 brew_bezier_sandwich (bottom_curve, inner_bottom_curve);
380 molecule.add_molecule(bottom_edge);
382 // TODO: Use horizontal slope with proper slope value rather
383 // than filled box for left edge, since the filled box stands
384 // out from the porrectus shape if the interval is big and the
385 // line thickness small. The difficulty here is to compute a
386 // proper slope value, as it should roughly be equal with the
387 // slope of the left end of the bezier curve.
388 Box left_edge_box (Interval (0, thickness),
389 Interval (-0.5*left_height, +0.5*left_height));
390 Molecule left_edge = Lookup::filledbox (left_edge_box);
391 molecule.add_molecule(left_edge);
393 Box right_edge_box (Interval (-thickness, 0),
394 Interval (-0.5*right_height, +0.5*right_height));
395 Molecule right_edge = Lookup::filledbox (right_edge_box);
396 right_edge.translate_axis (width, X_AXIS);
397 right_edge.translate_axis (corrected_interval / 2.0, Y_AXIS);
398 molecule.add_molecule(right_edge);
400 molecule.translate_axis (ypos_correction, Y_AXIS);
405 Porrectus::brew_mensural_molecule (Item *me,
411 Direction stem_direction)
413 Real space = Staff_symbol_referencer::staff_space (me);
414 Real height = 0.6 * space;
415 Molecule molecule = Molecule ();
419 bool consider_interval =
420 stem_direction * interval > 0.0;
422 Interval stem_box_x (0, thickness);
425 if (consider_interval)
427 Real y_length = max (interval/2.0*space, 1.2*space);
428 stem_box_y = Interval (0, y_length);
431 stem_box_y = Interval (0, space);
434 (stem_direction == UP) ?
436 -0.5*height - stem_box_y.length();
438 Box stem_box (stem_box_x, stem_box_y);
439 Molecule stem = Lookup::filledbox (stem_box);
440 stem.translate_axis (y_correction, Y_AXIS);
441 molecule.add_molecule(stem);
444 Real slope = (interval / 2.0 * space) / width;
446 // Compensate optical illusion regarding vertical position of left
447 // and right endings due to slope.
448 Real ypos_correction = -0.1*space * sign(slope);
449 Real slope_correction = 0.2*space * sign(slope);
450 Real corrected_slope = slope + slope_correction/width;
454 Molecule solid_head =
455 brew_horizontal_slope (width, corrected_slope, height);
456 molecule.add_molecule (solid_head);
461 brew_horizontal_slope (thickness, corrected_slope, height);
462 molecule.add_molecule(left_edge);
464 Molecule right_edge =
465 brew_horizontal_slope (thickness, corrected_slope, height);
466 right_edge.translate_axis (width-thickness, X_AXIS);
467 right_edge.translate_axis (corrected_slope * (width-thickness), Y_AXIS);
468 molecule.add_molecule(right_edge);
470 Molecule bottom_edge =
471 brew_horizontal_slope (width, corrected_slope, thickness);
472 bottom_edge.translate_axis (-0.5*height, Y_AXIS);
473 molecule.add_molecule (bottom_edge);
476 brew_horizontal_slope (width, corrected_slope, thickness);
477 top_edge.translate_axis (+0.5*height, Y_AXIS);
478 molecule.add_molecule (top_edge);
480 molecule.translate_axis (ypos_correction, Y_AXIS);
507 // TODO: Move this to class Lookup?
509 Porrectus::brew_bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
512 Need the weird order b.o. the way PS want its arguments
515 list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
516 list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
517 list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
518 list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
519 list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
520 list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
521 list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
522 list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
524 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
529 Interval x_extent = top_curve.extent (X_AXIS);
530 x_extent.unite (bottom_curve.extent (X_AXIS));
531 Interval y_extent = top_curve.extent (Y_AXIS);
532 y_extent.unite (bottom_curve.extent (Y_AXIS));
533 Box b (x_extent, y_extent);
535 return Molecule (b, horizontal_bend);
548 * (0,0) x /slope=dy/dx
555 // TODO: Move this to class Lookup?
557 Porrectus::brew_horizontal_slope (Real width, Real slope, Real height)
559 SCM width_scm = gh_double2scm (width);
560 SCM slope_scm = gh_double2scm (slope);
561 SCM height_scm = gh_double2scm (height);
562 SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
563 width_scm, slope_scm,
564 height_scm, SCM_UNDEFINED);
565 Box b (Interval (0, width),
566 Interval (-height/2, height/2 + width*slope));
567 return Molecule (b, horizontal_slope);