]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name.cc
release: 1.3.62
[lilypond.git] / lily / chord-name.cc
1 /*
2   chord-name.cc -- implement Chord_name
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "chord-name.hh"
10 #include "musical-request.hh"
11 #include "warn.hh"
12 #include "debug.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
15 #include "lookup.hh"
16 #include "staff-symbol-referencer.hh"
17
18
19 /*
20   TODO: move text lookup out of Chord_name
21  */
22
23 /*
24   word is roman text or property-styled text:
25    "text"
26    ("text" . property-alist)
27  */
28
29 Molecule
30 Chord_name::ly_word2molecule (SCM word, Real* x) const
31 {
32   *x = 0;
33
34   SCM options_alist = SCM_EOL;
35   if (gh_pair_p (word))
36     {
37       options_alist = gh_cdr (word);
38       word = gh_car (word);
39     }
40
41   if (gh_string_p (word))
42     {
43       /*
44         UGH. Should read from font metric structure.
45       */
46       Real ex = lookup_l ()->text ("", "x",
47                                    paper_l ()).extent (Y_AXIS).length ();
48       Real em = lookup_l ()->text ("", "m",
49                                    paper_l ()).extent (X_AXIS).length ();
50
51       String w = ly_scm2string (word);
52
53       String style;
54       SCM s = scm_assoc (ly_symbol2scm ("style"), options_alist);
55       if (s != SCM_BOOL_F)
56         {
57           style = ly_scm2string (gh_cdr (s));
58         }
59
60       Offset offset;
61       int size = 0;
62       /*
63         urg, `type'
64       */
65       s = scm_assoc (ly_symbol2scm ("type"), options_alist);
66       if (s != SCM_BOOL_F && ly_scm2string (gh_cdr (s)) == "super")
67         {
68           Real super_y = ex / 2;
69           offset = Offset (0, super_y);
70           if (!size)
71             size = -2;
72         }
73
74       s = scm_assoc (ly_symbol2scm ("size"), options_alist);
75       if (s != SCM_BOOL_F)
76         {
77           size = gh_scm2int (gh_cdr (s));
78         }
79
80       s = scm_assoc (ly_symbol2scm ("offset"), options_alist);
81       if (s != SCM_BOOL_F)
82         {
83           // hmm
84           SCM o = gh_cdr (s);
85           if (gh_pair_p (o))
86             offset = Offset (0, gh_scm2double (gh_cdr (o))) * ex;
87           *x = gh_scm2double (gh_car (o)) * em;
88         }
89
90       Molecule mol;
91       s = scm_assoc (ly_symbol2scm ("font"), options_alist);
92       if (s != SCM_BOOL_F && ly_scm2string (gh_cdr (s)) == "feta")
93         mol = paper_l ()->lookup_l (size)->afm_find (w);
94       else
95         mol = paper_l ()->lookup_l (size)->text (style, w, paper_l ());
96
97       mol.translate (offset);
98       return mol;
99     }
100   return Molecule ();
101 }
102
103 /*
104   ;; text: list of word
105   ;; word: string + optional list of property
106   ;; property: align, kern, font (?), size
107  */
108 Molecule
109 Chord_name::ly_text2molecule (SCM text) const
110 {
111   Molecule mol;
112   if (gh_list_p (text))
113     {
114       while (gh_cdr (text) != SCM_EOL)
115         {
116           Real x;
117           Molecule m = ly_word2molecule (gh_car (text), &x);
118           if (!m.empty_b ())
119             mol.add_at_edge (X_AXIS, RIGHT, m, x);
120           text = gh_cdr (text);
121         }
122       text = gh_car (text);
123     }  
124   Real x;
125   Molecule m = ly_word2molecule (text, &x);
126   if (!m.empty_b ())
127     mol.add_at_edge (X_AXIS, RIGHT, m, x);
128   return mol;
129 }
130
131 MAKE_SCHEME_SCORE_ELEMENT_CALLBACK(Chord_name,brew_molecule);
132
133 SCM
134 Chord_name::brew_molecule (SCM smob) 
135 {
136   Score_element *sc = unsmob_element (smob);
137   SCM style = sc->get_elt_property ("style");
138   if (style == SCM_UNDEFINED)
139     style = ly_str02scm ("banter");
140
141   SCM inversion = sc-> get_elt_property ("inversion");
142   if (inversion == SCM_UNDEFINED)
143     inversion = SCM_BOOL_F;
144
145   SCM bass =  sc->get_elt_property ("bass");
146   if (bass == SCM_UNDEFINED)
147     bass = SCM_BOOL_F;
148
149   SCM pitches =  sc->get_elt_property ("pitches");
150
151   SCM text = scm_eval (gh_list (ly_symbol2scm ("chord::user-name"),
152                                 style,
153                                 ly_quote_scm (pitches),
154                                 ly_quote_scm (gh_cons (inversion, bass)),
155                                 SCM_UNDEFINED));
156
157   return dynamic_cast<Chord_name*> (sc)->
158     ly_text2molecule (text).create_scheme ();
159 }
160
161 Chord_name::Chord_name (SCM s)
162   : Item (s)
163 {
164 }