2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
17 #include "dimensions.hh"
19 #include "string-convert.hh"
20 #include "file-path.hh"
22 #include "lily-guile.hh"
23 #include "molecule.hh"
25 #include "font-metric.hh"
26 #include "interval.hh"
29 Lookup::dot (Offset p, Real radius)
31 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
32 gh_double2scm (p[X_AXIS]),
33 gh_double2scm (p[Y_AXIS]),
34 gh_double2scm (radius),
37 box.add_point (p - Offset (radius, radius));
38 box.add_point (p + Offset (radius, radius));
39 return Molecule (box, at);
43 Lookup::beam (Real slope, Real width, Real thick)
45 Real height = slope * width;
46 Real min_y = (0 <? height) - thick/2;
47 Real max_y = (0 >? height) + thick/2;
51 Box b (Interval (0, width),
52 Interval (min_y, max_y));
55 SCM at = scm_list_n (ly_symbol2scm ("beam"),
56 gh_double2scm (width),
57 gh_double2scm (slope),
58 gh_double2scm (thick),
60 return Molecule (b, at);
64 Lookup::dashed_slur (Bezier b, Real thick, Real dash)
68 for (int i= 4; i -- ;)
70 l = gh_cons (ly_offset2scm (b.control_[i]), l);
73 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
74 gh_double2scm (thick),
79 Box box (Interval (0,0),Interval (0,0));
80 return Molecule (box, at);
84 Lookup::line (Real th, Offset f, Offset t)
86 SCM at = (scm_list_n (ly_symbol2scm ("draw-line"),
88 gh_double2scm (f[X_AXIS]),
89 gh_double2scm (f[Y_AXIS]),
90 gh_double2scm (t[X_AXIS]),
91 gh_double2scm (t[Y_AXIS]),
98 box[X_AXIS].widen (th/2);
99 box[Y_AXIS].widen (th/2);
101 return Molecule (box, at);
106 Lookup::blank (Box b)
108 return Molecule (b, scm_makfrom0str (""));
112 Lookup::filledbox (Box b)
114 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
115 gh_double2scm (-b[X_AXIS][LEFT]),
116 gh_double2scm (b[X_AXIS][RIGHT]),
117 gh_double2scm (-b[Y_AXIS][DOWN]),
118 gh_double2scm (b[Y_AXIS][UP]),
121 return Molecule (b,at);
127 * __________________________________
132 * |\ _ _ / v \ _ _ /| |
135 * | <------>| | extent
136 * | blot | | (Y_AXIS)
144 * x\_____/______________\_____/|_____v
148 * |<-------------------------->|
152 Lookup::roundfilledbox (Box b, Real blotdiameter)
154 if (b.x ().length () < blotdiameter)
156 programming_error (_f ("round filled box horizontal extent smaller than blot; decreasing blot"));
157 blotdiameter = b.x ().length ();
159 if (b.y ().length () < blotdiameter)
161 programming_error (_f ("round filled box vertical extent smaller than blot; decreasing blot"));
162 blotdiameter = b.y ().length ();
165 SCM at = (scm_list_n (ly_symbol2scm ("roundfilledbox"),
166 gh_double2scm (-b[X_AXIS][LEFT]),
167 gh_double2scm (b[X_AXIS][RIGHT]),
168 gh_double2scm (-b[Y_AXIS][DOWN]),
169 gh_double2scm (b[Y_AXIS][UP]),
170 gh_double2scm (blotdiameter),
173 return Molecule (b,at);
176 LY_DEFINE(ly_round_filled_box, "ly:round-filled-box",
179 "Make a box with rounded corners. B is a pair of number-pairs, and BLOT a number")
181 SCM_ASSERT_TYPE(gh_number_p (blot), blot, SCM_ARG2, __FUNCTION__, "number") ;
182 SCM_ASSERT_TYPE(gh_pair_p (b), b, SCM_ARG4, __FUNCTION__, "pair") ;
184 SCM_ASSERT_TYPE(ly_number_pair_p (gh_car (b)), gh_car (b), SCM_ARG1, __FUNCTION__, "number-pair") ;
185 SCM_ASSERT_TYPE(ly_number_pair_p (gh_cdr (b)), gh_cdr (b), SCM_ARG1, __FUNCTION__, "number-pair") ;
187 Interval x (ly_scm2interval (gh_car (b)));
188 Interval y (ly_scm2interval (gh_cdr (b)));
190 return Lookup::roundfilledbox (Box (x,y),
191 gh_scm2double (blot)).smobbed_copy();
197 * Create Molecule that represents a filled polygon with round edges.
201 * (a) Only outer (convex) edges are rounded.
203 * (b) This algorithm works as expected only for polygons whose edges
204 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
205 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
206 * will give a strange result. Even non-adjacent edges that just
207 * touch each other will in general not work as expected for non-null
210 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x(n-1), y(n-1))),
211 * if there is a natural number k such that blotdiameter is greater
212 * than the maximum of { | (x(k mod n), y(k mod n)) - (x((k+1) mod n),
213 * y((k+1) mod n)) |, | (x(k mod n), y(k mod n)) - (x((k+2) mod n),
214 * y((k+2) mod n)) |, | (x((k+1) mod n), y((k+1) mod n)) - (x((k+2)
215 * mod n), y((k+2) mod n)) | }, then the outline of the rounded
216 * polygon will exceed the outline of the core polygon. In other
217 * words: Do not draw rounded polygons that have a leg smaller or
218 * thinner than blotdiameter (or set blotdiameter to a sufficiently
219 * small value -- maybe even 0.0)!
221 * NOTE: Limitations (b) and (c) arise from the fact that round edges
222 * are made by moulding sharp edges to round ones rather than adding
223 * to a core filled polygon. For details of these two different
224 * approaches, see the thread upon the ledger lines patch that started
225 * on March 25, 2002 on the devel mailing list. The below version of
226 * round_filled_polygon() sticks to the moulding model, which the
227 * majority of the list participants finally voted for. This,
228 * however, results in the above limitations and a much increased
229 * complexity of the algorithm, since it has to compute a shrinked
230 * polygon -- which is not trivial define precisely and unambigously.
231 * With the other approach, one simply could move a circle of size
232 * blotdiameter along all edges of the polygon (which is what the
233 * postscript routine in the backend effectively does, but on the
234 * shrinked polygon). --jr
237 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
239 /* TODO: Maybe print a warning if one of the above limitations
240 applies to the given polygon. However, this is quite complicated
243 /* remove consecutive duplicate points */
244 const Real epsilon = 0.01;
245 for (int i = 0; i < points.size ();)
247 int next_i = (i + 1) % points.size ();
248 Real d = (points[i] - points[next_i]).length ();
255 /* special cases: degenerated polygons */
256 if (points.size () == 0)
258 if (points.size () == 1)
259 return dot (points[0], 0.5 * blotdiameter);
260 if (points.size () == 2)
261 return line (blotdiameter, points[0], points[1]);
263 /* shrink polygon in size by 0.5 * blotdiameter */
264 Array<Offset> shrinked_points;
265 shrinked_points.set_size (points.size ());
266 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
267 for (int i = 0; i < points.size (); i++)
270 int i1 = (i + 1) % points.size ();
271 int i2 = (i + 2) % points.size ();
272 Offset p0 = points[i0];
273 Offset p1 = points[i1];
274 Offset p2 = points[i2];
275 Offset p10 = p0 - p1;
276 Offset p12 = p2 - p1;
277 if (p10.length () != 0.0)
279 Real phi = p10.arg ();
280 // rotate (p2 - p0) by (-phi)
281 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
285 else if (q[Y_AXIS] < 0)
287 else {} // keep ccw unchanged
289 else {} // keep ccw unchanged
290 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
291 Offset p12n = (1.0 / p12.length ()) * p12;
292 Offset p13n = 0.5 * (p10n + p12n);
293 Offset p14n = 0.5 * (p10n - p12n);
295 Real d = p13n.length () * p14n.length (); // distance p3n to line(p1..p0)
297 // special case: p0, p1, p2 are on a single line => build
298 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
300 p13[X_AXIS] = p10[Y_AXIS];
301 p13[Y_AXIS] = -p10[X_AXIS];
302 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
305 p13 = (0.5 * blotdiameter / d) * p13n;
306 shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
309 /* build scm expression and bounding box */
310 SCM shrinked_points_scm = SCM_EOL;
312 for (int i = 0; i < shrinked_points.size (); i++)
314 SCM x = gh_double2scm (shrinked_points[i][X_AXIS]);
315 SCM y = gh_double2scm (shrinked_points[i][Y_AXIS]);
316 shrinked_points_scm = gh_cons (x, gh_cons (y, shrinked_points_scm));
317 box.add_point (points[i]);
319 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
320 ly_quote_scm (shrinked_points_scm),
321 gh_double2scm (blotdiameter),
324 Molecule polygon = Molecule (box, polygon_scm);
325 shrinked_points.clear ();
330 Lookup::frame (Box b, Real thick)
334 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
336 Axis o = Axis ((a+1)%NO_AXES);
340 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
341 edges[o][DOWN] = b[o][DOWN] - thick/2;
342 edges[o][UP] = b[o][UP] + thick/2;
344 m.add_molecule (filledbox (edges));
346 while (flip (&d) != LEFT);
353 Make a smooth curve along the points
356 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
358 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
360 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
362 back.control_[1] += perp;
363 back.control_[2] += perp;
365 curve.control_[1] -= perp;
366 curve.control_[2] -= perp;
371 scontrols[ i ] = ly_offset2scm (back.control_[i]);
373 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
376 Need the weird order b.o. the way PS want its arguments
378 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
382 list = gh_cons (scontrols[indices[i]], list);
386 SCM at = (scm_list_n (ly_symbol2scm ("bezier-bow"),
388 gh_double2scm (linethick),
390 Box b(curve.extent (X_AXIS),
391 curve.extent (Y_AXIS));
393 b[X_AXIS].unite (back.extent (X_AXIS));
394 b[Y_AXIS].unite (back.extent (Y_AXIS));
396 return Molecule (b, at);
423 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
426 Need the weird order b.o. the way PS want its arguments
429 list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
430 list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
431 list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
432 list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
433 list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
434 list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
435 list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
436 list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
438 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
443 Interval x_extent = top_curve.extent (X_AXIS);
444 x_extent.unite (bottom_curve.extent (X_AXIS));
445 Interval y_extent = top_curve.extent (Y_AXIS);
446 y_extent.unite (bottom_curve.extent (Y_AXIS));
447 Box b (x_extent, y_extent);
449 return Molecule (b, horizontal_bend);
462 * (0,0) x /slope=dy/dx
470 Lookup::horizontal_slope (Real width, Real slope, Real height)
472 SCM width_scm = gh_double2scm (width);
473 SCM slope_scm = gh_double2scm (slope);
474 SCM height_scm = gh_double2scm (height);
475 SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
476 width_scm, slope_scm,
477 height_scm, SCM_UNDEFINED);
478 Box b (Interval (0, width),
479 Interval (-height/2, height/2 + width*slope));
480 return Molecule (b, horizontal_slope);
487 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
490 String sym = ly_scm2string (ly_car (s));
491 String reg = ly_scm2string (ly_car (ly_cdr (s)));
493 if (sym == "Discant")
495 Molecule r = fm->find_by_name ("accordion-accDiscant");
497 if (reg.left_string (1) == "F")
499 Molecule d = fm->find_by_name ("accordion-accDot");
500 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
502 reg = reg.right_string (reg.length ()-1);
505 if (reg.left_string (3) == "EEE")
508 reg = reg.right_string (reg.length ()-3);
510 else if (reg.left_string (2) == "EE")
513 reg = reg.right_string (reg.length ()-2);
515 else if (reg.left_string (2) == "Eh")
518 reg = reg.right_string (reg.length ()-2);
520 else if (reg.left_string (1) == "E")
523 reg = reg.right_string (reg.length ()-1);
527 Molecule d = fm->find_by_name ("accordion-accDot");
528 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
533 Molecule d = fm->find_by_name ("accordion-accDot");
534 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
535 d.translate_axis (0.8 * staff_space PT, X_AXIS);
540 Molecule d = fm->find_by_name ("accordion-accDot");
541 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
542 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
545 if (reg.left_string (2) == "SS")
547 Molecule d = fm->find_by_name ("accordion-accDot");
548 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
549 d.translate_axis (0.4 * staff_space PT, X_AXIS);
551 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
553 reg = reg.right_string (reg.length ()-2);
555 if (reg.left_string (1) == "S")
557 Molecule d = fm->find_by_name ("accordion-accDot");
558 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
560 reg = reg.right_string (reg.length ()-1);
563 else if (sym == "Freebase")
565 Molecule r = fm->find_by_name ("accordion-accFreebase");
567 if (reg.left_string (1) == "F")
569 Molecule d = fm->find_by_name ("accordion-accDot");
570 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
572 reg = reg.right_string (reg.length ()-1);
576 Molecule d = fm->find_by_name ("accordion-accDot");
577 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
581 else if (sym == "Bayanbase")
583 Molecule r = fm->find_by_name ("accordion-accBayanbase");
585 if (reg.left_string (1) == "T")
587 Molecule d = fm->find_by_name ("accordion-accDot");
588 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
590 reg = reg.right_string (reg.length ()-1);
592 /* include 4' reed just for completeness. You don't want to use this. */
593 if (reg.left_string (1) == "F")
595 Molecule d = fm->find_by_name ("accordion-accDot");
596 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
598 reg = reg.right_string (reg.length ()-1);
600 if (reg.left_string (2) == "EE")
602 Molecule d = fm->find_by_name ("accordion-accDot");
603 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
604 d.translate_axis (0.4 * staff_space PT, X_AXIS);
606 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
608 reg = reg.right_string (reg.length ()-2);
610 if (reg.left_string (1) == "E")
612 Molecule d = fm->find_by_name ("accordion-accDot");
613 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
615 reg = reg.right_string (reg.length ()-1);
618 else if (sym == "Stdbase")
620 Molecule r = fm->find_by_name ("accordion-accStdbase");
622 if (reg.left_string (1) == "T")
624 Molecule d = fm->find_by_name ("accordion-accDot");
625 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
627 reg = reg.right_string (reg.length ()-1);
629 if (reg.left_string (1) == "F")
631 Molecule d = fm->find_by_name ("accordion-accDot");
632 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
634 reg = reg.right_string (reg.length ()-1);
636 if (reg.left_string (1) == "M")
638 Molecule d = fm->find_by_name ("accordion-accDot");
639 d.translate_axis (staff_space * 2 PT, Y_AXIS);
640 d.translate_axis (staff_space PT, X_AXIS);
642 reg = reg.right_string (reg.length ()-1);
644 if (reg.left_string (1) == "E")
646 Molecule d = fm->find_by_name ("accordion-accDot");
647 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
649 reg = reg.right_string (reg.length ()-1);
651 if (reg.left_string (1) == "S")
653 Molecule d = fm->find_by_name ("accordion-accDot");
654 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
656 reg = reg.right_string (reg.length ()-1);
659 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
661 else if (sym == "SB")
663 Molecule r = fm->find_by_name ("accordion-accSB");
666 else if (sym == "BB")
668 Molecule r = fm->find_by_name ("accordion-accBB");
671 else if (sym == "OldEE")
673 Molecule r = fm->find_by_name ("accordion-accOldEE");
676 else if (sym == "OldEES")
678 Molecule r = fm->find_by_name ("accordion-accOldEES");
685 Lookup::repeat_slash (Real w, Real s, Real t)
687 SCM wid = gh_double2scm (w);
688 SCM sl = gh_double2scm (s);
689 SCM thick = gh_double2scm (t);
690 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
691 wid, sl, thick, SCM_UNDEFINED);
693 Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
694 Interval (0, w * s));
696 return Molecule (b, slashnodot); // http://slashnodot.org
700 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude)
703 Axis other = Axis((a+1)%2);
705 b[other] = Interval(-1, 1) * thick * 0.5;
707 Molecule m = filledbox (b);
709 b[a] = Interval (iv[UP] - thick, iv[UP]);
710 Interval oi = Interval (-thick/2, thick/2 + fabs (protude)) ;
711 oi *= sign (protude);
713 m.add_molecule (filledbox (b));
714 b[a] = Interval (iv[DOWN], iv[DOWN] +thick);
715 m.add_molecule (filledbox(b));
721 Lookup::triangle (Interval iv, Real thick, Real protude)
725 b[Y_AXIS] = Interval (0 <? protude , 0 >? protude);
727 SCM s = scm_list_n (ly_symbol2scm ("symmetric-x-triangle"),
728 gh_double2scm (thick),
729 gh_double2scm (iv.length()),
730 gh_double2scm (protude), SCM_UNDEFINED);
732 return Molecule (b, s);
737 TODO: use rounded boxes.
739 LY_DEFINE(ly_bracket ,"ly:bracket",
741 (SCM a, SCM iv, SCM t, SCM p),
742 "Make a bracket in direction @var{a}. The extent of the bracket is "
743 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
744 "may be negative. The thickness is given by @var{t}.")
746 SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
747 SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
748 SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG3, __FUNCTION__, "number") ;
749 SCM_ASSERT_TYPE(gh_number_p (p), a, SCM_ARG4, __FUNCTION__, "number") ;
752 return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
754 gh_scm2double (p)).smobbed_copy ();