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