]> git.donarmstrong.com Git - lilypond.git/blob - lily/system-start-delimiter.cc
(class Grob): move pscore, dim_cache_,
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "system-start-delimiter.hh"
10
11
12 #include "spanner.hh"
13 #include "axis-group-interface.hh"
14 #include "output-def.hh"
15 #include "font-interface.hh"
16 #include "all-font-metrics.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "lookup.hh"
19 #include "item.hh"
20
21 #include "pointer-group-interface.hh"
22
23 Stencil
24 System_start_delimiter::staff_bracket (Grob *me, Real height)
25 {
26   Font_metric *fm = Font_interface::get_default_font (me);
27   Drul_array<Stencil> tips (fm->find_by_name ("brackettips.down"),
28                             fm->find_by_name ("brackettips.up"));
29
30   Real thickness = robust_scm2double (me->get_property ("thickness"), 0.25);
31
32   Real overlap = 0.1 * thickness;
33
34   Stencil bracket = Lookup::filled_box (Box (Interval (0, thickness),
35                                              Interval (-1, 1)
36                                              * (height / 2 + overlap)));
37
38   Direction d = DOWN;
39   do
40     bracket.add_at_edge (Y_AXIS, d, tips[d], -overlap, 0.0);
41   while (flip (&d) != DOWN)
42     ;
43
44   return bracket;
45 }
46
47 Stencil
48 System_start_delimiter::simple_bar (Grob *me, Real h)
49 {
50   Real lt = me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
51   Real w = lt * robust_scm2double (me->get_property ("thickness"), 1);
52   return Lookup::round_filled_box (Box (Interval (0, w), Interval (-h / 2, h / 2)),
53                                    lt);
54 }
55
56 MAKE_SCHEME_CALLBACK (System_start_delimiter, print, 1);
57 SCM
58 System_start_delimiter::print (SCM smob)
59 {
60   Spanner *me = unsmob_spanner (smob);
61   if (!me)
62     return SCM_EOL;
63
64   SCM s = me->get_property ("glyph");
65   if (!scm_is_string (s))
66     return SCM_EOL;
67   SCM gsym = scm_string_to_symbol (s);
68
69   Real staff_space = Staff_symbol_referencer::staff_space (me);
70
71   extract_grob_set (me, "elements", elts);
72   Grob *common = common_refpoint_of_array (elts, me, Y_AXIS);
73
74   Interval ext;
75
76   int non_empty_count = 0;
77   for (int i = elts.size (); i--;)
78     {
79       Spanner *sp = dynamic_cast<Spanner *> (elts[i]);
80
81       if (sp
82           && sp->get_bound (LEFT) == me->get_bound (LEFT))
83         {
84           Interval dims = sp->extent (common, Y_AXIS);
85           if (!dims.is_empty ())
86             {
87               non_empty_count ++;
88               ext.unite (dims);
89             }
90         }
91     }
92
93   if (gsym == ly_symbol2scm ("bar-line")
94       && non_empty_count <= 1)
95     {
96       me->suicide ();
97       return SCM_EOL;
98     }
99   
100   ext -= me->relative_coordinate (common, Y_AXIS);
101
102   Real len = ext.length () / staff_space;
103
104   if (ext.is_empty ()
105       || (robust_scm2double (me->get_property ("collapse-height"), 0.0) >= len))
106     {
107       me->suicide ();
108       return SCM_EOL;
109     }
110
111   Stencil m;
112
113   if (gsym == ly_symbol2scm ("bracket"))
114     m = staff_bracket (me, len);
115   else if (gsym == ly_symbol2scm ("brace"))
116     m = staff_brace (me, len);
117   else if (gsym == ly_symbol2scm ("bar-line"))
118     m = simple_bar (me, len);
119
120   m.translate_axis (ext.center (), Y_AXIS);
121   return m.smobbed_copy ();
122 }
123
124 Stencil
125 System_start_delimiter::staff_brace (Grob *me, Real y)
126 {
127   Font_metric *fm = 0;
128   /* We go through the style sheet to lookup the font file
129      name.  This is better than using find_font directly,
130      esp. because that triggers mktextfm for non-existent
131      fonts. */
132   SCM fam = scm_cons (ly_symbol2scm ("font-encoding"),
133                       ly_symbol2scm ("fetaBraces"));
134
135   SCM alist = scm_list_n (fam, SCM_UNDEFINED);
136   fm = select_font (me->layout (), scm_list_n (alist, SCM_UNDEFINED));
137
138   int lo = 0;
139   int hi = max (fm->count () - 1, 2);
140
141   /* do a binary search for each Y, not very efficient, but passable?  */
142   Box b;
143   do
144     {
145       int cmp = (lo + hi) / 2;
146       b = fm->get_indexed_char (cmp);
147       if (b[Y_AXIS].is_empty () || b[Y_AXIS].length () > y)
148         hi = cmp;
149       else
150         lo = cmp;
151     }
152   while (hi - lo > 1);
153
154   Stencil stil (fm->find_by_name ("brace" + to_string (lo)));
155   stil.translate_axis (-b[X_AXIS].length()/2, X_AXIS);
156
157   return stil;
158 }
159
160 ADD_INTERFACE (System_start_delimiter, "system-start-delimiter-interface",
161                "The brace, bracket or bar in front of the system. "
162                "It is implemented as a spanner.",
163
164                /* properties */
165                "collapse-height "
166                "glyph "
167                "thickness "
168                );