X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=lily%2Fledger-line-spanner.cc;h=f4bf36ef48cf8e64f574d5ad9d19fccb190a1fe1;hb=5e8d9233d3276215e643ebc414c1701308e03dad;hp=186f976fd78e77f104d28011c0fc7f30a0a33584;hpb=58bcc84c9480dae1b21bc24d8396b91fe19e0131;p=lilypond.git diff --git a/lily/ledger-line-spanner.cc b/lily/ledger-line-spanner.cc index 186f976fd7..f4bf36ef48 100644 --- a/lily/ledger-line-spanner.cc +++ b/lily/ledger-line-spanner.cc @@ -1,170 +1,142 @@ /* - ledger-line-spanner.cc -- implement Ledger_line_spanner + This file is part of LilyPond, the GNU music typesetter. - source file of the GNU LilyPond music typesetter + Copyright (C) 2004--2014 Han-Wen Nienhuys - (c) 2004--2005 Han-Wen Nienhuys + LilyPond is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + LilyPond is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LilyPond. If not, see . */ #include -#include +using namespace std; -#include "item.hh" #include "note-head.hh" #include "staff-symbol-referencer.hh" #include "staff-symbol.hh" #include "lookup.hh" #include "spanner.hh" -#include "group-interface.hh" +#include "pointer-group-interface.hh" #include "paper-column.hh" struct Ledger_line_spanner { DECLARE_SCHEME_CALLBACK (print, (SCM)); DECLARE_SCHEME_CALLBACK (set_spacing_rods, (SCM)); - static Stencil brew_ledger_lines (Grob *me, - int pos, - int interspaces, - Real, Real, - Interval x_extent, - Real left_shorten); - - static bool has_interface (Grob *); + DECLARE_GROB_INTERFACE (); }; -Stencil -Ledger_line_spanner::brew_ledger_lines (Grob *staff, - int pos, - int interspaces, - Real halfspace, - Real ledgerlinethickness, - Interval x_extent, - Real left_shorten) +static void +set_rods (Drul_array const ¤t_extents, + Drul_array const &previous_extents, + Item *current_column, + Item *previous_column, + Real min_length) { - int line_count = ((abs (pos) < interspaces) - ? 0 - : (abs (pos) - interspaces) / 2); - Stencil stencil; - if (line_count) + for (UP_and_DOWN (d)) { - Real blotdiameter = ledgerlinethickness; - Interval y_extent - = Interval (-0.5* (ledgerlinethickness), - +0.5* (ledgerlinethickness)); - Stencil proto_ledger_line - = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter); - - x_extent[LEFT] += left_shorten; - Stencil proto_first_line - = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter); - - Direction dir = (Direction)sign (pos); - Real offs = (Staff_symbol_referencer::on_staffline (staff, pos)) - ? 0.0 - : -dir * halfspace; - - offs += pos * halfspace; - for (int i = 0; i < line_count; i++) - { - Stencil ledger_line ((i == 0) - ? proto_first_line - : proto_ledger_line); - ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS); - stencil.add_stencil (ledger_line); - } + if (!current_extents[d].is_empty () + && !previous_extents[d].is_empty ()) + { + Rod rod; + rod.distance_ = 2 * min_length + /* + we go from right to left. + */ + - previous_extents[d][LEFT] + + current_extents[d][RIGHT]; + + rod.item_drul_[LEFT] = current_column; + rod.item_drul_[RIGHT] = previous_column; + rod.add_to_cols (); + } } - - return stencil; } -typedef std::map < int, Drul_array > Head_extents_map; -typedef std::map < int, Grob *> Column_map; - MAKE_SCHEME_CALLBACK (Ledger_line_spanner, set_spacing_rods, 1); SCM Ledger_line_spanner::set_spacing_rods (SCM smob) { - Spanner *me = dynamic_cast (unsmob_grob (smob)); + Spanner *me = dynamic_cast (Grob::unsmob (smob)); // find size of note heads. Grob *staff = Staff_symbol_referencer::get_staff_symbol (me); if (!staff) - return SCM_EOL; - - Link_array heads (extract_grob_array (me, ly_symbol2scm ("note-heads"))); - - if (heads.is_empty ()) - return SCM_EOL; + { + me->suicide (); + return SCM_EOL; + } Real min_length_fraction = robust_scm2double (me->get_property ("minimum-length-fraction"), 0.15); - Head_extents_map head_extents; - Column_map columns; + Drul_array current_extents; + Drul_array previous_extents; + Real current_head_width = 0.0; + Item *previous_column = 0; + Item *current_column = 0; + + Real halfspace = Staff_symbol::staff_space (staff) / 2; + + Interval staff_extent = staff->extent (staff, Y_AXIS); + staff_extent *= 1 / halfspace; - int interspaces = Staff_symbol::line_count (staff) - 1; - for (int i = heads.size (); i--;) + /* + Run through heads using a loop. Since Ledger_line_spanner can + contain a lot of noteheads, superlinear performance is too slow. + */ + extract_item_set (me, "note-heads", heads); + for (vsize i = heads.size (); i--;) { - Item *h = dynamic_cast (heads[i]); + Item *h = heads[i]; int pos = Staff_symbol_referencer::get_rounded_position (h); - if (pos - && abs (pos) > interspaces) - { - Grob *column = h->get_column (); - int rank = Paper_column::get_rank (column); - - Interval head_extent = h->extent (column, X_AXIS); - Direction vdir = Direction (sign (pos)); - if (!vdir) - continue; - - Interval prev_extent; - - Head_extents_map::iterator j = head_extents.find (rank); - if (j != head_extents.end ()) - prev_extent = (*j).second[vdir]; - else - columns[rank] = column; - - prev_extent.unite (head_extent); - head_extents[rank][vdir] = prev_extent; - } + if (Staff_symbol::ledger_positions (staff, pos).empty ()) + continue; + + /* Ambitus heads can appear out-of-order in heads[], + * but as part of prefatory matter, they need no rods */ + if (h->internal_has_interface (ly_symbol2scm ("ambitus-interface"))) + continue; + + Item *column = h->get_column (); + if (current_column != column) + { + set_rods (current_extents, previous_extents, + current_column, previous_column, + current_head_width * min_length_fraction); + + previous_column = current_column; + current_column = column; + previous_extents = current_extents; + + current_extents[DOWN].set_empty (); + current_extents[UP].set_empty (); + current_head_width = 0.0; + } + + Interval head_extent = h->extent (column, X_AXIS); + Direction vdir = Direction (sign (pos)); + if (!vdir) + continue; + + current_extents[vdir].unite (head_extent); + current_head_width = max (current_head_width, head_extent.length ()); } - for (Column_map::const_iterator c (columns.begin ()); c != columns.end (); c++) - { - Grob *column = (*c).second; - int rank = (*c).first; - - int next_rank = rank + 2; - - if (head_extents.find (next_rank) != head_extents.end ()) - { - Drul_array extents_left = head_extents[rank]; - Drul_array extents_right = head_extents[next_rank]; - - Direction d = DOWN; - do - { - if (!extents_right[d].is_empty () && !extents_right[d].is_empty ()) - { - Real l1 = extents_right[d].length () * min_length_fraction; - Real l2 = extents_left[d].length () * min_length_fraction; - - Rod rod; - rod.distance_ = l1 + l2 + (l1+ l2) / 2.0 - + extents_left[d][RIGHT] - - extents_right[d][LEFT]; - - rod.item_drul_[LEFT] = dynamic_cast (column); - rod.item_drul_[RIGHT] = dynamic_cast (columns[next_rank]); - rod.add_to_cols (); - } - } - while (flip (&d) != DOWN); - } - } + if (previous_column && current_column) + set_rods (current_extents, previous_extents, + current_column, previous_column, + current_head_width * min_length_fraction); return SCM_UNSPECIFIED; } @@ -174,7 +146,6 @@ struct Ledger_request Interval ledger_extent_; Interval head_extent_; int position_; - bool excentric_; Ledger_request () { ledger_extent_.set_empty (); @@ -183,20 +154,21 @@ struct Ledger_request } }; -typedef std::map < int, Drul_array > Ledger_requests; +typedef map < int, Drul_array > Ledger_requests; /* - TODO: ledger share a lot of info. Lots of room to optimize away common - use of objects/variables. + TODO: ledger share a lot of info. Lots of room to optimize away + common use of objects/variables. */ MAKE_SCHEME_CALLBACK (Ledger_line_spanner, print, 1); SCM Ledger_line_spanner::print (SCM smob) { - Spanner *me = dynamic_cast (unsmob_grob (smob)); - Link_array heads (extract_grob_array (me, ly_symbol2scm ("note-heads"))); + Spanner *me = dynamic_cast (Grob::unsmob (smob)); + + extract_grob_set (me, "note-heads", heads); - if (heads.is_empty ()) + if (heads.empty ()) return SCM_EOL; // find size of note heads. @@ -204,11 +176,15 @@ Ledger_line_spanner::print (SCM smob) if (!staff) return SCM_EOL; + Real halfspace = Staff_symbol::staff_space (staff) / 2; + + Interval staff_extent = staff->extent (staff, Y_AXIS); + staff_extent *= 1 / halfspace; + Real length_fraction = robust_scm2double (me->get_property ("length-fraction"), 0.25); Stencil ledgers; - Stencil default_ledger; Grob *common[NO_AXES]; @@ -216,33 +192,31 @@ Ledger_line_spanner::print (SCM smob) { Axis a = Axis (i); common[a] = common_refpoint_of_array (heads, me, a); - for (int i = heads.size (); i--;) - if (Grob *g = unsmob_grob (me->get_property ("accidental-grob"))) - common[a] = common[a]->common_refpoint (g, a); + for (vsize i = heads.size (); i--;) + if (Grob *g = Grob::unsmob (me->get_object ("accidental-grob"))) + common[a] = common[a]->common_refpoint (g, a); } - int interspaces = Staff_symbol::line_count (staff) - 1; Ledger_requests reqs; - for (int i = heads.size (); i--;) + for (vsize i = heads.size (); i--;) { Item *h = dynamic_cast (heads[i]); int pos = Staff_symbol_referencer::get_rounded_position (h); - if (pos - && abs (pos) > interspaces) - { - Interval head_extent = h->extent (common[X_AXIS], X_AXIS); - Interval ledger_extent = head_extent; - ledger_extent.widen (length_fraction * head_extent.length ()); - - Direction vdir = Direction (sign (pos)); - int rank = Paper_column::get_rank (h->get_column ()); - - reqs[rank][vdir].ledger_extent_.unite (ledger_extent); - reqs[rank][vdir].head_extent_.unite (head_extent); - reqs[rank][vdir].position_ - = vdir * ((vdir* reqs[rank][vdir].position_) >? (vdir *pos)); - } + if (pos && !staff_extent.contains (pos)) + { + Interval head_extent = h->extent (common[X_AXIS], X_AXIS); + Interval ledger_extent = head_extent; + ledger_extent.widen (length_fraction * head_extent.length ()); + + Direction vdir = Direction (sign (pos)); + int rank = h->get_column ()->get_rank (); + + reqs[rank][vdir].ledger_extent_.unite (ledger_extent); + reqs[rank][vdir].head_extent_.unite (head_extent); + reqs[rank][vdir].position_ + = vdir * max (vdir * reqs[rank][vdir].position_, vdir * pos); + } } // determine maximum size for non-colliding ledger. @@ -252,101 +226,126 @@ Ledger_line_spanner::print (SCM smob) i != reqs.end (); last = i++) { if (last == reqs.end ()) - { - continue; - } - - Direction d = DOWN; - do - { - if (abs (last->second[d].position_) > interspaces - && abs (i->second[d].position_) > interspaces) - { - Real center - = (last->second[d].head_extent_[RIGHT] - + i->second[d].head_extent_[LEFT]) / 2; - - Direction which = LEFT; - do - { - Ledger_request &lr = ((which == LEFT) ? *last : *i).second[d]; - - // due tilt of quarter note-heads - bool both - = (abs (last->second[d].position_) > interspaces + 1 - && abs (i->second[d].position_) > interspaces + 1); - - Real limit = (center + (both? which * gap / 2 : 0)); - lr.ledger_extent_.elem_ref (-which) - = which * (which * lr.ledger_extent_[-which] >? which * limit); - } - while (flip (&which) != LEFT); - } - } - while (flip (&d) != DOWN); + continue; + + for (DOWN_and_UP (d)) + { + if (!staff_extent.contains (last->second[d].position_) + && !staff_extent.contains (i->second[d].position_)) + { + Real center + = (last->second[d].head_extent_[RIGHT] + + i->second[d].head_extent_[LEFT]) / 2; + + for (LEFT_and_RIGHT (which)) + { + Ledger_request &lr = ((which == LEFT) ? * last : *i).second[d]; + + // due tilt of quarter note-heads + /* FIXME */ + bool both + = (!staff_extent.contains (last->second[d].position_ + - sign (last->second[d].position_)) + && !staff_extent.contains (i->second[d].position_ + - sign (i->second[d].position_))); + Real limit = (center + (both ? which * gap / 2 : 0)); + lr.ledger_extent_.at (-which) + = which * max (which * lr.ledger_extent_[-which], which * limit); + } + } + } } - // create ledgers for note heads + // create ledgers for note heads Real ledgerlinethickness = Staff_symbol::get_ledger_line_thickness (staff); - Real halfspace = Staff_symbol::staff_space (staff) / 2; - for (int i = heads.size (); i--;) + for (vsize i = heads.size (); i--;) { Item *h = dynamic_cast (heads[i]); int pos = Staff_symbol_referencer::get_rounded_position (h); - if (abs (pos) > interspaces + 1) - { - Interval head_size = h->extent (common[X_AXIS], X_AXIS); - Interval ledger_size = head_size; - ledger_size.widen (ledger_size.length ()* length_fraction); - - Interval max_size = reqs[Paper_column::get_rank (h->get_column ())][Direction (sign (pos))].ledger_extent_; - - ledger_size.intersect (max_size); - Real left_shorten = 0.0; - if (Grob *g = unsmob_grob (h->get_property ("accidental-grob"))) - { - Interval accidental_size = g->extent (common[X_AXIS], X_AXIS); - Real d - = linear_combination (Drul_array (accidental_size[RIGHT], - head_size[LEFT]), - 0.0); - - left_shorten = (-ledger_size[LEFT] + d) >? 0; - - /* - TODO: shorten 2 ledger lines for the case natural + - downstem. - */ - - } - - ledgers.add_stencil (brew_ledger_lines (staff, pos, interspaces, - halfspace, - ledgerlinethickness, - ledger_size, - left_shorten)); - } + vector ledger_positions = Staff_symbol::ledger_positions (staff, pos); + if (!ledger_positions.empty ()) + { + int ledger_count = ledger_positions.size (); + Interval head_size = h->extent (common[X_AXIS], X_AXIS); + Interval ledger_size = head_size; + ledger_size.widen (ledger_size.length () * length_fraction); + + if (pos && !staff_extent.contains (pos)) + { + Interval max_size = reqs[h->get_column ()->get_rank ()] + [Direction (sign (pos))].ledger_extent_; + + if (!max_size.is_empty ()) + ledger_size.intersect (max_size); + } + + for (int i = 0; i < ledger_count; i++) + { + Real lpos = ledger_positions[i]; + Interval x_extent = ledger_size; + + if (i == 0) + if (Grob *g = Grob::unsmob (h->get_object ("accidental-grob"))) + { + Interval accidental_size = g->extent (common[X_AXIS], X_AXIS); + Real d + = linear_combination (Drul_array (accidental_size[RIGHT], + head_size[LEFT]), + 0.0); + + Real left_shorten = max (-ledger_size[LEFT] + d, 0.0); + + x_extent[LEFT] += left_shorten; + /* + TODO: shorten 2 ledger lines for the case natural + + downstem. + */ + } + + Real blotdiameter = ledgerlinethickness; + Interval y_extent + = Interval (-0.5 * (ledgerlinethickness), + +0.5 * (ledgerlinethickness)); + Stencil ledger_line + = Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter); + + ledger_line.translate_axis ( lpos * halfspace, Y_AXIS); + ledgers.add_stencil (ledger_line); + } + } } ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS), - X_AXIS); + X_AXIS); return ledgers.smobbed_copy (); } ADD_INTERFACE (Ledger_line_spanner, - "ledger-line-interface", - "This spanner draws the ledger lines of a staff, for note heads that stick out. ", - "note-heads thickness minimum-length-fraction length-fraction gap"); + "This spanner draws the ledger lines of a staff. This is a" + " separate grob because it has to process all potential" + " collisions between all note heads. The thickness of ledger" + " lines is controlled by the @code{ledger-line-thickness}" + " property of the @ref{StaffSymbol} grob.", + + /* properties */ + "gap " + "length-fraction " + "minimum-length-fraction " + "note-heads " + ); struct Ledgered_interface { - static bool has_interface (Grob *); + DECLARE_GROB_INTERFACE (); }; ADD_INTERFACE (Ledgered_interface, - "ledgered-interface", - "Objects that need ledger lines.", - "no-ledgers"); + "Objects that need ledger lines, typically note heads. See" + " also @ref{ledger-line-spanner-interface}.", + + /* properties */ + "no-ledgers " + );