]> git.donarmstrong.com Git - lilypond.git/blob - lily/balloon.cc
bad
[lilypond.git] / lily / balloon.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "text-interface.hh"
21 #include "grob.hh"
22 #include "item.hh"
23 #include "line-interface.hh"
24 #include "lookup.hh"
25 #include "font-interface.hh"
26 #include "lily-guile.hh"
27 #include "output-def.hh"
28 #include "misc.hh"
29 #include "spanner.hh"
30 #include "international.hh"
31
32 class Balloon_interface
33 {
34 public:
35   DECLARE_SCHEME_CALLBACK (print, (SCM));
36   DECLARE_SCHEME_CALLBACK (print_spanner, (SCM));
37   DECLARE_GROB_INTERFACE ();
38 };
39
40 MAKE_SCHEME_CALLBACK (Balloon_interface, print, 1);
41 SCM
42 Balloon_interface::print (SCM smob)
43 {
44   Grob *me = unsmob_grob (smob);
45
46   Grob *p = me->get_parent (X_AXIS);
47   
48   Offset off (me->relative_coordinate (p, X_AXIS),
49               me->relative_coordinate (p, Y_AXIS));
50
51   Box b (p->extent (p, X_AXIS),
52          p->extent (p, Y_AXIS));
53
54   Real padding = robust_scm2double (me->get_property ("padding"), .1);
55   b.widen (padding, padding);
56
57   // FIXME
58   Stencil fr;
59   if (to_boolean (me->get_property ("annotation-balloon")))
60     fr = Lookup::frame (b, 0.1, 0.05);
61
62   SCM bt = me->get_property ("text");
63   SCM chain = Font_interface::text_font_alist_chain (me);
64
65   SCM stencil = Text_interface::interpret_markup (me->layout ()->self_scm (),
66                                                   chain, bt);
67
68   Stencil *text_stil = unsmob_stencil (stencil);
69
70   Offset z1;
71   for (int i = X_AXIS; i < NO_AXES; i++)
72     {
73       Axis a ((Axis)i);
74       z1[a] = b[a].linear_combination (sign (off[a]));
75       text_stil->align_to (a, -sign (off[a]));
76     }
77
78   Offset z2 = z1 + off;
79
80   if (to_boolean (me->get_property ("annotation-line")))
81     fr.add_stencil (Line_interface::line (me, z1, z2));
82
83   text_stil->translate (z2);
84   fr.add_stencil (*text_stil);
85
86   fr.translate (-off);
87   return fr.smobbed_copy ();
88 }
89
90 // ugh...code dup...hopefully can be consolidated w/ above one day
91 MAKE_SCHEME_CALLBACK (Balloon_interface, print_spanner, 1);
92 SCM
93 Balloon_interface::print_spanner (SCM smob)
94 {
95   Spanner *me = unsmob_spanner (smob);
96   Spanner *parent = unsmob_spanner (me->get_property ("parent-spanner"));
97   Spanner *p;
98   message (_f ("foo %d", robust_scm2int (me->get_property ("spanner-to-annotate"), 0)));
99   message (_f ("bar %d", robust_scm2int (me->broken_intos_[0]->get_property ("spanner-to-annotate"), 0)));
100   if (parent->broken_intos_.size () == 0)
101     p = parent;
102   else
103     p = parent->broken_intos_[robust_scm2int (me->get_property ("spanner-to-annotate"), 0) % parent->broken_intos_.size ()];
104
105   
106   Drul_array<Item *> bounds;
107   Direction d = LEFT;
108   
109   do
110     {
111       bounds[d] = me->get_bound (d);
112     }
113   while (flip (&d) != LEFT);
114   
115   Grob *commonx = bounds[LEFT]->common_refpoint (bounds[RIGHT], X_AXIS);
116
117   Offset off (me->relative_coordinate (commonx, X_AXIS),
118               me->relative_coordinate (p, Y_AXIS));
119   
120   Box b (p->extent (p, X_AXIS),
121          p->extent (p, Y_AXIS));
122
123   Real padding = robust_scm2double (me->get_property ("padding"), .1);
124   b.widen (padding, padding);
125
126   // FIXME
127   Stencil fr;
128   if (to_boolean (me->get_property ("annotation-balloon")))
129     fr = Lookup::frame (b, 0.1, 0.05);
130
131   SCM bt = me->get_property ("text");
132   SCM chain = Font_interface::text_font_alist_chain (me);
133
134   SCM stencil = Text_interface::interpret_markup (me->layout ()->self_scm (),
135                                                   chain, bt);
136
137   Stencil *text_stil = unsmob_stencil (stencil);
138
139   Offset z1;
140   for (int i = X_AXIS; i < NO_AXES; i++)
141     {
142       Axis a ((Axis)i);
143       z1[a] = b[a].linear_combination (sign (off[a]));
144       text_stil->align_to (a, -sign (off[a]));
145     }
146
147   Offset z2 = z1 + off;
148
149   if (to_boolean (me->get_property ("annotation-line")))
150     fr.add_stencil (Line_interface::line (me, z1, z2));
151
152   text_stil->translate (z2);
153   fr.add_stencil (*text_stil);
154
155   fr.translate (-off);
156   return fr.smobbed_copy ();
157 }
158
159 ADD_INTERFACE (Balloon_interface,
160                "A collection of routines to put text balloons around an"
161                " object.",
162
163                /* properties */
164                "annotation-balloon "
165                "annotation-line "
166                "padding "
167                "spanner-to-annotate "
168                "text "
169                );
170