summaryrefslogtreecommitdiff
path: root/lily/text-spanner-engraver.cc
blob: b7628c325c29fef4d5ec88aab8d164500a85278c (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
  text-spanner-engraver.cc -- implement Text_spanner_engraver

  source file of the GNU LilyPond music typesetter

  (c) 2000--2006 Jan Nieuwenhuizen <janneke@gnu.org>
*/

#include "engraver.hh"

#include "international.hh"
#include "note-column.hh"
#include "side-position-interface.hh"
#include "stream-event.hh"

#include "translator.icc"

class Text_spanner_engraver : public Engraver
{
public:
  TRANSLATOR_DECLARATIONS (Text_spanner_engraver);
protected:
  virtual void finalize ();
  DECLARE_ACKNOWLEDGER (note_column);
  DECLARE_TRANSLATOR_LISTENER (text_span);
  void stop_translation_timestep ();
  void process_music ();

private:
  Spanner *span_;
  Spanner *finished_;
  Stream_event *current_event_;
  Drul_array<Stream_event *> event_drul_;
  void typeset_all ();
};

Text_spanner_engraver::Text_spanner_engraver ()
{
  finished_ = 0;
  current_event_ = 0;
  span_ = 0;
  event_drul_[START] = 0;
  event_drul_[STOP] = 0;
}

IMPLEMENT_TRANSLATOR_LISTENER (Text_spanner_engraver, text_span);
void
Text_spanner_engraver::listen_text_span (Stream_event *ev)
{
  Direction d = to_dir (ev->get_property ("span-direction"));
  event_drul_[d] = ev;
}

void
Text_spanner_engraver::process_music ()
{
  if (event_drul_[STOP])
    {
      if (!span_)
	event_drul_[STOP]->origin ()->warning (_ ("can't find start of text spanner"));
      else
	{
	  finished_ = span_;
	  span_ = 0;
	  current_event_ = 0;
	}
    }

  if (event_drul_[START])
    {
      if (current_event_)
	event_drul_[START]->origin ()->warning (_ ("already have a text spanner"));
      else
	{
	  current_event_ = event_drul_[START];
	  span_ = make_spanner ("TextSpanner", event_drul_[START]->self_scm ());

	  Side_position_interface::set_axis (span_, Y_AXIS);
	  event_drul_[START] = 0;
	}
    }
}

void
Text_spanner_engraver::acknowledge_note_column (Grob_info info)
{
  Spanner *spans[2] ={span_, finished_};
  for (int i = 0; i < 2; i++)
    {
      if (spans[i])
	{
	  Side_position_interface::add_support (spans[i], info.grob ());
	  add_bound_item (spans[i], dynamic_cast<Item *> (info.grob ()));
	}
    }
}

void
Text_spanner_engraver::typeset_all ()
{
  if (finished_)
    {
      if (!finished_->get_bound (RIGHT))
	{
	  Grob *e = unsmob_grob (get_property ("currentMusicalColumn"));
	  finished_->set_bound (RIGHT, e);
	}
      finished_ = 0;
    }
}

void
Text_spanner_engraver::stop_translation_timestep ()
{
  if (span_ && !span_->get_bound (LEFT))
    {
      Grob *e = unsmob_grob (get_property ("currentMusicalColumn"));
      span_->set_bound (LEFT, e);
    }

  typeset_all ();
  event_drul_[START] = 0;
  event_drul_[STOP] = 0;
}

void
Text_spanner_engraver::finalize ()
{
  typeset_all ();
  if (span_)
    {
      current_event_->origin ()->warning (_ ("unterminated text spanner"));
      span_->suicide ();
      span_ = 0;
    }
}

ADD_ACKNOWLEDGER (Text_spanner_engraver, note_column);
ADD_TRANSLATOR (Text_spanner_engraver,
		/* doc */ "Create text spanner from a Music.",
		/* create */ "TextSpanner",
		/* accept */ "text-span-event",
		/* read */ "",
		/* write */ "");