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