]> git.donarmstrong.com Git - lilypond.git/blob - lily/page-layout-problem.cc
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond
[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   if (elements_.size ())
125     {
126       Real bottom_padding;
127
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 ())
134         {
135           SCM details = get_details (elements_.back ());
136           bottom_padding = robust_scm2double (ly_assoc_get (ly_symbol2scm ("bottom-space"),
137                                                             details,
138                                                             SCM_BOOL_F),
139                                               0.0);
140         }
141       page_height_ -= bottom_padding;
142     }
143 }
144
145 void
146 Page_layout_problem::set_header_height (Real height)
147 {
148   header_height_ = height;
149 }
150
151 void
152 Page_layout_problem::set_footer_height (Real height)
153 {
154   footer_height_ = height;
155 }
156
157 Grob*
158 Page_layout_problem::find_vertical_alignment (System *sys)
159 {
160   extract_grob_set (sys, "elements", elts);
161   for (vsize i = 0; i < elts.size (); ++i)
162     if (Align_interface::has_interface (elts[i]))
163       return elts[i];
164
165   return 0;
166 }
167
168 void
169 Page_layout_problem::append_system (System *sys, Spring const& spring, Real padding)
170 {
171   Grob *align = find_vertical_alignment (sys);
172   if (!align)
173     {
174       sys->programming_error ("no VerticalAlignment in system: can't do vertical spacing");
175       return;
176     }
177
178   align->set_property ("positioning-done", SCM_BOOL_T);
179
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,
183                                                                             false, 0, 0);
184
185   Skyline up_skyline (UP);
186   Skyline down_skyline (DOWN);
187   build_system_skyline (elts, minimum_offsets, &up_skyline, &down_skyline);
188
189   Real minimum_distance = up_skyline.distance (bottom_skyline_) + padding;
190
191   Spring spring_copy = spring;
192   spring_copy.ensure_min_distance (minimum_distance);
193   springs_.push_back (spring_copy);
194
195   bottom_skyline_ = down_skyline;
196   elements_.push_back (Element (elts, minimum_offsets));
197
198   // Add the springs for the VerticalAxisGroups in this system.
199
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)
207     {
208       if (is_spaceable (elts[i]))
209         {
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)
213             {
214               found_spaceable_staff = true;
215               last_spaceable_staff = i;
216               continue;
217             }
218
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);
222
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);
226
227           if (scm_is_pair (manual_dists))
228             {
229               if (scm_is_number (scm_car (manual_dists)))
230                 {
231                   Real dy = scm_to_double (scm_car (manual_dists));
232
233                   springs_.back ().set_distance (dy);
234                   springs_.back ().set_min_distance (dy);
235                   springs_.back ().set_inverse_stretch_strength (0);
236                 }
237               manual_dists = scm_cdr (manual_dists);
238             }
239           last_spaceable_staff = i;
240         }
241     }
242
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]);
248 }
249
250 void
251 Page_layout_problem::append_prob (Prob *prob, Spring const& spring, Real padding)
252 {
253   Skyline_pair *sky = Skyline_pair::unsmob (prob->get_property ("vertical-skylines"));
254   Real minimum_distance = 0;
255   if (sky)
256     {
257       minimum_distance = (*sky)[UP].distance (bottom_skyline_);
258       bottom_skyline_ = (*sky)[DOWN];
259     }
260   else if (Stencil *sten = unsmob_stencil (prob->get_property ("stencil")))
261     {
262       Interval iv = sten->extent (Y_AXIS);
263       minimum_distance = iv[UP] - bottom_skyline_.max_height ();
264
265       bottom_skyline_.clear ();
266       bottom_skyline_.set_minimum_height (iv[DOWN]);
267     }
268   minimum_distance += padding;
269
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));
274 }
275
276 void
277 Page_layout_problem::solve_rod_spring_problem (bool ragged)
278 {
279   Simple_spacer spacer;
280
281   for (vsize i = 0; i < springs_.size (); ++i)
282     spacer.add_spring (springs_[i]);
283
284   spacer.solve (page_height_, ragged);
285   solution_ = spacer.spring_positions ();
286 }
287
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?
293 SCM
294 Page_layout_problem::find_system_offsets ()
295 {
296   SCM system_offsets = SCM_EOL;
297   SCM *tail = &system_offsets;
298
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)
306     {
307       if (elements_[i].prob)
308         {
309           *tail = scm_cons (scm_from_double (solution_[spring_idx]), SCM_EOL);
310           tail = SCM_CDRLOC (*tail);
311
312           last_spaceable_line = 0;
313           last_spaceable_line_translation = -solution_[spring_idx];
314           spring_idx++;
315         }
316       else
317         {
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
321           // numbers are up.
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.
325
326           // These two positions are relative to the page (with positive numbers being
327           // down).
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;
331
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)
337             {
338               Grob *staff = elements_[i].staves[staff_idx];
339               staff->set_property ("system-Y-offset", scm_from_double (-system_position));
340
341               if (is_spaceable (staff))
342                 {
343                   // this is relative to the system: negative numbers are down.
344                   translation = system_position - solution_[spring_idx];
345                   spring_idx++;
346
347                   // Lay out any non-spaceable lines between this line and
348                   // the last one.
349                   if (loose_lines.size ())
350                     {
351                       loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
352                       loose_lines.push_back (staff);
353
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 ();
358                     }
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;
362
363                   staff->translate_axis (translation, Y_AXIS);
364                   found_spaceable_staff = true;
365                 }
366               else
367                 {
368                   if (loose_lines.empty ())
369                     loose_lines.push_back (last_spaceable_line);
370
371                   loose_lines.push_back (staff);
372                   if (staff_idx)
373                     loose_line_min_distances.push_back (min_offsets[staff_idx-1] - min_offsets[staff_idx]);
374                   else
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);
378                 }
379             }
380
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)
385             spring_idx++;
386
387           *tail = scm_cons (scm_from_double (system_position), SCM_EOL);
388           tail = SCM_CDRLOC (*tail);
389         }
390     }
391
392   if (loose_lines.size ())
393     {
394       loose_line_min_distances.push_back (0); // FIXME: should be the min distance to the footer.
395       loose_lines.push_back (0);
396
397       distribute_loose_lines (loose_lines, loose_line_min_distances,
398                               last_spaceable_line_translation, -page_height_);
399
400     }
401
402   assert (spring_idx == solution_.size () - 1);
403   return system_offsets;
404 }
405
406 // Given two lines that are already spaced (the first and last
407 // elements of loose_lines), distribute some unspaced lines between
408 // them.
409 void
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)
413 {
414   Simple_spacer spacer;
415   for (vsize i = 0; i + 1 < loose_lines.size (); ++i)
416     {
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]);
421
422       if ((spec == SCM_BOOL_F && loose_lines[0] && loose_lines.back ())
423           || !loose_lines[i]
424           || !loose_lines[i+1])
425         {
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);
432         }
433
434       spacer.add_spring (spring);
435     }
436
437   // Remember: offsets are decreasing, since we're going from UP to DOWN!
438   spacer.solve (first_translation - last_translation, false);
439
440   vector<Real> solution = spacer.spring_positions ();
441   for (vsize i = 1; i + 1 < solution.size (); ++i)
442     {
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);
445     }
446 }
447
448 SCM
449 Page_layout_problem::solution (bool ragged)
450 {
451   solve_rod_spring_problem (ragged);
452   return find_system_offsets ();
453 }
454
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.
461 //
462 // The upper skyline is relative to the top staff; the lower skyline is relative to
463 // the bottom staff.
464 void
465 Page_layout_problem::build_system_skyline (vector<Grob*> const& staves,
466                                            vector<Real> const& minimum_translations,
467                                            Skyline *up,
468                                            Skyline *down)
469 {
470   if (minimum_translations.empty ())
471     return;
472
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;
478
479   for (vsize i = 0; i < staves.size (); ++i)
480     {
481       Real dy = minimum_translations[i] - first_translation;
482       Grob *g = staves[i];
483       Skyline_pair *sky = Skyline_pair::unsmob (g->get_property ("vertical-skylines"));
484       if (sky)
485         {
486           up->raise (-dy);
487           up->merge ((*sky)[UP]);
488           up->raise (dy);
489
490           down->raise (-dy);
491           down->merge ((*sky)[DOWN]);
492           down->raise (dy);
493         }
494       if (is_spaceable (staves[i]))
495         {
496           if (!found_spaceable_staff)
497             {
498               found_spaceable_staff = true;
499               first_spaceable_dy = dy;
500             }
501           last_spaceable_dy = dy;
502         }
503     }
504
505   // Leave the up skyline at a position relative
506   // to the top spaceable staff.
507   up->raise (-first_spaceable_dy);
508
509   // Leave the down skyline at a position
510   // relative to the bottom spaceable staff.
511   down->raise (-last_spaceable_dy);
512 }
513
514 Interval
515 Page_layout_problem::prob_extent (Prob *p)
516 {
517   Stencil *sten = unsmob_stencil (p->get_property ("stencil"));
518   return sten ? sten->extent (Y_AXIS) : Interval (0, 0);
519 }
520
521 Interval
522 Page_layout_problem::first_staff_extent (Element const& e)
523 {
524   if (e.prob)
525     return prob_extent (e.prob);
526   else if (e.staves.size ())
527     return e.staves[0]->extent (e.staves[0], Y_AXIS);
528
529   return Interval (0, 0);
530 }
531
532 Interval
533 Page_layout_problem::last_staff_extent (Element const& e)
534 {
535   if (e.prob)
536     return prob_extent (e.prob);
537   else if (e.staves.size ())
538     return e.staves.back ()->extent (e.staves.back (), Y_AXIS);
539
540   return Interval (0, 0);
541 }
542
543 SCM
544 Page_layout_problem::get_details (Element const& elt)
545 {
546   if (elt.staves.empty ())
547     return SCM_EOL;
548
549   return get_details (elt.staves.back ()->get_system ());
550 }
551
552 SCM
553 Page_layout_problem::get_details (Grob *g)
554 {
555   Grob *left_bound = dynamic_cast<Spanner*> (g)->get_bound (LEFT);
556   return left_bound->get_property ("line-break-system-details");
557 }
558
559 bool
560 Page_layout_problem::is_spaceable (Grob *g)
561 {
562   return !scm_is_number (g->get_property ("staff-affinity"));
563 }
564
565 void
566 Page_layout_problem::mark_as_spaceable (Grob *g)
567 {
568   g->set_property ("staff-affinity", SCM_BOOL_F);
569 }
570
571 bool
572 Page_layout_problem::read_spacing_spec (SCM spec, Real* dest, SCM sym)
573 {
574   SCM pair = scm_sloppy_assq (sym, spec);
575   if (scm_is_pair (pair) && scm_is_number (scm_cdr (pair)))
576     {
577       *dest = scm_to_double (scm_cdr (pair));
578       return true;
579     }
580   return false;
581 }
582
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).
586 SCM
587 Page_layout_problem::get_spacing_spec (Grob *before, Grob *after)
588 {
589   if (!before || !after)
590     return SCM_BOOL_F;
591
592   if (is_spaceable (before))
593     {
594       if (is_spaceable (after))
595         return before->get_property ("next-staff-spacing");
596       else
597         {
598           Direction affinity = to_dir (after->get_property ("staff-affinity"));
599           return (affinity == DOWN) ? SCM_BOOL_F : after->get_property ("inter-staff-spacing");
600         }
601     }
602   else
603     {
604       if (is_spaceable (after))
605         {
606           Direction affinity = to_dir (before->get_property ("staff-affinity"));
607           return (affinity == UP) ? SCM_BOOL_F : before->get_property ("inter-staff-spacing");
608         }
609       else
610         {
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)
614             {
615               warning (_ ("staff-affinities should only decrease"));
616               after_affinity = before_affinity;
617             }
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");
622         }
623     }
624   return SCM_BOOL_F;
625 }
626
627 void
628 Page_layout_problem::alter_spring_from_spacing_spec (SCM spec, Spring* spring)
629 {
630   Real space;
631   Real stretch;
632   Real min_dist;
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 ();
638
639   if (read_spacing_spec (spec, &stretch, ly_symbol2scm ("stretchability")))
640     {
641       spring->set_inverse_stretch_strength (stretch);
642       spring->set_inverse_compress_strength (stretch);
643     }
644 }
645
646 vector<Grob*>
647 Page_layout_problem::filter_dead_elements (vector<Grob*> const& input)
648 {
649   vector<Grob*> output;
650   for (vsize i = 0; i < input.size (); ++i)
651     {
652       if (Hara_kiri_group_spanner::has_interface (input[i]))
653         Hara_kiri_group_spanner::consider_suicide (input[i]);
654
655       if (input[i]->is_live ())
656         output.push_back (input[i]);
657     }
658
659   return output;
660 }