2 page-layout-problem.cc -- space systems nicely on a page. If systems can
3 be stretched, do that too.
5 source file of the GNU LilyPond music typesetter
7 (c) 2009 Joe Neeman <joeneeman@gmail.com>
10 #include "page-layout-problem.hh"
12 #include "align-interface.hh"
13 #include "hara-kiri-group-spanner.hh"
14 #include "international.hh"
16 #include "output-def.hh"
17 #include "paper-book.hh"
18 #include "pointer-group-interface.hh"
20 #include "skyline-pair.hh"
23 Page_layout_problem::Page_layout_problem (Paper_book *pb, SCM page_scm, SCM systems)
24 : bottom_skyline_ (DOWN)
26 Prob *page = unsmob_prob (page_scm);
33 Stencil *head = unsmob_stencil (page->get_property ("head-stencil"));
34 Stencil *foot = unsmob_stencil (page->get_property ("foot-stencil"));
36 header_height_ = head ? head->extent (Y_AXIS).length () : 0;
37 footer_height_ = foot ? foot->extent (Y_AXIS).length () : 0;
38 page_height_ = robust_scm2double (page->get_property ("paper-height"), 100);
41 // Initially, bottom_skyline_ represents the top of the page. Make
42 // it solid, so that the top of the first system will be forced
43 // below the top of the printable area.
44 bottom_skyline_.set_minimum_height (-header_height_);
46 SCM between_system_spacing = SCM_EOL;
47 SCM after_title_spacing = SCM_EOL;
48 SCM before_title_spacing = SCM_EOL;
49 SCM between_title_spacing = SCM_EOL;
51 // first_system_spacing controls the spring from the top of the printable
52 // area to the first staff. It allows the user to control the offset of
53 // the first staff (as opposed to the top of the first system) from the
54 // top of the page. Similarly for last_system_spacing.
55 SCM first_system_spacing = SCM_EOL;
56 SCM last_system_spacing = SCM_EOL;
59 Output_def *paper = pb->paper_;
60 between_system_spacing = paper->c_variable ("between-system-spacing");
61 after_title_spacing = paper->c_variable ("after-title-spacing");
62 before_title_spacing = paper->c_variable ("before-title-spacing");
63 between_title_spacing = paper->c_variable ("between-title-spacing");
64 last_system_spacing = paper->c_variable ("last-system-spacing");
65 first_system_spacing = paper->c_variable ("first-system-spacing");
66 if (scm_is_pair (systems) && unsmob_prob (scm_car (systems)))
67 first_system_spacing = paper->c_variable ("first-system-title-spacing");
69 // Note: the page height here does _not_ reserve space for headers and
70 // footers. This is because we want to anchor the first-system-spacing
71 // spring at the _top_ of the header.
72 page_height_ -= robust_scm2double (paper->c_variable ("top-margin"), 0)
73 + robust_scm2double (paper->c_variable ("bottom-margin"), 0);
75 bool last_system_was_title = false;
78 for (SCM s = systems; scm_is_pair (s); s = scm_cdr (s))
80 bool first = (s == systems);
82 if (Grob *g = unsmob_grob (scm_car (s)))
84 System *sys = dynamic_cast<System*> (g);
87 programming_error ("got a grob for vertical spacing that wasn't a System");
91 SCM spec = first ? first_system_spacing
92 : (last_system_was_title ? after_title_spacing : between_system_spacing);
93 Spring spring (first ? 0 : 1, 0.0);
95 alter_spring_from_spacing_spec (spec, &spring);
96 read_spacing_spec (spec, &padding, ly_symbol2scm ("padding"));
98 append_system (sys, spring, padding);
99 last_system_was_title = false;
101 else if (Prob *p = unsmob_prob (scm_car (s)))
103 SCM spec = first ? first_system_spacing
104 : (last_system_was_title ? between_title_spacing : before_title_spacing);
105 Spring spring (first ? 0 : 1, 0.0);
107 alter_spring_from_spacing_spec (spec, &spring);
108 read_spacing_spec (spec, &padding, ly_symbol2scm ("padding"));
110 append_prob (p, spring, padding);
111 last_system_was_title = true;
114 programming_error ("got a system that was neither a Grob nor a Prob");
117 Spring last_spring (0, 0);
118 Real last_padding = 0;
119 alter_spring_from_spacing_spec (last_system_spacing, &last_spring);
120 read_spacing_spec (last_system_spacing, &last_padding, ly_symbol2scm ("padding"));
121 last_spring.ensure_min_distance (last_padding - bottom_skyline_.max_height () + footer_height_);
122 springs_.push_back (last_spring);
126 Page_layout_problem::set_header_height (Real height)
128 header_height_ = height;
132 Page_layout_problem::set_footer_height (Real height)
134 footer_height_ = height;
138 Page_layout_problem::find_vertical_alignment (System *sys)
140 extract_grob_set (sys, "elements", elts);
141 for (vsize i = 0; i < elts.size (); ++i)
142 if (Align_interface::has_interface (elts[i]))
149 Page_layout_problem::append_system (System *sys, Spring const& spring, Real padding)
151 Grob *align = find_vertical_alignment (sys);
154 sys->programming_error ("no VerticalAlignment in system: can't do vertical spacing");
158 align->set_property ("positioning-done", SCM_BOOL_T);
160 extract_grob_set (align, "elements", all_elts);
161 vector<Grob*> elts = filter_dead_elements (all_elts);
162 vector<Real> minimum_offsets = Align_interface::get_minimum_translations (align, elts, Y_AXIS,
165 Skyline up_skyline (UP);
166 Skyline down_skyline (DOWN);
167 build_system_skyline (elts, minimum_offsets, &up_skyline, &down_skyline);
169 Real minimum_distance = up_skyline.distance (bottom_skyline_) + padding;
171 Spring spring_copy = spring;
172 spring_copy.ensure_min_distance (minimum_distance);
173 springs_.push_back (spring_copy);
175 bottom_skyline_ = down_skyline;
176 elements_.push_back (Element (elts, minimum_offsets));
178 // Add the springs for the VerticalAxisGroups in this system.
180 // If the user has specified the offsets of the individual staves, fix the
181 // springs at the given distances. Otherwise, use stretchable springs.
182 SCM details = get_details (elements_.back ());
183 SCM manual_dists = ly_assoc_get (ly_symbol2scm ("alignment-distances"), details, SCM_EOL);
184 vsize last_spaceable_staff = 0;
185 bool found_spaceable_staff = false;
186 for (vsize i = 0; i < elts.size (); ++i)
188 if (is_spaceable (elts[i]))
190 // We don't add a spring for the first staff, since
191 // we are only adding springs _between_ staves here.
192 if (!found_spaceable_staff)
195 add_loose_lines_as_spaceable_lines (elts, minimum_offsets, 0, i-1);
197 found_spaceable_staff = true;
198 last_spaceable_staff = i;
202 Spring spring (0.5, 0.0);
203 SCM spec = elts[last_spaceable_staff]->get_property ("next-staff-spacing");
204 alter_spring_from_spacing_spec (spec, &spring);
206 springs_.push_back (spring);
207 Real min_distance = minimum_offsets[last_spaceable_staff] - minimum_offsets[i];
208 springs_.back ().ensure_min_distance (min_distance);
210 if (scm_is_pair (manual_dists))
212 if (scm_is_number (scm_car (manual_dists)))
214 Real dy = scm_to_double (scm_car (manual_dists));
216 springs_.back ().set_distance (dy);
217 springs_.back ().set_min_distance (dy);
218 springs_.back ().set_inverse_stretch_strength (0);
220 manual_dists = scm_cdr (manual_dists);
222 last_spaceable_staff = i;
225 // Any loose lines hanging off the end are treated as spaceable
226 // lines. This might give slightly weird results if the hanging
227 // systems have staff-affinity != UP. It's not quite clear what
228 // should happen in that case, though.
229 if (last_spaceable_staff + 1 < elts.size ())
230 add_loose_lines_as_spaceable_lines (elts, minimum_offsets,
231 found_spaceable_staff ? last_spaceable_staff + 1 : 0,
233 // Corner case: there was only one staff, and it wasn't spaceable.
234 // Mark it spaceable, because we do not allow non-spaceable staves
235 // to be at the top or bottom of a system.
236 else if (!found_spaceable_staff && elts.size ())
237 mark_as_spaceable (elts[0]);
240 // first and last are inclusive
242 Page_layout_problem::add_loose_lines_as_spaceable_lines (vector<Grob*> const& elts,
243 vector<Real> const& minimum_offsets,
244 vsize first, vsize last)
250 if (end + 1 < elts.size ())
253 for (vsize i = start; i < end; ++i)
255 SCM spec = get_spacing_spec (elts[i], elts[i+1]);
256 Spring spring (1.0, 0.0);
257 alter_spring_from_spacing_spec (spec, &spring);
258 if (spec == SCM_BOOL_F)
260 spring.set_inverse_compress_strength (10000);
261 spring.set_inverse_stretch_strength (10000);
264 Real min_distance = minimum_offsets[i] - minimum_offsets[i+1];
265 spring.ensure_min_distance (min_distance);
266 springs_.push_back (spring);
269 for (vsize i = first; i <= last; ++i)
270 mark_as_spaceable (elts[i]);
274 Page_layout_problem::append_prob (Prob *prob, Spring const& spring, Real padding)
276 Skyline_pair *sky = Skyline_pair::unsmob (prob->get_property ("vertical-skylines"));
277 Real minimum_distance = 0;
280 minimum_distance = (*sky)[UP].distance (bottom_skyline_);
281 bottom_skyline_ = (*sky)[DOWN];
283 else if (Stencil *sten = unsmob_stencil (prob->get_property ("stencil")))
285 Interval iv = sten->extent (Y_AXIS);
286 minimum_distance = iv[UP] - bottom_skyline_.max_height ();
288 bottom_skyline_.clear ();
289 bottom_skyline_.set_minimum_height (iv[DOWN]);
291 minimum_distance += padding;
293 Spring spring_copy = spring;
294 spring_copy.ensure_min_distance (minimum_distance);
295 springs_.push_back (spring_copy);
296 elements_.push_back (Element (prob));
300 Page_layout_problem::solve_rod_spring_problem (bool ragged)
302 Simple_spacer spacer;
304 for (vsize i = 0; i < springs_.size (); ++i)
305 spacer.add_spring (springs_[i]);
307 Real bottom_padding = 0;
308 Interval first_staff_iv (0, 0);
309 Interval last_staff_iv (0, 0);
310 if (elements_.size ())
312 first_staff_iv = first_staff_extent (elements_[0]);
313 last_staff_iv = last_staff_extent (elements_.back ());
315 // TODO: junk bottom-space now that we have last-spring-spacing?
316 // bottom-space has the flexibility that one can do it per-system.
317 // NOTE: bottom-space is misnamed since it is not stretchable space.
318 if (Prob *p = elements_.back ().prob)
319 bottom_padding = robust_scm2double (p->get_property ("bottom-space"), 0);
320 else if (elements_.back ().staves.size ())
322 SCM details = get_details (elements_.back ());
323 bottom_padding = robust_scm2double (ly_assoc_get (ly_symbol2scm ("bottom-space"),
330 spacer.solve (page_height_ - bottom_padding, ragged);
331 solution_ = spacer.spring_positions ();
334 // The solution_ vector stores the position of every live VerticalAxisGroup
335 // and every title. From that information,
336 // 1) within each system, stretch the staves so they land at the right position
337 // 2) find the offset of each system (relative to the printable area of the page).
338 // TODO: this function is getting too long, maybe split it up?
340 Page_layout_problem::find_system_offsets ()
342 SCM system_offsets = SCM_EOL;
343 SCM *tail = &system_offsets;
345 // spring_idx 0 is the top of the page. Interesting values start from 1.
346 vsize spring_idx = 1;
347 for (vsize i = 0; i < elements_.size (); ++i)
349 if (elements_[i].prob)
351 *tail = scm_cons (scm_from_double (solution_[spring_idx]), SCM_EOL);
352 tail = SCM_CDRLOC (*tail);
357 // Getting this signs right here is a little tricky. The configuration
358 // we return has zero at the top of the page and positive numbers further
359 // down, as does the solution_ vector. Within a staff, however, positive
361 // TODO: perhaps change the way the page 'configuration variable works so
362 // that it is consistent with the usual up/down sign conventions in
363 // Lilypond. Then this would be less confusing.
365 // These two positions are relative to the page (with positive numbers being
367 Real first_staff_position = solution_[spring_idx];
368 Real first_staff_min_translation = elements_[i].min_offsets.size () ? elements_[i].min_offsets[0] : 0;
369 Real system_position = first_staff_position + first_staff_min_translation;
371 // Position the staves within this system.
372 Real translation = 0;
373 vector<Grob*> loose_lines;
374 vector<Real> const& min_offsets = elements_[i].min_offsets;
375 vector<Real> loose_line_min_distances;
376 Grob *last_spaceable_line = 0;
377 Real last_spaceable_line_translation = 0;
378 for (vsize staff_idx = 0; staff_idx < elements_[i].staves.size (); ++staff_idx)
380 Grob *staff = elements_[i].staves[staff_idx];
382 if (is_spaceable (staff))
384 // this is relative to the system: negative numbers are down.
385 translation = system_position - solution_[spring_idx];
388 // Lay out any non-spaceable lines between this line and
390 if (loose_lines.size ())
392 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
393 loose_lines.push_back (staff);
394 distribute_loose_lines (loose_lines, loose_line_min_distances,
395 last_spaceable_line_translation, translation);
396 loose_lines.clear ();
397 loose_line_min_distances.clear ();
399 last_spaceable_line = staff;
400 last_spaceable_line_translation = translation;
402 staff->translate_axis (translation, Y_AXIS);
406 if (loose_lines.empty ())
407 loose_lines.push_back (last_spaceable_line);
409 loose_lines.push_back (staff);
410 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
414 // Corner case: even if a system has no live staves, it still takes up
415 // one spring (a system with one live staff also takes up one spring),
416 // which we need to increment past.
417 if (elements_[i].staves.empty ())
420 *tail = scm_cons (scm_from_double (system_position), SCM_EOL);
421 tail = SCM_CDRLOC (*tail);
425 assert (spring_idx == solution_.size () - 1);
426 return system_offsets;
429 // Given two lines that are already spaced (the first and last
430 // elements of loose_lines), distribute some unspaced lines between
433 Page_layout_problem::distribute_loose_lines (vector<Grob*> const &loose_lines,
434 vector<Real> const &min_distances,
435 Real first_translation, Real last_translation)
437 Simple_spacer spacer;
438 for (vsize i = 0; i + 1 < loose_lines.size (); ++i)
440 SCM spec = get_spacing_spec (loose_lines[i], loose_lines[i+1]);
441 Spring spring (1.0, 0.0);
442 alter_spring_from_spacing_spec (spec, &spring);
443 spring.ensure_min_distance (min_distances[i]);
445 if (spec == SCM_BOOL_F)
447 // Insert a very flexible spring, so it doesn't have much effect.
448 spring.set_inverse_stretch_strength (100000);
449 spring.set_inverse_compress_strength (100000);
452 spacer.add_spring (spring);
455 // Remember: offsets are decreasing, since we're going from UP to DOWN!
456 spacer.solve (first_translation - last_translation, false);
458 vector<Real> solution = spacer.spring_positions ();
459 for (vsize i = 1; i + 1 < solution.size (); ++i)
460 loose_lines[i]->translate_axis (first_translation - solution[i], Y_AXIS);
464 Page_layout_problem::solution (bool ragged)
466 solve_rod_spring_problem (ragged);
467 return find_system_offsets ();
470 // Build upper and lower skylines for a system. We don't yet know the positions
471 // of the staves within the system, so we make the skyline as conservative as
472 // possible. That is, for the upper skyline, we pretend that all of the staves
473 // in the system are packed together close to the top system; for the lower
474 // skyline, we pretend that all of the staves are packed together close to
475 // the bottom system.
477 // The upper skyline is relative to the top staff; the lower skyline is relative to
480 Page_layout_problem::build_system_skyline (vector<Grob*> const& staves,
481 vector<Real> const& minimum_translations,
485 if (minimum_translations.empty ())
488 assert (staves.size () == minimum_translations.size ());
489 Real first_translation = minimum_translations[0];
492 for (vsize i = 0; i < staves.size (); ++i)
494 Real dy = minimum_translations[i] - first_translation;
496 Skyline_pair *sky = Skyline_pair::unsmob (g->get_property ("vertical-skylines"));
500 up->merge ((*sky)[UP]);
504 down->merge ((*sky)[DOWN]);
511 // Leave the down skyline at a position
512 // relative to the bottom staff.
513 down->raise (-last_dy);
517 Page_layout_problem::prob_extent (Prob *p)
519 Stencil *sten = unsmob_stencil (p->get_property ("stencil"));
520 return sten ? sten->extent (Y_AXIS) : Interval (0, 0);
524 Page_layout_problem::first_staff_extent (Element const& e)
527 return prob_extent (e.prob);
528 else if (e.staves.size ())
529 return e.staves[0]->extent (e.staves[0], Y_AXIS);
531 return Interval (0, 0);
535 Page_layout_problem::last_staff_extent (Element const& e)
538 return prob_extent (e.prob);
539 else if (e.staves.size ())
540 return e.staves.back ()->extent (e.staves.back (), Y_AXIS);
542 return Interval (0, 0);
546 Page_layout_problem::get_details (Element const& elt)
548 if (elt.staves.empty ())
551 return get_details (elt.staves.back ()->get_system ());
555 Page_layout_problem::get_details (Grob *g)
557 Grob *left_bound = dynamic_cast<Spanner*> (g)->get_bound (LEFT);
558 return left_bound->get_property ("line-break-system-details");
562 Page_layout_problem::is_spaceable (Grob *g)
564 return !scm_is_number (g->get_property ("staff-affinity"));
568 Page_layout_problem::mark_as_spaceable (Grob *g)
570 g->set_property ("staff-affinity", SCM_BOOL_F);
574 Page_layout_problem::read_spacing_spec (SCM spec, Real* dest, SCM sym)
576 SCM pair = scm_sloppy_assq (sym, spec);
577 if (scm_is_pair (pair) && scm_is_number (scm_cdr (pair)))
579 *dest = scm_to_double (scm_cdr (pair));
585 // Returns the spacing spec connecting BEFORE to AFTER. A return
586 // value of SCM_BOOL_F means that there should be no spring (in
587 // practice, this means that we use a very flexible spring).
589 Page_layout_problem::get_spacing_spec (Grob *before, Grob *after)
591 if (is_spaceable (before))
593 if (is_spaceable (after))
594 return before->get_property ("next-staff-spacing");
597 Direction affinity = to_dir (after->get_property ("staff-affinity"));
598 return (affinity == DOWN) ? SCM_BOOL_F : after->get_property ("inter-staff-spacing");
603 if (is_spaceable (after))
605 Direction affinity = to_dir (before->get_property ("staff-affinity"));
606 return (affinity == UP) ? SCM_BOOL_F : before->get_property ("inter-staff-spacing");
610 Direction before_affinity = to_dir (before->get_property ("staff-affinity"));
611 Direction after_affinity = to_dir (after->get_property ("staff-affinity"));
612 if (after_affinity > before_affinity)
614 warning (_ ("staff-affinities should only decrease"));
615 after_affinity = before_affinity;
617 if (before_affinity != UP)
618 return before->get_property ("inter-loose-line-spacing");
619 else if (after_affinity != DOWN)
620 return before->get_property ("inter-loose-line-spacing");
627 Page_layout_problem::alter_spring_from_spacing_spec (SCM spec, Spring* spring)
632 if (read_spacing_spec (spec, &space, ly_symbol2scm ("space")))
633 spring->set_distance (space);
634 if (read_spacing_spec (spec, &min_dist, ly_symbol2scm ("minimum-distance")))
635 spring->set_min_distance (min_dist);
636 spring->set_default_strength ();
638 if (read_spacing_spec (spec, &stretch, ly_symbol2scm ("stretchability")))
640 spring->set_inverse_stretch_strength (stretch);
641 spring->set_inverse_compress_strength (stretch);
646 Page_layout_problem::filter_dead_elements (vector<Grob*> const& input)
648 vector<Grob*> output;
649 for (vsize i = 0; i < input.size (); ++i)
651 if (Hara_kiri_group_spanner::has_interface (input[i]))
652 Hara_kiri_group_spanner::consider_suicide (input[i]);
654 if (input[i]->is_live ())
655 output.push_back (input[i]);