]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
Merge branch 'master' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / accidental.cc
1 /*
2   accidental.cc -- implement Accidental_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2001--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "accidental-interface.hh"
10 #include "font-interface.hh"
11 #include "international.hh"
12 #include "item.hh"
13 #include "output-def.hh"
14 #include "paper-column.hh"
15 #include "pitch.hh"
16 #include "stencil.hh"
17
18 Stencil
19 parenthesize (Grob *me, Stencil m)
20 {
21   Font_metric * font
22     = Font_interface::get_default_font (me); 
23   Stencil open
24     = font->find_by_name ("accidentals.leftparen");
25   Stencil close
26     = font->find_by_name ("accidentals.rightparen");
27
28   m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0);
29   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0);
30
31   return m;
32 }
33
34
35 /* This callback exists for the sole purpose of allowing us to override
36    its pure equivalent to accidental-interface::pure-height */
37 MAKE_SCHEME_CALLBACK (Accidental_interface, height, 1);
38 SCM
39 Accidental_interface::height (SCM smob)
40 {
41   return Grob::stencil_height (smob);
42 }
43
44 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
45 SCM
46 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
47 {
48   Item *me = dynamic_cast<Item*> (unsmob_grob (smob));
49   int start = scm_to_int (start_scm);
50   int rank = me->get_column ()->get_rank ();
51
52   bool visible = to_boolean (me->get_property ("forced"))
53     || !unsmob_grob (me->get_object ("tie"))
54     || rank != start + 1; /* we are in the middle of a line */
55
56   return visible ? Grob::stencil_height (smob) : ly_interval2scm (Interval ());
57 }
58
59 vector<Box>
60 Accidental_interface::accurate_boxes (Grob *me, Grob **common)
61 {
62   Box b;
63   b[X_AXIS] = me->extent (me, X_AXIS);
64   b[Y_AXIS] = me->extent (me, Y_AXIS);
65
66   vector<Box> boxes;
67
68   bool parens = to_boolean (me->get_property ("parenthesized"));
69
70   SCM scm_style = me->get_property ("style");
71   if (!scm_is_symbol (scm_style)
72       && !to_boolean (me->get_property ("restore-first"))
73       && !parens)
74     {
75       Rational alteration = ly_scm2rational (me->get_property ("alteration"));
76       if (alteration == FLAT_ALTERATION)
77         {
78           Box stem = b;
79           Box bulb = b;
80
81           /*
82             we could make the stem thinner, but that places the flats
83             really close.
84           */
85           stem[X_AXIS][RIGHT] *= .5;
86
87           /*
88             To prevent vertical alignment for 6ths
89           */
90           stem[Y_AXIS] *= 1.1;
91           bulb[Y_AXIS][UP] *= .35;
92
93           boxes.push_back (bulb);
94           boxes.push_back (stem);
95         }
96       else if (alteration == NATURAL_ALTERATION)
97         {
98           Box lstem = b;
99           Box rstem = b;
100           Box belly = b;
101
102           lstem[Y_AXIS] *= 1.1;
103           rstem[Y_AXIS] *= 1.1;
104
105           belly[Y_AXIS] *= 0.75;
106           lstem[X_AXIS][RIGHT] *= .33;
107           rstem[X_AXIS][LEFT] = rstem[X_AXIS].linear_combination (1.0 / 3.0);
108           lstem[Y_AXIS][DOWN] = belly[Y_AXIS][DOWN];
109           rstem[Y_AXIS][UP] = belly[Y_AXIS][UP];
110           boxes.push_back (belly);
111           boxes.push_back (lstem);
112           boxes.push_back (rstem);
113         }
114       /*
115         TODO: add support for, double flat.
116       */
117     }
118
119   if (!boxes.size ())
120     boxes.push_back (b);
121
122   Offset o (me->relative_coordinate (common[X_AXIS], X_AXIS),
123             me->relative_coordinate (common[Y_AXIS], Y_AXIS));
124
125   for (vsize i = boxes.size (); i--;)
126     boxes[i].translate (o);
127
128   return boxes;
129 }
130
131 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
132 SCM
133 Accidental_interface::print (SCM smob)
134 {
135   Grob *me = unsmob_grob (smob);
136   Grob *tie = unsmob_grob (me->get_object ("tie"));
137
138   if (tie && !tie->original ()
139       && !to_boolean (me->get_property ("forced")))
140     {
141       me->suicide ();
142       return SCM_EOL;
143     }
144   
145   Font_metric *fm = Font_interface::get_default_font (me);
146
147   SCM alist = me->get_property ("glyph-name-alist");
148   SCM alt = me->get_property ("alteration");
149   SCM glyph_name = ly_assoc_get (alt, alist, SCM_BOOL_F);
150   
151   if (!scm_is_string (glyph_name))
152     {
153       me->warning (_f ("Could not find glyph-name for alteration %s",
154                        ly_scm2rational (alt).to_string ().c_str ()));
155       return SCM_EOL;
156     }
157   
158   Stencil mol (fm->find_by_name (scm_i_string_chars (glyph_name)));
159   if (to_boolean (me->get_property ("restore-first")))
160     {
161       /*
162         this isn't correct for ancient accidentals, but they don't
163         use double flats/sharps anyway.
164         */
165       Stencil acc (fm->find_by_name ("accidentals.natural"));
166
167       if (acc.is_empty ())
168         me->warning (_ ("natural alteration glyph not found"));
169       else
170         mol.add_at_edge (X_AXIS, LEFT, acc, 0.1);
171     }
172   
173   if (to_boolean (me->get_property ("parenthesized")))
174     mol = parenthesize (me, mol);
175
176   return mol.smobbed_copy ();
177 }
178
179   
180 ADD_INTERFACE (Accidental_interface,
181                "a single accidental",
182                
183                /* props */
184                "alteration "
185                "avoid-slur "
186                "forced "
187                "parenthesized "
188                "restore-first "
189                "glyph-name-alist "
190                "tie "
191                );