]> git.donarmstrong.com Git - lilypond.git/blob - lily/ledger-line-spanner.cc
* lily/ledger-line-engraver.cc: new file.
[lilypond.git] / lily / ledger-line-spanner.cc
1 /* 
2   ledger-line-spanner.cc --  implement Ledger_line_spanner
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   
8 */
9
10 #include <map>
11
12 #include "item.hh" 
13 #include "note-head.hh" 
14 #include "staff-symbol-referencer.hh" 
15 #include "staff-symbol.hh" 
16 #include "lookup.hh" 
17 #include "spanner.hh" 
18 #include "group-interface.hh" 
19 #include "paper-column.hh"
20 #include "ambitus.hh"
21
22 struct Ledger_line_spanner
23 {
24   DECLARE_SCHEME_CALLBACK (print, (SCM ));
25   static Stencil brew_ledger_lines (Grob *me,
26                                     int pos,
27                                     int interspaces,
28                                     Real, Real,
29                                     Interval x_extent,
30                                     Real left_shorten);
31
32   static bool has_interface (Grob*);
33 };
34
35
36 Stencil
37 Ledger_line_spanner::brew_ledger_lines (Grob *staff,
38                                         int pos,
39                                         int interspaces,
40                                         Real halfspace,
41                                         Real ledgerlinethickness,
42                                         Interval x_extent,
43                                         Real left_shorten)
44 {
45   int line_count = ((abs (pos) < interspaces)
46                     ? 0
47                     : (abs (pos) - interspaces) / 2);
48   Stencil stencil;
49   if (line_count)
50     {
51       Real blotdiameter = ledgerlinethickness;
52       Interval y_extent =
53         Interval (-0.5*(ledgerlinethickness),
54                   +0.5*(ledgerlinethickness));
55       Stencil proto_ledger_line =
56         Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
57
58       x_extent[LEFT] += left_shorten;
59       Stencil proto_first_line =
60         Lookup::round_filled_box (Box (x_extent, y_extent), blotdiameter);
61
62       Direction dir = (Direction)sign (pos);
63       Real offs = (Staff_symbol_referencer::on_staffline (staff, pos))
64         ? 0.0
65         : -dir * halfspace;
66
67
68       offs += pos * halfspace;
69       for (int i = 0; i < line_count; i++)
70         {
71           Stencil ledger_line ((i == 0) 
72                                 ? proto_first_line
73                                 : proto_ledger_line
74                                 );
75           ledger_line.translate_axis (-dir * halfspace * i * 2 + offs, Y_AXIS);
76           stencil.add_stencil (ledger_line);
77         }
78     }
79
80   return stencil;
81 }
82
83
84 struct Ledger_request
85 {
86   Interval ledger_extent_;
87   Interval head_extent_;
88   int position_;
89   bool excentric_;
90   Ledger_request () {
91     ledger_extent_.set_empty ();
92     head_extent_.set_empty ();
93     position_ = 0;
94   }
95 };
96
97 typedef std::map<int, Drul_array<Ledger_request> > Ledger_requests;
98
99
100 /*
101   TODO: ledger share a lot of info. Lots of room to optimize away common
102   use of objects/variables.
103  */
104 MAKE_SCHEME_CALLBACK (Ledger_line_spanner,print,1);
105 SCM
106 Ledger_line_spanner::print (SCM smob)
107 {
108   Spanner *me = dynamic_cast<Spanner*> (unsmob_grob (smob));
109   Link_array<Grob> heads (Pointer_group_interface__extract_grobs (me, (Grob*)0, "note-heads"));
110   Link_array<Grob> ambituses (Pointer_group_interface__extract_grobs (me, (Grob*)0, "ambituses"));
111
112   if (heads.is_empty () && ambituses.is_empty ())
113     return SCM_EOL;
114     
115   Stencil ledgers;
116   Stencil default_ledger;
117
118   Grob * common[NO_AXES];
119   
120   for (int i = X_AXIS;  i < NO_AXES; i++)
121     {
122       Axis a = Axis (i);
123       common[a] = common_refpoint_of_array (heads, me, a);
124       common[a] = common_refpoint_of_array (ambituses, common[a], a);
125       for (int i = heads.size (); i--; )
126         if (Grob * g = unsmob_grob (me->get_property ("accidental-grob")))
127           common[a] = common[a]->common_refpoint (g, a);
128     }
129
130   // find size of note heads.
131   Grob * staff = Staff_symbol_referencer::get_staff_symbol (me);
132   int interspaces = Staff_symbol::line_count (staff)-1;
133   Ledger_requests reqs;
134   Real length_fraction = 0.25;
135   for (int i = heads.size (); i--; )
136     {
137       Item *h = dynamic_cast<Item*> (heads[i]);
138       
139       int pos = Staff_symbol_referencer::get_rounded_position (h);
140       if (abs (pos) > interspaces)
141         {
142           Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
143           Interval ledger_extent = head_extent;
144           head_extent.widen (length_fraction * head_extent.length ());
145
146           Direction vdir = Direction (sign (pos));
147           int rank = Paper_column::get_rank (h->get_column ());
148           
149           reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
150           reqs[rank][vdir].head_extent_.unite (head_extent);
151           reqs[rank][vdir].position_ =
152             vdir * ((vdir* reqs[rank][vdir].position_) >? (vdir *pos));
153         }
154     }
155
156   // determine maximum size for non-colliding ledger.
157   Real gap = robust_scm2double (me->get_property ("gap"), 0.1);
158   Ledger_requests::iterator last (reqs.end ());
159   for (Ledger_requests::iterator i (reqs.begin ());
160        i != reqs.end (); last = i++)
161     {
162       if (last == reqs.end ())
163         {
164           continue;
165         }
166       
167       Direction d = DOWN;
168       do
169         {
170           if (abs (last->second[d].position_) > interspaces
171               && abs (i->second[d].position_) > interspaces)
172             {
173               Real center =  
174                 (last->second[d].head_extent_[RIGHT]
175                  + i->second[d].head_extent_[LEFT] )/2;
176
177               Direction which = LEFT;
178               do
179                 {
180                   Ledger_request &lr = ((which == LEFT) ? *last : *i).second[d];
181
182                   // due tilt of quarter note-heads
183                   bool both =
184                      (abs (last->second[d].position_) > interspaces + 1
185                       && abs (i->second[d].position_) > interspaces + 1);
186
187                   Real limit = (center + (both? which * gap/2 : 0));
188                   lr.ledger_extent_.elem_ref (-which)
189                     = which  * (which * lr.ledger_extent_[-which] >? which * limit);
190                 }
191               while (flip (&which) != LEFT); 
192             }
193         }
194       while (flip (&d) != DOWN); 
195     }
196
197   // create  ledgers for note heads
198   Real ledgerlinethickness =
199     Staff_symbol::get_ledger_line_thickness (staff);
200   Real halfspace = Staff_symbol::staff_space (me)/2;
201   for (int i = heads.size (); i--; )
202     {
203       Item *h = dynamic_cast<Item*> (heads[i]);
204       
205       int pos = Staff_symbol_referencer::get_rounded_position (h);
206       if (abs (pos) > interspaces + 1)
207         {
208           Interval ledger_size = h->extent (common[X_AXIS], X_AXIS);
209           ledger_size.widen (ledger_size.length ()* length_fraction);
210
211           Interval max_size = reqs[Paper_column::get_rank (h->get_column ())][Direction (sign(pos))].ledger_extent_;
212
213           ledger_size.intersect (max_size);
214           Real left_shorten =0.0;
215           if (Grob * g = unsmob_grob (h->get_property ("accidental-grob")))
216             {
217               Real d =
218                 linear_combination (Drul_array<Real> (h->extent (common[X_AXIS], X_AXIS)[LEFT],
219                                                       g->extent (common[X_AXIS], X_AXIS)[RIGHT]),
220                                 
221                                     0.5);
222
223               left_shorten =  (-ledger_size[LEFT] + d) >?  0 ;
224
225               /*
226                 TODO: shorten 2 ledger lines for the case natural +
227                 downstem.
228               */
229               
230             }
231
232           ledgers.add_stencil (brew_ledger_lines (staff, pos, interspaces,
233                                                   halfspace,
234                                                   ledgerlinethickness,
235                                                   ledger_size,
236                                                   left_shorten));
237         }
238     }
239
240   /* create  ledgers for ambitus.
241
242   TODO: split off separate function
243
244   */      
245   for (int i = ambituses.size (); i--; )
246     {
247       Item *a = dynamic_cast<Item*> (ambituses[i]);
248       Interval x_ext = ambituses[i]->extent (common[X_AXIS], X_AXIS);
249       x_ext.widen (length_fraction * x_ext.length ());
250       
251       Slice ps (Ambitus::get_positions (a));
252       Direction d = DOWN; 
253       do
254         {
255           if (abs (ps[d]) > interspaces + 1)
256             ledgers.add_stencil (brew_ledger_lines (staff, ps[d], interspaces,
257                                                     halfspace,
258                                                     ledgerlinethickness,
259                                                     x_ext, 0.0));
260         }
261       while (flip (&d) != DOWN);
262     }
263       
264   ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
265                           X_AXIS);
266   
267   return ledgers.smobbed_copy ();
268 }
269
270 ADD_INTERFACE (Ledger_line_spanner,
271                "ledger-line-interface",
272                "This spanner draws the ledger lines of a staff on note heads. ",
273                "note-heads thickness gap length minimum-length")