]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
006cc2150f86fa5de1d282cbeb84bda58d529386
[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 Molecule
64 Text_item::string2molecule (Grob *me, SCM text, SCM alist_chain)
65 {
66   SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
67                               alist_chain);
68   if (gh_pair_p (style) && gh_symbol_p (ly_cdr (style)))
69     alist_chain = Font_interface::add_style (me, ly_cdr (style), alist_chain);
70
71   Font_metric *fm = Font_interface::get_font (me, alist_chain);
72   
73   SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
74     
75   Molecule mol;
76   if (gh_pair_p (lookup) && ly_cdr (lookup) ==ly_symbol2scm ("name"))
77     mol = lookup_character (me, fm, text);
78   else
79     mol = lookup_text (me, fm, text);
80   
81   return mol;
82 }
83
84 Molecule
85 Text_item::lookup_character (Grob *, Font_metric*fm, SCM char_name)
86 {
87   return fm->find_by_name (ly_scm2string (char_name));
88 }
89
90
91
92 Molecule
93 Text_item::lookup_text (Grob *, Font_metric*fm, SCM text)
94 {
95   SCM list = scm_list_n (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
96
97   if (dynamic_cast<Virtual_font_metric*> (fm))
98     {
99       /*
100         ARGH.
101        */
102       programming_error ("Can't use virtual font for text.");
103     }
104   else
105     list = fontify_atom (fm, list);
106   
107   return Molecule (fm->text_dimension (ly_scm2string (text)), list);
108 }
109
110
111 /*
112   TODO:
113
114   DOCME.
115
116
117   MARKUP_TEXT must be compound (may not be simple string.)
118   
119  */
120 Molecule
121 Text_item::markup_text2molecule (Grob *me, SCM markup_text,
122                                  SCM alist_chain)
123 {
124   SCM f = me->get_paper ()->lookup_variable (ly_symbol2scm ("markup-to-properties"));
125   
126   SCM markup = ly_car (markup_text);
127   SCM text = ly_cdr (markup_text);
128
129   /* ARGRGRRGRARGRA
130    */
131
132   SCM abbrev = me->get_paper ()->lookup_variable (ly_symbol2scm ("abbreviation-alist"));
133   SCM style = me->get_paper ()->lookup_variable (ly_symbol2scm ("style-alist"));
134   
135   SCM p = gh_cons (scm_call_3 (f, abbrev, style, markup), alist_chain);
136
137   Real staff_space = Staff_symbol_referencer::staff_space (me);
138
139   /*
140     Line mode is default.
141    */
142   Axis axis = X_AXIS;
143
144   SCM a = ly_assoc_chain (ly_symbol2scm ("axis"), p);
145   if (gh_pair_p (a) && ly_axis_p (ly_cdr (a)))
146     axis = (Axis)gh_scm2int (ly_cdr (a));
147
148   Real baseline_skip = 0;
149   SCM b = ly_assoc_chain (ly_symbol2scm ("baseline-skip"), p);
150   if (gh_pair_p (b) && gh_number_p (ly_cdr (b)))
151     baseline_skip = gh_scm2double (ly_cdr (b)) * staff_space;
152   
153   Real kern[2] = {0,0};
154
155   SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
156   if (gh_pair_p (k) && gh_number_p (ly_cdr (k)))
157     kern[axis] = gh_scm2double (ly_cdr (k)) * staff_space;
158                              
159   Real raise = 0;
160   SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
161   if (gh_pair_p (r) && gh_number_p (ly_cdr (r)))
162     raise = gh_scm2double (ly_cdr (r)) * staff_space;
163   
164
165   Interval extent;
166   bool extent_b = false;
167   SCM e = ly_assoc_chain (ly_symbol2scm ("extent"), p);
168   if (gh_pair_p (e) && ly_number_pair_p (ly_cdr (e)))
169     {
170       extent = Interval (gh_scm2double (ly_cadr (e)) * staff_space,
171                        gh_scm2double (ly_cddr (e)) * staff_space);
172       extent_b = true;
173     }
174
175   Offset o (kern[X_AXIS], raise - kern[Y_AXIS]);
176   
177   Molecule mol = Lookup::filledbox (Box (Interval (0,0), Interval (0,0)));
178
179   SCM cp = ly_deep_copy (p);
180   if (raise)
181     {
182       SCM cr = ly_assoc_chain (ly_symbol2scm ("raise"), cp);
183       scm_set_cdr_x (cr, gh_int2scm (0));
184     }
185   
186   while (gh_pair_p (text))
187     {
188       Molecule m = text2molecule (me, ly_car (text), cp);
189
190       if (!m.empty_b ())
191         {
192           m.translate_axis (mol.extent (axis)[axis == X_AXIS ? RIGHT : DOWN]
193                             - (axis == Y_AXIS ? baseline_skip : 0),
194                             axis);
195           mol.add_molecule (m);
196         }
197       text = ly_cdr (text);
198     }
199   
200   
201   /* Set extend to markup evented value. */
202   if (extent_b)
203     {
204       Box b = mol.extent_box ();
205       SCM expr = mol.get_expr ();
206
207       b[axis] = extent;
208       mol = Molecule (b, expr);
209     }
210   
211   mol.translate (o);
212   
213   return mol;
214 }
215
216 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
217 SCM 
218 Text_item::brew_molecule (SCM smob)
219 {
220   Grob *me = unsmob_grob (smob);
221   
222   SCM text = me->get_grob_property ("text");
223
224   SCM properties = Font_interface::font_alist_chain (me);
225   Molecule mol = Text_item::text2molecule (me, text, properties);
226
227   SCM space = me->get_grob_property ("word-space");
228   if (gh_number_p (space))
229     {
230       Molecule m;
231       m.set_empty (false);
232       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
233                        * Staff_symbol_referencer::staff_space (me));
234     }
235   return mol.smobbed_copy (); 
236 }
237
238
239
240 ADD_INTERFACE (Text_item,"text-interface",
241   "A scheme markup text",
242   "text align baseline-skip lookup raise kern word-space magnify");