]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
0c62beee98ac81bcccd092d1d2f8625ef5cba9c4
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   ofog
8 */
9 #include "font-interface.hh"
10 #include "item.hh"
11 #include "stencil.hh"
12 #include "accidental-interface.hh"
13 #include "output-def.hh"
14 #include "pitch.hh"
15
16 /*
17   TODO: insert support for smaller cautionaries, tie-break-reminders.
18   Either here or in new-accidental-engraver.
19
20   'accidentals should go, for a single 'accidental property -- see
21   accidental-placement.cc
22
23 */
24 Stencil
25 parenthesize (Grob*me, Stencil m)
26 {
27   Stencil open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-leftparen"));
28   Stencil close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-rightparen"));
29
30   m.add_at_edge (X_AXIS, LEFT, Stencil (open), 0,0);
31   m.add_at_edge (X_AXIS, RIGHT, Stencil (close), 0,0);
32
33   return m;
34 }
35
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_property ("tie"));
43
44   if (tie && !tie->original_)
45     {
46       me->suicide ();
47     }
48   return SCM_UNSPECIFIED;
49 }
50
51 Array<Box>
52 Accidental_interface::accurate_boxes (Grob *a, Grob**common)
53 {
54   Box b;
55   b[X_AXIS] = a->extent (a, X_AXIS);
56   b[Y_AXIS] = a->extent (a, Y_AXIS);
57
58   Array<Box> boxes;
59   
60   bool parens = false;
61   if (to_boolean (a->get_property ("cautionary")))
62     {
63       SCM cstyle = a->get_property ("cautionary-style");
64       parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
65     }
66
67   SCM accs = a->get_property ("accidentals");
68   SCM scm_style = a->get_property ("style");
69   if (!scm_is_symbol (scm_style)
70       && !parens
71       && scm_ilength (accs) == 1)
72     {
73       if (scm_to_int (scm_car (accs)) == FLAT)
74         {
75           Box stem = b;
76           Box bulb = b;
77
78           /*
79             we could make the stem thinner, but that places the flats
80             really close.
81           */
82           stem[X_AXIS][RIGHT] *= .5;
83
84           /*
85             To prevent vertical alignment for 6ths
86            */
87           stem[Y_AXIS]  *= 1.1;  
88           bulb[Y_AXIS][UP] *= .35;
89
90           boxes.push (bulb);
91           boxes.push (stem);
92         }
93       
94       /*
95         TODO: add support for natural, double flat.
96        */
97     }
98
99   if (!boxes.size ())
100     boxes.push (b);
101
102   Offset o (a->relative_coordinate (common[X_AXIS],  X_AXIS),
103             a->relative_coordinate (common[Y_AXIS],  Y_AXIS));
104   for (int i = boxes.size (); i--;)
105     {
106       boxes[i].translate (o);
107     }
108   
109   return boxes;
110 }
111
112 /*
113  * Some styles do not provide all flavours of accidentals, e.g. there
114  * is currently no sharp accidental in vaticana style.  In these cases
115  * this function falls back to one of the other styles.
116  */
117
118 /*
119   todo: this sort of stuff in Scheme. --hwn.
120  */
121 String
122 Accidental_interface::get_fontcharname (String style, int alteration)
123 {
124   if (alteration == DOUBLE_FLAT
125       || alteration == DOUBLE_SHARP)
126     {
127       return to_string (alteration);
128     }
129   
130   if (style == "hufnagel")
131     switch (alteration)
132       {
133       case FLAT: return "hufnagel-1";
134       case 0: return "vaticana0";
135       case SHARP: return "mensural1";
136       }
137   if (style == "medicaea")
138     switch (alteration)
139       {
140       case FLAT: return "medicaea-1";
141       case 0: return "vaticana0";
142       case SHARP: return "mensural1";
143       }
144   if (style == "vaticana")
145     switch (alteration)
146       {
147       case FLAT: return "vaticana-1";
148       case 0: return "vaticana0";
149       case SHARP: return "mensural1";
150       }
151   if (style == "mensural")
152     switch (alteration)
153       {
154       case FLAT: return "mensural-1";
155       case 0: return "vaticana0";
156       case SHARP: return "mensural1";
157       }
158   
159   if (style == "neomensural")
160     style = ""; // currently same as default
161   if (style == "default")
162     style = "";
163   return style + to_string (alteration);
164 }
165
166 MAKE_SCHEME_CALLBACK (Accidental_interface,print,1);
167 SCM
168 Accidental_interface::print (SCM smob)
169 {
170   Grob *me = unsmob_grob (smob);
171   bool smaller = false;
172   bool parens = false;
173
174   bool caut  = to_boolean (me->get_property ("cautionary"));
175   if (caut)
176     {
177       SCM cstyle = me->get_property ("cautionary-style");
178       parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
179       smaller = ly_c_equal_p (cstyle, ly_symbol2scm ("smaller"));
180     }
181
182   SCM scm_style = me->get_property ("style");
183   String style;
184   if (scm_is_symbol (scm_style))
185     {
186       style = ly_symbol2string (scm_style);
187     }
188   else
189     {
190       /*
191         preferably no name for the default style.
192       */
193       style = "";
194     }
195
196   Font_metric *fm = 0;
197   if (smaller)
198     {
199       SCM ac = Font_interface::music_font_alist_chain (me);
200       /*
201         TODO: should calc font-size by adding -2 to current font-size
202       */
203       ac = scm_cons (scm_list_1 (scm_cons
204                                  (ly_symbol2scm ("font-size"),
205                                   scm_int2num (-2))),
206                      ac);
207       fm = select_font (me->get_paper (), ac);
208     }
209   else
210     fm = Font_interface::get_default_font (me);
211
212   Stencil mol;
213   for (SCM s = me->get_property ("accidentals");
214        scm_is_pair (s); s = scm_cdr (s))
215     {
216       int alteration = scm_to_int (scm_car (s));
217       String font_char = get_fontcharname (style, alteration);
218       Stencil acc (fm->find_by_name ("accidentals-" + font_char));
219
220       if (acc.is_empty ())
221         {
222           me->warning (_f ("accidental `%s' not found", font_char));
223         }
224       else
225         {
226           mol.add_at_edge (X_AXIS,  RIGHT, acc, 0.1,0);
227         }
228     }
229
230   if (parens)
231     mol = parenthesize (me, mol); 
232
233   return mol.smobbed_copy ();
234 }
235
236
237 /*
238   TODO: should move inside-slur into item?
239   
240  */
241 ADD_INTERFACE (Accidental_interface, "accidental-interface",
242               "a single accidental",
243                "inside-slur cautionary cautionary-style style tie accidentals");