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);
124 if (elements_.size ())
128 // TODO: junk bottom-space now that we have last-spring-spacing?
129 // bottom-space has the flexibility that one can do it per-system.
130 // NOTE: bottom-space is misnamed since it is not stretchable space.
131 if (Prob *p = elements_.back ().prob)
132 bottom_padding = robust_scm2double (p->get_property ("bottom-space"), 0);
133 else if (elements_.back ().staves.size ())
135 SCM details = get_details (elements_.back ());
136 bottom_padding = robust_scm2double (ly_assoc_get (ly_symbol2scm ("bottom-space"),
141 page_height_ -= bottom_padding;
146 Page_layout_problem::set_header_height (Real height)
148 header_height_ = height;
152 Page_layout_problem::set_footer_height (Real height)
154 footer_height_ = height;
158 Page_layout_problem::find_vertical_alignment (System *sys)
160 extract_grob_set (sys, "elements", elts);
161 for (vsize i = 0; i < elts.size (); ++i)
162 if (Align_interface::has_interface (elts[i]))
169 Page_layout_problem::append_system (System *sys, Spring const& spring, Real padding)
171 Grob *align = find_vertical_alignment (sys);
174 sys->programming_error ("no VerticalAlignment in system: can't do vertical spacing");
178 align->set_property ("positioning-done", SCM_BOOL_T);
180 extract_grob_set (align, "elements", all_elts);
181 vector<Grob*> elts = filter_dead_elements (all_elts);
182 vector<Real> minimum_offsets = Align_interface::get_minimum_translations (align, elts, Y_AXIS,
185 Skyline up_skyline (UP);
186 Skyline down_skyline (DOWN);
187 build_system_skyline (elts, minimum_offsets, &up_skyline, &down_skyline);
189 Real minimum_distance = up_skyline.distance (bottom_skyline_) + padding;
191 Spring spring_copy = spring;
192 spring_copy.ensure_min_distance (minimum_distance);
193 springs_.push_back (spring_copy);
195 bottom_skyline_ = down_skyline;
196 elements_.push_back (Element (elts, minimum_offsets));
198 // Add the springs for the VerticalAxisGroups in this system.
200 // If the user has specified the offsets of the individual staves, fix the
201 // springs at the given distances. Otherwise, use stretchable springs.
202 SCM details = get_details (elements_.back ());
203 SCM manual_dists = ly_assoc_get (ly_symbol2scm ("alignment-distances"), details, SCM_EOL);
204 vsize last_spaceable_staff = 0;
205 bool found_spaceable_staff = false;
206 for (vsize i = 0; i < elts.size (); ++i)
208 if (is_spaceable (elts[i]))
210 // We don't add a spring for the first staff, since
211 // we are only adding springs _between_ staves here.
212 if (!found_spaceable_staff)
214 found_spaceable_staff = true;
215 last_spaceable_staff = i;
219 Spring spring (0.5, 0.0);
220 SCM spec = elts[last_spaceable_staff]->get_property ("next-staff-spacing");
221 alter_spring_from_spacing_spec (spec, &spring);
223 springs_.push_back (spring);
224 Real min_distance = (found_spaceable_staff ? minimum_offsets[last_spaceable_staff] : 0) - minimum_offsets[i];
225 springs_.back ().ensure_min_distance (min_distance);
227 if (scm_is_pair (manual_dists))
229 if (scm_is_number (scm_car (manual_dists)))
231 Real dy = scm_to_double (scm_car (manual_dists));
233 springs_.back ().set_distance (dy);
234 springs_.back ().set_min_distance (dy);
235 springs_.back ().set_inverse_stretch_strength (0);
237 manual_dists = scm_cdr (manual_dists);
239 last_spaceable_staff = i;
243 // Corner case: there was only one staff, and it wasn't spaceable.
244 // Mark it spaceable, because we do not allow non-spaceable staves
245 // to be at the top or bottom of a system.
246 if (!found_spaceable_staff && elts.size ())
247 mark_as_spaceable (elts[0]);
251 Page_layout_problem::append_prob (Prob *prob, Spring const& spring, Real padding)
253 Skyline_pair *sky = Skyline_pair::unsmob (prob->get_property ("vertical-skylines"));
254 Real minimum_distance = 0;
257 minimum_distance = (*sky)[UP].distance (bottom_skyline_);
258 bottom_skyline_ = (*sky)[DOWN];
260 else if (Stencil *sten = unsmob_stencil (prob->get_property ("stencil")))
262 Interval iv = sten->extent (Y_AXIS);
263 minimum_distance = iv[UP] - bottom_skyline_.max_height ();
265 bottom_skyline_.clear ();
266 bottom_skyline_.set_minimum_height (iv[DOWN]);
268 minimum_distance += padding;
270 Spring spring_copy = spring;
271 spring_copy.ensure_min_distance (minimum_distance);
272 springs_.push_back (spring_copy);
273 elements_.push_back (Element (prob));
277 Page_layout_problem::solve_rod_spring_problem (bool ragged)
279 Simple_spacer spacer;
281 for (vsize i = 0; i < springs_.size (); ++i)
282 spacer.add_spring (springs_[i]);
284 spacer.solve (page_height_, ragged);
285 solution_ = spacer.spring_positions ();
288 // The solution_ vector stores the position of every live VerticalAxisGroup
289 // and every title. From that information,
290 // 1) within each system, stretch the staves so they land at the right position
291 // 2) find the offset of each system (relative to the printable area of the page).
292 // TODO: this function is getting too long, maybe split it up?
294 Page_layout_problem::find_system_offsets ()
296 SCM system_offsets = SCM_EOL;
297 SCM *tail = &system_offsets;
299 // spring_idx 0 is the top of the page. Interesting values start from 1.
300 vsize spring_idx = 1;
301 vector<Grob*> loose_lines;
302 vector<Real> loose_line_min_distances;
303 Grob *last_spaceable_line = 0;
304 Real last_spaceable_line_translation = 0;
305 for (vsize i = 0; i < elements_.size (); ++i)
307 if (elements_[i].prob)
309 *tail = scm_cons (scm_from_double (solution_[spring_idx]), SCM_EOL);
310 tail = SCM_CDRLOC (*tail);
312 last_spaceable_line = 0;
313 last_spaceable_line_translation = -solution_[spring_idx];
318 // Getting this signs right here is a little tricky. The configuration
319 // we return has zero at the top of the page and positive numbers further
320 // down, as does the solution_ vector. Within a staff, however, positive
322 // TODO: perhaps change the way the page 'configuration variable works so
323 // that it is consistent with the usual up/down sign conventions in
324 // Lilypond. Then this would be less confusing.
326 // These two positions are relative to the page (with positive numbers being
328 Real first_staff_position = solution_[spring_idx];
329 Real first_staff_min_translation = elements_[i].min_offsets.size () ? elements_[i].min_offsets[0] : 0;
330 Real system_position = first_staff_position + first_staff_min_translation;
332 // Position the staves within this system.
333 Real translation = 0;
334 vector<Real> const& min_offsets = elements_[i].min_offsets;
335 bool found_spaceable_staff = false;
336 for (vsize staff_idx = 0; staff_idx < elements_[i].staves.size (); ++staff_idx)
338 Grob *staff = elements_[i].staves[staff_idx];
339 staff->set_property ("system-Y-offset", scm_from_double (-system_position));
341 if (is_spaceable (staff))
343 // this is relative to the system: negative numbers are down.
344 translation = system_position - solution_[spring_idx];
347 // Lay out any non-spaceable lines between this line and
349 if (loose_lines.size ())
351 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
352 loose_lines.push_back (staff);
354 distribute_loose_lines (loose_lines, loose_line_min_distances,
355 last_spaceable_line_translation, translation - system_position);
356 loose_lines.clear ();
357 loose_line_min_distances.clear ();
359 last_spaceable_line = staff;
360 // Negative is down but the translation is relative to the whole page.
361 last_spaceable_line_translation = -system_position + translation;
363 staff->translate_axis (translation, Y_AXIS);
364 found_spaceable_staff = true;
368 if (loose_lines.empty ())
369 loose_lines.push_back (last_spaceable_line);
371 loose_lines.push_back (staff);
373 loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
375 // FIXME: this should reflect the min distance from the last staff
376 // to the first loose line of this staff.
377 loose_line_min_distances.push_back (0);
381 // Corner case: even if a system has no live staves, it still takes up
382 // one spring (a system with one live staff also takes up one spring),
383 // which we need to increment past.
384 if (!found_spaceable_staff)
387 *tail = scm_cons (scm_from_double (system_position), SCM_EOL);
388 tail = SCM_CDRLOC (*tail);
392 if (loose_lines.size ())
394 loose_line_min_distances.push_back (0); // FIXME: should be the min distance to the footer.
395 loose_lines.push_back (0);
397 distribute_loose_lines (loose_lines, loose_line_min_distances,
398 last_spaceable_line_translation, -page_height_);
402 assert (spring_idx == solution_.size () - 1);
403 return system_offsets;
406 // Given two lines that are already spaced (the first and last
407 // elements of loose_lines), distribute some unspaced lines between
410 Page_layout_problem::distribute_loose_lines (vector<Grob*> const &loose_lines,
411 vector<Real> const &min_distances,
412 Real first_translation, Real last_translation)
414 Simple_spacer spacer;
415 for (vsize i = 0; i + 1 < loose_lines.size (); ++i)
417 SCM spec = get_spacing_spec (loose_lines[i], loose_lines[i+1]);
418 Spring spring (1.0, 0.0);
419 alter_spring_from_spacing_spec (spec, &spring);
420 spring.ensure_min_distance (min_distances[i]);
422 if ((spec == SCM_BOOL_F && loose_lines[0] && loose_lines.back ())
424 || !loose_lines[i+1])
426 // Insert a very flexible spring, so it doesn't have much effect.
427 // TODO: set a default distance and a compress strength so that a
428 // lyrics line, for example, will stay closer to the top staff
429 // even in a compressed configuration.
430 spring.set_inverse_stretch_strength (100000);
431 spring.set_inverse_compress_strength (100000);
434 spacer.add_spring (spring);
437 // Remember: offsets are decreasing, since we're going from UP to DOWN!
438 spacer.solve (first_translation - last_translation, false);
440 vector<Real> solution = spacer.spring_positions ();
441 for (vsize i = 1; i + 1 < solution.size (); ++i)
443 Real system_offset = scm_to_double (loose_lines[i]->get_property ("system-Y-offset"));
444 loose_lines[i]->translate_axis (first_translation - solution[i] - system_offset, Y_AXIS);
449 Page_layout_problem::solution (bool ragged)
451 solve_rod_spring_problem (ragged);
452 return find_system_offsets ();
455 // Build upper and lower skylines for a system. We don't yet know the positions
456 // of the staves within the system, so we make the skyline as conservative as
457 // possible. That is, for the upper skyline, we pretend that all of the staves
458 // in the system are packed together close to the top system; for the lower
459 // skyline, we pretend that all of the staves are packed together close to
460 // the bottom system.
462 // The upper skyline is relative to the top staff; the lower skyline is relative to
465 Page_layout_problem::build_system_skyline (vector<Grob*> const& staves,
466 vector<Real> const& minimum_translations,
470 if (minimum_translations.empty ())
473 assert (staves.size () == minimum_translations.size ());
474 Real first_translation = minimum_translations[0];
475 Real last_spaceable_dy = 0;
476 Real first_spaceable_dy = 0;
477 bool found_spaceable_staff;
479 for (vsize i = 0; i < staves.size (); ++i)
481 Real dy = minimum_translations[i] - first_translation;
483 Skyline_pair *sky = Skyline_pair::unsmob (g->get_property ("vertical-skylines"));
487 up->merge ((*sky)[UP]);
491 down->merge ((*sky)[DOWN]);
494 if (is_spaceable (staves[i]))
496 if (!found_spaceable_staff)
498 found_spaceable_staff = true;
499 first_spaceable_dy = dy;
501 last_spaceable_dy = dy;
505 // Leave the up skyline at a position relative
506 // to the top spaceable staff.
507 up->raise (-first_spaceable_dy);
509 // Leave the down skyline at a position
510 // relative to the bottom spaceable staff.
511 down->raise (-last_spaceable_dy);
515 Page_layout_problem::prob_extent (Prob *p)
517 Stencil *sten = unsmob_stencil (p->get_property ("stencil"));
518 return sten ? sten->extent (Y_AXIS) : Interval (0, 0);
522 Page_layout_problem::first_staff_extent (Element const& e)
525 return prob_extent (e.prob);
526 else if (e.staves.size ())
527 return e.staves[0]->extent (e.staves[0], Y_AXIS);
529 return Interval (0, 0);
533 Page_layout_problem::last_staff_extent (Element const& e)
536 return prob_extent (e.prob);
537 else if (e.staves.size ())
538 return e.staves.back ()->extent (e.staves.back (), Y_AXIS);
540 return Interval (0, 0);
544 Page_layout_problem::get_details (Element const& elt)
546 if (elt.staves.empty ())
549 return get_details (elt.staves.back ()->get_system ());
553 Page_layout_problem::get_details (Grob *g)
555 Grob *left_bound = dynamic_cast<Spanner*> (g)->get_bound (LEFT);
556 return left_bound->get_property ("line-break-system-details");
560 Page_layout_problem::is_spaceable (Grob *g)
562 return !scm_is_number (g->get_property ("staff-affinity"));
566 Page_layout_problem::mark_as_spaceable (Grob *g)
568 g->set_property ("staff-affinity", SCM_BOOL_F);
572 Page_layout_problem::read_spacing_spec (SCM spec, Real* dest, SCM sym)
574 SCM pair = scm_sloppy_assq (sym, spec);
575 if (scm_is_pair (pair) && scm_is_number (scm_cdr (pair)))
577 *dest = scm_to_double (scm_cdr (pair));
583 // Returns the spacing spec connecting BEFORE to AFTER. A return
584 // value of SCM_BOOL_F means that there should be no spring (in
585 // practice, this means that we use a very flexible spring).
587 Page_layout_problem::get_spacing_spec (Grob *before, Grob *after)
589 if (!before || !after)
592 if (is_spaceable (before))
594 if (is_spaceable (after))
595 return before->get_property ("next-staff-spacing");
598 Direction affinity = to_dir (after->get_property ("staff-affinity"));
599 return (affinity == DOWN) ? SCM_BOOL_F : after->get_property ("inter-staff-spacing");
604 if (is_spaceable (after))
606 Direction affinity = to_dir (before->get_property ("staff-affinity"));
607 return (affinity == UP) ? SCM_BOOL_F : before->get_property ("inter-staff-spacing");
611 Direction before_affinity = to_dir (before->get_property ("staff-affinity"));
612 Direction after_affinity = to_dir (after->get_property ("staff-affinity"));
613 if (after_affinity > before_affinity)
615 warning (_ ("staff-affinities should only decrease"));
616 after_affinity = before_affinity;
618 if (before_affinity != UP)
619 return before->get_property ("inter-loose-line-spacing");
620 else if (after_affinity != DOWN)
621 return before->get_property ("inter-loose-line-spacing");
628 Page_layout_problem::alter_spring_from_spacing_spec (SCM spec, Spring* spring)
633 if (read_spacing_spec (spec, &space, ly_symbol2scm ("space")))
634 spring->set_distance (space);
635 if (read_spacing_spec (spec, &min_dist, ly_symbol2scm ("minimum-distance")))
636 spring->set_min_distance (min_dist);
637 spring->set_default_strength ();
639 if (read_spacing_spec (spec, &stretch, ly_symbol2scm ("stretchability")))
641 spring->set_inverse_stretch_strength (stretch);
642 spring->set_inverse_compress_strength (stretch);
647 Page_layout_problem::filter_dead_elements (vector<Grob*> const& input)
649 vector<Grob*> output;
650 for (vsize i = 0; i < input.size (); ++i)
652 if (Hara_kiri_group_spanner::has_interface (input[i]))
653 Hara_kiri_group_spanner::consider_suicide (input[i]);
655 if (input[i]->is_live ())
656 output.push_back (input[i]);