]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column.cc
Fix 464.
[lilypond.git] / lily / paper-column.cc
1 /*
2   paper-column.cc -- implement Paper_column
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "paper-column.hh"
10
11 #include "break-align-interface.hh"
12 #include "moment.hh"
13 #include "paper-score.hh"
14 #include "warn.hh"
15 #include "axis-group-interface.hh"
16 #include "spaceable-grob.hh"
17 #include "text-interface.hh"
18 #include "lookup.hh"
19 #include "font-interface.hh"
20 #include "output-def.hh"
21 #include "pointer-group-interface.hh"
22 #include "grob-array.hh"
23 #include "system.hh"
24 #include "spring.hh"
25 #include "lookup.hh"
26 #include "separation-item.hh"
27 #include "string-convert.hh"
28
29 Grob *
30 Paper_column::clone () const
31 {
32   return new Paper_column (*this);
33 }
34
35 void
36 Paper_column::do_break_processing ()
37 {
38   Item::do_break_processing ();
39 }
40
41 int
42 Paper_column::get_rank (Grob const *me)
43 {
44   return dynamic_cast<Paper_column const *> (me)->rank_;
45 }
46
47 System *
48 Paper_column::get_system () const
49 {
50   return system_;
51 }
52
53 void
54 Paper_column::set_system (System *s)
55 {
56   system_ = s;
57 }
58
59 Paper_column *
60 Paper_column::get_column () const
61 {
62   return (Paper_column *) (this);
63 }
64
65 Paper_column::Paper_column (SCM l)
66   : Item (l)
67 {
68   system_ = 0;
69   rank_ = -1;
70 }
71
72 Paper_column::Paper_column (Paper_column const &src)
73   : Item (src)
74 {
75   system_ = 0;
76   rank_ = src.rank_;
77 }
78
79 int
80 Paper_column::compare (Grob * const &a,
81                        Grob * const &b)
82 {
83   return sign (dynamic_cast<Paper_column*> (a)->rank_
84                - dynamic_cast<Paper_column*> (b)->rank_);
85 }
86
87 bool
88 Paper_column::less_than (Grob *const &a,
89                          Grob *const &b)
90 {
91   Paper_column *pa = dynamic_cast<Paper_column*> (a);
92   Paper_column *pb = dynamic_cast<Paper_column*> (b);
93   
94   return pa->rank_ < pb->rank_;
95 }
96
97 Moment
98 Paper_column::when_mom (Grob *me)
99 {
100   SCM m = me->get_property ("when");
101   if (Moment *when = unsmob_moment (m))
102     return *when;
103   return Moment (0);
104 }
105
106 bool
107 Paper_column::is_musical (Grob *me)
108 {
109   SCM m = me->get_property ("shortest-starter-duration");
110   Moment s (0);
111   if (unsmob_moment (m))
112     s = *unsmob_moment (m);
113   return s != Moment (0);
114 }
115
116 bool
117 Paper_column::is_used (Grob *me)
118 {
119   extract_grob_set (me, "elements", elts);
120   if (elts.size ())
121     return true;
122
123   extract_grob_set (me, "bounded-by-me", bbm);
124   if (bbm.size ())
125     return true;
126   
127   if (Paper_column::is_breakable (me))
128     return true;
129
130   if (to_boolean (me->get_property ("used")))
131     return true;
132   return false;
133 }
134
135 bool
136 Paper_column::is_breakable (Grob *me)
137 {
138   return scm_is_symbol (me->get_property ("line-break-permission"));
139 }
140
141 Real
142 Paper_column::minimum_distance (Grob *left, Grob *right)
143 {
144   Drul_array<Grob*> cols (left, right);
145   Drul_array<Skyline> skys = Drul_array<Skyline> (Skyline (RIGHT), Skyline (LEFT));
146
147   Direction d = LEFT;
148   do
149     {
150       Skyline_pair *sp = Skyline_pair::unsmob (cols[d]->get_property ("horizontal-skylines"));
151       if (sp)
152         skys[d] = (*sp)[-d];
153     }
154   while (flip (&d) != LEFT);
155
156   skys[RIGHT].merge (Separation_item::conditional_skyline (right, left));
157
158   return max (0.0, skys[LEFT].distance (skys[RIGHT]));
159 }
160
161 Interval
162 Paper_column::break_align_width (Grob *me)
163 {
164   if (is_musical (me))
165     {
166       me->programming_error ("tried to get break-align-width of a non-musical column");
167       return Interval (0, 0);
168     }
169
170   Grob *align = Pointer_group_interface::find_grob (me, ly_symbol2scm ("elements"),
171                                                     Break_alignment_interface::has_interface);
172   if (!align)
173     return Interval (0, 0);
174
175   return align->extent (me->get_parent (X_AXIS), X_AXIS);
176 }
177
178 /*
179   Print a vertical line and  the rank number, to aid debugging.
180 */
181 MAKE_SCHEME_CALLBACK (Paper_column, print, 1);
182 SCM
183 Paper_column::print (SCM p)
184 {
185   Paper_column *me = dynamic_cast<Paper_column*> (unsmob_grob (p));
186
187   string r = to_string (Paper_column::get_rank (me));
188
189   Moment *mom = unsmob_moment (me->get_property ("when"));
190   string when = mom ? mom->to_string () : "?/?";
191
192   Font_metric *musfont = Font_interface::get_default_font (me);
193   SCM properties = Font_interface::text_font_alist_chain (me);
194
195   SCM scm_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
196                                                   properties,
197                                                   ly_string2scm (r));
198   SCM when_mol = Text_interface::interpret_markup (me->layout ()->self_scm (),
199                                                    properties,
200                                                    ly_string2scm (when));
201   Stencil t = *unsmob_stencil (scm_mol);
202   t.add_at_edge (Y_AXIS, DOWN, *unsmob_stencil (when_mol), 0.1);
203   t.align_to (X_AXIS, CENTER);
204   t.align_to (Y_AXIS, DOWN);
205
206   Stencil l = Lookup::filled_box (Box (Interval (-0.01, 0.01),
207                                        Interval (-2, -1)));
208   
209   SCM small_letters = scm_cons (scm_acons (ly_symbol2scm ("font-size"),
210                                            scm_from_int (-6), SCM_EOL),
211                                 properties);
212   
213   int j = 0;
214   for (SCM s = me->get_object ("ideal-distances");
215        scm_is_pair (s); s = scm_cdr (s))
216     {
217       Spring *sp = unsmob_spring (scm_caar (s));
218       if (!unsmob_grob (scm_cdar (s))
219           || !unsmob_grob (scm_cdar (s))->get_system ())
220         continue;
221       
222       j++;
223       Real y = -j * 1 -3;
224       vector<Offset> pts;
225       pts.push_back (Offset (0, y));
226
227       Offset p2 (sp->distance (), y);
228       pts.push_back (p2);
229       
230       Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
231       Stencil head (musfont->find_by_name ("arrowheads.open.01"));
232
233       SCM distance_stc = Text_interface::interpret_markup (me->layout ()->self_scm (),
234                                                            small_letters,
235                                                            ly_string2scm (String_convert::form_string ("%5.2lf", sp->distance ())));
236       
237       id_stencil.add_stencil (unsmob_stencil (distance_stc)->translated (Offset (sp->distance ()/3, y+1)));
238       id_stencil.add_stencil (head.translated (p2));
239       id_stencil = id_stencil.in_color (0,0,1);
240       l.add_stencil (id_stencil);
241     }
242    
243   for (SCM s = me->get_object ("minimum-distances");
244        scm_is_pair (s); s = scm_cdr (s))
245     {
246       Real dist = scm_to_double (scm_cdar (s));
247       Grob *other =  unsmob_grob (scm_caar (s));
248       if (!other || other->get_system () != me->get_system ())
249         continue;
250
251       j++;
252       
253       Real y = -j * 1.0 -3.5;
254       vector<Offset> pts;
255       pts.push_back (Offset (0, y));
256
257       Offset p2 (dist, y);
258       pts.push_back (p2);
259
260       Stencil id_stencil = Lookup::points_to_line_stencil (0.1, pts);
261       Stencil head (musfont->find_by_name ("arrowheads.open.0M1"));
262       head.translate_axis (y, Y_AXIS);
263       id_stencil.add_stencil (head);
264
265       SCM distance_stc = Text_interface::interpret_markup (me->layout ()->self_scm (),
266                                                            small_letters,
267                                                            ly_string2scm (String_convert::form_string ("%5.2lf",
268                                                                                                        dist)));
269           
270       id_stencil.add_stencil (unsmob_stencil (distance_stc)->translated (Offset (dist/3, y-1)));
271  
272        
273       id_stencil = id_stencil.in_color (1,0,0);
274       l.add_stencil (id_stencil);
275     }
276   t.add_stencil (l);
277   return t.smobbed_copy ();
278 }
279
280 /*
281   This is all too hairy. We use bounded-by-me to make sure that some
282   columns are kept "alive". Unfortunately, when spanners are suicided,
283   this falls apart again, because suicided spanners are still in
284   bounded-by-me
285
286   THIS IS BROKEN KLUDGE. WE SHOULD INVENT SOMETHING BETTER.
287 */
288 MAKE_SCHEME_CALLBACK (Paper_column, before_line_breaking, 1);
289 SCM
290 Paper_column::before_line_breaking (SCM grob)
291 {
292   Grob *me = unsmob_grob (grob);
293
294   SCM bbm = me->get_object ("bounded-by-me");
295   Grob_array *ga = unsmob_grob_array (bbm);
296   if (!ga)
297     return SCM_UNSPECIFIED;
298
299   vector<Grob*> &array (ga->array_reference ());
300
301   for (vsize i = array.size (); i--;)
302     {
303       Grob *g = array[i];
304
305       if (!g || !g->is_live ())
306         /* UGH . potentially quadratic. */
307         array.erase (array.begin () + i);
308     }
309
310   return SCM_UNSPECIFIED;
311 }
312
313
314 ADD_INTERFACE (Paper_column,
315                "@code{Paper_column} objects form the top-most X-parents for items."
316                "  The are two types of columns: musical columns, where are attached to, and "
317                "  non-musical columns, where bar-lines, clefs etc. are attached to. "
318                "  The spacing engine determines the X-positions of these objects."
319                
320                "\n\n"
321                "They are\n"
322                "  numbered, the first (leftmost) is column 0. Numbering happens before\n"
323                "  line-breaking, and columns are not renumbered after line breaking.\n"
324                "  Since many columns go unused, you should only use the rank field to\n"
325                "  get ordering information.  Two adjacent columns may have\n"
326                "  non-adjacent numbers.\n",
327                
328
329                /* properties */
330                "between-cols "
331                "bounded-by-me "
332                "grace-spacing "
333                "labels "
334                "line-break-system-details "
335                "line-break-penalty "
336                "line-break-permission "
337                "page-break-penalty "
338                "page-break-permission "
339                "page-turn-penalty "
340                "page-turn-permission "
341                "rhythmic-location "
342                "shortest-playing-duration "
343                "shortest-starter-duration "
344                "spacing "
345                "used "
346                "when ");
347