]> git.donarmstrong.com Git - lilypond.git/blob - lily/separation-item.cc
Merge branch 'lilypond/translation' of ssh://jomand@git.sv.gnu.org/srv/git/lilypond...
[lilypond.git] / lily / separation-item.cc
1 /*
2   separation-item.cc -- implement Separation_item
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "separation-item.hh"
10
11 #include "axis-group-interface.hh"
12 #include "lookup.hh"
13 #include "note-head.hh"
14 #include "stencil.hh"
15 #include "skyline.hh"
16 #include "paper-column.hh"
17 #include "warn.hh"
18 #include "pointer-group-interface.hh"
19 #include "accidental-placement.hh"
20
21 void
22 Separation_item::add_item (Grob *s, Item *i)
23 {
24   assert (i);
25   Pointer_group_interface::add_grob (s, ly_symbol2scm ("elements"), i);
26 }
27
28 void
29 Separation_item::add_conditional_item (Grob *me, Grob *e)
30 {
31   Pointer_group_interface::add_grob (me, ly_symbol2scm ("conditional-elements"), e);
32 }
33
34 Real
35 Separation_item::set_distance (Item *l, Item *r, Real padding)
36 {
37   Drul_array<Skyline_pair*> lines (Skyline_pair::unsmob (l->get_property ("horizontal-skylines")),
38                                    Skyline_pair::unsmob (r->get_property ("horizontal-skylines")));
39   Skyline right = conditional_skyline (r, l);
40   right.merge ((*lines[RIGHT])[LEFT]);
41   
42   Real dist = padding + (*lines[LEFT])[RIGHT].distance (right);
43   if (dist > 0)
44     {
45       Rod rod;
46
47       rod.item_drul_ = Drul_array<Item*> (l, r);
48
49       rod.distance_ = dist;
50       rod.add_to_cols ();
51     }
52
53   return max (dist, 0.0);
54 }
55
56 bool
57 Separation_item::is_empty (Grob *me)
58 {
59   Skyline_pair *sky = Skyline_pair::unsmob (me->get_property ("horizontal-skylines"));
60   return (!sky || sky->is_empty ());
61 }
62
63 /*
64   Return the width of ME given that we are considering the object on
65   the LEFT.
66 */
67 Skyline
68 Separation_item::conditional_skyline (Grob *me, Grob *left)
69 {
70   vector<Box> bs = boxes (me, left);
71   return Skyline (bs, 0.1, Y_AXIS, LEFT);
72 }
73
74
75 MAKE_SCHEME_CALLBACK (Separation_item, calc_skylines,1);
76 SCM
77 Separation_item::calc_skylines (SCM smob)
78 {
79   Item *me = unsmob_item (smob);
80   vector<Box> bs = boxes (me, 0);
81   /* todo: the horizon_padding is somewhat arbitrary */
82   return Skyline_pair (bs, 0.1, Y_AXIS).smobbed_copy ();
83 }
84
85 /* if left is non-NULL, get the boxes corresponding to the
86    conditional-elements (conditioned on the grob LEFT). This
87    sounds more general than it is: conditional-elements are
88    always accidentals attached to a tied note.
89 */
90 vector<Box>
91 Separation_item::boxes (Grob *me, Grob *left)
92 {
93   Item *item = dynamic_cast<Item *> (me);
94
95   int very_large = INT_MAX;
96   Paper_column *pc = item->get_column ();
97   vector<Box> out;
98   extract_grob_set (me, left ? "conditional-elements" : "elements", read_only_elts);
99   vector<Grob*> elts;
100
101   if (left)
102     elts = Accidental_placement::get_relevant_accidentals (read_only_elts, left);
103   else
104     elts = read_only_elts;
105
106   Grob *ycommon = common_refpoint_of_array (elts, me, Y_AXIS);
107   
108   for (vsize i = 0; i < elts.size (); i++)
109     {
110       Item *il = dynamic_cast<Item *> (elts[i]);
111       if (pc != il->get_column ())
112         continue;
113
114       /* ugh. We want to exclude groups of grobs (so that we insert each grob
115          individually into the skyline instead of adding a single box that
116          bounds all of them). However, we can't exclude an axis-group that
117          adds to its childrens' stencil. Currently, this is just TrillPitchGroup;
118          hence the check for note-head-interface. */
119       if (Axis_group_interface::has_interface (il)
120           && !Note_head::has_interface (il))
121         continue;
122
123       Interval y (il->pure_height (ycommon, 0, very_large));
124       Interval x (il->extent (pc, X_AXIS));
125
126       Interval extra = robust_scm2interval (elts[i]->get_property ("extra-spacing-width"),
127                                             Interval (-0.1, 0.1));
128       x[LEFT] += extra[LEFT];
129       x[RIGHT] += extra[RIGHT];
130       if (to_boolean (elts[i]->get_property ("infinite-spacing-height")))
131         y = Interval (-infinity_f, infinity_f);
132  
133       if (!x.is_empty () && !y.is_empty ())
134       out.push_back (Box (x, y));
135     }
136
137   return out;      
138 }
139
140 extern bool debug_skylines;
141 MAKE_SCHEME_CALLBACK (Separation_item, print, 1)
142 SCM
143 Separation_item::print (SCM smob)
144 {
145   if (!debug_skylines)
146     return SCM_BOOL_F;
147
148   Grob *me = unsmob_grob (smob);
149   Stencil ret;
150   if (Skyline_pair *s = Skyline_pair::unsmob (me->get_property ("horizontal-skylines")))
151     {
152       ret.add_stencil (Lookup::points_to_line_stencil (0.1, (*s)[LEFT].to_points (Y_AXIS)).in_color (255, 255, 0));
153       ret.add_stencil (Lookup::points_to_line_stencil (0.1, (*s)[RIGHT].to_points (Y_AXIS)).in_color (0, 255, 255));
154     }
155   return ret.smobbed_copy ();
156 }
157
158 ADD_INTERFACE (Separation_item,
159                "Item that computes widths to generate spacing rods.",
160
161                /* properties */
162                "X-extent "
163                "conditional-elements "
164                "elements "
165                "padding "
166                "horizontal-skylines "
167                );