]> git.donarmstrong.com Git - lilypond.git/blob - lily/constrained-breaking.cc
0a185bf39d00e3b1be3ef34647ace171f0a138aa
[lilypond.git] / lily / constrained-breaking.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2011 Joe Neeman <joeneeman@gmail.com>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "constrained-breaking.hh"
21
22 #include "international.hh"
23 #include "main.hh"
24 #include "output-def.hh"
25 #include "page-layout-problem.hh"
26 #include "paper-column.hh"
27 #include "paper-score.hh"
28 #include "simple-spacer.hh"
29 #include "system.hh"
30 #include "warn.hh"
31
32 /*
33   We use the following optimal substructure. Let W (A) be our weight function.
34
35   Let A_{k, n} = (a_{k, n, 1}, ... a_{k, n, k}) be the optimal set of line breaks
36   for k systems and n potential breakpoints. a_{k, n, k} = n (it is the end of
37   the piece)
38
39   Then A_{k+1, m} is contructed from
40   min_ {k < j < m} ( W (A_{k, j} :: m) )
41   where by A::m we denote appending m to the list A
42
43   Indices in the code:
44
45   The above algorithm makes it easy to end at a point before the end of the
46   score (just find A_{k, m} for some m < breaks_.size () - 1). However, we must
47   add information for starting at a point after the beginning. One constructor
48   allows the specification of a list of starting columns, start_. We then have
49   start_.size () different solution arrays. state_[i] is the array for the
50   solution starting at column number start_[i].
51
52   The indices "start" and "end" refer to the index in the start_ array of the
53   desired starting and ending columns.
54
55   each solution array looks like
56    a_{1,1,1} a_{2,1,2} a_{3,1,3} . . .
57        X     a_{2,2,2} a_{3,2,3} . . .
58        X         X     a_{3,3,3} . . .
59        .         .         .     .
60        .         .         .       .
61   where the X's mark invalid solutions (can't have more systems than
62   breakpoints). Note that each value is of the form a_{x, n, x}. This is because
63   a breakpoint of the form a_{x, n, x-1} will also be called a_{x-1, m, x-1} for
64   some m < n. Each cell in the array stores the value of its m (ie. the
65   ending breakpoint of the previous line) as "prev_".
66
67   For finding A_{sys, brk}, let "me" be the (sys_count, brk) cell in our
68   solution array (state_[start][sys * rank + brk]).
69
70   Then A_{sys, brk} = A_{sys - 1, me.prev_} :: me
71 */
72
73 /*
74   start and sys here are indexed from 0.
75   brk is indexed from starting_breakpoints_[start]
76   (for brk, starting_breakpoints_[start] is the beginning
77   of the piece; the smallest value we should ever see here is
78   starting_breakpoints_[start] + 1) */
79 bool
80 Constrained_breaking::calc_subproblem (vsize start, vsize sys, vsize brk)
81 {
82   assert (sys < systems_);
83   assert (start < start_.size ());
84   assert (brk < breaks_.size ());
85
86   bool found_something = false;
87   vsize start_col = starting_breakpoints_[start];
88   Matrix<Constrained_break_node> &st = state_[start];
89   vsize max_index = brk - start_col;
90   for (vsize j = max_index; j-- > sys;)
91     {
92       if (0 == sys && j > 0)
93         continue; /* the first line cannot have its first break after the beginning */
94
95       Line_details const &cur = lines_.at (brk, j + start_col);
96       if (isinf (cur.force_))
97         break;
98
99       Real prev_f = 0;
100       Real prev_dem = 0;
101
102       if (sys > 0)
103         {
104           prev_f = st.at (j, sys - 1).details_.force_;
105           prev_dem = st.at (j, sys - 1).demerits_;
106         }
107       if (isinf (prev_dem))
108         continue;
109
110       Real dem = combine_demerits (cur.force_, prev_f) + prev_dem + cur.break_penalty_;
111       Constrained_break_node &n = st.at (max_index, sys);
112       if (dem < n.demerits_)
113         {
114           found_something = true;
115           n.demerits_ = dem;
116           n.details_ = cur;
117           n.prev_ = j;
118         }
119     }
120   return found_something;
121 }
122
123 Column_x_positions
124 Constrained_breaking::space_line (vsize i, vsize j)
125 {
126   bool ragged_right = to_boolean (pscore_->layout ()->c_variable ("ragged-right"));
127   bool ragged_last = to_boolean (pscore_->layout ()->c_variable ("ragged-last"));
128
129   vector<Grob *> line (all_.begin () + breaks_[i],
130                        all_.begin () + breaks_[j] + 1);
131   Interval line_dims = line_dimensions_int (pscore_->layout (), i);
132   bool last = j == breaks_.size () - 1;
133   bool ragged = ragged_right || (last && ragged_last);
134
135   /* As a special case, if there is only one line in the score and ragged-right
136      hasn't been specifically forbidden and the line is stretched, use
137      ragged spacing. */
138   if (last && i == 0
139       && lines_.at (i, j).force_ >= 0
140       && !scm_is_bool (pscore_->layout ()->c_variable ("ragged-right"))
141       && !scm_is_bool (pscore_->layout ()->c_variable ("ragged-last")))
142     ragged = true;
143
144   return get_line_configuration (line, line_dims[RIGHT] - line_dims[LEFT], line_dims[LEFT], ragged);
145 }
146
147 void
148 Constrained_breaking::resize (vsize systems)
149 {
150   systems_ = systems;
151
152   if (pscore_ && systems_ > valid_systems_)
153     {
154       for (vsize i = 0; i < state_.size (); i++)
155         state_[i].resize (breaks_.size () - starting_breakpoints_[i], systems_, Constrained_break_node ());
156
157       /* fill out the matrices */
158       for (vsize i = 0; i < state_.size (); i++)
159         for (vsize j = valid_systems_; j < systems_; j++)
160           for (vsize k = starting_breakpoints_[i] + j + 1; k < breaks_.size (); k++)
161             if (!calc_subproblem (i, j, k))
162               break; /* if we couldn't break this, it is too cramped already */
163       valid_systems_ = systems_;
164     }
165 }
166
167 vector<Column_x_positions>
168 Constrained_breaking::solve (vsize start, vsize end, vsize sys_count)
169 {
170   vsize start_brk = starting_breakpoints_[start];
171   vsize end_brk = prepare_solution (start, end, sys_count);
172
173   Matrix<Constrained_break_node> const &st = state_[start];
174   vector<Column_x_positions> ret;
175
176   /* find the first solution that satisfies constraints */
177   for (vsize sys = sys_count - 1; sys != VPOS; sys--)
178     {
179       for (vsize brk = end_brk; brk != VPOS; brk--)
180         {
181           if (!isinf (st.at (brk, sys).details_.force_))
182             {
183               if (brk != end_brk)
184                 {
185                   brk = st.at (brk, sys).prev_;
186                   sys--;
187                   warning (_ ("cannot find line breaking that satisfies constraints"));
188                   ret.push_back (space_line (brk, end_brk));
189                 }
190
191               /* build up the good part of the solution */
192               for (vsize cur_sys = sys; cur_sys != VPOS; cur_sys--)
193                 {
194                   vsize prev_brk = st.at (brk, cur_sys).prev_;
195                   assert (brk != VPOS);
196                   ret.push_back (space_line (prev_brk + start_brk, brk + start_brk));
197                   brk = prev_brk;
198                 }
199               reverse (ret);
200               return ret;
201             }
202         }
203     }
204   /* if we get to here, just put everything on one line */
205   warning (_ ("cannot find line breaking that satisfies constraints"));
206   ret.push_back (space_line (0, end_brk));
207   return ret;
208 }
209
210 vector<Column_x_positions>
211 Constrained_breaking::best_solution (vsize start, vsize end)
212 {
213   vsize min_systems = min_system_count (start, end);
214   vsize max_systems = max_system_count (start, end);
215   Real best_demerits = infinity_f;
216   vector<Column_x_positions> best_so_far;
217
218   for (vsize i = min_systems; i <= max_systems; i++)
219     {
220       vsize brk = prepare_solution (start, end, i);
221       Real dem = state_[start].at (brk, i - 1).demerits_;
222
223       if (dem < best_demerits)
224         {
225           best_demerits = dem;
226           best_so_far = solve (start, end, i);
227         }
228       else
229         {
230           vector<Column_x_positions> cur = solve (start, end, i);
231           bool too_many_lines = true;
232
233           for (vsize j = 0; j < cur.size (); j++)
234             if (cur[j].force_ < 0)
235               {
236                 too_many_lines = false;
237                 break;
238               }
239           if (too_many_lines)
240             return best_so_far;
241         }
242     }
243   if (best_so_far.size ())
244     return best_so_far;
245   return solve (start, end, max_systems);
246 }
247
248 std::vector<Line_details>
249 Constrained_breaking::line_details (vsize start, vsize end, vsize sys_count)
250 {
251   vsize end_brk = prepare_solution (start, end, sys_count);
252   Matrix<Constrained_break_node> const &st = state_[start];
253   vector<Line_details> ret;
254
255   /* This loop structure is C&Ped from solve(). */
256   /* find the first solution that satisfies constraints */
257   for (vsize sys = sys_count - 1; sys != VPOS; sys--)
258     {
259       for (vsize brk = end_brk; brk != VPOS; brk--)
260         {
261           if (!isinf (st.at (brk, sys).details_.force_))
262             {
263               if (brk != end_brk)
264                 {
265                   /*
266                     During initialize(), we only fill out a
267                     Line_details for lines that are valid (ie. not too
268                     long), otherwise line breaking becomes O(n^3).
269                     In case sys_count is such that no valid solution
270                     is found, we need to fill in the Line_details.
271                   */
272                   Line_details details;
273                   brk = st.at (brk, sys).prev_;
274                   sys--;
275                   fill_line_details (&details, brk, end_brk);
276                   ret.push_back (details);
277                 }
278
279               /* build up the good part of the solution */
280               for (vsize cur_sys = sys; cur_sys != VPOS; cur_sys--)
281                 {
282                   vsize prev_brk = st.at (brk, cur_sys).prev_;
283                   assert (brk != VPOS);
284                   ret.push_back (st.at (brk, cur_sys).details_);
285                   brk = prev_brk;
286                 }
287               reverse (ret);
288               return ret;
289             }
290         }
291     }
292
293   /* if we get to here, just put everything on one line */
294   Line_details details;
295   fill_line_details (&details, 0, end_brk);
296   ret.push_back (details);
297   return ret;
298 }
299
300 int
301 Constrained_breaking::min_system_count (vsize start, vsize end)
302 {
303   vsize sys_count;
304   vsize brk = prepare_solution (start, end, 1);
305   vsize rank = breaks_.size () - starting_breakpoints_[start];
306   Matrix<Constrained_break_node> const &st = state_[start];
307
308   /* sys_count < rank : rank is the # of breakpoints, we can't have more systems */
309   for (sys_count = 0; sys_count < rank; sys_count++)
310     {
311       if (sys_count >= valid_systems_)
312         {
313           resize (sys_count + 3);
314         }
315       if (!isinf (st.at (brk, sys_count).details_.force_))
316         return sys_count + 1;
317     }
318   /* no possible breaks satisfy constraints */
319   return 1;
320 }
321
322 int
323 Constrained_breaking::max_system_count (vsize start, vsize end)
324 {
325   vsize brk = (end >= start_.size ()) ? breaks_.size () - 1 : starting_breakpoints_[end];
326   return brk - starting_breakpoints_[start];
327 }
328
329 vsize
330 Constrained_breaking::prepare_solution (vsize start, vsize end, vsize sys_count)
331 {
332   assert (start < start_.size () && (end == VPOS || end <= start_.size ()));
333   assert (start < end);
334
335   resize (sys_count);
336   if (end == start_.size ())
337     end = VPOS;
338
339   vsize brk;
340   brk = end == VPOS ? breaks_.size () - 1 : starting_breakpoints_[end];
341   brk -= starting_breakpoints_[start];
342   return brk;
343 }
344
345 Constrained_breaking::Constrained_breaking (Paper_score *ps)
346 {
347   valid_systems_ = systems_ = 0;
348   start_.push_back (0);
349   pscore_ = ps;
350   initialize ();
351 }
352
353 Constrained_breaking::Constrained_breaking (Paper_score *ps, vector<vsize> const &start)
354   : start_ (start)
355 {
356   valid_systems_ = systems_ = 0;
357   pscore_ = ps;
358   initialize ();
359 }
360
361 static SCM
362 min_permission (SCM perm1, SCM perm2)
363 {
364   if (perm1 == ly_symbol2scm ("force"))
365     return perm2;
366   if (perm1 == ly_symbol2scm ("allow")
367       && perm2 != ly_symbol2scm ("force"))
368     return perm2;
369   return SCM_EOL;
370 }
371
372 /* find the forces for all possible lines and cache ragged_ and ragged_right_ */
373 void
374 Constrained_breaking::initialize ()
375 {
376   if (!pscore_)
377     return;
378
379   ragged_right_ = to_boolean (pscore_->layout ()->c_variable ("ragged-right"));
380   ragged_last_ = to_boolean (pscore_->layout ()->c_variable ("ragged-last"));
381   system_system_space_ = 0;
382   system_markup_space_ = 0;
383   system_system_padding_ = 0;
384   system_system_min_distance_ = 0;
385   score_system_padding_ = 0;
386   score_system_min_distance_ = 0;
387   score_markup_padding_ = 0;
388   score_markup_min_distance_ = 0;
389
390   Output_def *l = pscore_->layout ();
391
392   SCM spacing_spec = l->c_variable ("system-system-spacing");
393   SCM between_scores_spec = l->c_variable ("score-system-spacing");
394   SCM title_spec = l->c_variable ("score-markup-spacing");
395   SCM page_breaking_spacing_spec = l->c_variable ("page-breaking-system-system-spacing");
396
397   Page_layout_problem::read_spacing_spec (spacing_spec,
398                                           &system_system_space_,
399                                           ly_symbol2scm ("basic-distance"));
400   Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
401                                           &system_system_space_,
402                                           ly_symbol2scm ("basic-distance"));
403   Page_layout_problem::read_spacing_spec (title_spec,
404                                           &system_markup_space_,
405                                           ly_symbol2scm ("basic-distance"));
406
407   Page_layout_problem::read_spacing_spec (spacing_spec,
408                                           &system_system_padding_,
409                                           ly_symbol2scm ("padding"));
410   Page_layout_problem::read_spacing_spec (between_scores_spec,
411                                           &score_system_padding_,
412                                           ly_symbol2scm ("padding"));
413   Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
414                                           &system_system_padding_,
415                                           ly_symbol2scm ("padding"));
416   Page_layout_problem::read_spacing_spec (title_spec,
417                                           &score_markup_padding_,
418                                           ly_symbol2scm ("padding"));
419
420   Page_layout_problem::read_spacing_spec (between_scores_spec,
421                                           &score_system_min_distance_,
422                                           ly_symbol2scm ("minimum-distance"));
423   Page_layout_problem::read_spacing_spec (spacing_spec,
424                                           &system_system_min_distance_,
425                                           ly_symbol2scm ("minimum-distance"));
426   Page_layout_problem::read_spacing_spec (page_breaking_spacing_spec,
427                                           &system_system_min_distance_,
428                                           ly_symbol2scm ("minimum-distance"));
429   Page_layout_problem::read_spacing_spec (title_spec,
430                                           &score_markup_min_distance_,
431                                           ly_symbol2scm ("minimum-distance"));
432
433   Interval first_line = line_dimensions_int (pscore_->layout (), 0);
434   Interval other_lines = line_dimensions_int (pscore_->layout (), 1);
435   /* do all the rod/spring problems */
436   breaks_ = pscore_->get_break_indices ();
437   all_ = pscore_->root_system ()->used_columns ();
438   lines_.resize (breaks_.size (), breaks_.size (), Line_details ());
439   vector<Simple_spacer> spacers
440     = pscore_->root_system ()->get_simple_spacers (other_lines.length (),
441                                                    other_lines.length () - first_line.length (),
442                                                    ragged_right_);
443   for (vsize i = 0; i + 1 < breaks_.size (); i++)
444     {
445       for (vsize j = i + 1; j < breaks_.size (); j++)
446         {
447           bool last = j == breaks_.size () - 1;
448           bool ragged = ragged_right_ || (last && ragged_last_);
449           Line_details &line = lines_.at (j, i);
450
451           line.force_ = spacers[i * breaks_.size () + j].force_penalty (ragged_right_);
452           if (!spacers[i * breaks_.size () + j].fits ())
453             {
454               if (spacers[i * breaks_.size () + j].minimal_)
455                 line.force_ = -200000;
456               else
457                 line.force_ = infinity_f;
458             }
459           if (ragged && last && !isinf (line.force_))
460             line.force_ = (line.force_ < 0 && j > i + 1) ? infinity_f : 0;
461           if (!line.force_ && !spacers[i * breaks_.size () + j].line_len ())
462             line.force_ = infinity_f;
463           if (isinf (line.force_))
464             break;
465
466           fill_line_details (&line, i, j);
467         }
468     }
469
470   /* work out all the starting indices */
471   for (vsize i = 0; i < start_.size (); i++)
472     {
473       vsize j;
474       for (j = 0; j + 1 < breaks_.size () && breaks_[j] < start_[i]; j++)
475         ;
476       starting_breakpoints_.push_back (j);
477       start_[i] = breaks_[j];
478     }
479   state_.resize (start_.size ());
480 }
481
482 /*
483   Fills out all of the information contained in a Line_details,
484   except for information about horizontal spacing.
485 */
486 void
487 Constrained_breaking::fill_line_details (Line_details *const out, vsize start, vsize end)
488 {
489   int start_rank = Paper_column::get_rank (all_[breaks_[start]]);
490   int end_rank = Paper_column::get_rank (all_[breaks_[end]]);
491   System *sys = pscore_->root_system ();
492   Interval begin_of_line_extent = sys->begin_of_line_pure_height (start_rank, end_rank);
493   Interval rest_of_line_extent = sys->rest_of_line_pure_height (start_rank, end_rank);
494   bool last = (end == breaks_.size () - 1);
495
496   Grob *c = all_[breaks_[end]];
497   out->last_column_ = c;
498   out->break_penalty_ = robust_scm2double (c->get_property ("line-break-penalty"), 0);
499   out->page_penalty_ = robust_scm2double (c->get_property ("page-break-penalty"), 0);
500   out->turn_penalty_ = robust_scm2double (c->get_property ("page-turn-penalty"), 0);
501   out->break_permission_ = c->get_property ("line-break-permission");
502   out->page_permission_ = c->get_property ("page-break-permission");
503   out->turn_permission_ = c->get_property ("page-turn-permission");
504
505   /* turn permission should always be stricter than page permission
506      and page permission should always be stricter than line permission */
507   out->page_permission_ = min_permission (out->break_permission_,
508                                           out->page_permission_);
509   out->turn_permission_ = min_permission (out->page_permission_,
510                                           out->turn_permission_);
511
512   begin_of_line_extent = (begin_of_line_extent.is_empty ()
513                           || isnan (begin_of_line_extent[LEFT])
514                           || isnan (begin_of_line_extent[RIGHT]))
515                          ? Interval (0, 0) : begin_of_line_extent;
516   rest_of_line_extent = (rest_of_line_extent.is_empty ()
517                          || isnan (rest_of_line_extent[LEFT])
518                          || isnan (rest_of_line_extent[RIGHT]))
519                         ? Interval (0, 0) : rest_of_line_extent;
520   out->shape_ = Line_shape (begin_of_line_extent, rest_of_line_extent);
521   out->padding_ = last ? score_system_padding_ : system_system_padding_;
522   out->title_padding_ = score_markup_padding_;
523   out->min_distance_ = last ? score_system_min_distance_ : system_system_min_distance_;
524   out->title_min_distance_ = score_markup_min_distance_;
525   out->space_ = system_system_space_;
526   out->title_space_ = system_markup_space_;
527   out->inverse_hooke_ = out->full_height () + system_system_space_;
528
529   out->footnote_heights_ = sys->get_footnote_heights_in_range (start_rank, end_rank);
530   out->in_note_heights_ = sys->get_in_note_heights_in_range (start_rank, end_rank);
531
532   out->refpoint_extent_ = sys->pure_refpoint_extent (start_rank, end_rank);
533   if (out->refpoint_extent_.is_empty ())
534     out->refpoint_extent_ = Interval (0, 0);
535 }
536
537 Real
538 Constrained_breaking::combine_demerits (Real force, Real prev_force)
539 {
540   if (ragged_right_)
541     return force * force;
542
543   return force * force + (prev_force - force) * (prev_force - force);
544 }
545
546 Line_details::Line_details (Prob *pb, Output_def *paper)
547 {
548   SCM spec = paper->c_variable ("markup-system-spacing");
549   SCM title_spec = paper->c_variable ("markup-markup-spacing");
550   padding_ = 0;
551   title_padding_ = 0;
552   min_distance_ = 0;
553   title_min_distance_ = 0;
554   space_ = 0;
555   title_space_ = 0;
556   Page_layout_problem::read_spacing_spec (spec, &space_, ly_symbol2scm ("basic-distance"));
557   Page_layout_problem::read_spacing_spec (title_spec, &title_space_, ly_symbol2scm ("basic-distance"));
558   Page_layout_problem::read_spacing_spec (spec, &padding_, ly_symbol2scm ("padding"));
559   Page_layout_problem::read_spacing_spec (title_spec, &title_padding_, ly_symbol2scm ("padding"));
560   Page_layout_problem::read_spacing_spec (spec, &min_distance_, ly_symbol2scm ("minimum-distance"));
561   Page_layout_problem::read_spacing_spec (title_spec, &title_min_distance_, ly_symbol2scm ("minimum-distance"));
562
563   SCM footnotes = pb->get_property ("footnotes");
564
565   if (scm_is_pair (footnotes))
566     for (SCM s = footnotes; scm_is_pair (s); s = scm_cdr (s))
567       {
568         Stencil *sten = unsmob_stencil (scm_caddar (s));
569         if (!sten)
570           {
571             programming_error ("expecting stencil, got empty pointer");
572             continue;
573           }
574         footnote_heights_.push_back (sten->extent (Y_AXIS).length ());
575       }
576
577   last_column_ = 0;
578   force_ = 0;
579   Interval stencil_extent = unsmob_stencil (pb->get_property ("stencil"))->extent (Y_AXIS);
580   shape_ = Line_shape (stencil_extent, stencil_extent); // pretend it goes all the way across
581   tallness_ = 0;
582   bottom_padding_ = 0;
583   inverse_hooke_ = 1.0;
584   break_permission_ = ly_symbol2scm ("allow");
585   page_permission_ = pb->get_property ("page-break-permission");
586   turn_permission_ = pb->get_property ("page-turn-permission");
587   break_penalty_ = 0;
588   page_penalty_ = robust_scm2double (pb->get_property ("page-break-penalty"), 0);
589   turn_penalty_ = robust_scm2double (pb->get_property ("page-turn-penalty"), 0);
590   title_ = to_boolean (pb->get_property ("is-title"));
591   compressed_lines_count_ = 1;
592   compressed_nontitle_lines_count_ = title_ ? 0 : 1;
593   SCM last_scm = pb->get_property ("last-markup-line");
594   last_markup_line_ = to_boolean (last_scm);
595   SCM first_scm = pb->get_property ("first-markup-line");
596   first_markup_line_ = to_boolean (first_scm);
597   tight_spacing_ = to_boolean (pb->get_property ("tight-spacing"));
598   refpoint_extent_ = Interval (0, 0);
599 }
600
601 Real
602 Line_details::full_height () const
603 {
604   Interval ret;
605   ret.unite (shape_.begin_);
606   ret.unite (shape_.rest_);
607   return ret.length ();
608 }
609
610 Real
611 Line_details::tallness () const
612 {
613   return tallness_;
614 }
615
616 Real
617 Line_details::spring_length (Line_details const &next_line) const
618 {
619   // space_ measures the spring which goes from the bottom refpoint
620   // of this to the top refpoint of next_line. We want to return
621   // the stretchable space between the bottom of this's extent to
622   // the top of next_line's extent.
623   Real refpoint_dist = tallness_ + refpoint_extent_[DOWN] - next_line.refpoint_extent_[UP];
624   Real space = next_line.title_ ? title_space_ : space_;
625   return max (0.0, space - refpoint_dist);
626 }
627
628 Line_shape::Line_shape (Interval begin, Interval rest)
629 {
630   begin_ = begin;
631   rest_ = rest;
632 }
633
634 Line_shape
635 Line_shape::piggyback (Line_shape mount, Real padding) const
636 {
637   Real elevation = max (begin_[UP] - mount.begin_[DOWN], rest_[UP] - mount.rest_[DOWN]);
638   Interval begin = Interval (begin_[DOWN], elevation + mount.begin_[UP] + padding);
639   Interval rest = Interval (rest_[DOWN], elevation + mount.rest_[UP] + padding);
640   return Line_shape (begin, rest);
641 }