]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
* lily/include/lily-guile.hh: rename ly_c_X_p -> ly_is_X
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "note-head.hh"
10
11 #include <math.h>
12 #include <cctype>
13 #include <algorithm>            //  min, max
14
15 using namespace std; 
16
17 #include "directional-element-interface.hh"
18 #include "staff-symbol.hh"
19 #include "misc.hh"
20 #include "dots.hh"
21 #include "warn.hh"
22 #include "font-interface.hh"
23 #include "event.hh"
24 #include "rhythmic-head.hh"
25 #include "staff-symbol-referencer.hh"
26 #include "lookup.hh"
27 #include "output-def.hh"
28
29 /*
30   clean up the mess left by ledger line handling.
31 */
32 static Stencil
33 internal_print (Grob *me, String *font_char)
34 {
35   SCM style = me->get_property ("style");
36   if (!scm_is_symbol (style))
37     {
38       style = ly_symbol2scm ("default");
39     }
40
41   SCM log = scm_int2num (Note_head::get_balltype (me));
42   SCM proc = me->get_property ("glyph-name-procedure");
43
44   String suffix =  to_string (robust_scm2int (me->get_property ("duration-log"), 2));
45   if (ly_is_procedure (proc))
46     suffix = ly_scm2string (scm_call_2 (proc, log, style));
47   
48   Font_metric *fm = Font_interface::get_default_font (me);
49
50   Direction stem_dir = CENTER;
51   if (Grob *stem = unsmob_grob (me->get_property ("stem")))
52     {
53       stem_dir = get_grob_direction (stem);
54       if (stem_dir == CENTER)
55         programming_error ("must have stem dir for note head");
56     }
57
58   Stencil out;
59
60   String prefix = "noteheads.";
61   String idx
62     = prefix + ((stem_dir == UP) ? "u" : "d") + suffix;
63   out = fm->find_by_name (idx);
64   if (out.is_empty ())
65     {
66       idx = prefix + "s" + suffix;
67       out = fm->find_by_name (idx);
68     }
69
70   if (out.is_empty ())
71     {
72       me->warning (_f ("note head `%s' not found", idx.to_str0 ()));
73     }
74   else
75     {
76       *font_char = idx;
77     }
78
79   return out;
80 }
81
82 MAKE_SCHEME_CALLBACK (Note_head, print, 1);
83 SCM
84 Note_head::print (SCM smob)
85 {
86   Grob *me = unsmob_grob (smob);
87
88   String idx;
89   return internal_print (me, &idx).smobbed_copy ();
90 }
91
92 Real
93 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
94 {
95   SCM brewer = me->get_property ("print-function");
96   Font_metric *fm = Font_interface::get_default_font (me);
97
98   if (brewer == Note_head::print_proc)
99     {
100       String key;
101       internal_print (me, &key);
102
103       int k = fm->name_to_index (key);
104       if (k >= 0)
105         {
106           Box b = fm->get_indexed_char (k);
107           Offset wxwy = fm->attachment_point (key);
108           Interval v = b[a];
109           if (!v.is_empty ())
110             return 2 * (wxwy[a] - v.center ()) / v.length ();
111         }
112     }
113
114   /*
115     Fallback
116   */
117   SCM v = me->get_property ("stem-attachment-function");
118   if (!ly_is_procedure (v))
119     return 0.0;
120
121   SCM result = scm_call_2 (v, me->self_scm (), scm_int2num (a));
122   if (!scm_is_pair (result))
123     return 0.0;
124
125   result = (a == X_AXIS) ? scm_car (result) : scm_cdr (result);
126
127   return robust_scm2double (result, 0);
128 }
129
130 int
131 Note_head::get_balltype (Grob *me)
132 {
133   SCM s = me->get_property ("duration-log");
134   return scm_is_number (s) ? min (scm_to_int (s), 2) : 0;
135 }
136
137 ADD_INTERFACE (Note_head, "note-head-interface",
138                "Note head",
139                "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
140