2 break-align-interface.cc -- implement Break_alignment_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "break-align-interface.hh"
12 #include "align-interface.hh"
13 #include "axis-group-interface.hh"
14 #include "dimensions.hh"
15 #include "international.hh"
16 #include "output-def.hh"
17 #include "paper-column.hh"
18 #include "pointer-group-interface.hh"
19 #include "self-alignment-interface.hh"
20 #include "side-position-interface.hh"
26 This is tricky: we cannot modify 'elements, since callers are
27 iterating the same list. Reordering the list in-place, or resetting
28 'elements will skip elements in the loops of callers.
30 So we return the correct order as an array.
33 Break_alignment_interface::break_align_order (Item
*me
)
35 SCM order_vec
= me
->get_property ("break-align-orders");
36 if (!scm_is_vector (order_vec
)
37 || scm_c_vector_length (order_vec
) < 3)
40 SCM order
= scm_vector_ref (order_vec
,
41 scm_from_int (me
->break_status_dir () + 1));
49 Break_alignment_interface::ordered_elements (Grob
*grob
)
51 Item
*me
= dynamic_cast<Item
*> (grob
);
52 extract_grob_set (me
, "elements", elts
);
55 SCM order
= break_align_order (me
);
57 if (order
== SCM_BOOL_F
)
60 vector
<Grob
*> writable_elts (elts
);
62 Copy in order specified in BREAK-ALIGN-ORDER.
64 vector
<Grob
*> new_elts
;
65 for (; scm_is_pair (order
); order
= scm_cdr (order
))
67 SCM sym
= scm_car (order
);
69 for (vsize i
= writable_elts
.size (); i
--;)
71 Grob
*g
= writable_elts
[i
];
72 if (g
&& sym
== g
->get_property ("break-align-symbol"))
74 new_elts
.push_back (g
);
75 writable_elts
.erase (writable_elts
.begin () + i
);
84 Break_alignment_interface::add_element (Grob
*me
, Grob
*toadd
)
86 Align_interface::add_element (me
, toadd
);
89 MAKE_SCHEME_CALLBACK (Break_alignment_interface
, calc_positioning_done
, 1)
91 Break_alignment_interface::calc_positioning_done (SCM smob
)
93 Grob
*grob
= unsmob_grob (smob
);
94 Item
*me
= dynamic_cast<Item
*> (grob
);
97 me
->set_property ("positioning-done", SCM_BOOL_T
);
99 vector
<Grob
*> elems
= ordered_elements (me
);
100 vector
<Interval
> extents
;
102 int last_nonempty
= -1;
103 for (vsize i
= 0; i
< elems
.size (); i
++)
105 Interval y
= elems
[i
]->extent (elems
[i
], X_AXIS
);
106 extents
.push_back (y
);
112 while (idx
< extents
.size () && extents
[idx
].is_empty ())
115 vector
<Real
> offsets
;
116 offsets
.resize (elems
.size ());
117 for (vsize i
= 0; i
< offsets
.size ();i
++)
120 Real extra_right_space
= 0.0;
121 vsize edge_idx
= VPOS
;
122 while (idx
< elems
.size ())
124 vsize next_idx
= idx
+ 1;
125 while (next_idx
< elems
.size ()
126 && extents
[next_idx
].is_empty ())
129 Grob
*l
= elems
[idx
];
132 if (next_idx
< elems
.size ())
138 Find the first grob with a space-alist entry.
140 extract_grob_set (l
, "elements", elts
);
142 for (vsize i
= elts
.size (); i
--;)
147 && (elt
->get_property ("break-align-symbol")
148 == ly_symbol2scm ("left-edge")))
151 SCM l
= elt
->get_property ("space-alist");
159 SCM rsym
= r
? SCM_EOL
: ly_symbol2scm ("right-edge");
162 We used to use #'cause to find out the symbol and the spacing
163 table, but that gets icky when that grob is suicided for some
168 extract_grob_set (r
, "elements", elts
);
169 for (vsize i
= elts
.size ();
170 !scm_is_symbol (rsym
) && i
--;)
173 rsym
= elt
->get_property ("break-align-symbol");
177 if (rsym
== ly_symbol2scm ("left-edge"))
181 if (scm_is_symbol (rsym
))
182 entry
= scm_assq (rsym
, alist
);
184 bool entry_found
= scm_is_pair (entry
);
188 if (scm_is_symbol (rsym
))
189 sym_string
= ly_symbol2string (rsym
);
192 if (unsmob_grob (l
->get_property ("cause")))
193 orig_string
= unsmob_grob (l
->get_property ("cause"))->name ();
195 programming_error (_f ("No spacing entry from %s to `%s'",
196 orig_string
.c_str (),
197 sym_string
.c_str ()));
201 SCM type
= ly_symbol2scm ("extra-space");
205 entry
= scm_cdr (entry
);
207 distance
= scm_to_double (scm_cdr (entry
));
208 type
= scm_car (entry
);
213 if (type
== ly_symbol2scm ("extra-space"))
214 offsets
[next_idx
] = extents
[idx
][RIGHT
] + distance
215 - extents
[next_idx
][LEFT
];
216 /* should probably junk minimum-space */
217 else if (type
== ly_symbol2scm ("minimum-space"))
218 offsets
[next_idx
] = max (extents
[idx
][RIGHT
], distance
);
222 extra_right_space
= distance
;
223 if (idx
+ 1 < offsets
.size ())
224 offsets
[idx
+1] = extents
[idx
][RIGHT
] + distance
;
231 Interval total_extent
;
233 Real alignment_off
= 0.0;
234 for (vsize i
= 0; i
< offsets
.size (); i
++)
238 alignment_off
= -here
;
239 total_extent
.unite (extents
[i
] + here
);
242 if (total_extent
.is_empty ())
245 if (me
->break_status_dir () == LEFT
)
246 alignment_off
= -total_extent
[RIGHT
] - extra_right_space
;
247 else if (edge_idx
== VPOS
)
248 alignment_off
= -total_extent
[LEFT
];
250 here
= alignment_off
;
251 for (vsize i
= 0; i
< offsets
.size (); i
++)
254 elems
[i
]->translate_axis (here
, X_AXIS
);
262 MAKE_SCHEME_CALLBACK (Break_alignable_interface
, self_align_callback
, 1)
264 Break_alignable_interface::self_align_callback (SCM grob
)
266 Grob
*me
= unsmob_grob (grob
);
267 Item
*alignment
= dynamic_cast<Item
*> (me
->get_parent (X_AXIS
));
268 if (!Break_alignment_interface::has_interface (alignment
))
269 return scm_from_int (0);
271 SCM symbol_list
= me
->get_property ("break-align-symbols");
272 vector
<Grob
*> elements
= Break_alignment_interface::ordered_elements (alignment
);
273 if (elements
.size () == 0)
274 return scm_from_int (0);
276 int break_aligned_grob
= -1;
277 for (; scm_is_pair (symbol_list
); symbol_list
= scm_cdr (symbol_list
))
279 SCM sym
= scm_car (symbol_list
);
280 for (vsize i
= 0; i
< elements
.size (); i
++)
282 if (elements
[i
]->get_property ("break-align-symbol") == sym
)
284 if (Item::break_visible (elements
[i
])
285 && !elements
[i
]->extent (elements
[i
], X_AXIS
).is_empty ())
287 break_aligned_grob
= i
;
288 goto found_break_aligned_grob
; /* ugh. need to break out of 2 loops */
290 else if (break_aligned_grob
== -1)
291 break_aligned_grob
= i
;
296 found_break_aligned_grob
:
297 if (break_aligned_grob
== -1)
298 return scm_from_int (0);
300 Grob
*alignment_parent
= elements
[break_aligned_grob
];
301 Grob
*common
= me
->common_refpoint (alignment_parent
, X_AXIS
);
302 Real anchor
= robust_scm2double (alignment_parent
->get_property ("break-align-anchor"), 0);
304 return scm_from_double (alignment_parent
->relative_coordinate (common
, X_AXIS
)
305 - me
->relative_coordinate (common
, X_AXIS
)
309 MAKE_SCHEME_CALLBACK (Break_aligned_interface
, calc_average_anchor
, 1)
311 Break_aligned_interface::calc_average_anchor (SCM grob
)
313 Grob
*me
= unsmob_grob (grob
);
317 /* average the anchors of those children that have it set */
318 extract_grob_set (me
, "elements", elts
);
319 for (vsize i
= 0; i
< elts
.size (); i
++)
321 SCM anchor
= elts
[i
]->get_property ("break-align-anchor");
322 if (scm_is_number (anchor
))
325 avg
+= scm_to_double (anchor
);
329 return scm_from_double (count
> 0 ? avg
/ count
: 0);
332 MAKE_SCHEME_CALLBACK (Break_aligned_interface
, calc_extent_aligned_anchor
, 1)
334 Break_aligned_interface::calc_extent_aligned_anchor (SCM smob
)
336 Grob
*me
= unsmob_grob (smob
);
337 Real alignment
= robust_scm2double (me
->get_property ("break-align-anchor-alignment"), 0.0);
338 Interval iv
= me
->extent (me
, X_AXIS
);
340 if (isinf (iv
[LEFT
]) && isinf (iv
[RIGHT
])) /* avoid NaN */
341 return scm_from_double (0.0);
343 return scm_from_double (iv
.linear_combination (alignment
));
346 MAKE_SCHEME_CALLBACK (Break_aligned_interface
, calc_break_visibility
, 1)
348 Break_aligned_interface::calc_break_visibility (SCM smob
)
350 /* a BreakAlignGroup is break-visible iff it has one element that is break-visible */
351 Grob
*me
= unsmob_grob (smob
);
352 SCM ret
= scm_c_make_vector (3, SCM_EOL
);
353 extract_grob_set (me
, "elements", elts
);
354 for (int dir
= 0; dir
<= 2; dir
++)
356 bool visible
= false;
357 for (vsize i
= 0; i
< elts
.size (); i
++)
359 SCM vis
= elts
[i
]->get_property ("break-visibility");
360 if (scm_is_vector (vis
) && to_boolean (scm_c_vector_ref (vis
, dir
)))
363 scm_c_vector_set_x (ret
, dir
, scm_from_bool (visible
));
368 ADD_INTERFACE (Break_alignable_interface
,
369 "Object that is aligned on a break aligment.",
372 "break-align-symbols "
375 ADD_INTERFACE (Break_aligned_interface
,
376 "Items that are aligned in prefatory matter.\n"
378 "The spacing of these items is controlled by the"
379 " @code{space-alist} property. It contains a list"
380 " @code{break-align-symbol}s with a specification of the"
381 " associated space. The space specification can be\n"
384 "@item (minimum-space . @var{spc}))\n"
385 "Pad space until the distance is @var{spc}.\n"
386 "@item (fixed-space . @var{spc})\n"
387 "Set a fixed space.\n"
388 "@item (semi-fixed-space . @var{spc})\n"
389 "Set a space. Half of it is fixed and half is stretchable."
390 " (does not work at start of line. fixme)\n"
391 "@item (extra-space . @var{spc})\n"
392 "Add @var{spc} amount of space.\n"
395 "Special keys for the alist are @code{first-note} and"
396 " @code{next-note}, signifying the first note on a line, and"
397 " the next note halfway a line.\n"
399 "Rules for this spacing are much more complicated than this."
400 " See [Wanske] page 126--134, [Ross] page 143--147.",
403 "break-align-anchor "
404 "break-align-anchor-alignment "
405 "break-align-symbol "
409 ADD_INTERFACE (Break_alignment_interface
,
410 "The object that performs break aligment. See"
411 " @ref{break-aligned-interface}.",
415 "break-align-orders "