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