]> git.donarmstrong.com Git - lilypond.git/blob - lily/page-layout-problem.cc
61a7ebb90168cf26a747bdee0f31b269b50b7cb8
[lilypond.git] / lily / page-layout-problem.cc
1 /*
2   page-layout-problem.cc -- space systems nicely on a page. If systems can
3   be stretched, do that too.
4
5   source file of the GNU LilyPond music typesetter
6
7   (c) 2009 Joe Neeman <joeneeman@gmail.com>
8 */
9
10 #include "page-layout-problem.hh"
11
12 #include "align-interface.hh"
13 #include "hara-kiri-group-spanner.hh"
14 #include "international.hh"
15 #include "item.hh"
16 #include "output-def.hh"
17 #include "paper-book.hh"
18 #include "pointer-group-interface.hh"
19 #include "prob.hh"
20 #include "skyline-pair.hh"
21 #include "system.hh"
22
23 Page_layout_problem::Page_layout_problem (Paper_book *pb, SCM page_scm, SCM systems)
24   : bottom_skyline_ (DOWN)
25 {
26   Prob *page = unsmob_prob (page_scm);
27   header_height_ = 0;
28   footer_height_ = 0;
29   page_height_ = 100;
30
31   if (page)
32     {
33       Stencil *head = unsmob_stencil (page->get_property ("head-stencil"));
34       Stencil *foot = unsmob_stencil (page->get_property ("foot-stencil"));
35
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);
39     }
40
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_);
45
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;
50
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;
57   if (pb && pb->paper_)
58     {
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");
68
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);
74     }
75   bool last_system_was_title = false;
76
77
78   for (SCM s = systems; scm_is_pair (s); s = scm_cdr (s))
79     {
80       bool first = (s == systems);
81
82       if (Grob *g = unsmob_grob (scm_car (s)))
83         {
84           System *sys = dynamic_cast<System*> (g);
85           if (!sys)
86             {
87               programming_error ("got a grob for vertical spacing that wasn't a System");
88               continue;
89             }
90
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);
94           Real padding = 0.0;
95           alter_spring_from_spacing_spec (spec, &spring);
96           read_spacing_spec (spec, &padding, ly_symbol2scm ("padding"));
97
98           append_system (sys, spring, padding);
99           last_system_was_title = false;
100         }
101       else if (Prob *p = unsmob_prob (scm_car (s)))
102         {
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);
106           Real padding = 0.0;
107           alter_spring_from_spacing_spec (spec, &spring);
108           read_spacing_spec (spec, &padding, ly_symbol2scm ("padding"));
109
110           append_prob (p, spring, padding);
111           last_system_was_title = true;
112         }
113       else
114         programming_error ("got a system that was neither a Grob nor a Prob");
115     }
116
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);
123 }
124
125 void
126 Page_layout_problem::set_header_height (Real height)
127 {
128   header_height_ = height;
129 }
130
131 void
132 Page_layout_problem::set_footer_height (Real height)
133 {
134   footer_height_ = height;
135 }
136
137 Grob*
138 Page_layout_problem::find_vertical_alignment (System *sys)
139 {
140   extract_grob_set (sys, "elements", elts);
141   for (vsize i = 0; i < elts.size (); ++i)
142     if (Align_interface::has_interface (elts[i]))
143       return elts[i];
144
145   return 0;
146 }
147
148 void
149 Page_layout_problem::append_system (System *sys, Spring const& spring, Real padding)
150 {
151   Grob *align = find_vertical_alignment (sys);
152   if (!align)
153     {
154       sys->programming_error ("no VerticalAlignment in system: can't do vertical spacing");
155       return;
156     }
157
158   align->set_property ("positioning-done", SCM_BOOL_T);
159
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,
163                                                                             false, 0, 0);
164
165   Skyline up_skyline (UP);
166   Skyline down_skyline (DOWN);
167   build_system_skyline (elts, minimum_offsets, &up_skyline, &down_skyline);
168
169   Real minimum_distance = up_skyline.distance (bottom_skyline_) + padding;
170
171   Spring spring_copy = spring;
172   spring_copy.ensure_min_distance (minimum_distance);
173   springs_.push_back (spring_copy);
174
175   bottom_skyline_ = down_skyline;
176   elements_.push_back (Element (elts, minimum_offsets));
177
178   // Add the springs for the VerticalAxisGroups in this system.
179
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)
187     {
188       if (is_spaceable (elts[i]))
189         {
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)
193             {
194               if (i > 0)
195                 add_loose_lines_as_spaceable_lines (elts, minimum_offsets, 0, i-1);
196
197               found_spaceable_staff = true;
198               last_spaceable_staff = i;
199               continue;
200             }
201
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);
205
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);
209
210           if (scm_is_pair (manual_dists))
211             {
212               if (scm_is_number (scm_car (manual_dists)))
213                 {
214                   Real dy = scm_to_double (scm_car (manual_dists));
215
216                   springs_.back ().set_distance (dy);
217                   springs_.back ().set_min_distance (dy);
218                   springs_.back ().set_inverse_stretch_strength (0);
219                 }
220               manual_dists = scm_cdr (manual_dists);
221             }
222           last_spaceable_staff = i;
223         }
224     }
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,
232                                         elts.size () - 1);
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]);
238 }
239
240 // first and  last are inclusive
241 void
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)
245 {
246   vsize start = first;
247   vsize end = last;
248   if (start > 0)
249     --start;
250   if (end + 1 < elts.size ())
251     ++end;
252   
253   for (vsize i = start; i < end; ++i)
254     {
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)
259         {
260           spring.set_inverse_compress_strength (10000);
261           spring.set_inverse_stretch_strength (10000);
262         }
263
264       Real min_distance = minimum_offsets[i] - minimum_offsets[i+1];
265       spring.ensure_min_distance (min_distance);
266       springs_.push_back (spring);
267     }
268
269   for (vsize i = first; i <= last; ++i)
270     mark_as_spaceable (elts[i]);
271 }
272
273 void
274 Page_layout_problem::append_prob (Prob *prob, Spring const& spring, Real padding)
275 {
276   Skyline_pair *sky = Skyline_pair::unsmob (prob->get_property ("vertical-skylines"));
277   Real minimum_distance = 0;
278   if (sky)
279     {
280       minimum_distance = (*sky)[UP].distance (bottom_skyline_);
281       bottom_skyline_ = (*sky)[DOWN];
282     }
283   else if (Stencil *sten = unsmob_stencil (prob->get_property ("stencil")))
284     {
285       Interval iv = sten->extent (Y_AXIS);
286       minimum_distance = iv[UP] - bottom_skyline_.max_height ();
287
288       bottom_skyline_.clear ();
289       bottom_skyline_.set_minimum_height (iv[DOWN]);
290     }
291   minimum_distance += padding;
292
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));
297 }
298
299 void
300 Page_layout_problem::solve_rod_spring_problem (bool ragged)
301 {
302   Simple_spacer spacer;
303
304   for (vsize i = 0; i < springs_.size (); ++i)
305     spacer.add_spring (springs_[i]);
306
307   Real bottom_padding = 0;
308   Interval first_staff_iv (0, 0);
309   Interval last_staff_iv (0, 0);
310   if (elements_.size ())
311     {
312       first_staff_iv = first_staff_extent (elements_[0]);
313       last_staff_iv = last_staff_extent (elements_.back ());
314
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 ())
321         {
322           SCM details = get_details (elements_.back ());
323           bottom_padding = robust_scm2double (ly_assoc_get (ly_symbol2scm ("bottom-space"),
324                                                             details,
325                                                             SCM_BOOL_F),
326                                               0.0);
327         }
328     }
329
330   spacer.solve (page_height_ - bottom_padding, ragged);
331   solution_ = spacer.spring_positions ();
332 }
333
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?
339 SCM
340 Page_layout_problem::find_system_offsets ()
341 {
342   SCM system_offsets = SCM_EOL;
343   SCM *tail = &system_offsets;
344
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)
348     {
349       if (elements_[i].prob)
350         {
351           *tail = scm_cons (scm_from_double (solution_[spring_idx]), SCM_EOL);
352           tail = SCM_CDRLOC (*tail);
353           spring_idx++;
354         }
355       else
356         {
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
360           // numbers are up.
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.
364
365           // These two positions are relative to the page (with positive numbers being
366           // down).
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;
370
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)
379             {
380               Grob *staff = elements_[i].staves[staff_idx];
381
382               if (is_spaceable (staff))
383                 {
384                   // this is relative to the system: negative numbers are down.
385                   translation = system_position - solution_[spring_idx];
386                   spring_idx++;
387
388                   // Lay out any non-spaceable lines between this line and
389                   // the last one.
390                   if (loose_lines.size ())
391                     {
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 ();
398                     }
399                   last_spaceable_line = staff;
400                   last_spaceable_line_translation = translation;
401
402                   staff->translate_axis (translation, Y_AXIS);
403                 }
404               else
405                 {
406                   if (loose_lines.empty ())
407                     loose_lines.push_back (last_spaceable_line);
408
409                   loose_lines.push_back (staff);
410                   loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
411                 }
412             }
413
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 ())
418             spring_idx++;
419
420           *tail = scm_cons (scm_from_double (system_position), SCM_EOL);
421           tail = SCM_CDRLOC (*tail);
422         }
423     }
424
425   assert (spring_idx == solution_.size () - 1);
426   return system_offsets;
427 }
428
429 // Given two lines that are already spaced (the first and last
430 // elements of loose_lines), distribute some unspaced lines between
431 // them.
432 void
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)
436 {
437   Simple_spacer spacer;
438   for (vsize i = 0; i + 1 < loose_lines.size (); ++i)
439     {
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]);
444
445       if (spec == SCM_BOOL_F)
446         {
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);
450         }
451
452       spacer.add_spring (spring);
453     }
454
455   // Remember: offsets are decreasing, since we're going from UP to DOWN!
456   spacer.solve (first_translation - last_translation, false);
457
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);
461 }
462
463 SCM
464 Page_layout_problem::solution (bool ragged)
465 {
466   solve_rod_spring_problem (ragged);
467   return find_system_offsets ();
468 }
469
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.
476 //
477 // The upper skyline is relative to the top staff; the lower skyline is relative to
478 // the bottom staff.
479 void
480 Page_layout_problem::build_system_skyline (vector<Grob*> const& staves,
481                                            vector<Real> const& minimum_translations,
482                                            Skyline *up,
483                                            Skyline *down)
484 {
485   if (minimum_translations.empty ())
486     return;
487
488   assert (staves.size () == minimum_translations.size ());
489   Real first_translation = minimum_translations[0];
490   Real last_dy = 0;
491
492   for (vsize i = 0; i < staves.size (); ++i)
493     {
494       Real dy = minimum_translations[i] - first_translation;
495       Grob *g = staves[i];
496       Skyline_pair *sky = Skyline_pair::unsmob (g->get_property ("vertical-skylines"));
497       if (sky)
498         {
499           up->raise (-dy);
500           up->merge ((*sky)[UP]);
501           up->raise (dy);
502
503           down->raise (-dy);
504           down->merge ((*sky)[DOWN]);
505           down->raise (dy);
506
507           last_dy = dy;
508         }
509     }
510
511   // Leave the down skyline at a position
512   // relative to the bottom staff.
513   down->raise (-last_dy);
514 }
515
516 Interval
517 Page_layout_problem::prob_extent (Prob *p)
518 {
519   Stencil *sten = unsmob_stencil (p->get_property ("stencil"));
520   return sten ? sten->extent (Y_AXIS) : Interval (0, 0);
521 }
522
523 Interval
524 Page_layout_problem::first_staff_extent (Element const& e)
525 {
526   if (e.prob)
527     return prob_extent (e.prob);
528   else if (e.staves.size ())
529     return e.staves[0]->extent (e.staves[0], Y_AXIS);
530
531   return Interval (0, 0);
532 }
533
534 Interval
535 Page_layout_problem::last_staff_extent (Element const& e)
536 {
537   if (e.prob)
538     return prob_extent (e.prob);
539   else if (e.staves.size ())
540     return e.staves.back ()->extent (e.staves.back (), Y_AXIS);
541
542   return Interval (0, 0);
543 }
544
545 SCM
546 Page_layout_problem::get_details (Element const& elt)
547 {
548   if (elt.staves.empty ())
549     return SCM_EOL;
550
551   return get_details (elt.staves.back ()->get_system ());
552 }
553
554 SCM
555 Page_layout_problem::get_details (Grob *g)
556 {
557   Grob *left_bound = dynamic_cast<Spanner*> (g)->get_bound (LEFT);
558   return left_bound->get_property ("line-break-system-details");
559 }
560
561 bool
562 Page_layout_problem::is_spaceable (Grob *g)
563 {
564   return !scm_is_number (g->get_property ("staff-affinity"));
565 }
566
567 void
568 Page_layout_problem::mark_as_spaceable (Grob *g)
569 {
570   g->set_property ("staff-affinity", SCM_BOOL_F);
571 }
572
573 bool
574 Page_layout_problem::read_spacing_spec (SCM spec, Real* dest, SCM sym)
575 {
576   SCM pair = scm_sloppy_assq (sym, spec);
577   if (scm_is_pair (pair) && scm_is_number (scm_cdr (pair)))
578     {
579       *dest = scm_to_double (scm_cdr (pair));
580       return true;
581     }
582   return false;
583 }
584
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).
588 SCM
589 Page_layout_problem::get_spacing_spec (Grob *before, Grob *after)
590 {
591   if (is_spaceable (before))
592     {
593       if (is_spaceable (after))
594         return before->get_property ("next-staff-spacing");
595       else
596         {
597           Direction affinity = to_dir (after->get_property ("staff-affinity"));
598           return (affinity == DOWN) ? SCM_BOOL_F : after->get_property ("inter-staff-spacing");
599         }
600     }
601   else
602     {
603       if (is_spaceable (after))
604         {
605           Direction affinity = to_dir (before->get_property ("staff-affinity"));
606           return (affinity == UP) ? SCM_BOOL_F : before->get_property ("inter-staff-spacing");
607         }
608       else
609         {
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)
613             {
614               warning (_ ("staff-affinities should only decrease"));
615               after_affinity = before_affinity;
616             }
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");
621         }
622     }
623   return SCM_BOOL_F;
624 }
625
626 void
627 Page_layout_problem::alter_spring_from_spacing_spec (SCM spec, Spring* spring)
628 {
629   Real space;
630   Real stretch;
631   Real min_dist;
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 ();
637
638   if (read_spacing_spec (spec, &stretch, ly_symbol2scm ("stretchability")))
639     {
640       spring->set_inverse_stretch_strength (stretch);
641       spring->set_inverse_compress_strength (stretch);
642     }
643 }
644
645 vector<Grob*>
646 Page_layout_problem::filter_dead_elements (vector<Grob*> const& input)
647 {
648   vector<Grob*> output;
649   for (vsize i = 0; i < input.size (); ++i)
650     {
651       if (Hara_kiri_group_spanner::has_interface (input[i]))
652         Hara_kiri_group_spanner::consider_suicide (input[i]);
653
654       if (input[i]->is_live ())
655         output.push_back (input[i]);
656     }
657
658   return output;
659 }