]> git.donarmstrong.com Git - lilypond.git/blob - lily/kievan-ligature-engraver.cc
85d6d8dc0fb66dcf0eb85d9cb4603908c24e0eef
[lilypond.git] / lily / kievan-ligature-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2013--2014 Aleksandr Andreev <aleksandr.andreev@gmail.com>
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 "coherent-ligature-engraver.hh"
21 #include "font-interface.hh"
22 #include "international.hh"
23 #include "kievan-ligature.hh"
24 #include "paper-column.hh"
25 #include "rhythmic-head.hh"
26 #include "spanner.hh"
27 #include "stream-event.hh"
28 #include "warn.hh"
29
30 #include "translator.icc"
31
32 class Kievan_ligature_engraver : public Coherent_ligature_engraver
33 {
34
35 protected:
36   virtual Spanner *create_ligature_spanner ();
37   virtual void build_ligature (Spanner *ligature,
38                                vector<Grob_info> const &primitives);
39   DECLARE_TRANSLATOR_LISTENER (ligature);
40
41 public:
42   TRANSLATOR_DECLARATIONS (Kievan_ligature_engraver);
43
44 private:
45   void fold_up_primitives (vector<Grob_info> const &primitives, Real padding, Real &min_length);
46 };
47
48 IMPLEMENT_TRANSLATOR_LISTENER (Kievan_ligature_engraver, ligature);
49 void
50 Kievan_ligature_engraver::listen_ligature (Stream_event *ev)
51 {
52   Ligature_engraver::listen_ligature (ev);
53 }
54
55 Kievan_ligature_engraver::Kievan_ligature_engraver ()
56 {
57
58 }
59
60 Spanner *
61 Kievan_ligature_engraver::create_ligature_spanner ()
62 {
63   return make_spanner ("KievanLigature", SCM_EOL);
64 }
65
66 void
67 Kievan_ligature_engraver::fold_up_primitives (vector<Grob_info> const &primitives,
68                                                 Real padding, Real &min_length)
69 {
70   Item *first = 0;
71   Real accumul_acc_space = 0.0;
72   // start us off with some padding on the left
73   min_length = padding;
74
75   for (vsize i = 0; i < primitives.size (); i++)
76     {
77       Item *current = dynamic_cast<Item *> (primitives[i].grob ());
78       Interval my_ext = current->extent (current, X_AXIS);
79       Real head_width = my_ext.length ();
80       if (i == 0)
81          first = current;
82
83       // must keep track of accidentals in spacing problem
84       Grob *acc_gr = unsmob_grob (current->get_object ("accidental-grob"));
85       if (acc_gr && i > 0)
86         {
87            Interval acc_ext = acc_gr->extent (acc_gr, X_AXIS);
88            accumul_acc_space += acc_ext.length();
89         }
90
91       move_related_items_to_column (current, first->get_column (),
92                                     min_length);
93
94       // check if we have any dots
95       if (size_t const dot_count = Rhythmic_head::dot_count (current))
96         {
97           Grob *dot_gr = Rhythmic_head::get_dots (current);
98
99           head_width += Font_interface::get_default_font (current)->
100               find_by_name ("dots.dotkievan").extent (X_AXIS).length() -
101               0.5 * (padding - accumul_acc_space);
102
103           dot_gr->translate_axis (0.5 * (padding - accumul_acc_space), X_AXIS);
104         }
105
106       // add more padding if we have an accidental coming up
107       if (i < primitives.size () - 1)
108         {
109            Item *next = dynamic_cast<Item *> (primitives[i + 1].grob ());
110            Grob *acc_gr = unsmob_grob (next->get_object ("accidental-grob"));
111            if (acc_gr)
112              {
113                 Interval acc_ext = acc_gr->extent (acc_gr, X_AXIS);
114                 padding += acc_ext.length();
115              }
116         }
117
118       min_length += head_width + padding - accumul_acc_space;
119
120     }
121
122 }
123
124 void
125 Kievan_ligature_engraver::build_ligature (Spanner *ligature,
126                                             vector<Grob_info> const &primitives)
127 {
128   Real min_length;
129
130   Real padding = robust_scm2double (ligature->get_property ("padding"), 0.0);
131   fold_up_primitives (primitives, padding, min_length);
132   if (robust_scm2double (ligature->get_property ("minimum-length"), 0.0)
133       < min_length)
134     ligature->set_property ("minimum-length", scm_from_double (min_length));
135
136 }
137
138 ADD_ACKNOWLEDGER (Kievan_ligature_engraver, rest);
139 ADD_ACKNOWLEDGER (Kievan_ligature_engraver, ligature_head);
140
141 ADD_TRANSLATOR (Kievan_ligature_engraver,
142                 /* doc */
143                 "Handle @code{Kievan_ligature_events} by glueing Kievan"
144                 " heads together.",
145
146                 /* create */
147                 "KievanLigature ",
148
149                 /* read */
150                 "",
151
152                 /* write */
153                 ""
154                );