]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-head.cc
Nitpick run.
[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 "music.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     style = ly_symbol2scm ("default");
38
39   SCM log = scm_from_int (Note_head::get_balltype (me));
40   String suffix = to_string (min (robust_scm2int (me->get_property ("duration-log"), 2), 2));
41   if (style != ly_symbol2scm ("default"))
42     {
43       SCM proc = me->get_property ("glyph-name-procedure");
44       if (ly_is_procedure (proc))
45         suffix = ly_scm2string (scm_call_2 (proc, log, style));
46     }
47   Font_metric *fm = Font_interface::get_default_font (me);
48
49   String idx = "noteheads.s" + suffix;
50
51   Stencil out = fm->find_by_name (idx);
52   if (out.is_empty ())
53     {
54       String prefix = "noteheads.";
55       Grob *stem = unsmob_grob (me->get_object ("stem"));
56       Direction stem_dir = stem ? get_grob_direction (stem) : CENTER;
57
58       if (stem_dir == CENTER)
59         programming_error ("must have stem dir for note head");
60       String idx = prefix + ((stem_dir == UP) ? "u" : "d") + suffix;
61       out = fm->find_by_name (idx);
62     }
63
64   if (out.is_empty ())
65     {
66       me->warning (_f ("note head `%s' not found", idx.to_str0 ()));
67       out = Stencil (Box (Interval (0, 0), Interval (0, 0)), SCM_EOL);
68     }
69   else
70     *font_char = idx;
71
72   return out;
73 }
74
75 MAKE_SCHEME_CALLBACK (Note_head, print, 1);
76 SCM
77 Note_head::print (SCM smob)
78 {
79   Grob *me = unsmob_grob (smob);
80
81   String idx;
82   return internal_print (me, &idx).smobbed_copy ();
83 }
84
85 Real
86 Note_head::stem_attachment_coordinate (Grob *me, Axis a)
87 {
88   SCM brewer = me->get_property ("print-function");
89   Font_metric *fm = Font_interface::get_default_font (me);
90
91   if (brewer == Note_head::print_proc)
92     {
93       String key;
94       internal_print (me, &key);
95
96       int k = fm->name_to_index (key);
97       if (k >= 0)
98         {
99           Box b = fm->get_indexed_char (k);
100           Offset wxwy = fm->attachment_point (key);
101           Interval v = b[a];
102           if (!v.is_empty ())
103             return 2 * (wxwy[a] - v.center ()) / v.length ();
104         }
105     }
106
107   /*
108     Fallback
109   */
110   SCM v = me->get_property ("stem-attachment-function");
111   if (!ly_is_procedure (v))
112     return 0.0;
113
114   SCM result = scm_call_2 (v, me->self_scm (), scm_from_int (a));
115   if (!scm_is_pair (result))
116     return 0.0;
117
118   result = (a == X_AXIS) ? scm_car (result) : scm_cdr (result);
119
120   return robust_scm2double (result, 0);
121 }
122
123 int
124 Note_head::get_balltype (Grob *me)
125 {
126   SCM s = me->get_property ("duration-log");
127   return scm_is_number (s) ? min (scm_to_int (s), 2) : 0;
128 }
129
130 ADD_INTERFACE (Note_head, "note-head-interface",
131                "Note head",
132                "note-names glyph-name-procedure accidental-grob style stem-attachment-function");
133