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