]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-signature.cc
263817c7082fc82b724b9b765965a312487d85ea
[lilypond.git] / lily / time-signature.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2012 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 "time-signature.hh"
21
22 #include "grob.hh"
23 #include "font-interface.hh"
24 #include "international.hh"
25 #include "output-def.hh"
26 #include "staff-symbol.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "text-interface.hh"
29 #include "warn.hh"
30
31 /*
32   TODO:
33
34   this file should go ; The formatting can completely be done with
35   markups.
36 */
37
38 MAKE_SCHEME_CALLBACK (Time_signature, print, 1);
39 SCM
40 Time_signature::print (SCM smob)
41 {
42   Grob *me = unsmob_grob (smob);
43   SCM st = me->get_property ("style");
44   SCM frac = me->get_property ("fraction");
45   int n = 4;
46   int d = 4;
47   if (scm_is_pair (frac))
48     {
49       n = scm_to_int (scm_car (frac));
50       d = scm_to_int (scm_cdr (frac));
51     }
52
53   Stencil m;
54   if (st == ly_symbol2scm ("single-digit"))
55     m = numbered_time_signature (me, n, 0);
56   else if (scm_is_symbol (st))
57     m = special_time_signature (me, st, n, d);
58   else
59     m = numbered_time_signature (me, n, d);
60
61   /*
62     position the signature centred on the staff line
63     nearest to the middle of the staff
64   */
65   if (Grob *staff = Staff_symbol_referencer::get_staff_symbol (me))
66     {
67       std::vector<Real> const linepos = Staff_symbol::line_positions (staff);
68       if (!linepos.empty ())
69         {
70           Interval const span = Staff_symbol::line_span (staff);
71           Real const mid = span.center ();
72           Real pos = linepos.front ();
73           Real dist = fabs (pos - mid);
74           for (std::vector<Real>::const_iterator
75                i = linepos.begin (), e = linepos.end ();
76                ++i != e;)
77             {
78               double const d = fabs (*i - mid);
79               if (d < dist)
80                 {
81                   pos = *i;
82                   dist = d;
83                 }
84             }
85
86           m.translate_axis
87           (pos * Staff_symbol_referencer::staff_space (me) / 2, Y_AXIS);
88         }
89     }
90
91   return m.smobbed_copy ();
92 }
93
94 Stencil
95 Time_signature::special_time_signature (Grob *me, SCM scm_style, int n, int d)
96 {
97   string style = robust_symbol2string (scm_style, "default");
98
99   if (style == "numbered")
100     return numbered_time_signature (me, n, d);
101
102   if ((style == "default") || (style == ""))
103     style = to_string ("C");
104
105   if (style == "C")
106     {
107       if /* neither C2/2 nor C4/4 */
108       (((n != 2) || (d != 2))
109           && ((n != 4) || (d != 4)))
110         return numbered_time_signature (me, n, d);
111     }
112
113   string char_name = style + to_string (n) + to_string (d);
114   me->set_property ("font-encoding", ly_symbol2scm ("fetaMusic"));
115   Stencil out = Font_interface::get_default_font (me)
116                 ->find_by_name ("timesig." + char_name);
117   if (!out.is_empty ())
118     return out;
119
120   /* If there is no such symbol, we default to the numbered style.
121      (Here really with a warning!) */
122   me->warning (_f ("time signature symbol `%s' not found; "
123                    "reverting to numbered style", char_name));
124   return numbered_time_signature (me, n, d);
125 }
126
127 Stencil
128 Time_signature::numbered_time_signature (Grob *me, int num, int den)
129 {
130   SCM chain = me->get_property_alist_chain (Font_interface::text_font_alist_chain (me));
131   chain = scm_cons (scm_list_1 (scm_cons (ly_symbol2scm ("font-encoding"),
132                                           ly_symbol2scm ("fetaText"))),
133                     chain);
134
135   SCM sn = Text_interface::interpret_markup (me->layout ()->self_scm (), chain,
136                                              ly_string2scm (to_string (num)));
137   SCM sd = Text_interface::interpret_markup (me->layout ()->self_scm (), chain,
138                                              ly_string2scm (to_string (den)));
139
140   Stencil n = *unsmob_stencil (sn);
141   Stencil d = *unsmob_stencil (sd);
142
143   n.align_to (X_AXIS, CENTER);
144   d.align_to (X_AXIS, CENTER);
145   Stencil m;
146   if (den)
147     {
148       m.add_at_edge (Y_AXIS, UP, n, 0.0);
149       m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
150     }
151   else
152     {
153       m = n;
154       m.align_to (Y_AXIS, CENTER);
155     }
156
157   m.align_to (X_AXIS, LEFT);
158
159   return m;
160 }
161
162 ADD_INTERFACE (Time_signature,
163                "A time signature, in different styles.  The following values"
164                " for @code{style} are are recognized:\n"
165                "\n"
166                "@table @code\n"
167                "@item C\n"
168                "4/4 and 2/2 are typeset as C and struck C, respectively."
169                "  All other time signatures are written with two digits."
170                "  The value @code{default} is equivalent to @code{C}.\n"
171                "@item neomensural\n"
172                "2/2, 3/2, 2/4, 3/4, 4/4, 6/4, 9/4, 4/8, 6/8, and 9/8 are"
173                " typeset with neo-mensural style mensuration marks.  All"
174                " other time signatures are written with two digits.\n"
175                "@item mensural\n"
176                "2/2, 3/2, 2/4, 3/4, 4/4, 6/4, 9/4, 4/8, 6/8, and 9/8 are"
177                " typeset with mensural style mensuration marks.  All other"
178                " time signatures are written with two digits.\n"
179                "@item single-digit\n"
180                "All time signatures are typeset with a single digit, e.g.,"
181                " 3/2 is written as 3.\n"
182                "@item numbered\n"
183                "All time signatures are typeset with two digits.\n"
184                "@end table",
185
186                /* properties */
187                "fraction "
188                "style "
189               );