]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-signature.cc
* flower/include/std-string.hh:
[lilypond.git] / lily / time-signature.cc
1 /*
2   time-signature.cc -- implement Time_signature
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "time-signature.hh"
10
11 #include "font-interface.hh"
12 #include "international.hh"
13 #include "output-def.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "text-interface.hh"
16 #include "warn.hh"
17
18 /*
19   TODO:
20
21   this file should go ; The formatting can completely be done with
22   markups.
23 */
24
25 MAKE_SCHEME_CALLBACK (Time_signature, print, 1);
26 SCM
27 Time_signature::print (SCM smob)
28 {
29   Grob *me = unsmob_grob (smob);
30   SCM st = me->get_property ("style");
31   SCM frac = me->get_property ("fraction");
32   int n = 4;
33   int d = 4;
34   if (scm_is_pair (frac))
35     {
36       n = scm_to_int (scm_car (frac));
37       d = scm_to_int (scm_cdr (frac));
38     }
39
40   Stencil m;
41   if (st == ly_symbol2scm ("single-digit"))
42     m = numbered_time_signature (me, n, 0);
43   else if (scm_is_symbol (st))
44     m = special_time_signature (me, st, n, d);
45   else
46     m = numbered_time_signature (me, n, d);
47
48   if (Staff_symbol_referencer::line_count (me) % 2 == 0)
49     m.translate_axis (Staff_symbol_referencer::staff_space (me) / 2, Y_AXIS);
50
51   return m.smobbed_copy ();
52 }
53
54 Stencil
55 Time_signature::special_time_signature (Grob *me, SCM scm_style, int n, int d)
56 {
57   string style = ly_scm2string (scm_symbol_to_string (scm_style));
58
59   if (style == "numbered")
60     return numbered_time_signature (me, n, d);
61
62   if ((style == "default") || (style == ""))
63     style = to_string ("C");
64
65   if (style == "C")
66     {
67       if /* neither C2/2 nor C4/4 */
68         (((n != 2) || (d != 2))
69          && ((n != 4) || (d != 4)))
70         return numbered_time_signature (me, n, d);
71     }
72
73   string char_name = style + to_string (n) + to_string (d);
74   me->set_property ("font-encoding", ly_symbol2scm ("fetaMusic"));
75   Stencil out = Font_interface::get_default_font (me)
76     ->find_by_name ("timesig." + char_name);
77   if (!out.is_empty ())
78     return out;
79
80   /* If there is no such symbol, we default to the numbered style.
81      (Here really with a warning!) */
82   me->warning (_f ("time signature symbol `%s' not found; "
83                    "reverting to numbered style", char_name));
84   return numbered_time_signature (me, n, d);
85 }
86
87 Stencil
88 Time_signature::numbered_time_signature (Grob *me, int num, int den)
89 {
90   SCM chain = me->get_property_alist_chain (Font_interface::text_font_alist_chain (me));
91   chain = scm_cons (scm_list_1 (scm_cons (ly_symbol2scm ("font-encoding"),
92                                           ly_symbol2scm ("fetaNumber"))),
93                     chain);
94
95   SCM sn = Text_interface::interpret_markup (me->layout ()->self_scm (), chain,
96                                              scm_makfrom0str (to_string (num).c_str ()));
97   SCM sd = Text_interface::interpret_markup (me->layout ()->self_scm (), chain,
98                                              scm_makfrom0str (to_string (den).c_str ()));
99
100   Stencil n = *unsmob_stencil (sn);
101   Stencil d = *unsmob_stencil (sd);
102
103   n.align_to (X_AXIS, CENTER);
104   d.align_to (X_AXIS, CENTER);
105   Stencil m;
106   if (den)
107     {
108       m.add_at_edge (Y_AXIS, UP, n, 0.0, 0);
109       m.add_at_edge (Y_AXIS, DOWN, d, 0.0, 0);
110     }
111   else
112     {
113       m = n;
114       m.align_to (Y_AXIS, CENTER);
115     }
116
117   m.align_to (X_AXIS, LEFT);
118
119   return m;
120 }
121
122 ADD_INTERFACE (Time_signature, "time-signature-interface",
123                "A time signature, in different styles.\n"
124                "  The following values for 'style are are recognized:\n"
125                "\n"
126                "    @table @samp\n"
127                "      @item @code{C}\n"
128                "        4/4 and 2/2 are typeset as C and struck C, respectively.  All\n"
129                "        other time signatures are written with two digits.\n"
130                "\n"
131                "      @item @code{neomensural}\n"
132                "        2/2, 3/2, 2/4, 3/4, 4/4, 6/4, 9/4, 4/8, 6/8 and 9/8 are\n"
133                "        typeset with neo-mensural style mensuration marks.  All other time\n"
134                "        signatures are written with two digits.\n"
135                "\n"
136                "      @item @code{mensural}\n"
137                "        2/2, 3/2, 2/4, 3/4, 4/4, 6/4, 9/4, 4/8, 6/8 and 9/8 are\n"
138                "        typeset with mensural style mensuration marks.  All other time\n"
139                "        signatures are written with two digits.\n"
140                "\n"
141                "      @item @code{single-digit}\n"
142                "        All time signatures are typeset with a single\n"
143                "        digit, e.g. 3/2 is written as 3.\n"
144                "    @end table\n"
145                "\n"
146                "See also the test-file @file{input/test/time.ly}.\n",
147                "fraction style");