]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
Make pure-height on accidentals ignore tied accidentals after breaks.
[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--2006 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, 0);
29   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0, 0);
30
31   return m;
32 }
33
34 /*
35   Hmm. Need separate callback, or perhaps #'live bool property.
36  */
37 MAKE_SCHEME_CALLBACK (Accidental_interface, after_line_breaking, 1);
38 SCM
39 Accidental_interface::after_line_breaking (SCM smob)
40 {
41   Grob *me = unsmob_grob (smob);
42   Grob *tie = unsmob_grob (me->get_object ("tie"));
43
44   if (tie && !tie->original ()
45       && !to_boolean (me->get_property ("forced")))
46     {
47       me->suicide ();
48     }
49  
50   return SCM_UNSPECIFIED;
51 }
52
53 MAKE_SCHEME_CALLBACK (Accidental_interface, pure_height, 3);
54 SCM
55 Accidental_interface::pure_height (SCM smob, SCM start_scm, SCM)
56 {
57   Item *me = dynamic_cast<Item*> (unsmob_grob (smob));
58   int start = scm_to_int (start_scm);
59   int rank = me->get_column ()->get_rank ();
60
61   bool visible = to_boolean (me->get_property ("forced"))
62     || !unsmob_grob (me->get_object ("tie"))
63     || rank != start + 1; /* we are in the middle of a line */
64
65   return visible ? Grob::stencil_height (smob) : ly_interval2scm (Interval ());
66 }
67
68 vector<Box>
69 Accidental_interface::accurate_boxes (Grob *me, Grob **common)
70 {
71   Box b;
72   b[X_AXIS] = me->extent (me, X_AXIS);
73   b[Y_AXIS] = me->extent (me, Y_AXIS);
74
75   vector<Box> boxes;
76
77   bool parens = to_boolean (me->get_property ("parenthesized"));
78
79   SCM scm_style = me->get_property ("style");
80   if (!scm_is_symbol (scm_style)
81       && !to_boolean (me->get_property ("restore-first"))
82       && !parens)
83     {
84       Rational alteration = ly_scm2rational (me->get_property ("alteration"));
85       if (alteration == FLAT_ALTERATION)
86         {
87           Box stem = b;
88           Box bulb = b;
89
90           /*
91             we could make the stem thinner, but that places the flats
92             really close.
93           */
94           stem[X_AXIS][RIGHT] *= .5;
95
96           /*
97             To prevent vertical alignment for 6ths
98           */
99           stem[Y_AXIS] *= 1.1;
100           bulb[Y_AXIS][UP] *= .35;
101
102           boxes.push_back (bulb);
103           boxes.push_back (stem);
104         }
105       else if (alteration == NATURAL_ALTERATION)
106         {
107           Box lstem = b;
108           Box rstem = b;
109           Box belly = b;
110
111           lstem[Y_AXIS] *= 1.1;
112           rstem[Y_AXIS] *= 1.1;
113
114           belly[Y_AXIS] *= 0.75;
115           lstem[X_AXIS][RIGHT] *= .33;
116           rstem[X_AXIS][LEFT] = rstem[X_AXIS].linear_combination (1.0 / 3.0);
117           lstem[Y_AXIS][DOWN] = belly[Y_AXIS][DOWN];
118           rstem[Y_AXIS][UP] = belly[Y_AXIS][UP];
119           boxes.push_back (belly);
120           boxes.push_back (lstem);
121           boxes.push_back (rstem);
122         }
123       /*
124         TODO: add support for, double flat.
125       */
126     }
127
128   if (!boxes.size ())
129     boxes.push_back (b);
130
131   Offset o (me->relative_coordinate (common[X_AXIS], X_AXIS),
132             me->relative_coordinate (common[Y_AXIS], Y_AXIS));
133
134   for (vsize i = boxes.size (); i--;)
135     boxes[i].translate (o);
136
137   return boxes;
138 }
139
140 MAKE_SCHEME_CALLBACK (Accidental_interface, print, 1);
141 SCM
142 Accidental_interface::print (SCM smob)
143 {
144   Grob *me = unsmob_grob (smob);
145
146   Font_metric *fm = Font_interface::get_default_font (me);
147
148   SCM alist = me->get_property ("glyph-name-alist");
149   SCM alt = me->get_property ("alteration");
150   SCM glyph_name = ly_assoc_get (alt, alist, SCM_BOOL_F);
151   
152   if (!scm_is_string (glyph_name))
153     {
154       me->warning (_f ("Could not find glyph-name for alteration %s",
155                        ly_scm2rational (alt).to_string ().c_str ()));
156       return SCM_EOL;
157     }
158   
159   Stencil mol (fm->find_by_name (scm_i_string_chars (glyph_name)));
160   if (to_boolean (me->get_property ("restore-first")))
161     {
162       /*
163         this isn't correct for ancient accidentals, but they don't
164         use double flats/sharps anyway.
165         */
166       Stencil acc (fm->find_by_name ("accidentals.natural"));
167
168       if (acc.is_empty ())
169         me->warning (_ ("natural alteration glyph not found"));
170       else
171         mol.add_at_edge (X_AXIS, LEFT, acc, 0.1, 0);
172     }
173   
174   if (to_boolean (me->get_property ("parenthesized")))
175     mol = parenthesize (me, mol);
176
177   return mol.smobbed_copy ();
178 }
179
180   
181 ADD_INTERFACE (Accidental_interface,
182                "a single accidental",
183                
184                /* props */
185                "alteration "
186                "avoid-slur "
187                "forced "
188                "parenthesized "
189                "restore-first "
190                "glyph-name-alist "
191                "tie "
192                );