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 "axis-group-interface.hh"
14 #include "hara-kiri-group-spanner.hh"
15 #include "international.hh"
17 #include "output-def.hh"
18 #include "paper-book.hh"
19 #include "pointer-group-interface.hh"
21 #include "skyline-pair.hh"
24 Page_layout_problem::Page_layout_problem (Paper_book *pb, SCM page_scm, SCM systems)
25 : bottom_skyline_ (DOWN)
27 Prob *page = unsmob_prob (page_scm);
36 Stencil *head = unsmob_stencil (page->get_property ("head-stencil"));
37 Stencil *foot = unsmob_stencil (page->get_property ("foot-stencil"));
39 header_height_ = head ? head->extent (Y_AXIS).length () : 0;
40 footer_height_ = foot ? foot->extent (Y_AXIS).length () : 0;
41 page_height_ = robust_scm2double (page->get_property ("paper-height"), 100);
44 // Initially, bottom_skyline_ represents the top of the page. Make
45 // it solid, so that the top of the first system will be forced
46 // below the top of the printable area.
47 bottom_skyline_.set_minimum_height (-header_height_);
49 SCM between_system_spacing = SCM_EOL;
50 SCM after_title_spacing = SCM_EOL;
51 SCM before_title_spacing = SCM_EOL;
52 SCM between_title_spacing = SCM_EOL;
54 // first_system_spacing controls the spring from the top of the printable
55 // area to the first staff. It allows the user to control the offset of
56 // the first staff (as opposed to the top of the first system) from the
57 // top of the page. Similarly for last_system_spacing.
58 SCM first_system_spacing = SCM_EOL;
59 SCM last_system_spacing = SCM_EOL;
62 Output_def *paper = pb->paper_;
63 between_system_spacing = paper->c_variable ("between-system-spacing");
64 after_title_spacing = paper->c_variable ("after-title-spacing");
65 before_title_spacing = paper->c_variable ("before-title-spacing");
66 between_title_spacing = paper->c_variable ("between-title-spacing");
67 last_system_spacing = paper->c_variable ("last-system-spacing");
68 first_system_spacing = paper->c_variable ("first-system-spacing");
69 if (scm_is_pair (systems) && unsmob_prob (scm_car (systems)))
70 first_system_spacing = paper->c_variable ("first-system-title-spacing");
72 // Note: the page height here does _not_ reserve space for headers and
73 // footers. This is because we want to anchor the first-system-spacing
74 // spring at the _top_ of the header.
75 page_height_ -= robust_scm2double (paper->c_variable ("top-margin"), 0)
76 + robust_scm2double (paper->c_variable ("bottom-margin"), 0);
78 read_spacing_spec (first_system_spacing, &header_padding_, ly_symbol2scm ("padding"));
79 read_spacing_spec (last_system_spacing, &footer_padding_, ly_symbol2scm ("padding"));
81 bool last_system_was_title = false;
84 for (SCM s = systems; scm_is_pair (s); s = scm_cdr (s))
86 bool first = (s == systems);
88 if (Grob *g = unsmob_grob (scm_car (s)))
90 System *sys = dynamic_cast<System*> (g);
93 programming_error ("got a grob for vertical spacing that wasn't a System");
97 SCM spec = first ? first_system_spacing
98 : (last_system_was_title ? after_title_spacing : between_system_spacing);
99 Spring spring (first ? 0 : 1, 0.0);
101 alter_spring_from_spacing_spec (spec, &spring);
102 read_spacing_spec (spec, &padding, ly_symbol2scm ("padding"));
104 append_system (sys, spring, padding);
105 last_system_was_title = false;
107 else if (Prob *p = unsmob_prob (scm_car (s)))
109 SCM spec = first ? first_system_spacing
110 : (last_system_was_title ? between_title_spacing : before_title_spacing);
111 Spring spring (first ? 0 : 1, 0.0);
113 alter_spring_from_spacing_spec (spec, &spring);
114 read_spacing_spec (spec, &padding, ly_symbol2scm ("padding"));
116 append_prob (p, spring, padding);
117 last_system_was_title = true;
120 programming_error ("got a system that was neither a Grob nor a Prob");
123 Spring last_spring (0, 0);
124 Real last_padding = 0;
125 alter_spring_from_spacing_spec (last_system_spacing, &last_spring);
126 read_spacing_spec (last_system_spacing, &last_padding, ly_symbol2scm ("padding"));
127 last_spring.ensure_min_distance (last_padding - bottom_skyline_.max_height () + footer_height_);
128 springs_.push_back (last_spring);
130 if (elements_.size ())
134 // TODO: junk bottom-space now that we have last-system-spacing?
135 // bottom-space has the flexibility that one can do it per-system.
136 // NOTE: bottom-space is misnamed since it is not stretchable space.
137 if (Prob *p = elements_.back ().prob)
138 bottom_padding = robust_scm2double (p->get_property ("bottom-space"), 0);
139 else if (elements_.back ().staves.size ())
141 SCM details = get_details (elements_.back ());
142 bottom_padding = robust_scm2double (ly_assoc_get (ly_symbol2scm ("bottom-space"),
147 page_height_ -= bottom_padding;
152 Page_layout_problem::set_header_height (Real height)
154 header_height_ = height;
158 Page_layout_problem::set_footer_height (Real height)
160 footer_height_ = height;
164 Page_layout_problem::find_vertical_alignment (System *sys)
166 extract_grob_set (sys, "elements", elts);
167 for (vsize i = 0; i < elts.size (); ++i)
168 if (Align_interface::has_interface (elts[i]))
175 Page_layout_problem::append_system (System *sys, Spring const& spring, Real padding)
177 Grob *align = find_vertical_alignment (sys);
180 sys->programming_error ("no VerticalAlignment in system: can't do vertical spacing");
184 align->set_property ("positioning-done", SCM_BOOL_T);
186 extract_grob_set (align, "elements", all_elts);
187 vector<Grob*> elts = filter_dead_elements (all_elts);
188 vector<Real> minimum_offsets = Align_interface::get_minimum_translations (align, elts, Y_AXIS,
191 Skyline up_skyline (UP);
192 Skyline down_skyline (DOWN);
193 build_system_skyline (elts, minimum_offsets, &up_skyline, &down_skyline);
195 Real minimum_distance = up_skyline.distance (bottom_skyline_) + padding;
197 Spring spring_copy = spring;
198 spring_copy.ensure_min_distance (minimum_distance);
199 springs_.push_back (spring_copy);
201 bottom_skyline_ = down_skyline;
202 elements_.push_back (Element (elts, minimum_offsets));
204 // Add the springs for the VerticalAxisGroups in this system.
206 // If the user has specified the offsets of the individual staves, fix the
207 // springs at the given distances. Otherwise, use stretchable springs.
208 SCM details = get_details (elements_.back ());
209 SCM manual_dists = ly_assoc_get (ly_symbol2scm ("alignment-distances"), details, SCM_EOL);
210 vsize last_spaceable_staff = 0;
211 bool found_spaceable_staff = false;
212 for (vsize i = 0; i < elts.size (); ++i)
214 if (is_spaceable (elts[i]))
216 // We don't add a spring for the first staff, since
217 // we are only adding springs _between_ staves here.
218 if (!found_spaceable_staff)
220 found_spaceable_staff = true;
221 last_spaceable_staff = i;
225 Spring spring (0.5, 0.0);
226 SCM spec = elts[last_spaceable_staff]->get_property ("next-staff-spacing");
227 alter_spring_from_spacing_spec (spec, &spring);
229 springs_.push_back (spring);
230 Real min_distance = (found_spaceable_staff ? minimum_offsets[last_spaceable_staff] : 0) - minimum_offsets[i];
231 springs_.back ().ensure_min_distance (min_distance);
233 if (scm_is_pair (manual_dists))
235 if (scm_is_number (scm_car (manual_dists)))
237 Real dy = scm_to_double (scm_car (manual_dists));
239 springs_.back ().set_distance (dy);
240 springs_.back ().set_min_distance (dy);
241 springs_.back ().set_inverse_stretch_strength (0);
243 manual_dists = scm_cdr (manual_dists);
245 last_spaceable_staff = i;
249 // Corner case: there was only one staff, and it wasn't spaceable.
250 // Mark it spaceable, because we do not allow non-spaceable staves
251 // to be at the top or bottom of a system.
252 if (!found_spaceable_staff && elts.size ())
253 mark_as_spaceable (elts[0]);
257 Page_layout_problem::append_prob (Prob *prob, Spring const& spring, Real padding)
259 Skyline_pair *sky = Skyline_pair::unsmob (prob->get_property ("vertical-skylines"));
260 Real minimum_distance = 0;
263 minimum_distance = (*sky)[UP].distance (bottom_skyline_);
264 bottom_skyline_ = (*sky)[DOWN];
266 else if (Stencil *sten = unsmob_stencil (prob->get_property ("stencil")))
268 Interval iv = sten->extent (Y_AXIS);
269 minimum_distance = iv[UP] - bottom_skyline_.max_height ();
271 bottom_skyline_.clear ();
272 bottom_skyline_.set_minimum_height (iv[DOWN]);
274 minimum_distance += padding;
276 Spring spring_copy = spring;
277 spring_copy.ensure_min_distance (minimum_distance);
278 springs_.push_back (spring_copy);
279 elements_.push_back (Element (prob));
283 Page_layout_problem::solve_rod_spring_problem (bool ragged)
285 Simple_spacer spacer;
287 for (vsize i = 0; i < springs_.size (); ++i)
288 spacer.add_spring (springs_[i]);
290 spacer.solve (page_height_, ragged);
291 solution_ = spacer.spring_positions ();
294 // The solution_ vector stores the position of every live VerticalAxisGroup
295 // and every title. From that information,
296 // 1) within each system, stretch the staves so they land at the right position
297 // 2) find the offset of each system (relative to the printable area of the page).
298 // TODO: this function is getting too long, maybe split it up?
300 Page_layout_problem::find_system_offsets ()
302 SCM system_offsets = SCM_EOL;
303 SCM *tail = &system_offsets;
305 // spring_idx 0 is the top of the page. Interesting values start from 1.
306 vsize spring_idx = 1;
307 vector<Grob*> loose_lines;
308 vector<Real> loose_line_min_distances;
309 Grob *last_spaceable_line = 0;
310 Real last_spaceable_line_translation = 0;
311 for (vsize i = 0; i < elements_.size (); ++i)
313 if (elements_[i].prob)
315 *tail = scm_cons (scm_from_double (solution_[spring_idx]), SCM_EOL);
316 tail = SCM_CDRLOC (*tail);
318 // Lay out any non-spaceable lines between this line and
320 if (loose_lines.size ())
322 Interval loose_extent = loose_lines.back ()->extent (loose_lines.back (), Y_AXIS);
323 Interval prob_extent = unsmob_stencil (elements_[i].prob->get_property ("stencil"))->extent (Y_AXIS);
324 Real min_distance = -loose_extent[DOWN] + prob_extent[UP]; // TODO: include padding/minimum-distance
326 loose_line_min_distances.push_back (min_distance);
327 loose_lines.push_back (0);
329 distribute_loose_lines (loose_lines, loose_line_min_distances,
330 last_spaceable_line_translation, -solution_[spring_idx]);
331 loose_lines.clear ();
332 loose_line_min_distances.clear ();
335 last_spaceable_line = 0;
336 last_spaceable_line_translation = -solution_[spring_idx];
341 // Getting this signs right here is a little tricky. The configuration
342 // we return has zero at the top of the page and positive numbers further
343 // down, as does the solution_ vector. Within a staff, however, positive
345 // TODO: perhaps change the way the page 'configuration variable works so
346 // that it is consistent with the usual up/down sign conventions in
347 // Lilypond. Then this would be less confusing.
349 // These two positions are relative to the page (with positive numbers being
351 Real first_staff_position = solution_[spring_idx];
352 Real first_staff_min_translation = elements_[i].min_offsets.size () ? elements_[i].min_offsets[0] : 0;
353 Real system_position = first_staff_position + first_staff_min_translation;
355 // Position the staves within this system.
356 Real translation = 0;
357 vector<Real> const& min_offsets = elements_[i].min_offsets;
358 bool found_spaceable_staff = false;
359 for (vsize staff_idx = 0; staff_idx < elements_[i].staves.size (); ++staff_idx)
361 Grob *staff = elements_[i].staves[staff_idx];
362 staff->set_property ("system-Y-offset", scm_from_double (-system_position));
364 if (is_spaceable (staff))
366 // this is relative to the system: negative numbers are down.
367 translation = system_position - solution_[spring_idx];
370 // Lay out any non-spaceable lines between this line and
372 if (loose_lines.size ())
374 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
375 loose_lines.push_back (staff);
377 distribute_loose_lines (loose_lines, loose_line_min_distances,
378 last_spaceable_line_translation, translation - system_position);
379 loose_lines.clear ();
380 loose_line_min_distances.clear ();
382 last_spaceable_line = staff;
383 // Negative is down but the translation is relative to the whole page.
384 last_spaceable_line_translation = -system_position + translation;
386 staff->translate_axis (translation, Y_AXIS);
387 found_spaceable_staff = true;
391 if (loose_lines.empty ())
392 loose_lines.push_back (last_spaceable_line);
394 loose_lines.push_back (staff);
396 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
400 if (last_spaceable_line)
401 min_dist = Axis_group_interface::minimum_distance (last_spaceable_line,
404 else // distance to the top margin
405 min_dist = header_padding_ + header_height_ + staff->extent (staff, Y_AXIS)[UP];
407 loose_line_min_distances.push_back (min_dist);
412 // Corner case: even if a system has no live staves, it still takes up
413 // one spring (a system with one live staff also takes up one spring),
414 // which we need to increment past.
415 if (!found_spaceable_staff)
418 *tail = scm_cons (scm_from_double (system_position), SCM_EOL);
419 tail = SCM_CDRLOC (*tail);
423 if (loose_lines.size ())
425 Grob *last = loose_lines.back ();
426 Interval last_ext = last->extent (last, Y_AXIS);
427 loose_line_min_distances.push_back (-last_ext[DOWN] + footer_height_ + footer_padding_);
428 loose_lines.push_back (0);
430 distribute_loose_lines (loose_lines, loose_line_min_distances,
431 last_spaceable_line_translation, -page_height_);
435 assert (spring_idx == solution_.size () - 1);
436 return system_offsets;
439 // Given two lines that are already spaced (the first and last
440 // elements of loose_lines), distribute some unspaced lines between
443 Page_layout_problem::distribute_loose_lines (vector<Grob*> const &loose_lines,
444 vector<Real> const &min_distances,
445 Real first_translation, Real last_translation)
447 Simple_spacer spacer;
448 for (vsize i = 0; i + 1 < loose_lines.size (); ++i)
450 SCM spec = get_spacing_spec (loose_lines[i], loose_lines[i+1]);
451 Spring spring (1.0, 0.0);
452 alter_spring_from_spacing_spec (spec, &spring);
453 spring.ensure_min_distance (min_distances[i]);
455 if ((spec == SCM_BOOL_F && loose_lines[0] && loose_lines.back ())
457 || !loose_lines[i+1])
459 // Insert a very flexible spring, so it doesn't have much effect.
460 // TODO: set a default distance and a compress strength so that a
461 // lyrics line, for example, will stay closer to the top staff
462 // even in a compressed configuration.
463 spring.set_inverse_stretch_strength (100000);
464 spring.set_inverse_compress_strength (100000);
467 spacer.add_spring (spring);
470 // Remember: offsets are decreasing, since we're going from UP to DOWN!
471 spacer.solve (first_translation - last_translation, false);
473 vector<Real> solution = spacer.spring_positions ();
474 for (vsize i = 1; i + 1 < solution.size (); ++i)
476 Real system_offset = scm_to_double (loose_lines[i]->get_property ("system-Y-offset"));
477 loose_lines[i]->translate_axis (first_translation - solution[i] - system_offset, Y_AXIS);
482 Page_layout_problem::solution (bool ragged)
484 solve_rod_spring_problem (ragged);
485 return find_system_offsets ();
488 // Build upper and lower skylines for a system. We don't yet know the positions
489 // of the staves within the system, so we make the skyline as conservative as
490 // possible. That is, for the upper skyline, we pretend that all of the staves
491 // in the system are packed together close to the top system; for the lower
492 // skyline, we pretend that all of the staves are packed together close to
493 // the bottom system.
495 // The upper skyline is relative to the top staff; the lower skyline is relative to
498 Page_layout_problem::build_system_skyline (vector<Grob*> const& staves,
499 vector<Real> const& minimum_translations,
503 if (minimum_translations.empty ())
506 assert (staves.size () == minimum_translations.size ());
507 Real first_translation = minimum_translations[0];
508 Real last_spaceable_dy = 0;
509 Real first_spaceable_dy = 0;
510 bool found_spaceable_staff;
512 for (vsize i = 0; i < staves.size (); ++i)
514 Real dy = minimum_translations[i] - first_translation;
516 Skyline_pair *sky = Skyline_pair::unsmob (g->get_property ("vertical-skylines"));
520 up->merge ((*sky)[UP]);
524 down->merge ((*sky)[DOWN]);
527 if (is_spaceable (staves[i]))
529 if (!found_spaceable_staff)
531 found_spaceable_staff = true;
532 first_spaceable_dy = dy;
534 last_spaceable_dy = dy;
538 // Leave the up skyline at a position relative
539 // to the top spaceable staff.
540 up->raise (-first_spaceable_dy);
542 // Leave the down skyline at a position
543 // relative to the bottom spaceable staff.
544 down->raise (-last_spaceable_dy);
548 Page_layout_problem::prob_extent (Prob *p)
550 Stencil *sten = unsmob_stencil (p->get_property ("stencil"));
551 return sten ? sten->extent (Y_AXIS) : Interval (0, 0);
555 Page_layout_problem::first_staff_extent (Element const& e)
558 return prob_extent (e.prob);
559 else if (e.staves.size ())
560 return e.staves[0]->extent (e.staves[0], Y_AXIS);
562 return Interval (0, 0);
566 Page_layout_problem::last_staff_extent (Element const& e)
569 return prob_extent (e.prob);
570 else if (e.staves.size ())
571 return e.staves.back ()->extent (e.staves.back (), Y_AXIS);
573 return Interval (0, 0);
577 Page_layout_problem::get_details (Element const& elt)
579 if (elt.staves.empty ())
582 return get_details (elt.staves.back ()->get_system ());
586 Page_layout_problem::get_details (Grob *g)
588 Grob *left_bound = dynamic_cast<Spanner*> (g)->get_bound (LEFT);
589 return left_bound->get_property ("line-break-system-details");
593 Page_layout_problem::is_spaceable (Grob *g)
595 return !scm_is_number (g->get_property ("staff-affinity"));
599 Page_layout_problem::mark_as_spaceable (Grob *g)
601 g->set_property ("staff-affinity", SCM_BOOL_F);
605 Page_layout_problem::read_spacing_spec (SCM spec, Real* dest, SCM sym)
607 SCM pair = scm_sloppy_assq (sym, spec);
608 if (scm_is_pair (pair) && scm_is_number (scm_cdr (pair)))
610 *dest = scm_to_double (scm_cdr (pair));
616 // Returns the spacing spec connecting BEFORE to AFTER. A return
617 // value of SCM_BOOL_F means that there should be no spring (in
618 // practice, this means that we use a very flexible spring).
620 Page_layout_problem::get_spacing_spec (Grob *before, Grob *after)
622 if (!before || !after)
625 if (is_spaceable (before))
627 if (is_spaceable (after))
628 return before->get_property ("next-staff-spacing");
631 Direction affinity = to_dir (after->get_property ("staff-affinity"));
632 return (affinity == DOWN) ? SCM_BOOL_F : after->get_property ("inter-staff-spacing");
637 if (is_spaceable (after))
639 Direction affinity = to_dir (before->get_property ("staff-affinity"));
640 return (affinity == UP) ? SCM_BOOL_F : before->get_property ("inter-staff-spacing");
644 Direction before_affinity = to_dir (before->get_property ("staff-affinity"));
645 Direction after_affinity = to_dir (after->get_property ("staff-affinity"));
646 if (after_affinity > before_affinity)
648 warning (_ ("staff-affinities should only decrease"));
649 after_affinity = before_affinity;
651 if (before_affinity != UP)
652 return before->get_property ("inter-loose-line-spacing");
653 else if (after_affinity != DOWN)
654 return before->get_property ("inter-loose-line-spacing");
661 Page_layout_problem::alter_spring_from_spacing_spec (SCM spec, Spring* spring)
666 if (read_spacing_spec (spec, &space, ly_symbol2scm ("space")))
667 spring->set_distance (space);
668 if (read_spacing_spec (spec, &min_dist, ly_symbol2scm ("minimum-distance")))
669 spring->set_min_distance (min_dist);
670 spring->set_default_strength ();
672 if (read_spacing_spec (spec, &stretch, ly_symbol2scm ("stretchability")))
674 spring->set_inverse_stretch_strength (stretch);
675 spring->set_inverse_compress_strength (stretch);
680 Page_layout_problem::filter_dead_elements (vector<Grob*> const& input)
682 vector<Grob*> output;
683 for (vsize i = 0; i < input.size (); ++i)
685 if (Hara_kiri_group_spanner::has_interface (input[i]))
686 Hara_kiri_group_spanner::consider_suicide (input[i]);
688 if (input[i]->is_live ())
689 output.push_back (input[i]);