]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-signature.cc
release: 1.5.37
[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   m.align_to (X_AXIS, LEFT);
82   if (!m.empty_b ()) 
83     return m;
84
85   String message =
86       "unknown time signature `" + s +
87       "'; resorting to default layout with numbers";
88   warning (_ (message.ch_C ()));
89
90   // Resort to default layout with numbers
91   return time_signature (me, n, d);
92 }
93
94
95 Molecule
96 Time_signature::time_signature (Grob*me,int num, int den)
97 {
98   SCM chain = Font_interface::font_alist_chain (me);
99
100   Molecule n = Text_item::text2molecule (me,
101                                          ly_str02scm (to_str (num).ch_C ()),
102                                          chain);
103   Molecule d = Text_item::text2molecule (me,
104                                          ly_str02scm (to_str (den).ch_C ()),
105                                          chain);
106   n.align_to (X_AXIS, CENTER);
107   d.align_to (X_AXIS, CENTER);
108   Molecule m;
109   if (den)
110     {
111       m.add_at_edge (Y_AXIS, UP, n, 0.0);
112       m.add_at_edge (Y_AXIS, DOWN, d, 0.0);
113     }
114   else
115     {
116       m = n;
117       m.align_to (Y_AXIS, CENTER);
118     }
119
120   m.align_to (X_AXIS, LEFT);
121   
122   return m;
123 }
124