]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
release: 1.5.19
[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--2001 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
18 #include "staff-symbol-referencer.hh"
19
20 /*
21   build a ledger line for small pieces.
22  */
23 Molecule
24 Note_head::ledger_line (Interval xwid, Grob *me) 
25 {
26   Drul_array<Molecule> endings;
27   endings[LEFT] = Font_interface::get_default_font (me)->find_by_name ("noteheads-ledgerending");
28   Molecule *e = &endings[LEFT];
29   endings[RIGHT] = *e;
30   
31   Real thick = e->extent (Y_AXIS).length ();
32   Real len = e->extent (X_AXIS).length () - thick;
33
34   Molecule total;
35   Direction d = LEFT;
36   do {
37     endings[d].translate_axis (xwid[d] - endings[d].extent (X_AXIS)[d], X_AXIS);
38     total.add_molecule (endings[d]);    
39   } while ((flip (&d)) != LEFT);
40
41   Real xpos = xwid [LEFT] + len;
42
43   while (xpos + len + thick /2 <= xwid[RIGHT])
44     {
45       e->translate_axis (len, X_AXIS);
46       total.add_molecule (*e);
47       xpos += len;
48     }
49
50   return total;
51 }
52
53
54 Molecule
55 Note_head::ledger_lines (Grob*me, int count, Direction dir, Interval idw)
56 {
57   Real inter_f = Staff_symbol_referencer::staff_space (me)/2;
58   Molecule ledger (ledger_line (idw, me));
59
60   ledger.set_empty (true);
61   Real offs = (Staff_symbol_referencer::on_staffline (me))
62     ? 0.0
63     : -dir * inter_f;
64
65   Molecule legs;
66   for (int i=0; i < count; i++)
67     {
68       Molecule s (ledger);
69       s.translate_axis (-dir * inter_f * i*2 + offs,
70                         Y_AXIS);
71       legs.add_molecule (s);
72     }
73
74   return legs;
75 }
76
77 MAKE_SCHEME_CALLBACK (Note_head,brew_molecule,1);
78
79 SCM
80 Note_head::brew_molecule (SCM smob)  
81 {
82   Grob *me = unsmob_grob (smob);
83
84   int sz = Staff_symbol_referencer::line_count (me)-1;
85   int p = (int)  rint (Staff_symbol_referencer::position_f (me));
86   int streepjes_i = abs (p) < sz 
87     ? 0
88     : (abs (p) - sz) /2;
89
90   SCM style  = me->get_grob_property ("style");
91   if (!gh_symbol_p (style))
92     {
93       return SCM_EOL;
94     }
95
96   /*
97     ugh: use gh_call () / scm_apply ().
98
99     UGH: use grob-property.
100   */
101   SCM log = me->get_grob_property ("duration-log");
102   SCM exp = scm_list_n (ly_symbol2scm ("find-notehead-symbol"), log,
103                         ly_quote_scm (style),
104                         SCM_UNDEFINED);
105   String name = "noteheads-" + ly_scm2string (scm_primitive_eval (exp));
106   Molecule out = Font_interface::get_default_font (me)->find_by_name (name);
107
108   
109   if (streepjes_i) 
110     {
111       Direction dir = (Direction)sign (p);
112       Interval hd = out.extent (X_AXIS);
113       Real hw = hd.length ()/4;
114       out.add_molecule (ledger_lines (me, streepjes_i, dir,
115                                       Interval (hd[LEFT] - hw,
116                                                 hd[RIGHT] + hw)));
117     }
118   
119   return out.smobbed_copy ();
120 }
121
122 bool
123 Note_head::has_interface (Grob*m)
124 {
125   return m&& m->has_interface (ly_symbol2scm ("note-head-interface"));
126 }
127
128
129 MAKE_SCHEME_CALLBACK (Note_head,brew_ez_molecule,1);
130
131 SCM
132 Note_head::brew_ez_molecule (SCM smob)
133 {
134   Grob *me = unsmob_grob (smob);
135   int l = gh_scm2int (me->get_grob_property ("duration-log"));
136
137   int b = (l >= 2);
138   SCM at = scm_list_n (ly_symbol2scm ("ez-ball"),
139                     me->get_grob_property ("note-character"),
140                     gh_int2scm (b),
141                     gh_int2scm (1-b),
142                     SCM_UNDEFINED);
143   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
144   Molecule m (bx, at);
145   int p = (int)  rint (Staff_symbol_referencer::position_f (me));
146
147   int sz = Staff_symbol_referencer::line_count (me)-1;
148   int streepjes_i = abs (p) < sz 
149     ? 0
150     : (abs (p) - sz) /2;
151
152  if (streepjes_i)
153    {
154       Direction dir = (Direction)sign (p);
155       Interval hd = m.extent (X_AXIS);
156       Real hw = hd.length ()/4;
157       m.add_molecule (ledger_lines (me, streepjes_i, dir,
158                                       Interval (hd[LEFT] - hw,
159                                                 hd[RIGHT] + hw)));
160     }
161   
162   return m.smobbed_copy ();
163 }
164
165
166 Real
167 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
168 {
169   SCM v = me->get_grob_property ("stem-attachment-function");
170
171   if (!gh_procedure_p (v))
172     return 0.0;
173
174   SCM st = me->get_grob_property ("style");
175   SCM result = gh_apply (v, scm_list_n (st, SCM_UNDEFINED));
176
177   if (!gh_pair_p (result))
178     return 0.0;
179
180   result = (a == X_AXIS) ? ly_car (result) : ly_cdr (result);
181   
182   return gh_number_p (result) ?  gh_scm2double (result) : 0.0;
183 }