]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-signature.cc
2002-12-06 Han-Wen Nienhuys <hanwen@cs.uu.nl>
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10
11 #include "molecule.hh"
12 #include "text-item.hh"
13 #include "time-signature.hh"
14 #include "paper-def.hh"
15 #include "font-interface.hh"
16 #include "warn.hh"
17 #include "staff-symbol-referencer.hh"
18
19 MAKE_SCHEME_CALLBACK (Time_signature, brew_molecule, 1);
20 /* TODO: make different functions for special and normal timesigs. */
21 SCM
22 Time_signature::brew_molecule (SCM smob) 
23 {
24   Grob * me = unsmob_grob (smob);
25   SCM st = me->get_grob_property ("style");
26   SCM frac = me->get_grob_property ("fraction");
27   int n = 4;
28   int d = 4;
29   if (gh_pair_p (frac))
30     {
31       n = gh_scm2int (ly_car (frac));
32       d = gh_scm2int (ly_cdr (frac));
33     }
34
35   Molecule m;
36   if (gh_symbol_p (st))
37     {
38       String style (ly_scm2string (scm_symbol_to_string (st)));
39       if (style[0]=='1')
40         {
41           m = numbered_time_signature (me, n, 0);
42         }
43       else
44         {
45           m = special_time_signature (me, st, n, d);
46         }
47     }
48   else
49     m = numbered_time_signature (me, n,d);
50
51   if (Staff_symbol_referencer::line_count (me) % 2 == 0)
52     m.translate_axis (Staff_symbol_referencer::staff_space (me)/2 , Y_AXIS);
53
54   return m.smobbed_copy ();
55 }
56
57 Molecule
58 Time_signature::special_time_signature (Grob *me, SCM scm_style, int n, int d)
59 {
60   String style = ly_scm2string (scm_symbol_to_string (scm_style));
61
62   if (style == "numbered")
63     return numbered_time_signature (me, n, d);
64
65   if ((style == "default") || (style == ""))
66     style = to_string ("C");
67
68   if (style == "C")
69     {
70       if /* neither C2/2 nor C4/4 */
71         (((n != 2) || (d != 2)) && 
72          ((n != 4) || (d != 4)))
73         {
74           return numbered_time_signature (me, n, d);
75         }
76     }
77
78   String char_name = style + to_string (n) + "/" + to_string (d);
79   me->set_grob_property ("font-family", ly_symbol2scm ("music"));
80   Molecule out = Font_interface::get_default_font (me)
81     ->find_by_name ("timesig-" + char_name);
82   if (!out.empty_b ())
83     return out;
84
85   /* If there is no such symbol, we default to the numbered style.
86     (Here really with a warning!) */
87   me->warning (_f ("time signature symbol `%s' not found; "
88                    "reverting to numbered style", char_name));
89   return numbered_time_signature (me, n, d);
90 }
91
92 Molecule
93 Time_signature::numbered_time_signature (Grob*me,int num, int den)
94 {
95   SCM chain = Font_interface::font_alist_chain (me);
96   me->set_grob_property ("font-family", ly_symbol2scm ("number"));
97
98   Molecule n =
99     Text_item::text2molecule (me, scm_makfrom0str (to_string (num).to_str0 ()),
100                               chain);
101   Molecule d =
102     Text_item::text2molecule (me, scm_makfrom0str (to_string (den).to_str0 ()),
103                               chain);
104   n.align_to (X_AXIS, CENTER);
105   d.align_to (X_AXIS, CENTER);
106   Molecule m;
107   if (den)
108     {
109       m.add_at_edge (Y_AXIS, UP, n, 0.0, 0);
110       m.add_at_edge (Y_AXIS, DOWN, d, 0.0,0);
111     }
112   else
113     {
114       m = n;
115       m.align_to (Y_AXIS, CENTER);
116     }
117
118   m.align_to (X_AXIS, LEFT);
119   
120   return m;
121 }
122
123 ADD_INTERFACE (Time_signature, "time-signature-interface",
124   "A time signature, in different styles.
125   The following values for 'style are are recognized:
126
127     @table @samp
128       @item @code{C}
129         4/4 and 2/2 are typeset as C and struck C, respectively.  All
130         other time signatures are written with two digits.
131
132       @item @code{neo_mensural}
133         2/2, 3/2, 2/4, 3/4, 4/4, 6/4, 9/4, 4/8, 6/8 and 9/8 are
134         typeset with neo-mensural style mensuration marks.  All other time
135         signatures are written with two digits.
136
137       @item @code{mensural}
138         2/2, 3/2, 2/4, 3/4, 4/4, 6/4, 9/4, 4/8, 6/8 and 9/8 are
139         typeset with mensural style mensuration marks.  All other time
140         signatures are written with two digits.
141
142       @item @code{1xxx}
143         All time signatures are typeset with a single
144         digit, e.g. 3/2 is written as 3. (Any symbol starting
145         with the digit @code{1} will do.)
146     @end table
147
148 See also the test-file @file{input/test/time.ly}.
149 ",
150   "fraction style");