]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
* Documentation/topdocs/NEWS.texi (Top): add quarter tones.
[lilypond.git] / lily / accidental.cc
1 /*
2   accidental.cc -- implement Accidental_interface
3
4   (c) 2001--2003 Han-Wen Nienhuys
5   
6  */
7 #include "font-interface.hh"
8 #include "item.hh"
9 #include "molecule.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 Molecule
25 parenthesize (Grob*me, Molecule m)
26 {
27   Molecule open = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-leftparen"));
28   Molecule close = Font_interface::get_default_font (me)->find_by_name (String ("accidentals-rightparen"));
29   m.add_at_edge (X_AXIS, LEFT, Molecule (open), 0,0);
30   m.add_at_edge (X_AXIS, RIGHT, Molecule (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_grob_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_grob_property ("cautionary")))
61     {
62       SCM cstyle = a->get_grob_property ("cautionary-style");
63       parens = gh_equal_p (cstyle, ly_symbol2scm ("parentheses"));
64
65     }
66
67   SCM accs = a->get_grob_property ("accidentals");
68   SCM scm_style = a->get_grob_property ("style");
69   if (!gh_symbol_p (scm_style)
70       && !parens
71       && scm_ilength (accs) == 1)
72     {
73       if (gh_scm2int (gh_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           bulb[Y_AXIS][UP] *= .35;
84
85           boxes.push (bulb);
86           boxes.push (stem);
87         }
88       /*
89         TODO: add support for natural, double flat.
90        */
91     }
92
93   if (!boxes.size())
94     boxes.push (b);
95
96   Offset o (a->relative_coordinate (common[X_AXIS],  X_AXIS),
97             a->relative_coordinate (common[Y_AXIS],  Y_AXIS));
98   for(int i = boxes.size(); i--;)
99     {
100       boxes[i].translate(o);
101     }
102   
103   return boxes;
104 }
105
106 /*
107  * Some styles do not provide all flavours of accidentals, e.g. there
108  * is currently no sharp accidental in vaticana style.  In these cases
109  * this function falls back to one of the other styles.
110  */
111
112 /*
113   todo: this sort of stuff in Scheme. --hwn.
114  */
115 String
116 Accidental_interface::get_fontcharname (String style, int alteration)
117 {
118   if (alteration == DOUBLE_FLAT
119       || alteration == DOUBLE_SHARP)
120     {
121       return to_string (alteration);
122     }
123   
124   if (style == "hufnagel")
125     switch (alteration)
126       {
127       case FLAT: return "hufnagel-1";
128       case 0: return "vaticana0";
129       case SHARP: return "mensural1";
130       }
131   if (style == "medicaea")
132     switch (alteration)
133       {
134       case FLAT: return "medicaea-1";
135       case 0: return "vaticana0";
136       case SHARP: return "mensural1";
137       }
138   if (style == "vaticana")
139     switch (alteration)
140       {
141       case FLAT: return "vaticana-1";
142       case 0: return "vaticana0";
143       case SHARP: return "mensural1";
144       }
145   if (style == "mensural")
146     switch (alteration)
147       {
148       case FLAT: return "mensural-1";
149       case 0: return "vaticana0";
150       case SHARP: return "mensural1";
151       }
152   
153   if (style == "neo_mensural")
154     style = ""; // currently same as default
155   if (style == "default")
156     style = "";
157   return style + to_string (alteration);
158 }
159
160 MAKE_SCHEME_CALLBACK (Accidental_interface,brew_molecule,1);
161 SCM
162 Accidental_interface::brew_molecule (SCM smob)
163 {
164   Grob *me = unsmob_grob (smob);
165   bool smaller = false;
166   bool parens = false;
167
168   bool caut  = to_boolean (me->get_grob_property ("cautionary"));
169   if (caut)
170     {
171       SCM cstyle = me->get_grob_property ("cautionary-style");
172       parens = gh_equal_p (cstyle, ly_symbol2scm ("parentheses"));
173       smaller = gh_equal_p (cstyle, ly_symbol2scm ("smaller"));
174     }
175
176   SCM scm_style = me->get_grob_property ("style");
177   String style;
178   if (gh_symbol_p (scm_style))
179     {
180       style = ly_symbol2string (scm_style);
181     }
182   else
183     {
184       /*
185         preferably no name for the default style.
186       */
187       style = "";
188     }
189
190   Font_metric *fm = 0;
191   if (smaller)
192     {
193       SCM ac = Font_interface::font_alist_chain (me);
194       ac = gh_cons (gh_cons (gh_cons
195                              (ly_symbol2scm ("font-relative-size"),
196                               scm_int2num (-1)), SCM_EOL),
197                     ac);
198       fm = select_font (me->get_paper (), ac);
199     }
200   else
201     fm = Font_interface::get_default_font (me);
202
203   Molecule mol;
204   for (SCM s = me->get_grob_property ("accidentals");
205        gh_pair_p (s); s = gh_cdr (s))
206     {
207       int alteration = gh_scm2int (gh_car (s));
208       String font_char = get_fontcharname (style, alteration);
209       Molecule acc (fm->find_by_name ("accidentals-" + font_char));
210
211       if (acc.empty_b())
212         {
213           me->warning (_f ("accidental `%s' not found", font_char));
214         }
215       else
216         {
217           mol.add_at_edge (X_AXIS,  RIGHT, acc, 0.1,0);
218         }
219     }
220
221   if (parens)
222     mol = parenthesize (me, mol); 
223
224   return mol.smobbed_copy();
225 }
226
227
228
229 ADD_INTERFACE (Accidental_interface, "accidental-interface",
230               "a single accidental",
231                "cautionary cautionary-style style tie accidentals");