]> 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
21 struct Ledger_line_spanner
22 {
23   DECLARE_SCHEME_CALLBACK (print, (SCM ));
24   static Stencil brew_ledger_lines (Grob *me,
25                              int pos,
26                              int interspaces,
27                              Interval x_extent,
28                              Real left_shorten);
29
30   static bool has_interface (Grob*);
31 };
32
33
34 Stencil
35 Ledger_line_spanner::brew_ledger_lines (Grob *me,
36                                           int pos,
37                                           int interspaces,
38                                           Interval x_extent,
39                                           Real left_shorten)
40 {
41   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
42   Real inter_f = Staff_symbol_referencer::staff_space (me)/2;
43   int line_count = ((abs (pos) < interspaces)
44                     ? 0
45                     : (abs (pos) - interspaces) / 2);
46   Stencil stencil;
47   if (line_count)
48     {
49       Real ledgerlinethickness =
50         Staff_symbol::get_ledger_line_thickness (staff);
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 (me, pos))
64         ? 0.0
65         : -dir * inter_f;
66
67
68       offs += pos * inter_f;
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 * inter_f * 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
111   Stencil ledgers;
112   Stencil default_ledger;
113
114   Grob * common[NO_AXES];
115   for (int i = X_AXIS;  i < NO_AXES; i++)
116     common[Axis (i)] = common_refpoint_of_array (heads, me, Axis(i));
117
118   int interspaces = Staff_symbol_referencer::line_count (me)-1;
119   Ledger_requests reqs;
120   Real length_fraction = 0.25  * 2;
121   for (int i = heads.size (); i--; )
122     {
123       Item *h = dynamic_cast<Item*> (heads[i]);
124       
125       int pos = Staff_symbol_referencer::get_rounded_position (h);
126       if (abs (pos) > interspaces + 1)
127         {
128           Interval head_extent = h->extent (common[X_AXIS], X_AXIS);
129           Interval ledger_extent = Interval (head_extent.linear_combination (-1 - length_fraction),
130                                              head_extent.linear_combination (1 + length_fraction));
131
132           Direction vdir = Direction (sign (pos));
133           int rank = Paper_column::get_rank (h->get_column ());
134           
135           reqs[rank][vdir].ledger_extent_.unite (ledger_extent);
136           reqs[rank][vdir].head_extent_.unite (head_extent);
137           reqs[rank][vdir].position_ =
138             vdir * ((vdir* reqs[rank][vdir].position_) >? (vdir *pos));
139         }
140     }
141
142   Real gap = robust_scm2double (me->get_property ("gap"), 0.15);
143   Ledger_requests::iterator last (reqs.end ());
144   for (Ledger_requests::iterator i (reqs.begin ());
145        i != reqs.end (); last = i++)
146     {
147       if (last == reqs.end ())
148         {
149           continue;
150         }
151       
152       Direction d = DOWN;
153       do
154         {
155           if (abs (last->second[d].position_) > interspaces + 1
156               && abs (i->second[d].position_) > interspaces + 1)
157             {
158               Real center =  
159                 (last->second[d].head_extent_[RIGHT]
160                  + i->second[d].head_extent_[LEFT] )/2;
161
162               Direction which = LEFT;
163               do
164                 {
165                   Ledger_request &lr = ((which == LEFT) ? *last : *i).second[d];
166
167
168                   // due tilt of quarter note-heads 
169                   Real excentricity = 0; //.1;
170                   Real limit = (center + which * gap/2 + excentricity);
171                   lr.ledger_extent_.elem_ref (-which)
172                     = which  * (which * lr.ledger_extent_[-which] >? which * limit);
173                 }
174               while (flip (&which) != LEFT); 
175             }
176         }
177       while (flip (&d) != DOWN); 
178     }
179
180   for (Ledger_requests::const_iterator i (reqs.begin ());
181        i != reqs.end (); i++)
182     {
183       Direction d = DOWN;
184       do
185         {
186           Ledger_request lr = (*i).second[d];
187           ledgers.add_stencil (brew_ledger_lines (me,
188                                                   lr.position_,
189                                                   interspaces,
190                                                   lr.ledger_extent_,
191                                                   0.0));
192         }
193       while (flip (&d) != DOWN); 
194     }
195
196   ledgers.translate_axis (-me->relative_coordinate (common[X_AXIS], X_AXIS),
197                           X_AXIS);
198   
199   return ledgers.smobbed_copy ();
200 }
201
202 ADD_INTERFACE (Ledger_line_spanner,
203                "ledger-line-interface",
204                "This spanner draws the ledger lines of a staff on note heads. ",
205                "note-heads thickness gap length minimum-length")