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