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