]> git.donarmstrong.com Git - lilypond.git/blob - lily/line-interface.cc
Merge master into nested-bookparts
[lilypond.git] / lily / line-interface.cc
1 /*
2   line-interface.cc -- implement Line_interface
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "line-interface.hh"
10
11 #include "staff-symbol-referencer.hh"
12 #include "lookup.hh"
13 #include "output-def.hh"
14 #include "grob.hh"
15 #include "font-interface.hh"
16
17 Stencil
18 Line_interface::make_arrow (Offset begin, Offset end,
19                             Real thick,
20                             Real length, Real width)
21 {
22   Real angle = (end - begin).arg ();
23   vector<Offset> points;
24
25   points.push_back (Offset (0, 0));
26   points.push_back (Offset (-length, width));
27   points.push_back (Offset (-length, -width));
28
29   for (vsize i = 0; i < points.size (); i++)
30     points[i] = points[i] * complex_exp (Offset (0, angle)) + end;
31
32   return Lookup::round_filled_polygon (points, thick);
33 }
34
35 Stencil
36 Line_interface::make_trill_line (Grob *me,
37                                  Offset from,
38                                  Offset to)
39 {
40   Offset dz = (to-from);
41   SCM alist_chain = Font_interface::text_font_alist_chain (me);
42   SCM style_alist = scm_list_n (scm_cons (ly_symbol2scm ("font-encoding"),
43                                           ly_symbol2scm ("fetaMusic")),
44                                 SCM_UNDEFINED);
45
46   Font_metric *fm = select_font (me->layout (),
47                                  scm_cons (style_alist,
48                                            alist_chain));
49
50   Stencil elt = fm->find_by_name ("scripts.trill_element");
51   elt.align_to (Y_AXIS, CENTER);
52   Real elt_len = elt.extent (X_AXIS).length ();
53   if (elt_len <= 0.0)
54     {
55       programming_error ("can't find scripts.trill_element");
56       return Stencil ();
57     }
58       
59   Stencil line;
60   Real len = 0.0;
61   do
62     {
63       line.add_at_edge (X_AXIS, RIGHT, elt, 0);
64       len = line.extent (X_AXIS).length ();
65     }
66   while (len + elt_len < dz.length ());
67
68   line.rotate (dz.arg (), Offset (LEFT, CENTER));
69   line.translate (from);
70
71   return line; 
72 }
73
74
75 Stencil
76 Line_interface::make_zigzag_line (Grob *me,
77                                   Offset from,
78                                   Offset to)
79 {
80   Offset dz = to -from;
81
82   Real thick = Staff_symbol_referencer::line_thickness (me);
83   thick *= robust_scm2double (me->get_property ("thickness"), 1.0); // todo: staff sym referencer? 
84
85   Real staff_space = Staff_symbol_referencer::staff_space (me);
86
87   Real w = robust_scm2double (me->get_property ("zigzag-width"), 1) * staff_space;
88   int count = (int) ceil (dz.length () / w);
89   w = dz.length () / count;
90
91   Real l = robust_scm2double (me->get_property ("zigzag-length"), 1) * w;
92   Real h = l > w / 2 ? sqrt (l * l - w * w / 4) : 0;
93
94   Offset rotation_factor = complex_exp (Offset (0, dz.arg ()));
95
96   Offset points[3];
97   points[0] = Offset (0, -h / 2);
98   points[1] = Offset (w / 2, h / 2);
99   points[2] = Offset (w, -h / 2);
100   for (int i = 0; i < 3; i++)
101     points[i] = complex_multiply (points[i], rotation_factor);
102
103   Stencil squiggle (Line_interface::make_line (thick, points[0], points[1]));
104   squiggle.add_stencil (Line_interface::make_line (thick, points[1], points[2]));
105
106   Stencil total;
107   for (int i = 0; i < count; i++)
108     {
109       Stencil moved_squiggle (squiggle);
110       moved_squiggle.translate (from + Offset (i * w, 0) * rotation_factor);
111       total.add_stencil (moved_squiggle);
112     }
113
114   return total;
115 }
116
117
118 Stencil
119 Line_interface::make_dashed_line (Real thick, Offset from, Offset to,
120                                   Real dash_period, Real dash_fraction)
121 {
122   dash_fraction = min (max (dash_fraction, 0.0), 1.0);
123   Real on = dash_fraction * dash_period + thick;
124   Real off = max (0.0, dash_period - on);
125
126   SCM at = scm_list_n (ly_symbol2scm ("dashed-line"),
127                        scm_from_double (thick),
128                        scm_from_double (on),
129                        scm_from_double (off),
130                        scm_from_double (to[X_AXIS] - from[X_AXIS]),
131                        scm_from_double (to[Y_AXIS] - from[Y_AXIS]),
132                        scm_from_double (0.0),
133                        SCM_UNDEFINED);
134
135   Box box;
136   box.add_point (Offset (0, 0));
137   box.add_point (to - from);
138
139   box[X_AXIS].widen (thick / 2);
140   box[Y_AXIS].widen (thick / 2);
141
142   Stencil m = Stencil (box, at);
143   m.translate (from);
144   return m;
145 }
146
147 Stencil
148 Line_interface::make_line (Real th, Offset from, Offset to)
149 {
150   SCM at = scm_list_n (ly_symbol2scm ("draw-line"),
151                        scm_from_double (th),
152                        scm_from_double (from[X_AXIS]),
153                        scm_from_double (from[Y_AXIS]),
154                        scm_from_double (to[X_AXIS]),
155                        scm_from_double (to[Y_AXIS]),
156                        SCM_UNDEFINED);
157
158   Box box;
159   box.add_point (from);
160   box.add_point (to);
161
162   box[X_AXIS].widen (th / 2);
163   box[Y_AXIS].widen (th / 2);
164
165   return Stencil (box, at);
166 }
167
168 Stencil
169 Line_interface::arrows (Grob *me, Offset from, Offset to,
170                         bool from_arrow,
171                         bool to_arrow)
172 {
173   Stencil a;
174   if (from_arrow || to_arrow)
175     {
176       Real thick = Staff_symbol_referencer::line_thickness (me)
177         * robust_scm2double (me->get_property ("thickness"), 1);
178       Real ss = Staff_symbol_referencer::staff_space (me);
179
180       Real len = robust_scm2double (me->get_property ("arrow-length"), 1.3 * ss);
181       Real wid = robust_scm2double (me->get_property ("arrow-width"), 0.5 * ss);
182
183       if (to_arrow)
184         a.add_stencil (make_arrow (from, to, thick, len, wid));
185
186       if (from_arrow)
187         a.add_stencil (make_arrow (to, from, thick, len, wid));
188     }
189
190   return a;
191 }
192
193 Stencil
194 Line_interface::line (Grob *me, Offset from, Offset to)
195 {
196   Real thick = Staff_symbol_referencer::line_thickness (me)
197     * robust_scm2double (me->get_property ("thickness"), 1);
198
199   SCM type = me->get_property ("style");
200   if (type == ly_symbol2scm ("zigzag"))
201     {
202       return make_zigzag_line (me, from, to);
203     }
204   else if (type == ly_symbol2scm ("trill"))
205     return make_trill_line (me, from, to);
206   
207   Stencil stencil;
208
209   if (type == ly_symbol2scm ("dashed-line") || type == ly_symbol2scm ("dotted-line"))
210     {
211
212       Real fraction
213         = type == ly_symbol2scm ("dotted-line")
214         ? 0.0
215         : robust_scm2double (me->get_property ("dash-fraction"), 0.4);
216
217       fraction = min (max (fraction, 0.0), 1.0);
218       Real period = Staff_symbol_referencer::staff_space (me)
219         * robust_scm2double (me->get_property ("dash-period"), 1.0);
220
221       if (period <= 0)
222         return Stencil ();
223
224       Real len = (to-from).length ();
225       
226       int n = (int) rint ((len - period * fraction) / period);
227       n = max (0, n);
228       if (n > 0)
229         {
230           /*
231             TODO: figure out something intelligent for really short
232             sections.
233            */
234           period = ((to-from).length () - period * fraction) / n;
235         }
236       stencil = make_dashed_line (thick, from, to, period, fraction);
237     }
238   else
239     stencil = make_line (thick, from, to);
240
241   return stencil;
242 }
243
244 ADD_INTERFACE (Line_interface,
245                "Generic line objects.  Any object using lines supports this."
246                "  The property @code{style} can be @code{line},"
247                " @code{dashed-line}, @code{trill}, @code{dotted-line} or"
248                " @code{zigzag}.\n"
249                "\n"
250                "For @code{dashed-line}, the length of the dashes is tuned"
251                " with @code{dash-fraction}.  If the latter is set to@tie{}0, a"
252                " dotted line is produced.  If @code{dash-period} is negative,"
253                " the line is made transparent.",
254
255                /* properties */
256                "dash-period "
257                "dash-fraction "
258                "thickness "
259                "style "
260                "zigzag-length "
261                "zigzag-width "
262                "arrow-length "
263                "arrow-width ")
264