]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-spanner.cc
82d840a5e879746db0a34a4026237ac2c469dbdb
[lilypond.git] / lily / text-spanner.cc
1 /*
2   text-spanner.cc -- implement Text_spanner
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "molecule.hh"
10 #include "text-item.hh"
11 #include "text-spanner.hh"
12 #include "spanner.hh"
13 #include "lookup.hh"
14 #include "dimensions.hh"
15 #include "paper-def.hh"
16 #include "debug.hh"
17 #include "paper-column.hh"
18 #include "staff-symbol-referencer.hh"
19
20 /*
21   Generic Text spanner:
22
23   type: "line", "dashed-line", "dotted-line"
24   edge-text: ("le" . "re")
25   text-style: "italic"
26   egde-height: (lh . rh)
27
28   "le"--------------"re"
29                     |^ 
30                     |v rh
31
32   fine tuning:
33
34   dash-period 
35   dash-length 
36   line-thickness
37
38   TODO:
39     - vertical start / vertical end (fixme-name) |
40     - contination types (vert. star, vert. end)  |-> eat volta-spanner
41     - more styles
42     - more texts/positions
43     - style: hairpin ?
44  */
45
46 MAKE_SCHEME_CALLBACK (Text_spanner, brew_molecule, 1);
47
48 SCM
49 Text_spanner::brew_molecule (SCM smob) 
50 {
51   Score_element *me= unsmob_element (smob);
52   Spanner *spanner = dynamic_cast<Spanner*> (me);
53
54   Real staff_space = Staff_symbol_referencer::staff_space (me);
55   Real thickness = me->paper_l ()->get_var ("stafflinethickness");  
56   
57
58   Drul_array<bool> broken;
59   Direction d = LEFT;
60   do
61     {
62       Paper_column* s = dynamic_cast<Paper_column*>(spanner->get_bound (d)); // UGH
63       broken[d] = (!s->musical_b ());
64     }
65   while (flip (&d) != LEFT);
66   
67 #if 0
68   SCM s = me->get_elt_property ("text-style");
69
70   String text_style = "italic";
71   if (gh_string_p (s))
72     text_style = ly_scm2string (s);
73 #endif
74
75   SCM properties = gh_append2 (me->immutable_property_alist_,
76                                me->mutable_property_alist_);
77   SCM edge_text = me->get_elt_property ("edge-text");
78   Drul_array<Molecule> edge;
79   if (gh_pair_p (edge_text))
80     {
81       Direction d = LEFT;
82       do
83         {
84           SCM text = index_cell (edge_text, d);
85           edge[d] = Text_item::text2molecule (me, text, properties);
86           if (!edge[d].empty_b ())
87             edge[d].align_to (Y_AXIS, CENTER);
88         }
89       while (flip (&d) != LEFT);
90     }
91
92   Drul_array<Real> shorten;
93   shorten[LEFT] = 0;
94   shorten[RIGHT] = 0;
95
96   SCM s = me->get_elt_property ("shorten");
97   if (gh_pair_p (s))
98     {
99       shorten[LEFT] = gh_scm2double (gh_car (s)) * staff_space;
100       shorten[RIGHT] = gh_scm2double (gh_cdr (s)) * staff_space;
101     }
102
103   Real broken_left =  spanner->get_broken_left_end_align ();
104   Real width = spanner->spanner_length ();
105   Score_element *bnd = spanner->get_bound (RIGHT);
106   width += bnd->extent (bnd, X_AXIS).length ();
107   width -= broken_left;
108   width -= shorten[LEFT] + shorten[RIGHT];
109   width -= edge[LEFT].extent (X_AXIS).length ()
110     + edge[RIGHT].extent (X_AXIS).length ();
111
112   if (width < 0)
113     {
114       warning (_ ("Text_spanner too small"));
115       width = 0;
116     }
117
118
119   String type = "dashed-line";
120   s = me->get_elt_property ("type");
121   if (gh_string_p (s))
122     type = ly_scm2string (s);
123
124   Molecule line;
125   Drul_array<Molecule> edge_line;
126   if (type == "line"
127       || type == "dashed-line"
128       || type == "dotted-line")
129     {
130       Real thick = thickness;
131       s = me->get_elt_property ("line-thickness");
132       if (gh_number_p (s))
133         thick *= gh_scm2double (s);
134   
135       // maybe these should be in line-thickness?
136       Real length = staff_space;
137       s = me->get_elt_property ("dash-length");
138       if (gh_number_p (s))
139         length = gh_scm2double (s) * staff_space;
140
141       Real period = 2 * length + thick;
142       s = me->get_elt_property ("dash-period");
143       if (gh_number_p (s))
144         period = gh_scm2double (s) * staff_space;
145       
146       if (type == "dotted-line")
147         length = thick;
148         
149       if (type == "line")
150         length = period + thick;
151
152       Real on = length - thick;
153       Real off = period - on;
154
155       SCM list = gh_list (ly_symbol2scm ("dashed-line"),
156                           gh_double2scm (thick),
157                           gh_double2scm (on),
158                           gh_double2scm (off),
159                           gh_double2scm (width),
160                           gh_double2scm (0),
161                           SCM_UNDEFINED);
162
163       Box b (Interval (0, width), Interval (-thick / 2, thick / 2));
164       line = Molecule (b, list);
165
166       s = me->get_elt_property ("edge-height");
167       if (gh_pair_p (s))
168         {
169           Direction d = LEFT;
170           int dir = me->get_elt_property ("direction");
171           do
172             {
173               Real dy = gh_scm2double (index_cell (s, d)) * - dir;
174               if (dy)
175                 {
176                   SCM list = gh_list (ly_symbol2scm ("dashed-line"),
177                                       gh_double2scm (thick),
178                                       gh_double2scm (on),
179                                       gh_double2scm (off),
180                                       gh_double2scm (0),
181                                       gh_double2scm (dy),
182                                       SCM_UNDEFINED);
183                   
184                   Box b (Interval (0, thick),
185                          dy > 0
186                          ? Interval (0, dy)
187                          : Interval (dy, 0));
188                   edge_line[d] = Molecule (b, list);
189                 }
190             }
191           while (flip (&d) != LEFT);
192         }
193     }
194
195   Molecule m;
196   if (!edge[LEFT].empty_b ())
197     m = edge[LEFT];
198
199   if (!edge_line[LEFT].empty_b ())
200     m.add_at_edge (X_AXIS, RIGHT, edge_line[LEFT], 0);
201   if (!line.empty_b ())
202     m.add_at_edge (X_AXIS, RIGHT, line, 0);
203   if (!edge_line[RIGHT].empty_b ())
204     m.add_at_edge (X_AXIS, RIGHT, edge_line[RIGHT], 0);
205   if (!edge[RIGHT].empty_b ())
206     m.add_at_edge (X_AXIS, RIGHT, edge[RIGHT], 0);
207   m.translate_axis (broken_left, X_AXIS);
208
209   return m.create_scheme ();
210 }
211
212