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