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 ("bottom-system-spacing");
68 first_system_spacing = paper->c_variable ("top-system-spacing");
69 if (scm_is_pair (systems) && unsmob_prob (scm_car (systems)))
70 first_system_spacing = paper->c_variable ("top-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 top-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 ())
132 Real bottom_padding = 0;
134 // TODO: junk bottom-space now that we have bottom-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::append_system (System *sys, Spring const& spring, Real padding)
166 Grob *align = sys->get_vertical_alignment ();
170 align->set_property ("positioning-done", SCM_BOOL_T);
172 extract_grob_set (align, "elements", all_elts);
173 vector<Grob*> elts = filter_dead_elements (all_elts);
174 vector<Real> minimum_offsets = Align_interface::get_minimum_translations (align, elts, Y_AXIS,
177 Skyline up_skyline (UP);
178 Skyline down_skyline (DOWN);
179 build_system_skyline (elts, minimum_offsets, &up_skyline, &down_skyline);
181 Real minimum_distance = up_skyline.distance (bottom_skyline_) + padding;
183 Spring spring_copy = spring;
184 spring_copy.ensure_min_distance (minimum_distance);
185 springs_.push_back (spring_copy);
187 bottom_skyline_ = down_skyline;
188 elements_.push_back (Element (elts, minimum_offsets));
190 // Add the springs for the VerticalAxisGroups in this system.
192 // If the user has specified the offsets of the individual staves, fix the
193 // springs at the given distances. Otherwise, use stretchable springs.
194 SCM details = get_details (elements_.back ());
195 SCM manual_dists = ly_assoc_get (ly_symbol2scm ("alignment-distances"), details, SCM_EOL);
196 vsize last_spaceable_staff = 0;
197 bool found_spaceable_staff = false;
198 for (vsize i = 0; i < elts.size (); ++i)
200 if (is_spaceable (elts[i]))
202 // We don't add a spring for the first staff, since
203 // we are only adding springs _between_ staves here.
204 if (!found_spaceable_staff)
206 found_spaceable_staff = true;
207 last_spaceable_staff = i;
211 Spring spring (0.5, 0.0);
212 SCM spec = elts[last_spaceable_staff]->get_property ("next-staff-spacing");
213 alter_spring_from_spacing_spec (spec, &spring);
215 springs_.push_back (spring);
216 Real min_distance = (found_spaceable_staff ? minimum_offsets[last_spaceable_staff] : 0) - minimum_offsets[i];
217 springs_.back ().ensure_min_distance (min_distance);
219 if (scm_is_pair (manual_dists))
221 if (scm_is_number (scm_car (manual_dists)))
223 Real dy = scm_to_double (scm_car (manual_dists));
225 springs_.back ().set_distance (dy);
226 springs_.back ().set_min_distance (dy);
227 springs_.back ().set_inverse_stretch_strength (0);
229 manual_dists = scm_cdr (manual_dists);
231 last_spaceable_staff = i;
235 // Corner case: there was only one staff, and it wasn't spaceable.
236 // Mark it spaceable, because we do not allow non-spaceable staves
237 // to be at the top or bottom of a system.
238 if (!found_spaceable_staff && elts.size ())
239 mark_as_spaceable (elts[0]);
243 Page_layout_problem::append_prob (Prob *prob, Spring const& spring, Real padding)
245 Skyline_pair *sky = Skyline_pair::unsmob (prob->get_property ("vertical-skylines"));
246 Real minimum_distance = 0;
249 minimum_distance = (*sky)[UP].distance (bottom_skyline_);
250 bottom_skyline_ = (*sky)[DOWN];
252 else if (Stencil *sten = unsmob_stencil (prob->get_property ("stencil")))
254 Interval iv = sten->extent (Y_AXIS);
255 minimum_distance = iv[UP] - bottom_skyline_.max_height ();
257 bottom_skyline_.clear ();
258 bottom_skyline_.set_minimum_height (iv[DOWN]);
260 minimum_distance += padding;
262 Spring spring_copy = spring;
263 spring_copy.ensure_min_distance (minimum_distance);
264 springs_.push_back (spring_copy);
265 elements_.push_back (Element (prob));
269 Page_layout_problem::solve_rod_spring_problem (bool ragged)
271 Simple_spacer spacer;
273 for (vsize i = 0; i < springs_.size (); ++i)
274 spacer.add_spring (springs_[i]);
276 spacer.solve (page_height_, ragged);
277 solution_ = spacer.spring_positions ();
280 // The solution_ vector stores the position of every live VerticalAxisGroup
281 // and every title. From that information,
282 // 1) within each system, stretch the staves so they land at the right position
283 // 2) find the offset of each system (relative to the printable area of the page).
284 // TODO: this function is getting too long, maybe split it up?
286 Page_layout_problem::find_system_offsets ()
288 SCM system_offsets = SCM_EOL;
289 SCM *tail = &system_offsets;
291 // spring_idx 0 is the top of the page. Interesting values start from 1.
292 vsize spring_idx = 1;
293 vector<Grob*> loose_lines;
294 vector<Real> loose_line_min_distances;
295 Grob *last_spaceable_line = 0;
296 Real last_spaceable_line_translation = 0;
297 for (vsize i = 0; i < elements_.size (); ++i)
299 if (elements_[i].prob)
301 *tail = scm_cons (scm_from_double (solution_[spring_idx]), SCM_EOL);
302 tail = SCM_CDRLOC (*tail);
304 // Lay out any non-spaceable lines between this line and
306 if (loose_lines.size ())
308 Interval loose_extent = loose_lines.back ()->extent (loose_lines.back (), Y_AXIS);
309 Interval prob_extent = unsmob_stencil (elements_[i].prob->get_property ("stencil"))->extent (Y_AXIS);
310 Real min_distance = -loose_extent[DOWN] + prob_extent[UP]; // TODO: include padding/minimum-distance
312 loose_line_min_distances.push_back (min_distance);
313 loose_lines.push_back (0);
315 distribute_loose_lines (loose_lines, loose_line_min_distances,
316 last_spaceable_line_translation, -solution_[spring_idx]);
317 loose_lines.clear ();
318 loose_line_min_distances.clear ();
321 last_spaceable_line = 0;
322 last_spaceable_line_translation = -solution_[spring_idx];
327 // Getting this signs right here is a little tricky. The configuration
328 // we return has zero at the top of the page and positive numbers further
329 // down, as does the solution_ vector. Within a staff, however, positive
331 // TODO: perhaps change the way the page 'configuration variable works so
332 // that it is consistent with the usual up/down sign conventions in
333 // Lilypond. Then this would be less confusing.
335 // These two positions are relative to the page (with positive numbers being
337 Real first_staff_position = solution_[spring_idx];
338 Real first_staff_min_translation = elements_[i].min_offsets.size () ? elements_[i].min_offsets[0] : 0;
339 Real system_position = first_staff_position + first_staff_min_translation;
341 // Position the staves within this system.
342 Real translation = 0;
343 vector<Real> const& min_offsets = elements_[i].min_offsets;
344 bool found_spaceable_staff = false;
345 for (vsize staff_idx = 0; staff_idx < elements_[i].staves.size (); ++staff_idx)
347 Grob *staff = elements_[i].staves[staff_idx];
348 staff->set_property ("system-Y-offset", scm_from_double (-system_position));
350 if (is_spaceable (staff))
352 // this is relative to the system: negative numbers are down.
353 translation = system_position - solution_[spring_idx];
356 // Lay out any non-spaceable lines between this line and
358 if (loose_lines.size ())
360 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
361 loose_lines.push_back (staff);
363 distribute_loose_lines (loose_lines, loose_line_min_distances,
364 last_spaceable_line_translation, translation - system_position);
365 loose_lines.clear ();
366 loose_line_min_distances.clear ();
368 last_spaceable_line = staff;
369 // Negative is down but the translation is relative to the whole page.
370 last_spaceable_line_translation = -system_position + translation;
372 staff->translate_axis (translation, Y_AXIS);
373 found_spaceable_staff = true;
377 if (loose_lines.empty ())
378 loose_lines.push_back (last_spaceable_line);
380 loose_lines.push_back (staff);
382 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
386 if (last_spaceable_line)
387 min_dist = Axis_group_interface::minimum_distance (last_spaceable_line,
390 else // distance to the top margin
391 min_dist = header_padding_ + header_height_ + staff->extent (staff, Y_AXIS)[UP];
393 loose_line_min_distances.push_back (min_dist);
398 // Corner case: even if a system has no live staves, it still takes up
399 // one spring (a system with one live staff also takes up one spring),
400 // which we need to increment past.
401 if (!found_spaceable_staff)
404 *tail = scm_cons (scm_from_double (system_position), SCM_EOL);
405 tail = SCM_CDRLOC (*tail);
409 if (loose_lines.size ())
411 Grob *last = loose_lines.back ();
412 Interval last_ext = last->extent (last, Y_AXIS);
413 loose_line_min_distances.push_back (-last_ext[DOWN] + footer_height_ + footer_padding_);
414 loose_lines.push_back (0);
416 distribute_loose_lines (loose_lines, loose_line_min_distances,
417 last_spaceable_line_translation, -page_height_);
421 assert (spring_idx == solution_.size () - 1);
422 return system_offsets;
425 // Given two lines that are already spaced (the first and last
426 // elements of loose_lines), distribute some unspaced lines between
429 Page_layout_problem::distribute_loose_lines (vector<Grob*> const &loose_lines,
430 vector<Real> const &min_distances,
431 Real first_translation, Real last_translation)
433 Simple_spacer spacer;
434 for (vsize i = 0; i + 1 < loose_lines.size (); ++i)
436 SCM spec = get_spacing_spec (loose_lines[i], loose_lines[i+1]);
437 Spring spring (1.0, 0.0);
438 alter_spring_from_spacing_spec (spec, &spring);
439 spring.ensure_min_distance (min_distances[i]);
441 if ((spec == SCM_BOOL_F && loose_lines[0] && loose_lines.back ())
443 || !loose_lines[i+1])
445 // Insert a very flexible spring, so it doesn't have much effect.
446 // TODO: set a default distance and a compress strength so that a
447 // lyrics line, for example, will stay closer to the top staff
448 // even in a compressed configuration.
449 spring.set_inverse_stretch_strength (100000);
450 spring.set_inverse_compress_strength (100000);
453 spacer.add_spring (spring);
456 // Remember: offsets are decreasing, since we're going from UP to DOWN!
457 spacer.solve (first_translation - last_translation, false);
459 vector<Real> solution = spacer.spring_positions ();
460 for (vsize i = 1; i + 1 < solution.size (); ++i)
462 Real system_offset = scm_to_double (loose_lines[i]->get_property ("system-Y-offset"));
463 loose_lines[i]->translate_axis (first_translation - solution[i] - system_offset, Y_AXIS);
468 Page_layout_problem::solution (bool ragged)
470 solve_rod_spring_problem (ragged);
471 return find_system_offsets ();
474 // Build upper and lower skylines for a system. We don't yet know the positions
475 // of the staves within the system, so we make the skyline as conservative as
476 // possible. That is, for the upper skyline, we pretend that all of the staves
477 // in the system are packed together close to the top system; for the lower
478 // skyline, we pretend that all of the staves are packed together close to
479 // the bottom system.
481 // The upper skyline is relative to the top staff; the lower skyline is relative to
484 Page_layout_problem::build_system_skyline (vector<Grob*> const& staves,
485 vector<Real> const& minimum_translations,
489 if (minimum_translations.empty ())
492 assert (staves.size () == minimum_translations.size ());
493 Real first_translation = minimum_translations[0];
494 Real last_spaceable_dy = 0;
495 Real first_spaceable_dy = 0;
496 bool found_spaceable_staff = false;
498 for (vsize i = 0; i < staves.size (); ++i)
500 Real dy = minimum_translations[i] - first_translation;
502 Skyline_pair *sky = Skyline_pair::unsmob (g->get_property ("vertical-skylines"));
506 up->merge ((*sky)[UP]);
510 down->merge ((*sky)[DOWN]);
513 if (is_spaceable (staves[i]))
515 if (!found_spaceable_staff)
517 found_spaceable_staff = true;
518 first_spaceable_dy = dy;
520 last_spaceable_dy = dy;
524 // Leave the up skyline at a position relative
525 // to the top spaceable staff.
526 up->raise (-first_spaceable_dy);
528 // Leave the down skyline at a position
529 // relative to the bottom spaceable staff.
530 down->raise (-last_spaceable_dy);
534 Page_layout_problem::prob_extent (Prob *p)
536 Stencil *sten = unsmob_stencil (p->get_property ("stencil"));
537 return sten ? sten->extent (Y_AXIS) : Interval (0, 0);
541 Page_layout_problem::first_staff_extent (Element const& e)
544 return prob_extent (e.prob);
545 else if (e.staves.size ())
546 return e.staves[0]->extent (e.staves[0], Y_AXIS);
548 return Interval (0, 0);
552 Page_layout_problem::last_staff_extent (Element const& e)
555 return prob_extent (e.prob);
556 else if (e.staves.size ())
557 return e.staves.back ()->extent (e.staves.back (), Y_AXIS);
559 return Interval (0, 0);
563 Page_layout_problem::get_details (Element const& elt)
565 if (elt.staves.empty ())
568 return get_details (elt.staves.back ()->get_system ());
572 Page_layout_problem::get_details (Grob *g)
574 Grob *left_bound = dynamic_cast<Spanner*> (g)->get_bound (LEFT);
575 return left_bound->get_property ("line-break-system-details");
579 Page_layout_problem::is_spaceable (Grob *g)
581 return !scm_is_number (g->get_property ("staff-affinity"));
585 Page_layout_problem::mark_as_spaceable (Grob *g)
587 g->set_property ("staff-affinity", SCM_BOOL_F);
591 Page_layout_problem::read_spacing_spec (SCM spec, Real* dest, SCM sym)
593 SCM pair = scm_sloppy_assq (sym, spec);
594 if (scm_is_pair (pair) && scm_is_number (scm_cdr (pair)))
596 *dest = scm_to_double (scm_cdr (pair));
602 // Returns the spacing spec connecting BEFORE to AFTER. A return
603 // value of SCM_BOOL_F means that there should be no spring (in
604 // practice, this means that we use a very flexible spring).
606 Page_layout_problem::get_spacing_spec (Grob *before, Grob *after)
608 if (!before || !after)
611 if (is_spaceable (before))
613 if (is_spaceable (after))
614 return before->get_property ("next-staff-spacing");
617 Direction affinity = to_dir (after->get_property ("staff-affinity"));
618 return (affinity == DOWN) ? SCM_BOOL_F : after->get_property ("inter-staff-spacing");
623 if (is_spaceable (after))
625 Direction affinity = to_dir (before->get_property ("staff-affinity"));
626 return (affinity == UP) ? SCM_BOOL_F : before->get_property ("inter-staff-spacing");
630 Direction before_affinity = to_dir (before->get_property ("staff-affinity"));
631 Direction after_affinity = to_dir (after->get_property ("staff-affinity"));
632 if (after_affinity > before_affinity)
634 warning (_ ("staff-affinities should only decrease"));
635 after_affinity = before_affinity;
637 if (before_affinity != UP)
638 return before->get_property ("inter-loose-line-spacing");
639 else if (after_affinity != DOWN)
640 return before->get_property ("inter-loose-line-spacing");
647 Page_layout_problem::alter_spring_from_spacing_spec (SCM spec, Spring* spring)
652 if (read_spacing_spec (spec, &space, ly_symbol2scm ("space")))
653 spring->set_distance (space);
654 if (read_spacing_spec (spec, &min_dist, ly_symbol2scm ("minimum-distance")))
655 spring->set_min_distance (min_dist);
656 spring->set_default_strength ();
658 if (read_spacing_spec (spec, &stretch, ly_symbol2scm ("stretchability")))
660 spring->set_inverse_stretch_strength (stretch);
661 spring->set_inverse_compress_strength (stretch);
666 Page_layout_problem::filter_dead_elements (vector<Grob*> const& input)
668 vector<Grob*> output;
669 for (vsize i = 0; i < input.size (); ++i)
671 if (Hara_kiri_group_spanner::has_interface (input[i]))
672 Hara_kiri_group_spanner::consider_suicide (input[i]);
674 if (input[i]->is_live ())
675 output.push_back (input[i]);