2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2002 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);
177 * Create Molecule that represents a filled polygon with round edges.
181 * (a) Only outer (convex) edges are rounded.
183 * (b) This algorithm works as expected only for polygons whose edges
184 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
185 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
186 * will give a strange result. Even non-adjacent edges that just
187 * touch each other will in general not work as expected for non-null
190 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x(n-1), y(n-1))),
191 * if there is a natural number k such that blotdiameter is greater
192 * than the maximum of { | (x(k mod n), y(k mod n)) - (x((k+1) mod n),
193 * y((k+1) mod n)) |, | (x(k mod n), y(k mod n)) - (x((k+2) mod n),
194 * y((k+2) mod n)) |, | (x((k+1) mod n), y((k+1) mod n)) - (x((k+2)
195 * mod n), y((k+2) mod n)) | }, then the outline of the rounded
196 * polygon will exceed the outline of the core polygon. In other
197 * words: Do not draw rounded polygons that have a leg smaller or
198 * thinner than blotdiameter (or set blotdiameter to a sufficiently
199 * small value -- maybe even 0.0)!
201 * NOTE: Limitations (b) and (c) arise from the fact that round edges
202 * are made by moulding sharp edges to round ones rather than adding
203 * to a core filled polygon. For details of these two different
204 * approaches, see the thread upon the ledger lines patch that started
205 * on March 25, 2002 on the devel mailing list. The below version of
206 * round_filled_polygon() sticks to the moulding model, which the
207 * majority of the list participants finally voted for. This,
208 * however, results in the above limitations and a much increased
209 * complexity of the algorithm, since it has to compute a shrinked
210 * polygon -- which is not trivial define precisely and unambigously.
211 * With the other approach, one simply could move a circle of size
212 * blotdiameter along all edges of the polygon (which is what the
213 * postscript routine in the backend effectively does, but on the
214 * shrinked polygon). --jr
217 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
219 /* TODO: Maybe print a warning if one of the above limitations
220 applies to the given polygon. However, this is quite complicated
223 /* remove consecutive duplicate points */
224 const Real epsilon = 0.01;
225 for (int i = 0; i < points.size ();)
227 int next_i = (i + 1) % points.size ();
228 Real d = (points[i] - points[next_i]).length ();
235 /* special cases: degenerated polygons */
236 if (points.size () == 0)
238 if (points.size () == 1)
239 return dot (points[0], 0.5 * blotdiameter);
240 if (points.size () == 2)
241 return line (blotdiameter, points[0], points[1]);
243 /* shrink polygon in size by 0.5 * blotdiameter */
244 Array<Offset> shrinked_points;
245 shrinked_points.set_size (points.size ());
246 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
247 for (int i = 0; i < points.size (); i++)
250 int i1 = (i + 1) % points.size ();
251 int i2 = (i + 2) % points.size ();
252 Offset p0 = points[i0];
253 Offset p1 = points[i1];
254 Offset p2 = points[i2];
255 Offset p10 = p0 - p1;
256 Offset p12 = p2 - p1;
257 if (p10.length () != 0.0)
259 Real phi = p10.arg ();
260 // rotate (p2 - p0) by (-phi)
261 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
265 else if (q[Y_AXIS] < 0)
267 else {} // keep ccw unchanged
269 else {} // keep ccw unchanged
270 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
271 Offset p12n = (1.0 / p12.length ()) * p12;
272 Offset p13n = 0.5 * (p10n + p12n);
273 Offset p14n = 0.5 * (p10n - p12n);
275 Real d = p13n.length () * p14n.length (); // distance p3n to line(p1..p0)
277 // special case: p0, p1, p2 are on a single line => build
278 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
280 p13[X_AXIS] = p10[Y_AXIS];
281 p13[Y_AXIS] = -p10[X_AXIS];
282 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
285 p13 = (0.5 * blotdiameter / d) * p13n;
286 shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
289 /* build scm expression and bounding box */
290 SCM shrinked_points_scm = SCM_EOL;
292 for (int i = 0; i < shrinked_points.size (); i++)
294 SCM x = gh_double2scm (shrinked_points[i][X_AXIS]);
295 SCM y = gh_double2scm (shrinked_points[i][Y_AXIS]);
296 shrinked_points_scm = gh_cons (x, gh_cons (y, shrinked_points_scm));
297 box.add_point (points[i]);
299 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
300 ly_quote_scm (shrinked_points_scm),
301 gh_double2scm (blotdiameter),
304 Molecule polygon = Molecule (box, polygon_scm);
305 shrinked_points.clear ();
310 Lookup::frame (Box b, Real thick)
314 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
316 Axis o = Axis ((a+1)%NO_AXES);
320 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
321 edges[o][DOWN] = b[o][DOWN] - thick/2;
322 edges[o][UP] = b[o][UP] + thick/2;
324 m.add_molecule (filledbox (edges));
326 while (flip (&d) != LEFT);
333 Make a smooth curve along the points
336 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
338 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
340 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI/2)) * 0.5;
342 back.control_[1] += perp;
343 back.control_[2] += perp;
345 curve.control_[1] -= perp;
346 curve.control_[2] -= perp;
351 scontrols[ i ] = ly_offset2scm (back.control_[i]);
353 scontrols[i+4] = ly_offset2scm (curve.control_[i]);
356 Need the weird order b.o. the way PS want its arguments
358 int indices[]= {5, 6, 7, 4, 1, 2, 3, 0};
362 list = gh_cons (scontrols[indices[i]], list);
366 SCM at = (scm_list_n (ly_symbol2scm ("bezier-bow"),
368 gh_double2scm (linethick),
370 Box b(curve.extent (X_AXIS),
371 curve.extent (Y_AXIS));
373 b[X_AXIS].unite (back.extent (X_AXIS));
374 b[Y_AXIS].unite (back.extent (Y_AXIS));
376 return Molecule (b, at);
403 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
406 Need the weird order b.o. the way PS want its arguments
409 list = gh_cons (ly_offset2scm (bottom_curve.control_[3]), list);
410 list = gh_cons (ly_offset2scm (bottom_curve.control_[0]), list);
411 list = gh_cons (ly_offset2scm (bottom_curve.control_[1]), list);
412 list = gh_cons (ly_offset2scm (bottom_curve.control_[2]), list);
413 list = gh_cons (ly_offset2scm (top_curve.control_[0]), list);
414 list = gh_cons (ly_offset2scm (top_curve.control_[3]), list);
415 list = gh_cons (ly_offset2scm (top_curve.control_[2]), list);
416 list = gh_cons (ly_offset2scm (top_curve.control_[1]), list);
418 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
423 Interval x_extent = top_curve.extent (X_AXIS);
424 x_extent.unite (bottom_curve.extent (X_AXIS));
425 Interval y_extent = top_curve.extent (Y_AXIS);
426 y_extent.unite (bottom_curve.extent (Y_AXIS));
427 Box b (x_extent, y_extent);
429 return Molecule (b, horizontal_bend);
442 * (0,0) x /slope=dy/dx
450 Lookup::horizontal_slope (Real width, Real slope, Real height)
452 SCM width_scm = gh_double2scm (width);
453 SCM slope_scm = gh_double2scm (slope);
454 SCM height_scm = gh_double2scm (height);
455 SCM horizontal_slope = scm_list_n (ly_symbol2scm ("beam"),
456 width_scm, slope_scm,
457 height_scm, SCM_UNDEFINED);
458 Box b (Interval (0, width),
459 Interval (-height/2, height/2 + width*slope));
460 return Molecule (b, horizontal_slope);
467 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
470 String sym = ly_scm2string (ly_car (s));
471 String reg = ly_scm2string (ly_car (ly_cdr (s)));
473 if (sym == "Discant")
475 Molecule r = fm->find_by_name ("accordion-accDiscant");
477 if (reg.left_string (1) == "F")
479 Molecule d = fm->find_by_name ("accordion-accDot");
480 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
482 reg = reg.right_string (reg.length ()-1);
485 if (reg.left_string (3) == "EEE")
488 reg = reg.right_string (reg.length ()-3);
490 else if (reg.left_string (2) == "EE")
493 reg = reg.right_string (reg.length ()-2);
495 else if (reg.left_string (2) == "Eh")
498 reg = reg.right_string (reg.length ()-2);
500 else if (reg.left_string (1) == "E")
503 reg = reg.right_string (reg.length ()-1);
507 Molecule d = fm->find_by_name ("accordion-accDot");
508 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
513 Molecule d = fm->find_by_name ("accordion-accDot");
514 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
515 d.translate_axis (0.8 * staff_space PT, X_AXIS);
520 Molecule d = fm->find_by_name ("accordion-accDot");
521 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
522 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
525 if (reg.left_string (2) == "SS")
527 Molecule d = fm->find_by_name ("accordion-accDot");
528 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
529 d.translate_axis (0.4 * staff_space PT, X_AXIS);
531 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
533 reg = reg.right_string (reg.length ()-2);
535 if (reg.left_string (1) == "S")
537 Molecule d = fm->find_by_name ("accordion-accDot");
538 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
540 reg = reg.right_string (reg.length ()-1);
543 else if (sym == "Freebase")
545 Molecule r = fm->find_by_name ("accordion-accFreebase");
547 if (reg.left_string (1) == "F")
549 Molecule d = fm->find_by_name ("accordion-accDot");
550 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
552 reg = reg.right_string (reg.length ()-1);
556 Molecule d = fm->find_by_name ("accordion-accDot");
557 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
561 else if (sym == "Bayanbase")
563 Molecule r = fm->find_by_name ("accordion-accBayanbase");
565 if (reg.left_string (1) == "T")
567 Molecule d = fm->find_by_name ("accordion-accDot");
568 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
570 reg = reg.right_string (reg.length ()-1);
572 /* include 4' reed just for completeness. You don't want to use this. */
573 if (reg.left_string (1) == "F")
575 Molecule d = fm->find_by_name ("accordion-accDot");
576 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
578 reg = reg.right_string (reg.length ()-1);
580 if (reg.left_string (2) == "EE")
582 Molecule d = fm->find_by_name ("accordion-accDot");
583 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
584 d.translate_axis (0.4 * staff_space PT, X_AXIS);
586 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
588 reg = reg.right_string (reg.length ()-2);
590 if (reg.left_string (1) == "E")
592 Molecule d = fm->find_by_name ("accordion-accDot");
593 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
595 reg = reg.right_string (reg.length ()-1);
598 else if (sym == "Stdbase")
600 Molecule r = fm->find_by_name ("accordion-accStdbase");
602 if (reg.left_string (1) == "T")
604 Molecule d = fm->find_by_name ("accordion-accDot");
605 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
607 reg = reg.right_string (reg.length ()-1);
609 if (reg.left_string (1) == "F")
611 Molecule d = fm->find_by_name ("accordion-accDot");
612 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
614 reg = reg.right_string (reg.length ()-1);
616 if (reg.left_string (1) == "M")
618 Molecule d = fm->find_by_name ("accordion-accDot");
619 d.translate_axis (staff_space * 2 PT, Y_AXIS);
620 d.translate_axis (staff_space PT, X_AXIS);
622 reg = reg.right_string (reg.length ()-1);
624 if (reg.left_string (1) == "E")
626 Molecule d = fm->find_by_name ("accordion-accDot");
627 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
629 reg = reg.right_string (reg.length ()-1);
631 if (reg.left_string (1) == "S")
633 Molecule d = fm->find_by_name ("accordion-accDot");
634 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
636 reg = reg.right_string (reg.length ()-1);
639 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
641 else if (sym == "SB")
643 Molecule r = fm->find_by_name ("accordion-accSB");
646 else if (sym == "BB")
648 Molecule r = fm->find_by_name ("accordion-accBB");
651 else if (sym == "OldEE")
653 Molecule r = fm->find_by_name ("accordion-accOldEE");
656 else if (sym == "OldEES")
658 Molecule r = fm->find_by_name ("accordion-accOldEES");
665 Lookup::repeat_slash (Real w, Real s, Real t)
667 SCM wid = gh_double2scm (w);
668 SCM sl = gh_double2scm (s);
669 SCM thick = gh_double2scm (t);
670 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
671 wid, sl, thick, SCM_UNDEFINED);
673 Box b (Interval (0, w + sqrt (sqr(t/s) + sqr (t))),
674 Interval (0, w * s));
676 return Molecule (b, slashnodot); // http://slashnodot.org
680 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude)
683 Axis other = Axis((a+1)%2);
685 b[other] = Interval(-1, 1) * thick * 0.5;
687 Molecule m = filledbox (b);
689 b[a] = Interval (iv[UP] - thick, iv[UP]);
690 Interval oi = Interval (-thick/2, thick/2 + fabs (protude)) ;
691 oi *= sign (protude);
693 m.add_molecule (filledbox (b));
694 b[a] = Interval (iv[DOWN], iv[DOWN] +thick);
695 m.add_molecule (filledbox(b));
701 Lookup::triangle (Interval iv, Real thick, Real protude)
705 b[Y_AXIS] = Interval (0 <? protude , 0 >? protude);
707 SCM s = scm_list_n (ly_symbol2scm ("symmetric-x-triangle"),
708 gh_double2scm (thick),
709 gh_double2scm (iv.length()),
710 gh_double2scm (protude), SCM_UNDEFINED);
712 return Molecule (b, s);
717 TODO: use rounded boxes.
719 LY_DEFINE(ly_bracket ,"ly:bracket",
721 (SCM a, SCM iv, SCM t, SCM p),
722 "Make a bracket in direction @var{a}. The extent of the bracket is "
723 "given by @var{iv}. The wings protude by an amount of @var{p}, which "
724 "may be negative. The thickness is given by @var{t}.")
726 SCM_ASSERT_TYPE(ly_axis_p (a), a, SCM_ARG1, __FUNCTION__, "axis") ;
727 SCM_ASSERT_TYPE(ly_number_pair_p (iv), iv, SCM_ARG2, __FUNCTION__, "number pair") ;
728 SCM_ASSERT_TYPE(gh_number_p (t), a, SCM_ARG3, __FUNCTION__, "number") ;
729 SCM_ASSERT_TYPE(gh_number_p (p), a, SCM_ARG4, __FUNCTION__, "number") ;
732 return Lookup::bracket ((Axis)gh_scm2int (a), ly_scm2interval (iv),
734 gh_scm2double (p)).smobbed_copy ();