]> git.donarmstrong.com Git - lilypond.git/blob - lily/balloon.cc
Implements annotations for footnotes.
[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
31 class Balloon_interface
32 {
33 public:
34   DECLARE_SCHEME_CALLBACK (print, (SCM));
35   DECLARE_SCHEME_CALLBACK (print_spanner, (SCM));
36   DECLARE_GROB_INTERFACE ();
37
38   static SCM internal_balloon_print (Grob *me, Grob *p, Offset off);
39 };
40
41 MAKE_SCHEME_CALLBACK (Balloon_interface, print, 1);
42 SCM
43 Balloon_interface::print (SCM smob)
44 {
45   Grob *me = unsmob_grob (smob);
46
47   if (Item *item = dynamic_cast<Item *> (me))
48     if (!Item::break_visible (item))
49       return SCM_EOL;
50
51   Grob *p = me->get_parent (X_AXIS);
52
53   Offset off (me->relative_coordinate (p, X_AXIS),
54               me->relative_coordinate (p, Y_AXIS));
55
56   return internal_balloon_print (me, p, off);
57 }
58
59 // ugh...code dup...hopefully can be consolidated w/ above one day
60 MAKE_SCHEME_CALLBACK (Balloon_interface, print_spanner, 1);
61 SCM
62 Balloon_interface::print_spanner (SCM smob)
63 {
64   Spanner *me = unsmob_spanner (smob);
65   Grob *orig = me->original ();
66
67   if (orig)
68     {
69       // TODO : consolidate code dup from System::get_footnote_grobs_in_range
70       int pos = orig->spanned_rank_interval ()[LEFT];
71       Real spanner_placement = min (1.0,
72                                     max (robust_scm2double (me->get_property ("spanner-placement"), -1.0),
73                                          -1.0));
74
75       spanner_placement = (spanner_placement + 1.0) / 2.0;
76       int rpos = orig->spanned_rank_interval ()[RIGHT];
77       pos = (int)((rpos - pos) * spanner_placement + pos + 0.5);
78
79       if (pos < me->spanned_rank_interval () [LEFT])
80         return SCM_EOL;
81       if (pos >= me->spanned_rank_interval () [RIGHT] && (me->spanned_rank_interval () [RIGHT] != orig->spanned_rank_interval () [RIGHT]))
82         return SCM_EOL;
83     }
84
85
86   Spanner *p = dynamic_cast<Spanner *> (me->get_parent (Y_AXIS));
87
88   if (!p)
89     return SCM_EOL;
90
91   Offset off (me->relative_coordinate (me->get_bound (LEFT), X_AXIS),
92               me->relative_coordinate (p, Y_AXIS));
93   return internal_balloon_print (me, p, off);
94 }
95
96 SCM
97 Balloon_interface::internal_balloon_print (Grob *me, Grob *p, Offset off)
98 {
99   Box b (p->extent (p, X_AXIS),
100          p->extent (p, Y_AXIS));
101   Real padding = robust_scm2double (me->get_property ("padding"), .1);
102   b.widen (padding, padding);
103
104   // FIXME
105   Stencil fr;
106   if (to_boolean (me->get_property ("annotation-balloon")))
107     fr = Lookup::frame (b, 0.1, 0.05);
108
109   SCM bt = me->get_property ("text");
110   SCM chain = Font_interface::text_font_alist_chain (me);
111
112   SCM stencil = Text_interface::interpret_markup (me->layout ()->self_scm (),
113                                                   chain, bt);
114
115   Stencil *text_stil = unsmob_stencil (stencil);
116
117   Offset z1;
118   for (int i = X_AXIS; i < NO_AXES; i++)
119     {
120       Axis a ((Axis)i);
121       z1[a] = b[a].linear_combination (sign (off[a]));
122       text_stil->align_to (a, -sign (off[a]));
123     }
124
125   Offset z2 = z1 + off;
126
127   if (to_boolean (me->get_property ("annotation-line")))
128     fr.add_stencil (Line_interface::line (me, z1, z2));
129
130   text_stil->translate (z2);
131   fr.add_stencil (*text_stil);
132
133   fr.translate (-off);
134   return fr.smobbed_copy ();
135 }
136
137 ADD_INTERFACE (Balloon_interface,
138                "A collection of routines to put text balloons around an"
139                " object.",
140
141                /* properties */
142                "annotation-balloon "
143                "annotation-line "
144                "padding "
145                "spanner-placement "
146                "text "
147                );
148