]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / accidental.cc
1 /*
2   accidental.cc -- implement Accidental_interface
3
4   (c) 2001--2004 Han-Wen Nienhuys
5   
6  */
7 #include "font-interface.hh"
8 #include "item.hh"
9 #include "stencil.hh"
10 #include "accidental-interface.hh"
11 #include "paper-def.hh"
12 #include "pitch.hh"
13
14 /*
15   TODO: insert support for smaller cautionaries, tie-break-reminders.
16   Either here or in new-accidental-engraver.
17
18   'accidentals should go, for a single 'accidental property -- see
19   accidental-placement.cc
20
21 */
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   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_equal_p (cstyle, ly_symbol2scm ("parentheses"));
64
65     }
66
67   SCM accs = a->get_property ("accidentals");
68   SCM scm_style = a->get_property ("style");
69   if (!ly_symbol_p (scm_style)
70       && !parens
71       && scm_ilength (accs) == 1)
72     {
73       if (ly_scm2int (ly_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 == "neo_mensural")
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_equal_p (cstyle, ly_symbol2scm ("parentheses"));
179       smaller = ly_equal_p (cstyle, ly_symbol2scm ("smaller"));
180     }
181
182   SCM scm_style = me->get_property ("style");
183   String style;
184   if (ly_symbol_p (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::text_font_alist_chain (me);
200       ac = scm_cons (scm_cons (scm_cons
201                              (ly_symbol2scm ("font-size"),
202                               scm_int2num (-2)), SCM_EOL),
203                     ac);
204       fm = select_font (me->get_paper (), ac);
205     }
206   else
207     fm = Font_interface::get_default_font (me);
208
209   Stencil mol;
210   for (SCM s = me->get_property ("accidentals");
211        ly_pair_p (s); s = ly_cdr (s))
212     {
213       int alteration = ly_scm2int (ly_car (s));
214       String font_char = get_fontcharname (style, alteration);
215       Stencil acc (fm->find_by_name ("accidentals-" + font_char));
216
217       if (acc.is_empty ())
218         {
219           me->warning (_f ("accidental `%s' not found", font_char));
220         }
221       else
222         {
223           mol.add_at_edge (X_AXIS,  RIGHT, acc, 0.1,0);
224         }
225     }
226
227   if (parens)
228     mol = parenthesize (me, mol); 
229
230   return mol.smobbed_copy ();
231 }
232
233
234
235 ADD_INTERFACE (Accidental_interface, "accidental-interface",
236               "a single accidental",
237                "cautionary cautionary-style style tie accidentals");