]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
c33180540dbf3e849eb8a21c2bd9ff282a4f3bf0
[lilypond.git] / lily / system-start-delimiter.cc
1 /*
2   system-start-delimiter.cc -- implement System_start_delimiter
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "system-start-delimiter.hh"
10 #include "text-interface.hh" 
11 #include "all-font-metrics.hh"
12 #include "axis-group-interface.hh"
13 #include "font-interface.hh"
14 #include "item.hh"
15 #include "line-interface.hh"
16 #include "lookup.hh"
17 #include "output-def.hh"
18 #include "pointer-group-interface.hh"
19 #include "spanner.hh"
20 #include "staff-symbol-referencer.hh"
21
22 Stencil
23 System_start_delimiter::staff_bracket (Grob *me, Real height)
24 {
25   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
26                       ly_symbol2scm ("fetaMusic"));
27
28   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
29   Font_metric *fm = select_font (me->layout (), scm_list_n (alist, SCM_UNDEFINED));
30
31   Drul_array<Stencil> tips (fm->find_by_name ("brackettips.down"),
32                             fm->find_by_name ("brackettips.up"));
33
34   Real thickness = robust_scm2double (me->get_property ("thickness"), 0.25);
35
36   Real overlap = 0.1 * thickness;
37
38   Box box (Interval (0, thickness),
39            Interval (-1, 1)
40            * (height / 2 + overlap));
41   
42   Stencil bracket = Lookup::filled_box (box);
43   Direction d = DOWN;
44   do
45     bracket.add_at_edge (Y_AXIS, d, tips[d], -overlap, 0.0);
46   while (flip (&d) != DOWN);
47   bracket = Stencil (box, bracket.expr ());
48
49   bracket.translate_axis (-0.8, X_AXIS);
50   
51   return bracket;
52 }
53
54 Stencil
55 System_start_delimiter::line_bracket (Grob *me, Real height)
56 {
57   Real thick
58     = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"))
59     * robust_scm2double (me->get_property ("thickness"), 1);
60   Real w = 0.8;
61   
62   Stencil tip1 = Line_interface::make_line (thick,
63                                            Offset (0, -height/2),
64                                            Offset (w, -height/2));
65   Stencil tip2 = Line_interface::make_line (thick,
66                                             Offset (0, height/2),
67                                             Offset (w, height/2));
68   Stencil vline = Line_interface::make_line (thick,
69                                              Offset (0, -height/2),
70                                              Offset (0, height/2));
71
72   vline.add_stencil (tip1);
73   vline.add_stencil (tip2);
74   vline.translate_axis (-w, X_AXIS);
75   return vline;
76 }
77
78 Stencil
79 System_start_delimiter::text (Grob *me_grob, Real h)
80 {
81   (void) h;
82   
83   Spanner *me = dynamic_cast<Spanner*> (me_grob);
84   SCM t = me->get_property ("text");
85   if (me->get_break_index () == 0)
86     t = me->get_property ("long-text");
87            
88   
89   SCM chain = Font_interface::text_font_alist_chain (me);
90
91   SCM scm_stencil = Text_interface::is_markup (t)
92     ? Text_interface::interpret_markup (me->layout ()->self_scm (), chain, t)
93     : SCM_EOL;
94
95   
96   if (Stencil *p = unsmob_stencil (scm_stencil))
97     {
98       SCM align_y  = me_grob->get_property ("self-alignment-Y");
99       if (scm_is_number (align_y))
100         p->align_to (Y_AXIS, robust_scm2double (align_y, 0.0));
101       return *p;
102     }
103   return Stencil();
104 }
105
106 Stencil
107 System_start_delimiter::simple_bar (Grob *me, Real h)
108 {
109   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
110   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
111   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
112                                    lt);
113 }
114
115 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
116 SCM
117 System_start_delimiter::print (SCM smob)
118 {
119   Spanner *me = unsmob_spanner (smob);
120   extract_grob_set (me, "elements", elts);
121   Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
122
123   Interval ext;
124   int non_empty_count = 0;
125   for (vsize i = elts.size (); i--;)
126     {
127       Spanner *sp = dynamic_cast<Spanner *> (elts[i]);
128
129       if (sp
130           && sp->get_bound (LEFT) == me->get_bound (LEFT))
131         {
132           Interval dims = sp->extent (common, Y_AXIS);
133           if (!dims.is_empty ())
134             {
135               non_empty_count ++;
136               ext.unite (dims);
137             }
138         }
139     }
140
141   SCM glyph_sym = me->get_property ("style");
142   Real len = ext.length ();
143   if (ext.is_empty ()
144       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= ext.length ())
145       || (glyph_sym == ly_symbol2scm ("bar-line")
146           && non_empty_count <= 1))
147     {
148       me->suicide ();
149       return SCM_UNSPECIFIED;
150     }
151
152   Stencil m;
153   if (glyph_sym == ly_symbol2scm ("bracket"))
154     m = staff_bracket (me, len);
155   else if (glyph_sym == ly_symbol2scm ("brace"))
156     m = staff_brace (me, len);
157   else if (glyph_sym == ly_symbol2scm ("bar-line"))
158     m = simple_bar (me, len);
159   else if (glyph_sym == ly_symbol2scm ("line-bracket"))
160     m = line_bracket (me, len);
161   else if (glyph_sym == ly_symbol2scm ("text"))
162     m = text (me, len);
163
164   m.translate_axis (ext.center (), Y_AXIS);
165   return m.smobbed_copy ();
166 }
167
168 Stencil
169 System_start_delimiter::staff_brace (Grob *me, Real y)
170 {
171   Font_metric *fm = 0;
172   /* We go through the style sheet to lookup the font file
173      name.  This is better than using find_font directly,
174      esp. because that triggers mktextfm for non-existent
175 >     fonts. */
176   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
177                       ly_symbol2scm ("fetaBraces"));
178
179   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
180   fm = select_font (me->layout (), scm_list_n (alist, SCM_UNDEFINED));
181
182   int
183     lo = 0;
184   int hi = max ((int) fm->count () - 1, 2);
185
186   /* do a binary search for each Y, not very efficient, but passable?  */
187   Box b;
188   do
189     {
190       int cmp = (lo + hi) / 2;
191       b = fm->get_indexed_char (cmp);
192       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
193         hi = cmp;
194       else
195         lo = cmp;
196     }
197   while (hi - lo > 1);
198
199   Stencil stil (fm->find_by_name ("brace" + to_string (lo)));
200   stil.translate_axis (-b[X_AXIS].length()/2, X_AXIS);
201
202   stil.translate_axis (-0.2, X_AXIS);
203   
204   return stil;
205 }
206
207 ADD_INTERFACE (System_start_delimiter, "system-start-delimiter-interface",
208                "The brace, bracket or bar in front of the system. "
209                ,
210
211                /* properties */
212                "collapse-height "
213                "style "
214                "text "
215                "long-text "
216                "self-alignment-Y "
217                "thickness "
218                );