2 lookup.cc -- implement simple Lookup methods.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
16 #include "line-interface.hh"
18 #include "dimensions.hh"
20 #include "string-convert.hh"
21 #include "file-path.hh"
23 #include "lily-guile.hh"
24 #include "font-metric.hh"
27 Lookup::dot (Offset p, Real radius)
29 SCM at = (scm_list_n (ly_symbol2scm ("dot"),
30 scm_make_real (p[X_AXIS]),
31 scm_make_real (p[Y_AXIS]),
32 scm_make_real (radius),
35 box.add_point (p - Offset (radius, radius));
36 box.add_point (p + Offset (radius, radius));
37 return Stencil (box, at);
50 * (0, 0)x /slope = dy/dx
58 Lookup::beam (Real slope, Real width, Real thick, Real blot)
60 Real height = slope * width;
61 Real min_y = min (0., height) - thick / 2;
62 Real max_y = max (0., height) + thick / 2;
64 Box b (Interval (0, width),
65 Interval (min_y, max_y));
67 SCM at = scm_list_n (ly_symbol2scm ("beam"),
68 scm_make_real (width),
69 scm_make_real (slope),
70 scm_make_real (thick),
73 return Stencil (b, at);
77 Lookup::dashed_slur (Bezier b, Real thick, Real dash_period, Real dash_fraction)
81 Real on = dash_fraction * dash_period;
82 Real off = dash_period - on;
86 l = scm_cons (ly_offset2scm (b.control_[i]), l);
89 SCM at = (scm_list_n (ly_symbol2scm ("dashed-slur"),
90 scm_make_real (thick),
96 Box box (Interval (0, 0), Interval (0, 0));
97 return Stencil (box, at);
101 Lookup::horizontal_line (Interval w, Real th)
103 SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
105 scm_make_real (w[LEFT]),
107 scm_make_real (w[RIGHT]),
113 box[Y_AXIS] = Interval (-th / 2, th / 2);
115 return Stencil (box, at);
119 Lookup::blank (Box b)
121 return Stencil (b, scm_makfrom0str (""));
125 Lookup::filled_box (Box b)
127 SCM at = (scm_list_n (ly_symbol2scm ("filledbox"),
128 scm_make_real (-b[X_AXIS][LEFT]),
129 scm_make_real (b[X_AXIS][RIGHT]),
130 scm_make_real (-b[Y_AXIS][DOWN]),
131 scm_make_real (b[Y_AXIS][UP]),
134 return Stencil (b, at);
140 * __________________________________
145 * |\ _ _ / v \ _ _ /| |
148 * | <------>| | extent
149 * | blot | | (Y_AXIS)
157 * x\_____/______________\_____/|_____v
161 * |<-------------------------->|
162 * Box extent (X_AXIS)
165 Lookup::round_filled_box (Box b, Real blotdiameter)
167 if (b.x ().length () < blotdiameter)
169 blotdiameter = b.x ().length ();
171 if (b.y ().length () < blotdiameter)
173 blotdiameter = b.y ().length ();
176 SCM at = (scm_list_n (ly_symbol2scm ("round-filled-box"),
177 scm_make_real (-b[X_AXIS][LEFT]),
178 scm_make_real (b[X_AXIS][RIGHT]),
179 scm_make_real (-b[Y_AXIS][DOWN]),
180 scm_make_real (b[Y_AXIS][UP]),
181 scm_make_real (blotdiameter),
184 return Stencil (b, at);
188 * Create Stencil that represents a filled polygon with round edges.
192 * (a) Only outer (convex) edges are rounded.
194 * (b) This algorithm works as expected only for polygons whose edges
195 * do not intersect. For example, the polygon ((0, 0), (q, 0), (0,
196 * q), (q, q)) has an intersection at point (q/2, q/2) and therefore
197 * will give a strange result. Even non-adjacent edges that just
198 * touch each other will in general not work as expected for non-null
201 * (c) Given a polygon ((x0, y0), (x1, y1), ... , (x (n-1), y (n-1))),
202 * if there is a natural number k such that blotdiameter is greater
203 * than the maximum of { | (x (k mod n), y (k mod n)) - (x ((k+1) mod n),
204 * y ((k+1) mod n)) |, | (x (k mod n), y (k mod n)) - (x ((k+2) mod n),
205 * y ((k+2) mod n)) |, | (x ((k+1) mod n), y ((k+1) mod n)) - (x ((k+2)
206 * mod n), y ((k+2) mod n)) | }, then the outline of the rounded
207 * polygon will exceed the outline of the core polygon. In other
208 * words: Do not draw rounded polygons that have a leg smaller or
209 * thinner than blotdiameter (or set blotdiameter to a sufficiently
210 * small value -- maybe even 0.0)!
212 * NOTE: Limitations (b) and (c) arise from the fact that round edges
213 * are made by moulding sharp edges to round ones rather than adding
214 * to a core filled polygon. For details of these two different
215 * approaches, see the thread upon the ledger lines patch that started
216 * on March 25, 2002 on the devel mailing list. The below version of
217 * round_filled_polygon () sticks to the moulding model, which the
218 * majority of the list participants finally voted for. This,
219 * however, results in the above limitations and a much increased
220 * complexity of the algorithm, since it has to compute a shrinked
221 * polygon -- which is not trivial define precisely and unambigously.
222 * With the other approach, one simply could move a circle of size
223 * blotdiameter along all edges of the polygon (which is what the
224 * postscript routine in the backend effectively does, but on the
225 * shrinked polygon). --jr
228 Lookup::round_filled_polygon (Array<Offset> points, Real blotdiameter)
230 /* TODO: Maybe print a warning if one of the above limitations
231 applies to the given polygon. However, this is quite complicated
234 /* remove consecutive duplicate points */
235 const Real epsilon = 0.01;
236 for (int i = 0; i < points.size ();)
238 int next_i = (i + 1) % points.size ();
239 Real d = (points[i] - points[next_i]).length ();
246 /* special cases: degenerated polygons */
247 if (points.size () == 0)
249 if (points.size () == 1)
250 return dot (points[0], 0.5 * blotdiameter);
251 if (points.size () == 2)
252 return Line_interface::make_line (blotdiameter, points[0], points[1]);
254 /* shrink polygon in size by 0.5 * blotdiameter */
255 Array<Offset> shrinked_points;
256 shrinked_points.set_size (points.size ());
257 bool ccw = 1; // true, if three adjacent points are counterclockwise ordered
258 for (int i = 0; i < points.size (); i++)
261 int i1 = (i + 1) % points.size ();
262 int i2 = (i + 2) % points.size ();
263 Offset p0 = points[i0];
264 Offset p1 = points[i1];
265 Offset p2 = points[i2];
266 Offset p10 = p0 - p1;
267 Offset p12 = p2 - p1;
268 if (p10.length () != 0.0)
270 Real phi = p10.arg ();
271 // rotate (p2 - p0) by (-phi)
272 Offset q = complex_multiply (p2 - p0, complex_exp (Offset (1.0, -phi)));
276 else if (q[Y_AXIS] < 0)
278 else {} // keep ccw unchanged
280 else {} // keep ccw unchanged
281 Offset p10n = (1.0 / p10.length ()) * p10; // normalize length to 1.0
282 Offset p12n = (1.0 / p12.length ()) * p12;
283 Offset p13n = 0.5 * (p10n + p12n);
284 Offset p14n = 0.5 * (p10n - p12n);
286 Real d = p13n.length () * p14n.length (); // distance p3n to line (p1..p0)
288 // special case: p0, p1, p2 are on a single line => build
289 // vector orthogonal to (p2-p0) of length 0.5 blotdiameter
291 p13[X_AXIS] = p10[Y_AXIS];
292 p13[Y_AXIS] = -p10[X_AXIS];
293 p13 = (0.5 * blotdiameter / p13.length ()) * p13;
296 p13 = (0.5 * blotdiameter / d) * p13n;
297 shrinked_points[i1] = p1 + ((ccw) ? p13 : -p13);
300 /* build scm expression and bounding box */
301 SCM shrinked_points_scm = SCM_EOL;
303 for (int i = 0; i < shrinked_points.size (); i++)
305 SCM x = scm_make_real (shrinked_points[i][X_AXIS]);
306 SCM y = scm_make_real (shrinked_points[i][Y_AXIS]);
307 shrinked_points_scm = scm_cons (x, scm_cons (y, shrinked_points_scm));
308 box.add_point (points[i]);
310 SCM polygon_scm = scm_list_n (ly_symbol2scm ("polygon"),
311 ly_quote_scm (shrinked_points_scm),
312 scm_make_real (blotdiameter),
316 Stencil polygon = Stencil (box, polygon_scm);
317 shrinked_points.clear ();
325 Lookup::frame (Box b, Real thick, Real blot)
329 for (Axis a = X_AXIS; a < NO_AXES; a = Axis (a + 1))
331 Axis o = Axis ((a + 1)%NO_AXES);
335 edges[a] = b[a][d] + 0.5 * thick * Interval (-1, 1);
336 edges[o][DOWN] = b[o][DOWN] - thick / 2;
337 edges[o][UP] = b[o][UP] + thick / 2;
339 m.add_stencil (round_filled_box (edges, blot));
341 while (flip (&d) != LEFT);
347 Make a smooth curve along the points
350 Lookup::slur (Bezier curve, Real curvethick, Real linethick)
352 Real alpha = (curve.control_[3] - curve.control_[0]).arg ();
354 Offset perp = curvethick * complex_exp (Offset (0, alpha + M_PI / 2)) * 0.5;
356 back.control_[1] += perp;
357 back.control_[2] += perp;
359 curve.control_[1] -= perp;
360 curve.control_[2] -= perp;
364 for (int i = 4; i--;)
365 scontrols[ i ] = ly_offset2scm (back.control_[i]);
366 for (int i = 4; i--;)
367 scontrols[i + 4] = ly_offset2scm (curve.control_[i]);
370 Need the weird order b.o. the way PS want its arguments
372 int indices[] = {5, 6, 7, 4, 1, 2, 3, 0};
374 for (int i = 8; i--;)
376 list = scm_cons (scontrols[indices[i]], list);
379 SCM at = (scm_list_n (ly_symbol2scm ("bezier-sandwich"),
381 scm_make_real (linethick),
383 Box b (curve.extent (X_AXIS),
384 curve.extent (Y_AXIS));
386 b[X_AXIS].unite (back.extent (X_AXIS));
387 b[Y_AXIS].unite (back.extent (Y_AXIS));
389 return Stencil (b, at);
416 Lookup::bezier_sandwich (Bezier top_curve, Bezier bottom_curve)
419 Need the weird order b.o. the way PS want its arguments
422 list = scm_cons (ly_offset2scm (bottom_curve.control_[3]), list);
423 list = scm_cons (ly_offset2scm (bottom_curve.control_[0]), list);
424 list = scm_cons (ly_offset2scm (bottom_curve.control_[1]), list);
425 list = scm_cons (ly_offset2scm (bottom_curve.control_[2]), list);
426 list = scm_cons (ly_offset2scm (top_curve.control_[0]), list);
427 list = scm_cons (ly_offset2scm (top_curve.control_[3]), list);
428 list = scm_cons (ly_offset2scm (top_curve.control_[2]), list);
429 list = scm_cons (ly_offset2scm (top_curve.control_[1]), list);
431 SCM horizontal_bend = scm_list_n (ly_symbol2scm ("bezier-sandwich"),
436 Interval x_extent = top_curve.extent (X_AXIS);
437 x_extent.unite (bottom_curve.extent (X_AXIS));
438 Interval y_extent = top_curve.extent (Y_AXIS);
439 y_extent.unite (bottom_curve.extent (Y_AXIS));
440 Box b (x_extent, y_extent);
442 return Stencil (b, horizontal_bend);
449 Lookup::accordion (SCM s, Real staff_space, Font_metric *fm)
452 String sym = ly_scm2string (scm_car (s));
453 String reg = ly_scm2string (scm_car (scm_cdr (s)));
455 if (sym == "Discant")
457 Stencil r = fm->find_by_name ("accordion.accDiscant");
459 if (reg.left_string (1) == "F")
461 Stencil d = fm->find_by_name ("accordion.accDot");
462 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
464 reg = reg.right_string (reg.length () - 1);
467 if (reg.left_string (3) == "EEE")
470 reg = reg.right_string (reg.length () - 3);
472 else if (reg.left_string (2) == "EE")
475 reg = reg.right_string (reg.length () - 2);
477 else if (reg.left_string (2) == "Eh")
480 reg = reg.right_string (reg.length () - 2);
482 else if (reg.left_string (1) == "E")
485 reg = reg.right_string (reg.length () - 1);
489 Stencil d = fm->find_by_name ("accordion.accDot");
490 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
495 Stencil d = fm->find_by_name ("accordion.accDot");
496 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
497 d.translate_axis (0.8 * staff_space PT, X_AXIS);
502 Stencil d = fm->find_by_name ("accordion.accDot");
503 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
504 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
507 if (reg.left_string (2) == "SS")
509 Stencil d = fm->find_by_name ("accordion.accDot");
510 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
511 d.translate_axis (0.4 * staff_space PT, X_AXIS);
513 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
515 reg = reg.right_string (reg.length () - 2);
517 if (reg.left_string (1) == "S")
519 Stencil d = fm->find_by_name ("accordion.accDot");
520 d.translate_axis (0.5 * staff_space PT, Y_AXIS);
522 reg = reg.right_string (reg.length () - 1);
525 else if (sym == "Freebase")
527 Stencil r = fm->find_by_name ("accordion.accFreebase");
529 if (reg.left_string (1) == "F")
531 Stencil d = fm->find_by_name ("accordion.accDot");
532 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
534 reg = reg.right_string (reg.length () - 1);
538 Stencil d = fm->find_by_name ("accordion.accDot");
539 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
543 else if (sym == "Bayanbase")
545 Stencil r = fm->find_by_name ("accordion.accBayanbase");
547 if (reg.left_string (1) == "T")
549 Stencil d = fm->find_by_name ("accordion.accDot");
550 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
552 reg = reg.right_string (reg.length () - 1);
554 /* include 4' reed just for completeness. You don't want to use this. */
555 if (reg.left_string (1) == "F")
557 Stencil d = fm->find_by_name ("accordion.accDot");
558 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
560 reg = reg.right_string (reg.length () - 1);
562 if (reg.left_string (2) == "EE")
564 Stencil d = fm->find_by_name ("accordion.accDot");
565 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
566 d.translate_axis (0.4 * staff_space PT, X_AXIS);
568 d.translate_axis (-0.8 * staff_space PT, X_AXIS);
570 reg = reg.right_string (reg.length () - 2);
572 if (reg.left_string (1) == "E")
574 Stencil d = fm->find_by_name ("accordion.accDot");
575 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
577 reg = reg.right_string (reg.length () - 1);
580 else if (sym == "Stdbase")
582 Stencil r = fm->find_by_name ("accordion.accStdbase");
584 if (reg.left_string (1) == "T")
586 Stencil d = fm->find_by_name ("accordion.accDot");
587 d.translate_axis (staff_space * 3.5 PT, Y_AXIS);
589 reg = reg.right_string (reg.length () - 1);
591 if (reg.left_string (1) == "F")
593 Stencil d = fm->find_by_name ("accordion.accDot");
594 d.translate_axis (staff_space * 2.5 PT, Y_AXIS);
596 reg = reg.right_string (reg.length () - 1);
598 if (reg.left_string (1) == "M")
600 Stencil d = fm->find_by_name ("accordion.accDot");
601 d.translate_axis (staff_space * 2 PT, Y_AXIS);
602 d.translate_axis (staff_space PT, X_AXIS);
604 reg = reg.right_string (reg.length () - 1);
606 if (reg.left_string (1) == "E")
608 Stencil d = fm->find_by_name ("accordion.accDot");
609 d.translate_axis (staff_space * 1.5 PT, Y_AXIS);
611 reg = reg.right_string (reg.length () - 1);
613 if (reg.left_string (1) == "S")
615 Stencil d = fm->find_by_name ("accordion.accDot");
616 d.translate_axis (staff_space * 0.5 PT, Y_AXIS);
618 reg = reg.right_string (reg.length () - 1);
621 /* ugh maybe try to use regular font for S.B. and B.B and only use one font
623 else if (sym == "SB")
625 Stencil r = fm->find_by_name ("accordion.accSB");
628 else if (sym == "BB")
630 Stencil r = fm->find_by_name ("accordion.accBB");
633 else if (sym == "OldEE")
635 Stencil r = fm->find_by_name ("accordion.accOldEE");
638 else if (sym == "OldEES")
640 Stencil r = fm->find_by_name ("accordion.accOldEES");
647 Lookup::repeat_slash (Real w, Real s, Real t)
651 Array<Offset> points ;
652 Real blotdiameter = 0.0;
659 return Lookup::round_filled_polygon (points, blotdiameter);
662 SCM wid = scm_make_real (w);
663 SCM sl = scm_make_real (s);
664 SCM thick = scm_make_real (t);
665 SCM slashnodot = scm_list_n (ly_symbol2scm ("repeat-slash"),
666 wid, sl, thick, SCM_UNDEFINED);
668 Box b (Interval (0, w + sqrt (sqr (t / s) + sqr (t))),
669 Interval (0, w * s));
671 return Stencil (b, slashnodot); // http://slashnodot.org
675 Lookup::bracket (Axis a, Interval iv, Real thick, Real protude, Real blot)
678 Axis other = Axis ((a + 1)%2);
680 b[other] = Interval (-1, 1) * thick * 0.5;
682 Stencil m = round_filled_box (b, blot);
684 b[a] = Interval (iv[UP] - thick, iv[UP]);
685 Interval oi = Interval (-thick / 2, thick / 2 + fabs (protude));
686 oi *= sign (protude);
688 m.add_stencil (round_filled_box (b, blot));
689 b[a] = Interval (iv[DOWN], iv[DOWN] + thick);
690 m.add_stencil (round_filled_box (b, blot));
696 Lookup::triangle (Interval iv, Real thick, Real protude)
699 b[X_AXIS] = Interval (0, iv.length ());
700 b[Y_AXIS] = Interval (min (0., protude), max (0.0, protude));
702 Offset z1 (iv[LEFT], 0);
703 Offset z2 (iv[RIGHT], 0);
704 Offset z3 ((z1 + z2)[X_AXIS] / 2, protude);
707 TODO: move Triangle to Line_interface ?
709 Stencil tri = Line_interface::make_line (thick, z1, z2);
710 tri.add_stencil (Line_interface::make_line (thick, z2, z3));
711 tri.add_stencil (Line_interface::make_line (thick, z3, z1));