]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
2002-12-06 Han-Wen Nienhuys <hanwen@cs.uu.nl>
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8  */
9 #include <math.h>
10
11 #include "warn.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 #include "lookup.hh"
21 #include "virtual-font-metric.hh"
22
23 /*
24
25   TEXT: STRING
26         | (MARKUP? TEXT+)
27         ;
28
29   HEAD: MARKUP-ITEM | (MARKUP-ITEM+)
30
31   MARKUP-ITEM: PROPERTY | ABBREV | FONT-STYLE
32   PROPERTY: (key . value)
33   ABBREV: rows lines roman music bold italic named super sub text
34    
35 */
36
37 Molecule
38 Text_item::text2molecule (Grob *me, SCM text, SCM alist_chain) 
39 {
40   if (gh_string_p (text))
41     return string2molecule (me, text, alist_chain);
42   else if (gh_pair_p (text))
43     {
44       /* urg, why not just do  this in markup_text2molecule ? */
45       if (gh_string_p (ly_car (text)))
46         return markup_text2molecule (me,
47                                      gh_append2 (scm_list_n (SCM_EOL,
48                                                          SCM_UNDEFINED),
49                                                  text),
50                                      alist_chain);
51       /*
52         Allow (faulty) texts that are in an extra list:
53         #'(("foo"))
54        */
55       else if (scm_ilength (text) <= 1)
56         return text2molecule (me, ly_car (text), alist_chain);
57       else
58         return markup_text2molecule (me, text, alist_chain);
59     }
60   return Molecule ();
61 }
62
63
64 MAKE_SCHEME_CALLBACK(Text_item,text_to_molecule,3);
65 SCM
66 Text_item::text_to_molecule (SCM grob, SCM props, SCM markup)
67 {
68   Grob *me = unsmob_grob (grob);
69   
70   return Text_item::text2molecule (me, markup, props).smobbed_copy();
71 }
72
73
74 Molecule
75 Text_item::string2molecule (Grob *me, SCM text, SCM alist_chain)
76 {
77   SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
78                               alist_chain);
79   if (gh_pair_p (style) && gh_symbol_p (ly_cdr (style)))
80     alist_chain = Font_interface::add_style (me, ly_cdr (style), alist_chain);
81
82   Font_metric *fm = Font_interface::get_font (me, alist_chain);
83   
84   SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
85     
86   Molecule mol;
87   if (gh_pair_p (lookup) && ly_cdr (lookup) ==ly_symbol2scm ("name"))
88     mol = lookup_character (me, fm, text);
89   else
90     mol = lookup_text (me, fm, text);
91   
92   return mol;
93 }
94
95 Molecule
96 Text_item::lookup_character (Grob *, Font_metric*fm, SCM char_name)
97 {
98   return fm->find_by_name (ly_scm2string (char_name));
99 }
100
101
102
103 Molecule
104 Text_item::lookup_text (Grob *, Font_metric*fm, SCM text)
105 {
106   SCM list = scm_list_n (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
107
108   if (dynamic_cast<Virtual_font_metric*> (fm))
109     {
110       /*
111         ARGH.
112        */
113       programming_error ("Can't use virtual font for text.");
114     }
115   else
116     list = fontify_atom (fm, list);
117   
118   return Molecule (fm->text_dimension (ly_scm2string (text)), list);
119 }
120
121
122 /*
123   TODO:
124
125   DOCME.
126
127
128   MARKUP_TEXT must be compound (may not be simple string.)
129   
130  */
131 Molecule
132 Text_item::markup_text2molecule (Grob *me, SCM markup_text,
133                                  SCM alist_chain)
134 {
135   SCM f = me->get_paper ()->lookup_variable (ly_symbol2scm ("markup-to-properties"));
136   
137   SCM markup = ly_car (markup_text);
138   SCM text = ly_cdr (markup_text);
139
140   /* ARGRGRRGRARGRA
141    */
142
143   SCM abbrev = me->get_paper ()->lookup_variable (ly_symbol2scm ("abbreviation-alist"));
144   SCM style = me->get_paper ()->lookup_variable (ly_symbol2scm ("style-alist"));
145   
146   SCM p = gh_cons (scm_call_3 (f, abbrev, style, markup), alist_chain);
147
148   Real staff_space = Staff_symbol_referencer::staff_space (me);
149
150   /*
151     Line mode is default.
152    */
153   Axis axis = X_AXIS;
154
155   SCM a = ly_assoc_chain (ly_symbol2scm ("axis"), p);
156   if (gh_pair_p (a) && ly_axis_p (ly_cdr (a)))
157     axis = (Axis)gh_scm2int (ly_cdr (a));
158
159   Real baseline_skip = 0;
160   SCM b = ly_assoc_chain (ly_symbol2scm ("baseline-skip"), p);
161   if (gh_pair_p (b) && gh_number_p (ly_cdr (b)))
162     baseline_skip = gh_scm2double (ly_cdr (b)) * staff_space;
163   
164   Real kern[2] = {0,0};
165
166   SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
167   if (gh_pair_p (k) && gh_number_p (ly_cdr (k)))
168     kern[axis] = gh_scm2double (ly_cdr (k)) * staff_space;
169                              
170   Real raise = 0;
171   SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
172   if (gh_pair_p (r) && gh_number_p (ly_cdr (r)))
173     raise = gh_scm2double (ly_cdr (r)) * staff_space;
174   
175
176   Interval extent;
177   bool extent_b = false;
178   SCM e = ly_assoc_chain (ly_symbol2scm ("extent"), p);
179   if (gh_pair_p (e) && ly_number_pair_p (ly_cdr (e)))
180     {
181       extent = Interval (gh_scm2double (ly_cadr (e)) * staff_space,
182                        gh_scm2double (ly_cddr (e)) * staff_space);
183       extent_b = true;
184     }
185
186   Offset o (kern[X_AXIS], raise - kern[Y_AXIS]);
187   
188   Molecule mol = Lookup::filledbox (Box (Interval (0,0), Interval (0,0)));
189
190   SCM cp = ly_deep_copy (p);
191   if (raise)
192     {
193       SCM cr = ly_assoc_chain (ly_symbol2scm ("raise"), cp);
194       scm_set_cdr_x (cr, gh_int2scm (0));
195     }
196   
197   while (gh_pair_p (text))
198     {
199       Molecule m = text2molecule (me, ly_car (text), cp);
200
201       if (!m.empty_b ())
202         {
203           m.translate_axis (mol.extent (axis)[axis == X_AXIS ? RIGHT : DOWN]
204                             - (axis == Y_AXIS ? baseline_skip : 0),
205                             axis);
206           mol.add_molecule (m);
207         }
208       text = ly_cdr (text);
209     }
210   
211   
212   /* Set extend to markup evented value. */
213   if (extent_b)
214     {
215       Box b = mol.extent_box ();
216       SCM expr = mol.get_expr ();
217
218       b[axis] = extent;
219       mol = Molecule (b, expr);
220     }
221   
222   mol.translate (o);
223   
224   return mol;
225 }
226
227 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
228 SCM 
229 Text_item::brew_molecule (SCM smob)
230 {
231   Grob *me = unsmob_grob (smob);
232   
233   SCM text = me->get_grob_property ("text");
234
235   SCM properties = Font_interface::font_alist_chain (me);
236   Molecule mol = Text_item::text2molecule (me, text, properties);
237
238   SCM space = me->get_grob_property ("word-space");
239   if (gh_number_p (space))
240     {
241       Molecule m;
242       m.set_empty (false);
243       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
244                        * Staff_symbol_referencer::staff_space (me), 0);
245     }
246   return mol.smobbed_copy (); 
247 }
248
249
250
251 ADD_INTERFACE (Text_item,"text-interface",
252   "A scheme markup text",
253   "text axis baseline-skip extent lookup raise kern word-space");