]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
release: 1.5.29
[lilypond.git] / lily / note-head.cc
1 /*
2   notehead.cc -- implement Note_head
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include <math.h>
9
10 #include "misc.hh"
11 #include "dots.hh"
12 #include "note-head.hh"
13 #include "debug.hh"
14 #include "font-interface.hh"
15 #include "molecule.hh"
16 #include "musical-request.hh"
17 #include "rhythmic-head.hh"
18
19 /*
20   Note_head contains the code for printing note heads and the ledger lines.
21   
22   TODO: maybe it's worthwhile to split away the Ledger lines into a
23   separate grob.  */
24
25 #include "staff-symbol-referencer.hh"
26
27 /*
28   build a ledger line for small pieces.
29  */
30 Molecule
31 Note_head::ledger_line (Interval xwid, Grob *me) 
32 {
33   Drul_array<Molecule> endings;
34   endings[LEFT] = Font_interface::get_default_font (me)->find_by_name ("noteheads-ledgerending");
35   Molecule *e = &endings[LEFT];
36   endings[RIGHT] = *e;
37   
38   Real thick = e->extent (Y_AXIS).length ();
39   Real len = e->extent (X_AXIS).length () - thick;
40
41   Molecule total;
42   Direction d = LEFT;
43   do {
44     endings[d].translate_axis (xwid[d] - endings[d].extent (X_AXIS)[d], X_AXIS);
45     total.add_molecule (endings[d]);    
46   } while ((flip (&d)) != LEFT);
47
48   Real xpos = xwid [LEFT] + len;
49
50   while (xpos + len + thick /2 <= xwid[RIGHT])
51     {
52       e->translate_axis (len, X_AXIS);
53       total.add_molecule (*e);
54       xpos += len;
55     }
56
57   return total;
58 }
59
60
61 Molecule
62 Note_head::ledger_lines (Grob*me,
63                          bool take_space,
64                          int count, Direction dir, Interval idw)
65 {
66   Real inter_f = Staff_symbol_referencer::staff_space (me)/2;
67
68   /*
69     idw ?
70
71     (who's that ?  :-)
72
73
74     --hwn 
75    */
76   Molecule ledger (ledger_line (idw, me));
77
78   if (!take_space)
79     ledger.set_empty (true);
80   
81   Real offs = (Staff_symbol_referencer::on_staffline (me))
82     ? 0.0
83     : -dir * inter_f;
84
85   Molecule legs;
86   for (int i=0; i < count; i++)
87     {
88       Molecule s (ledger);
89       s.translate_axis (-dir * inter_f * i*2 + offs,
90                         Y_AXIS);
91       legs.add_molecule (s);
92     }
93
94   return legs;
95 }
96
97 Molecule
98 internal_brew_molecule (Grob *me,  bool ledger_take_space)
99 {
100   int sz = Staff_symbol_referencer::line_count (me)-1;
101   int p = (int)  rint (Staff_symbol_referencer::position_f (me));
102   int streepjes_i = abs (p) < sz 
103     ? 0
104     : (abs (p) - sz) /2;
105
106   SCM style  = me->get_grob_property ("style");
107   if (!gh_symbol_p (style))
108     {
109       return Molecule();
110     }
111
112   /*
113     ugh: use gh_call () / scm_apply ().
114
115     UGH: use grob-property.
116   */
117   SCM log = gh_int2scm (Rhythmic_head::balltype_i (me));
118   SCM exp = scm_list_n (ly_symbol2scm ("find-notehead-symbol"), log,
119                         ly_quote_scm (style),
120                         SCM_UNDEFINED);
121   String name = "noteheads-" + ly_scm2string (scm_primitive_eval (exp));
122   Molecule out = Font_interface::get_default_font (me)->find_by_name (name);
123
124   if (streepjes_i) 
125     {
126       Direction dir = (Direction)sign (p);
127       Interval hd = out.extent (X_AXIS);
128       Real hw = hd.length ()/4;
129       out.add_molecule (Note_head::ledger_lines (me, ledger_take_space, streepjes_i, dir,
130                                       Interval (hd[LEFT] - hw,
131                                                 hd[RIGHT] + hw)));
132     }
133   return out;
134 }
135
136 MAKE_SCHEME_CALLBACK (Note_head,brew_molecule,1);
137
138 SCM
139 Note_head::brew_molecule (SCM smob)  
140 {
141   Grob *me = unsmob_grob (smob);
142   return internal_brew_molecule (me, true).smobbed_copy ();
143 }
144
145 /*
146   Compute the width the head without ledgers.
147  */
148 Interval
149 Note_head::head_extent (Grob *me, Axis a)
150 {
151   return  internal_brew_molecule (me, false).extent (a);
152 }
153
154 bool
155 Note_head::has_interface (Grob*m)
156 {
157   return m&& m->has_interface (ly_symbol2scm ("note-head-interface"));
158 }
159
160
161 MAKE_SCHEME_CALLBACK (Note_head,brew_ez_molecule,1);
162
163 /*
164   TODO: ledger lines are causing  a mess again, now with accidentals and
165   ez-note heads.
166  */ 
167 SCM
168 Note_head::brew_ez_molecule (SCM smob)
169 {
170   Grob *me = unsmob_grob (smob);
171   int l = Rhythmic_head::balltype_i (me);
172
173   int b = (l >= 2);
174   SCM at = scm_list_n (ly_symbol2scm ("ez-ball"),
175                     me->get_grob_property ("note-character"),
176                     gh_int2scm (b),
177                     gh_int2scm (1-b),
178                     SCM_UNDEFINED);
179   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
180   Molecule m (bx, at);
181   int p = (int)  rint (Staff_symbol_referencer::position_f (me));
182
183   int sz = Staff_symbol_referencer::line_count (me)-1;
184   int streepjes_i = abs (p) < sz 
185     ? 0
186     : (abs (p) - sz) /2;
187
188  if (streepjes_i)
189    {
190       Direction dir = (Direction)sign (p);
191       Interval hd = m.extent (X_AXIS);
192       Real hw = hd.length ()/4;
193       m.add_molecule (ledger_lines (me, false, streepjes_i, dir,
194                                     Interval (hd[LEFT] - hw,
195                                                 hd[RIGHT] + hw)));
196     }
197   
198   return m.smobbed_copy ();
199 }
200
201
202 Real
203 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
204 {
205   SCM v = me->get_grob_property ("stem-attachment-function");
206
207   if (!gh_procedure_p (v))
208     return 0.0;
209
210   SCM st = me->get_grob_property ("style");
211   SCM result = gh_apply (v, scm_list_n (st, SCM_UNDEFINED));
212
213   if (!gh_pair_p (result))
214     return 0.0;
215
216   result = (a == X_AXIS) ? ly_car (result) : ly_cdr (result);
217   
218   return gh_number_p (result) ?  gh_scm2double (result) : 0.0;
219 }