]> git.donarmstrong.com Git - lilypond.git/blob - lily/porrectus.cc
release: 1.5.47
[lilypond.git] / lily / porrectus.cc
1 /*
2   porrectus.cc -- implement Porrectus
3
4   Copyright (c) 2001--2002  Juergen Reuter
5
6   written for the GNU LilyPond music typesetter
7
8   TODO: --> see porrectus-engraver.cc
9 */
10
11 #include "staff-symbol-referencer.hh"
12 #include "porrectus.hh"
13 #include "item.hh"
14 #include "molecule.hh"
15 #include "pitch.hh"
16 #include "lookup.hh"
17 #include "debug.hh"
18 #include "dimensions.hh"
19 #include "direction.hh"
20 #include "bezier.hh"
21 #include "font-interface.hh"
22 #include "paper-def.hh"
23 #include "math.h" // rint
24
25 void
26 Porrectus::set_left_head (Grob *me, Item *left_head)
27 {
28   if (left_head != 0)
29     {
30       me->set_grob_property ("left-head", left_head->self_scm());
31     }
32   else
33     {
34       programming_error (_ ("(left_head == 0)"));
35       me->set_grob_property ("left-head", SCM_EOL);
36     }
37 }
38
39 Item *
40 Porrectus::get_left_head (Grob *me)
41 {
42   SCM left_head_scm = me->get_grob_property ("left-head");
43   if (left_head_scm == SCM_EOL)
44     {
45       programming_error (_ ("undefined left_head"));
46       return 0;
47     }
48   else
49     {
50       Item *left_head = unsmob_item (left_head_scm);
51       return left_head;
52     }
53 }
54
55 void
56 Porrectus::set_right_head (Grob *me, Item *right_head)
57 {
58   if (right_head != 0)
59     {
60       me->set_grob_property ("right-head", right_head->self_scm());
61     }
62   else
63     {
64       programming_error (_ ("(right_head == 0)"));
65       me->set_grob_property ("right-head", SCM_EOL);
66     }
67 }
68
69 Item *
70 Porrectus::get_right_head (Grob *me)
71 {
72   SCM right_head_scm = me->get_grob_property ("right-head");
73   if (right_head_scm == SCM_EOL)
74     {
75       programming_error (_ ("undefined right_head"));
76       return 0;
77     }
78   else
79     {
80       Item *right_head = unsmob_item (right_head_scm);
81       return right_head;
82     }
83 }
84
85 // Uugh.  The following two functions are almost duplicated code from
86 // custos.cc, which itself is similar to code in note-head.cc.  Maybe
87 // this should be moved to staff-symbol-referencer.cc?
88 Molecule
89 Porrectus::create_ledger_line (Interval x_extent, Grob *me) 
90 {
91   Molecule line;
92   Molecule slice = Font_interface::get_default_font (me)->find_by_name ("noteheads-ledgerending");
93   Interval slice_x_extent = slice.extent (X_AXIS);
94   Interval slice_y_extent = slice.extent (Y_AXIS);
95
96   // Create left ending of ledger line.
97   Molecule left_ending = slice;
98   left_ending.translate_axis (x_extent[LEFT] - slice_x_extent[LEFT], X_AXIS);
99   if (x_extent.length () > slice_x_extent.length ())
100     line.add_molecule (left_ending);
101
102   // Create right ending of ledger line.
103   Molecule right_ending = slice;
104   right_ending.translate_axis (x_extent[RIGHT] - slice_x_extent[RIGHT],
105                                X_AXIS);
106   line.add_molecule (right_ending);
107
108   // Fill out space between left and right ending of ledger line by
109   // lining up a series of slices in a row between them.
110   Molecule fill_out_slice = left_ending;
111   Real thick = slice_y_extent.length ();
112   Real delta_x = slice_x_extent.length () - thick;
113   Real xpos = x_extent [LEFT] + 2*delta_x + thick/2; // TODO: check: thick*2?
114   while (xpos <= x_extent[RIGHT])
115     {
116       fill_out_slice.translate_axis (delta_x, X_AXIS);
117       line.add_molecule (fill_out_slice);
118       xpos += delta_x;
119     }
120
121   return line;
122 }
123
124 Molecule
125 Porrectus::create_streepjes (Grob *me,
126                              int pos,
127                              int interspaces,
128                              Interval extent)
129 {
130   Real inter_f = Staff_symbol_referencer::staff_space (me)/2;
131   int streepjes_i = abs (pos) < interspaces
132     ? 0
133     : (abs (pos) - interspaces) /2;
134   Molecule molecule = Molecule();
135   if (streepjes_i) 
136     {
137       Direction dir = (Direction)sign (pos);
138       Molecule ledger_line (create_ledger_line (extent, me));
139       ledger_line.set_empty (true);
140       Real offs = (Staff_symbol_referencer::on_staffline (me, pos))
141         ? 0.0
142         : -dir * inter_f;
143       for (int i = 0; i < streepjes_i; i++)
144         {
145           Molecule streep (ledger_line);
146           streep.translate_axis (-dir * inter_f * i * 2 + offs,
147                                  Y_AXIS);
148           molecule.add_molecule (streep);
149         }
150     }
151   return molecule;
152 }
153
154 MAKE_SCHEME_CALLBACK (Porrectus,brew_molecule,1);
155 SCM 
156 Porrectus::brew_molecule (SCM smob)
157 {
158   Item *me = (Item *)unsmob_grob (smob);
159
160   Item *left_head = get_left_head (me);
161   Item *right_head = get_right_head (me);
162   if (!left_head || !right_head)
163     {
164       me->warning (_ ("junking lonely porrectus"));
165       me->suicide ();
166       return SCM_EOL;
167     }
168
169   SCM scm_style = me->get_grob_property ("style");
170   String style;
171   if ((gh_symbol_p (scm_style)) && (scm_style != SCM_EOL))
172     style = ly_scm2string (scm_symbol_to_string (scm_style));
173   else {
174     me->warning (_ ("porrectus style undefined; using mensural"));
175     style = "mensural";
176   }
177
178   bool solid = to_boolean (me->get_grob_property ("solid"));
179   bool add_stem = to_boolean (me->get_grob_property ("add-stem"));
180
181   /*
182
183   TODO:
184
185   ugr. why not  called direction?
186     
187    */
188   SCM stem_direction_scm = me->get_grob_property ("direction");
189   Direction stem_direction =
190     gh_number_p (stem_direction_scm) ? to_dir (stem_direction_scm) : DOWN;
191   if (!stem_direction)
192     stem_direction = DOWN;
193
194
195   /*
196     TODO: revise name.
197    */
198   bool auto_properties = to_boolean (me->get_grob_property ("auto-properties"));
199   if (auto_properties)
200       // determine add_stem and stem_direction automatically from durations
201     {
202       if (String::compare_i (style, "mensural") != 0)
203         me->warning (String("auto-property should be used for\r\n") +
204                  String("mensural style porrectus only; trying anyway"));
205
206       int left_duration =
207           gh_scm2int (left_head->get_grob_property ("duration-log"));
208       int right_duration =
209           gh_scm2int (right_head->get_grob_property ("duration-log"));
210
211       if ((left_duration == -1) && (right_duration == -1))
212         {
213           // brevis -- brevis:
214           // cum proprietate et sine perfectione (c.s.)
215           add_stem = true;
216           stem_direction = DOWN;
217         }
218       else if ((left_duration == -2) && (right_duration == -1))
219         {
220           // longa -- brevis:
221           // sine proprietate et sine perfectione (s.s.)
222           add_stem = false;
223         }
224       else if ((left_duration == 0) && (right_duration == 0))
225         {
226           // semibrevis -- semibrevis:
227           // cum opposita proprietate (c.o.p.)
228           add_stem = true;
229           stem_direction = UP;
230         }
231       else
232         {
233           me->warning (String("auto-property: failed determining porrectus\r\n") +
234                    String("properties due to improper durations; ") +
235                    String("using user-supplied properties"));
236         }
237     }
238
239   Real left_position_f = Staff_symbol_referencer::position_f (left_head);
240   Real right_position_f = Staff_symbol_referencer::position_f (right_head);
241   Real interval = right_position_f - left_position_f;
242
243   Molecule molecule;
244
245   SCM line_thickness_scm = me->get_grob_property ("thickness");
246   Real line_thickness;
247   if (gh_number_p (line_thickness_scm))
248     {
249       line_thickness = gh_scm2double (line_thickness_scm);
250     }
251   else
252     {
253       line_thickness = 1.0;
254     }
255   Real thickness =
256     line_thickness * me->paper_l ()->get_var ("stafflinethickness");
257
258   SCM porrectus_width_scm = me->get_grob_property ("width");
259   Real porrectus_width;
260   if (gh_number_p (porrectus_width_scm))
261     {
262       porrectus_width = gh_scm2double (porrectus_width_scm);
263     }
264   else
265     {
266       porrectus_width = 2.4;
267     }
268   Real width = porrectus_width * Staff_symbol_referencer::staff_space (me);
269
270   if (String::compare_i (style, "vaticana") == 0)
271     molecule = brew_vaticana_molecule (me, interval,
272                                        solid, width, thickness,
273                                        add_stem, stem_direction);
274   else if (String::compare_i (style, "mensural") == 0)
275     molecule = brew_mensural_molecule (me, interval,
276                                        solid, width, thickness,
277                                        add_stem, stem_direction);
278   else
279     return SCM_EOL;
280
281   Real space = Staff_symbol_referencer::staff_space (me);
282   Real head_extent = molecule.extent (X_AXIS).length ();
283   Interval extent (-0.2 * head_extent, 1.2 * head_extent);
284   int interspaces = Staff_symbol_referencer::line_count (me)-1;
285
286   molecule.translate_axis (left_position_f * space/2, Y_AXIS);
287
288   Molecule left_head_streepjes =
289     create_streepjes (me, (int)rint (left_position_f), interspaces, extent);
290   left_head_streepjes.translate_axis (left_position_f * space/2, Y_AXIS);
291   molecule.add_molecule (left_head_streepjes);
292
293   Molecule right_head_streepjes =
294     create_streepjes (me, (int)rint (right_position_f), interspaces, extent);
295   right_head_streepjes.translate_axis (right_position_f * space/2, Y_AXIS);
296   molecule.add_molecule (right_head_streepjes);
297
298   return molecule.smobbed_copy();
299 }
300
301 Molecule
302 Porrectus::brew_vaticana_molecule (Item *me,
303                                    Real interval,
304                                    bool solid,
305                                    Real width,
306                                    Real thickness,
307                                    bool add_stem,
308                                    Direction stem_direction)
309 {
310   if (interval >= 0.0)
311     {
312       me->warning (_ ("ascending vaticana style porrectus"));
313     }
314
315   Real space = Staff_symbol_referencer::staff_space (me);
316   Molecule molecule = Molecule ();
317   Real right_height = 0.6 * space;
318
319   // Compensate thickness that appears to be smaller in steep section
320   // of bend.
321   Real left_height = right_height + min (0.12 * abs(interval), 0.3) * space;
322
323   if (add_stem)
324     {
325       bool consider_interval =
326         stem_direction * interval > 0.0;
327
328       Interval stem_box_x (0, thickness);
329       Interval stem_box_y;
330
331       if (consider_interval)
332         {
333           Real y_length = max (abs(interval)/2.0*space +
334                                (right_height-left_height),
335                                1.2*space);
336           stem_box_y = Interval (0, y_length);
337         }
338       else
339         stem_box_y = Interval (0, space);
340
341       Real y_correction =
342         (stem_direction == UP) ?
343         +0.5*left_height :
344         -0.5*left_height - stem_box_y.length();
345
346       Box stem_box (stem_box_x, stem_box_y);
347       Molecule stem = Lookup::filledbox (stem_box);
348       stem.translate_axis (y_correction, Y_AXIS);
349       molecule.add_molecule(stem);
350     }
351
352   // Compensate optical illusion regarding vertical position of left
353   // and right endings due to curved shape.
354   Real ypos_correction = -0.1*space * sign(interval);
355   Real interval_correction = 0.2*space * sign(interval);
356   Real corrected_interval = interval*space + interval_correction;
357
358   // middle curve of vaticana style porrectus
359   Bezier curve;
360   curve.control_[0] = Offset (0.00 * width, 0.0);
361   curve.control_[1] = Offset (0.33 * width, corrected_interval / 2.0);
362   curve.control_[2] = Offset (0.66 * width, corrected_interval / 2.0);
363   curve.control_[3] = Offset (1.00 * width, corrected_interval / 2.0);
364
365   Bezier top_curve = curve, bottom_curve = curve;
366   for (int i = 0; i < 4; i++)
367     {
368       Real thickness = 0.33 * ((3 - i)*left_height + i*right_height);
369       top_curve.control_[i] += Offset (0, +0.5*thickness);
370       bottom_curve.control_[i] += Offset (0, -0.5*thickness);
371     }
372
373   if (solid)
374     {
375       Molecule solid_head =
376         Lookup::bezier_sandwich (top_curve, bottom_curve);
377       molecule.add_molecule (solid_head);
378     }
379   else // outline
380     {
381       Bezier inner_top_curve = top_curve;
382       inner_top_curve.translate (Offset (0.0, -thickness));
383       Molecule top_edge =
384         Lookup::bezier_sandwich (top_curve, inner_top_curve);
385       molecule.add_molecule(top_edge);
386
387       Bezier inner_bottom_curve = bottom_curve;
388       inner_bottom_curve.translate (Offset (0.0, +thickness));
389       Molecule bottom_edge =
390         Lookup::bezier_sandwich (bottom_curve, inner_bottom_curve);
391       molecule.add_molecule(bottom_edge);
392
393       // TODO: Use horizontal slope with proper slope value rather
394       // than filled box for left edge, since the filled box stands
395       // out from the porrectus shape if the interval is big and the
396       // line thickness small.  The difficulty here is to compute a
397       // proper slope value, as it should roughly be equal with the
398       // slope of the left end of the bezier curve.
399       Box left_edge_box (Interval (0, thickness),
400                          Interval (-0.5*left_height, +0.5*left_height));
401       Molecule left_edge = Lookup::filledbox (left_edge_box);
402       molecule.add_molecule(left_edge);
403
404       Box right_edge_box (Interval (-thickness, 0),
405                           Interval (-0.5*right_height, +0.5*right_height));
406       Molecule right_edge = Lookup::filledbox (right_edge_box);
407       right_edge.translate_axis (width, X_AXIS);
408       right_edge.translate_axis (corrected_interval / 2.0, Y_AXIS);
409       molecule.add_molecule(right_edge);
410     }
411   molecule.translate_axis (ypos_correction, Y_AXIS);
412   return molecule;
413 }
414
415 Molecule
416 Porrectus::brew_mensural_molecule (Item *me,
417                                    Real interval,
418                                    bool solid,
419                                    Real width,
420                                    Real thickness,
421                                    bool add_stem,
422                                    Direction stem_direction)
423 {
424   Real space = Staff_symbol_referencer::staff_space (me);
425   Real height = 0.6 * space;
426   Molecule molecule = Molecule ();
427
428   if (add_stem)
429     {
430       bool consider_interval =
431         stem_direction * interval > 0.0;
432
433       Interval stem_box_x (0, thickness);
434       Interval stem_box_y;
435
436       if (consider_interval)
437         {
438           Real y_length = max (interval/2.0*space, 1.2*space);
439           stem_box_y = Interval (0, y_length);
440         }
441       else
442         stem_box_y = Interval (0, space);
443
444       Real y_correction =
445         (stem_direction == UP) ?
446         +0.5*height :
447         -0.5*height - stem_box_y.length();
448
449       Box stem_box (stem_box_x, stem_box_y);
450       Molecule stem = Lookup::filledbox (stem_box);
451       stem.translate_axis (y_correction, Y_AXIS);
452       molecule.add_molecule(stem);
453     }
454
455   Real slope = (interval / 2.0 * space) / width;
456
457   // Compensate optical illusion regarding vertical position of left
458   // and right endings due to slope.
459   Real ypos_correction = -0.1*space * sign(slope);
460   Real slope_correction = 0.2*space * sign(slope);
461   Real corrected_slope = slope + slope_correction/width;
462
463   if (solid)
464     {
465       Molecule solid_head =
466         Lookup::horizontal_slope (width, corrected_slope, height);
467       molecule.add_molecule (solid_head);
468     }
469   else // outline
470     {
471       Molecule left_edge =
472         Lookup::horizontal_slope (thickness, corrected_slope, height);
473       molecule.add_molecule(left_edge);
474
475       Molecule right_edge =
476         Lookup::horizontal_slope (thickness, corrected_slope, height);
477       right_edge.translate_axis (width-thickness, X_AXIS);
478       right_edge.translate_axis (corrected_slope * (width-thickness), Y_AXIS);
479       molecule.add_molecule(right_edge);
480
481       Molecule bottom_edge =
482         Lookup::horizontal_slope (width, corrected_slope, thickness);
483       bottom_edge.translate_axis (-0.5*height, Y_AXIS);
484       molecule.add_molecule (bottom_edge);
485
486       Molecule top_edge =
487         Lookup::horizontal_slope (width, corrected_slope, thickness);
488       top_edge.translate_axis (+0.5*height, Y_AXIS);
489       molecule.add_molecule (top_edge);
490     }
491   molecule.translate_axis (ypos_correction, Y_AXIS);
492   return molecule;
493 }
494
495
496 ADD_INTERFACE (Porrectus,"porrectus-interface",
497   "A porrectus ligature, joining two note heads into a single grob.",
498   "left-head right-head width add-stem auto-properties solid direction");
499