]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental.cc
($(outdir)/%.pdf): add DVIPS_FLAGS. This will
[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_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       /*
90         TODO: add support for natural, double flat.
91        */
92     }
93
94   if (!boxes.size())
95     boxes.push (b);
96
97   Offset o (a->relative_coordinate (common[X_AXIS],  X_AXIS),
98             a->relative_coordinate (common[Y_AXIS],  Y_AXIS));
99   for(int i = boxes.size(); i--;)
100     {
101       boxes[i].translate(o);
102     }
103   
104   return boxes;
105 }
106
107 /*
108  * Some styles do not provide all flavours of accidentals, e.g. there
109  * is currently no sharp accidental in vaticana style.  In these cases
110  * this function falls back to one of the other styles.
111  */
112
113 /*
114   todo: this sort of stuff in Scheme. --hwn.
115  */
116 String
117 Accidental_interface::get_fontcharname (String style, int alteration)
118 {
119   if (alteration == DOUBLE_FLAT
120       || alteration == DOUBLE_SHARP)
121     {
122       return to_string (alteration);
123     }
124   
125   if (style == "hufnagel")
126     switch (alteration)
127       {
128       case FLAT: return "hufnagel-1";
129       case 0: return "vaticana0";
130       case SHARP: return "mensural1";
131       }
132   if (style == "medicaea")
133     switch (alteration)
134       {
135       case FLAT: return "medicaea-1";
136       case 0: return "vaticana0";
137       case SHARP: return "mensural1";
138       }
139   if (style == "vaticana")
140     switch (alteration)
141       {
142       case FLAT: return "vaticana-1";
143       case 0: return "vaticana0";
144       case SHARP: return "mensural1";
145       }
146   if (style == "mensural")
147     switch (alteration)
148       {
149       case FLAT: return "mensural-1";
150       case 0: return "vaticana0";
151       case SHARP: return "mensural1";
152       }
153   
154   if (style == "neo_mensural")
155     style = ""; // currently same as default
156   if (style == "default")
157     style = "";
158   return style + to_string (alteration);
159 }
160
161 MAKE_SCHEME_CALLBACK (Accidental_interface,print,1);
162 SCM
163 Accidental_interface::print (SCM smob)
164 {
165   Grob *me = unsmob_grob (smob);
166   bool smaller = false;
167   bool parens = false;
168
169   bool caut  = to_boolean (me->get_grob_property ("cautionary"));
170   if (caut)
171     {
172       SCM cstyle = me->get_grob_property ("cautionary-style");
173       parens = gh_equal_p (cstyle, ly_symbol2scm ("parentheses"));
174       smaller = gh_equal_p (cstyle, ly_symbol2scm ("smaller"));
175     }
176
177   SCM scm_style = me->get_grob_property ("style");
178   String style;
179   if (gh_symbol_p (scm_style))
180     {
181       style = ly_symbol2string (scm_style);
182     }
183   else
184     {
185       /*
186         preferably no name for the default style.
187       */
188       style = "";
189     }
190
191   Font_metric *fm = 0;
192   if (smaller)
193     {
194       SCM ac = Font_interface::font_alist_chain (me);
195       ac = gh_cons (gh_cons (gh_cons
196                              (ly_symbol2scm ("font-relative-size"),
197                               scm_int2num (-1)), SCM_EOL),
198                     ac);
199       fm = select_font (me->get_paper (), ac);
200     }
201   else
202     fm = Font_interface::get_default_font (me);
203
204   Stencil mol;
205   for (SCM s = me->get_grob_property ("accidentals");
206        gh_pair_p (s); s = gh_cdr (s))
207     {
208       int alteration = gh_scm2int (gh_car (s));
209       String font_char = get_fontcharname (style, alteration);
210       Stencil acc (fm->find_by_name ("accidentals-" + font_char));
211
212       if (acc.is_empty ())
213         {
214           me->warning (_f ("accidental `%s' not found", font_char));
215         }
216       else
217         {
218           mol.add_at_edge (X_AXIS,  RIGHT, acc, 0.1,0);
219         }
220     }
221
222   if (parens)
223     mol = parenthesize (me, mol); 
224
225   return mol.smobbed_copy();
226 }
227
228
229
230 ADD_INTERFACE (Accidental_interface, "accidental-interface",
231               "a single accidental",
232                "cautionary cautionary-style style tie accidentals");