]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
release: 1.3.144
[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 MAKE_SCHEME_CALLBACK (Note_head,brew_molecule,1);
55
56 SCM
57 Note_head::brew_molecule (SCM smob)  
58 {
59   Grob *me = unsmob_grob (smob);
60
61   
62   Real inter_f = Staff_symbol_referencer::staff_space (me)/2;
63   int sz = Staff_symbol_referencer::line_count (me)-1;
64   int p = (int)  rint (Staff_symbol_referencer::position_f (me));
65   int streepjes_i = abs (p) < sz 
66     ? 0
67     : (abs (p) - sz) /2;
68
69   SCM style  = me->get_grob_property ("style");
70   if (!gh_symbol_p (style))
71     {
72       return SCM_EOL;
73     }
74
75   /*
76     ugh: use gh_call ()
77
78     UGH: use grob-property.
79   */
80   Molecule out = Font_interface::get_default_font (me)->find_by_name (String ("noteheads-") + 
81                 ly_scm2string (scm_eval2 (gh_list (ly_symbol2scm ("find-notehead-symbol"),
82                                                   me->get_grob_property ("duration-log"),
83                                                   ly_quote_scm (style),
84                                                   SCM_UNDEFINED),
85                                           SCM_EOL)));
86
87   if (streepjes_i) 
88     {
89       Direction dir = (Direction)sign (p);
90       Interval hd = out.extent (X_AXIS);
91       Real hw = hd.length ()/4;
92       Molecule ledger (ledger_line (Interval (hd[LEFT] - hw,
93                                                hd[RIGHT] + hw), me));
94       
95
96       ledger.set_empty (true);
97       Real offs = (Staff_symbol_referencer::on_staffline (me))
98         ? 0.0
99         : -dir * inter_f;
100
101       for (int i=0; i < streepjes_i; i++)
102         {
103           Molecule s (ledger);
104           s.translate_axis (-dir * inter_f * i*2 + offs,
105                             Y_AXIS);
106           out.add_molecule (s);
107         }
108     }
109   return out.smobbed_copy ();
110 }
111
112 bool
113 Note_head::has_interface (Grob*m)
114 {
115   return m&& m->has_interface (ly_symbol2scm ("note-head-interface"));
116 }
117
118
119 MAKE_SCHEME_CALLBACK (Note_head,brew_ez_molecule,1);
120
121 SCM
122 Note_head::brew_ez_molecule (SCM smob)
123 {
124   Grob *me = unsmob_grob (smob);
125   int l = gh_scm2int (me->get_grob_property ("duration-log"));
126
127   int b = (l >= 2);
128   SCM at = gh_list (ly_symbol2scm ("ez-ball"),
129                     me->get_grob_property ("note-character"),
130                     gh_int2scm (b),
131                     gh_int2scm (1-b),
132                     SCM_UNDEFINED);
133   Box bx (Interval (0, 1.0), Interval (-0.5, 0.5));
134   Molecule m (bx, at);
135
136   return m.smobbed_copy ();
137 }
138
139
140 Real
141 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
142 {
143   SCM v = me->get_grob_property ("stem-attachment-function");
144
145   if (!gh_procedure_p (v))
146     return 0.0;
147
148   SCM st = me->get_grob_property ("style");
149   SCM result = gh_apply (v, gh_list (st, SCM_UNDEFINED));
150
151   if (!gh_pair_p (result))
152     return 0.0;
153
154   result = (a == X_AXIS) ? gh_car (result) : gh_cdr (result);
155   
156   return gh_number_p (result) ?  gh_scm2double (result) : 0.0;
157 }