]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
release: 1.3.105
[lilypond.git] / lily / text-item.cc
1 /*   
2   text-item.cc -- implement Text_item
3
4   source file of the GNU LilyPond music typesetter
5   
6  (c) 1998--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8  */
9 #include <math.h>
10
11 #include "debug.hh"
12 #include "text-item.hh"
13 #include "paper-def.hh"
14 #include "font-interface.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "main.hh"
18 #include "all-font-metrics.hh"
19 #include "afm.hh"
20
21
22 /*
23     TEXT : STRING | (MARKUP SENTENCE)
24     MARKUP: PROPERTY | ABBREV
25     SENTENCE: TEXT | SENTENCE TEXT
26     PROPERTY: (key . value)
27     ABBREV: rows lines roman music bold italic named super sub text, or any font-style
28  */
29
30 /*
31   TODO:
32
33   rewrite routines and syntax to be like
34
35   TEXT: STRING
36       | (head-expression* TEXT*)
37       ;
38
39   head-expression is a list, containing a tag and a variable number of
40   arguments. If necessary, the number of arguments can be stored in a alist,
41
42   '(
43    (tag1 . argcount1)
44    (tag2 . argcount2)
45
46    ... etc
47    
48    )
49
50    or even entries like
51
52    (tag . (argcount function-to-handle-the-tag  ))
53   
54  */
55
56 Molecule
57 Text_item::text2molecule (Score_element *me, SCM text, SCM alist_chain) 
58 {
59   if (gh_string_p (text))
60     return string2molecule (me, text, alist_chain);
61   else if (gh_list_p (text))
62     {
63       if (!gh_pair_p (gh_car (text)) && gh_string_p (gh_car (text)))
64         return string2molecule (me, gh_car (text), alist_chain);
65       else
66         return markup_sentence2molecule (me, text, alist_chain);
67     }
68   return Molecule ();
69 }
70              
71 Molecule
72 Text_item::string2molecule (Score_element *me, SCM text, SCM alist_chain)
73 {
74   SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
75                               alist_chain);
76   if  (gh_pair_p (style) && gh_symbol_p (gh_cdr (style)))
77     alist_chain = Font_interface::add_style (me, gh_cdr(style), alist_chain);
78
79   Font_metric *fm = Font_interface::get_font (me, alist_chain);
80   
81   SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
82     
83   Molecule mol;
84   if (gh_pair_p (lookup) && gh_cdr (lookup) ==ly_symbol2scm ("name"))
85     mol = lookup_character (me, fm, text);
86   else
87     mol = lookup_text (me, fm, text);
88   
89   return mol;
90 }
91
92 Molecule
93 Text_item::lookup_character (Score_element *, Font_metric*fm, SCM char_name)
94 {
95   return fm->find_by_name (ly_scm2string (char_name));
96 }
97
98
99 Molecule
100 Text_item::lookup_text (Score_element *me, Font_metric*fm, SCM text)
101 {
102 #if 0
103   /*
104     Fixme; should be done differently, move to font-interface?
105    */
106
107   SCM magnification = me->get_elt_property ("font-magnification");
108
109   Font_metric* metric = 0;
110   if (gh_number_p (magnification))
111     {
112
113       Real realmag = pow (1.2, gh_scm2int (magnification));
114       metric = all_fonts_global_p->find_scaled (ly_scm2string (font_name), realmag);
115
116       assert (false);
117     }
118
119 #endif
120
121   SCM list = gh_list (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
122   list = fontify_atom (fm, list);
123   
124   return Molecule (fm->text_dimension (ly_scm2string (text)), list);
125 }
126
127 Molecule
128 Text_item::markup_sentence2molecule (Score_element *me, SCM markup_sentence,
129                                      SCM alist_chain)
130 {
131   /*
132     FIXME
133
134     huh?
135    */
136   // return Molecule ();
137   
138   SCM sheet = me->paper_l ()->style_sheet_;
139   SCM f = gh_cdr (scm_assoc (ly_symbol2scm ("markup-to-properties"), sheet));
140   
141   SCM markup = gh_car (markup_sentence);
142   SCM sentence = gh_cdr (markup_sentence);
143   
144   SCM p = gh_cons  (gh_call2 (f, sheet, markup), alist_chain);
145
146   Axis align = X_AXIS;
147   SCM a = ly_assoc_chain (ly_symbol2scm ("align"), p);
148   if (gh_pair_p (a) && gh_number_p (gh_cdr (a)))
149     align = (Axis)gh_scm2int (gh_cdr (a));
150
151   Real staff_space = Staff_symbol_referencer::staff_space (me);
152   Real kern = 0;
153   SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
154   if (gh_pair_p (k) && gh_number_p (gh_cdr (k)))
155     kern = gh_scm2double (gh_cdr (k)) * staff_space;
156                              
157   Real raise = 0;
158   SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
159   if (gh_pair_p (r) && gh_number_p (gh_cdr (r)))
160     raise = gh_scm2double (gh_cdr (r)) * staff_space;
161
162   Offset o (align == X_AXIS ? kern : 0,
163             (align == Y_AXIS ? - kern : 0) + raise);
164
165   Molecule mol;
166   while (gh_pair_p (sentence))
167     {
168       Molecule m = text2molecule (me, gh_car (sentence), p);
169       if (!m.empty_b ())
170         {
171           m.translate (o);
172           mol.add_at_edge (align, align == X_AXIS ? RIGHT : DOWN, m, 0);
173         }
174       sentence = gh_cdr (sentence);
175     }
176   return mol;
177 }
178
179 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
180 SCM 
181 Text_item::brew_molecule (SCM smob)
182 {
183   Score_element *me = unsmob_element (smob);
184   
185   SCM text = me->get_elt_property ("text");
186
187   SCM properties = Font_interface::font_alist_chain (me);
188   Molecule mol = Text_item::text2molecule (me, text, properties);
189
190   SCM space = me->get_elt_property ("word-space");
191   if (gh_number_p (space))
192     {
193       Molecule m;
194       m.set_empty (false);
195       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
196                        * Staff_symbol_referencer::staff_space (me));
197     }
198   return mol.smobbed_copy (); 
199 }
200