#include "item.hh"
#include "paper-column.hh"
-#include <map>
-
MAKE_SCHEME_CALLBACK (Fingering_column, calc_positioning_done, 1);
SCM
Fingering_column::calc_positioning_done (SCM smob)
{
Grob *me = unsmob_grob (smob);
- Real padding = robust_scm2double (me->get_property ("padding"), 0.0);
if (!me->is_live ())
return SCM_BOOL_T;
- map<Grob *, bool> shifted;
-
- Real ss = Staff_symbol_referencer::staff_space (me);
-
me->set_property ("positioning-done", SCM_BOOL_T);
extract_grob_set (me, "fingerings", const_fingerings);
return SCM_BOOL_T;
}
- // order the fingerings from bottom to top
vector<Grob *> fingerings;
for (vsize i = 0; i < const_fingerings.size (); i++)
fingerings.push_back (const_fingerings[i]);
- vector_sort (fingerings, pure_position_less);
Grob *common[2] = {common_refpoint_of_array (fingerings, me, X_AXIS),
common_refpoint_of_array (fingerings, me, Y_AXIS)};
+ Real padding = robust_scm2double (me->get_property ("padding"), 0.2);
+
+ // order the fingerings from bottom to top
+ vector_sort (fingerings, pure_position_less);
+
+ vector<Real> shift(fingerings.size());
+
+ // Try stacking the fingerings top-to-bottom, and then bottom-to-top.
+ // Use the average of the resulting stacked locations as the final positions
+ for (UP_and_DOWN (d))
+ {
+ Real stack_end = -d * infinity_f;
+ Interval prev_x_ext;
+ for (vsize i = (d == UP)? 0 : fingerings.size() - 1;
+ i < fingerings.size ();
+ i += d)
+ {
+ Interval x_ext = fingerings[i]->extent(common[X_AXIS], X_AXIS);
+ Interval y_ext = fingerings[i]->extent(fingerings[i], Y_AXIS);
+ Real parent_y = fingerings[i]->get_parent(Y_AXIS)
+ ->relative_coordinate(common[Y_AXIS], Y_AXIS);
+
+ // Checking only between sequential neighbors, seems good enough
+ if (!intersection(x_ext, prev_x_ext).is_empty())
+ stack_end += d * (y_ext.length() + padding);
+ // minmax() returns whichever is further along in direction d
+ stack_end = minmax(d, stack_end, parent_y);
+
+ shift[i] += 0.5 * (stack_end - y_ext[d] - parent_y);
+
+ prev_x_ext = x_ext;
+ }
+ }
+
for (vsize i = 0; i < fingerings.size (); i++)
- fingerings[i]->translate_axis (-fingerings[i]->extent (common[Y_AXIS], Y_AXIS).length () / 2, Y_AXIS);
-
- for (vsize i = min (fingerings.size () - 1, fingerings.size () / 2 + 1); i >= 1; i--)
- for (vsize j = i; j--;)
- {
- Interval ex_i = fingerings[i]->extent (common[X_AXIS], X_AXIS);
- Interval ex_j = fingerings[j]->extent (common[X_AXIS], X_AXIS);
- Interval ey_i = fingerings[i]->extent (common[Y_AXIS], Y_AXIS);
- Interval ey_j = fingerings[j]->extent (common[Y_AXIS], Y_AXIS);
- Real tval = min (0.0, (ey_i[DOWN] - ey_j[UP] - padding) / 2);
- if (tval != 0.0 && !intersection (ex_i, ex_j).is_empty ())
- {
- if (shifted[fingerings[i]] || shifted[fingerings[j]])
- fingerings[j]->translate_axis (tval * 2, Y_AXIS);
- else
- {
- fingerings[i]->translate_axis (-tval, Y_AXIS);
- fingerings[j]->translate_axis (tval, Y_AXIS);
- }
- shifted[fingerings[i]] = true;
- shifted[fingerings[j]] = true;
- }
- }
-
- for (vsize i = fingerings.size () / 2 - 1; i < fingerings.size () - 1; i++)
- for (vsize j = i + 1; j < fingerings.size (); j++)
- {
- Interval ex_i = fingerings[i]->extent (common[X_AXIS], X_AXIS);
- Interval ex_j = fingerings[j]->extent (common[X_AXIS], X_AXIS);
- Interval ey_i = fingerings[i]->extent (common[Y_AXIS], Y_AXIS);
- Interval ey_j = fingerings[j]->extent (common[Y_AXIS], Y_AXIS);
- Real tval = max (0.0, (ey_i[UP] - ey_j[DOWN] + padding) / 2);
- if (tval != 0.0 && !intersection (ex_i, ex_j).is_empty ())
- {
- if (shifted[fingerings[i]] || shifted[fingerings[j]])
- fingerings[j]->translate_axis (tval * 2, Y_AXIS);
- else
- {
- fingerings[i]->translate_axis (-tval, Y_AXIS);
- fingerings[j]->translate_axis (tval, Y_AXIS);
- }
- shifted[fingerings[i]] = true;
- shifted[fingerings[j]] = true;
- }
- }
-
-
- return SCM_BOOL_T;
+ fingerings[i]->translate_axis(shift[i], Y_AXIS);
+
+ return SCM_BOOL_T;
}
void