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