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