Add arrowed sharp signs
[lilypond.git] / lily / paper-column.cc
blobc6b7c9c85c1aec0f9f5f9957937bd2b785f6b495
1 /*
2 paper-column.cc -- implement Paper_column
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "paper-column.hh"
11 #include "axis-group-interface.hh"
12 #include "break-align-interface.hh"
13 #include "font-interface.hh"
14 #include "grob-array.hh"
15 #include "lookup.hh"
16 #include "lookup.hh"
17 #include "moment.hh"
18 #include "output-def.hh"
19 #include "paper-score.hh"
20 #include "pointer-group-interface.hh"
21 #include "rhythmic-head.hh"
22 #include "separation-item.hh"
23 #include "skyline-pair.hh"
24 #include "spaceable-grob.hh"
25 #include "spring.hh"
26 #include "string-convert.hh"
27 #include "system.hh"
28 #include "text-interface.hh"
29 #include "warn.hh"
31 Grob *
32 Paper_column::clone () const
34 return new Paper_column (*this);
37 void
38 Paper_column::do_break_processing ()
40 Item::do_break_processing ();
43 int
44 Paper_column::get_rank (Grob const *me)
46 return dynamic_cast<Paper_column const *> (me)->rank_;
49 System *
50 Paper_column::get_system () const
52 return system_;
55 void
56 Paper_column::set_system (System *s)
58 system_ = s;
61 Paper_column *
62 Paper_column::get_column () const
64 return (Paper_column *) (this);
67 Paper_column::Paper_column (SCM l)
68 : Item (l)
70 system_ = 0;
71 rank_ = -1;
74 Paper_column::Paper_column (Paper_column const &src)
75 : Item (src)
77 system_ = 0;
78 rank_ = src.rank_;
81 int
82 Paper_column::compare (Grob * const &a,
83 Grob * const &b)
85 return sign (dynamic_cast<Paper_column*> (a)->rank_
86 - dynamic_cast<Paper_column*> (b)->rank_);
89 bool
90 Paper_column::less_than (Grob *const &a,
91 Grob *const &b)
93 Paper_column *pa = dynamic_cast<Paper_column*> (a);
94 Paper_column *pb = dynamic_cast<Paper_column*> (b);
96 return pa->rank_ < pb->rank_;
99 Moment
100 Paper_column::when_mom (Grob *me)
102 SCM m = me->get_property ("when");
103 if (Moment *when = unsmob_moment (m))
104 return *when;
105 return Moment (0);
108 bool
109 Paper_column::is_musical (Grob *me)
111 SCM m = me->get_property ("shortest-starter-duration");
112 Moment s (0);
113 if (unsmob_moment (m))
114 s = *unsmob_moment (m);
115 return s != Moment (0);
118 bool
119 Paper_column::is_used (Grob *me)
121 extract_grob_set (me, "elements", elts);
122 if (elts.size ())
123 return true;
125 extract_grob_set (me, "bounded-by-me", bbm);
126 if (bbm.size ())
127 return true;
129 if (Paper_column::is_breakable (me))
130 return true;
132 if (to_boolean (me->get_property ("used")))
133 return true;
134 return false;
137 bool
138 Paper_column::is_breakable (Grob *me)
140 return scm_is_symbol (me->get_property ("line-break-permission"));
143 Real
144 Paper_column::minimum_distance (Grob *left, Grob *right)
146 Drul_array<Grob*> cols (left, right);
147 Drul_array<Skyline> skys = Drul_array<Skyline> (Skyline (RIGHT), Skyline (LEFT));
149 Direction d = LEFT;
152 Skyline_pair *sp = Skyline_pair::unsmob (cols[d]->get_property ("horizontal-skylines"));
153 if (sp)
154 skys[d] = (*sp)[-d];
156 while (flip (&d) != LEFT);
158 skys[RIGHT].merge (Separation_item::conditional_skyline (right, left));
160 return max (0.0, skys[LEFT].distance (skys[RIGHT]));
163 Interval
164 Paper_column::break_align_width (Grob *me)
166 Grob *p = me->get_parent (X_AXIS);
168 if (is_musical (me))
170 me->programming_error ("tried to get break-align-width of a non-musical column");
171 return Interval (0, 0) + me->relative_coordinate (p, X_AXIS);
174 Grob *align = Pointer_group_interface::find_grob (me, ly_symbol2scm ("elements"),
175 Break_alignment_interface::has_interface);
176 if (!align)
177 return Interval (0, 0) + me->relative_coordinate (p, X_AXIS);
179 return align->extent (p, X_AXIS);
183 Print a vertical line and the rank number, to aid debugging.
185 MAKE_SCHEME_CALLBACK (Paper_column, print, 1);
187 Paper_column::print (SCM p)
189 Paper_column *me = dynamic_cast<Paper_column*> (unsmob_grob (p));
191 string r = to_string (Paper_column::get_rank (me));
193 Moment *mom = unsmob_moment (me->get_property ("when"));
194 string when = mom ? mom->to_string () : "?/?";
196 Font_metric *musfont = Font_interface::get_default_font (me);
197 SCM properties = Font_interface::text_font_alist_chain (me);
199 SCM scm_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
200 properties,
201 ly_string2scm (r));
202 SCM when_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
203 properties,
204 ly_string2scm (when));
205 Stencil t = *unsmob_stencil (scm_mol);
206 t.add_at_edge (Y_AXIS, DOWN, *unsmob_stencil (when_mol), 0.1);
207 t.align_to (X_AXIS, CENTER);
208 t.align_to (Y_AXIS, DOWN);
210 Stencil l = Lookup::filled_box (Box (Interval (-0.01, 0.01),
211 Interval (-2, -1)));
213 SCM small_letters = scm_cons (scm_acons (ly_symbol2scm ("font-size"),
214 scm_from_int (-6), SCM_EOL),
215 properties);
217 int j = 0;
218 for (SCM s = me->get_object ("ideal-distances");
219 scm_is_pair (s); s = scm_cdr (s))
221 Spring *sp = unsmob_spring (scm_caar (s));
222 if (!unsmob_grob (scm_cdar (s))
223 || !unsmob_grob (scm_cdar (s))->get_system ())
224 continue;
226 j++;
227 Real y = -j * 1 -3;
228 vector<Offset> pts;
229 pts.push_back (Offset (0, y));
231 Offset p2 (sp->distance (), y);
232 pts.push_back (p2);
234 Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
235 Stencil head (musfont->find_by_name ("arrowheads.open.01"));
237 SCM distance_stc = Text_interface::interpret_markup (me->layout ()->self_scm (),
238 small_letters,
239 ly_string2scm (String_convert::form_string ("%5.2lf", sp->distance ())));
241 id_stencil.add_stencil (unsmob_stencil (distance_stc)->translated (Offset (sp->distance ()/3, y+1)));
242 id_stencil.add_stencil (head.translated (p2));
243 id_stencil = id_stencil.in_color (0,0,1);
244 l.add_stencil (id_stencil);
247 for (SCM s = me->get_object ("minimum-distances");
248 scm_is_pair (s); s = scm_cdr (s))
250 Real dist = scm_to_double (scm_cdar (s));
251 Grob *other = unsmob_grob (scm_caar (s));
252 if (!other || other->get_system () != me->get_system ())
253 continue;
255 j++;
257 Real y = -j * 1.0 -3.5;
258 vector<Offset> pts;
259 pts.push_back (Offset (0, y));
261 Offset p2 (dist, y);
262 pts.push_back (p2);
264 Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
265 Stencil head (musfont->find_by_name ("arrowheads.open.0M1"));
266 head.translate_axis (y, Y_AXIS);
267 id_stencil.add_stencil (head);
269 SCM distance_stc = Text_interface::interpret_markup (me->layout ()->self_scm (),
270 small_letters,
271 ly_string2scm (String_convert::form_string ("%5.2lf",
272 dist)));
274 id_stencil.add_stencil (unsmob_stencil (distance_stc)->translated (Offset (dist/3, y-1)));
277 id_stencil = id_stencil.in_color (1,0,0);
278 l.add_stencil (id_stencil);
280 t.add_stencil (l);
281 return t.smobbed_copy ();
285 This is all too hairy. We use bounded-by-me to make sure that some
286 columns are kept "alive". Unfortunately, when spanners are suicided,
287 this falls apart again, because suicided spanners are still in
288 bounded-by-me
290 THIS IS BROKEN KLUDGE. WE SHOULD INVENT SOMETHING BETTER.
292 MAKE_SCHEME_CALLBACK (Paper_column, before_line_breaking, 1);
294 Paper_column::before_line_breaking (SCM grob)
296 Grob *me = unsmob_grob (grob);
298 SCM bbm = me->get_object ("bounded-by-me");
299 Grob_array *ga = unsmob_grob_array (bbm);
300 if (!ga)
301 return SCM_UNSPECIFIED;
303 vector<Grob*> &array (ga->array_reference ());
305 for (vsize i = array.size (); i--;)
307 Grob *g = array[i];
309 if (!g || !g->is_live ())
310 /* UGH . potentially quadratic. */
311 array.erase (array.begin () + i);
314 return SCM_UNSPECIFIED;
317 /* FIXME: This is a hack that we use to identify columns that used to
318 contain note-heads but whose note-heads were moved by one of the ligature
319 engravers. Once the ligature engravers are fixed to behave nicely, this
320 function can be removed.
322 bool
323 Paper_column::is_extraneous_column_from_ligature (Grob *me)
325 if (!is_musical (me))
326 return false;
328 // If all the note-heads that I think are my children actually belong
329 // to another column, then I am extraneous.
330 extract_grob_set (me, "elements", elts);
331 bool has_notehead = false;
332 for (vsize i = 0; i < elts.size (); i++)
334 if (Rhythmic_head::has_interface (elts[i]))
336 has_notehead = true;
337 if (dynamic_cast<Item*> (elts[i])->get_column () == me)
338 return false;
341 return has_notehead;
345 ADD_INTERFACE (Paper_column,
346 "@code{Paper_column} objects form the top-most X@tie{}parents"
347 " for items. There are two types of columns: musical columns,"
348 " where are attached to, and non-musical columns, where"
349 " bar-lines, clefs, etc., are attached to. The spacing engine"
350 " determines the X@tie{}positions of these objects.\n"
351 "\n"
352 "They are numbered, the first (leftmost) is column@tie{}0."
353 " Numbering happens before line breaking, and columns are not"
354 " renumbered after line breaking. Since many columns go"
355 " unused, you should only use the rank field to get ordering"
356 " information. Two adjacent columns may have non-adjacent"
357 " numbers.",
359 /* properties */
360 "between-cols "
361 "bounded-by-me "
362 "grace-spacing "
363 "labels "
364 "line-break-system-details "
365 "line-break-penalty "
366 "line-break-permission "
367 "page-break-penalty "
368 "page-break-permission "
369 "page-turn-penalty "
370 "page-turn-permission "
371 "rhythmic-location "
372 "shortest-playing-duration "
373 "shortest-starter-duration "
374 "spacing "
375 "used "
376 "when "