]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-signature.cc
release: 1.5.33
[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
18 MAKE_SCHEME_CALLBACK (Time_signature,brew_molecule,1);
19 /*
20   TODO: make different functions for special and normal timesigs.
21  */
22 SCM
23 Time_signature::brew_molecule (SCM smob) 
24 {
25   Grob * me = unsmob_grob (smob);
26   SCM st = me->get_grob_property ("style");
27   SCM frac = me->get_grob_property ("fraction");
28   int n = 4;
29   int d = 4;
30   if (gh_pair_p (frac))
31     {
32       n = gh_scm2int (ly_car (frac));
33       d = gh_scm2int (ly_cdr (frac));
34     }
35
36   
37   if (gh_symbol_p (st))
38     {
39       String style (ly_scm2string (scm_symbol_to_string (st)));
40       if (style[0]=='1')
41         {
42           return time_signature (me, n, 0).smobbed_copy ();
43         }
44       else
45         {
46           return special_time_signature (me, style, n, d).smobbed_copy ();
47         }
48     }
49   else
50     return time_signature (me, n,d).smobbed_copy ();
51 }
52
53 Molecule
54 Time_signature::special_time_signature (Grob*me, String s, int n, int d)
55 {
56   /*
57     Randomly probing the font sucks?
58   */
59   
60   SCM alist_chain = Font_interface::font_alist_chain (me);
61   
62   SCM style_chain =
63     Font_interface::add_style (me, ly_symbol2scm ("timesig-symbol"),
64                                alist_chain);
65
66   Font_metric *feta = Font_interface::get_font (me, style_chain);
67
68   /*
69     First guess: s contains only the signature style, append fraction.
70   */
71   String symbolname = "timesig-" + s + to_str (n) + "/" + to_str (d);
72   
73   Molecule m = feta->find_by_name (symbolname);
74   if (!m.empty_b ()) 
75     return m;
76
77   /*
78     Second guess: s contains the full signature name
79   */
80   m = feta->find_by_name ("timesig-" + s);
81   if (!m.empty_b ()) 
82     return m;
83
84   String message =
85       "unknown time signature `" + s +
86       "'; resorting to default layout with numbers";
87   warning (_ (message.ch_C ()));
88
89   // Resort to default layout with numbers
90   return time_signature (me, n, d);
91 }
92
93
94 Molecule
95 Time_signature::time_signature (Grob*me,int num, int den)
96 {
97   SCM chain = Font_interface::font_alist_chain (me);
98
99   Molecule n = Text_item::text2molecule (me,
100                                          ly_str02scm (to_str (num).ch_C ()),
101                                          chain);
102   Molecule d = Text_item::text2molecule (me,
103                                          ly_str02scm (to_str (den).ch_C ()),
104                                          chain);
105   n.align_to (X_AXIS, CENTER);
106   d.align_to (X_AXIS, CENTER);
107   Molecule m;
108   if (den)
109     {
110       m.add_at_edge (Y_AXIS, UP, n, 0.0);
111       m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
112     }
113   else
114     {
115       m = n;
116       m.align_to (Y_AXIS, CENTER);
117     }
118   return m;
119 }
120