]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
7ed5d1ed8d665534ebfe338110cf189779100f4c
[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
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 Molecule
92 Text_item::lookup_text (Grob *me, Font_metric*fm, SCM text)
93 {
94   SCM list = scm_list_n (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
95   list = fontify_atom (fm, list);
96   
97   return Molecule (fm->text_dimension (ly_scm2string (text)), list);
98 }
99
100
101 /*
102   TODO:
103
104   DOCME.
105
106
107   MARKUP_TEXT must be compound (may not be simple string.)
108   
109  */
110 Molecule
111 Text_item::markup_text2molecule (Grob *me, SCM markup_text,
112                                  SCM alist_chain)
113 {
114   SCM sheet = me->paper_l ()->style_sheet_;
115   SCM f = ly_cdr (scm_assoc (ly_symbol2scm ("markup-to-properties"), sheet));
116   
117   SCM markup = ly_car (markup_text);
118   SCM text = ly_cdr (markup_text);
119
120   SCM p = gh_cons (gh_call2 (f, sheet, markup), alist_chain);
121
122   Real staff_space = Staff_symbol_referencer::staff_space (me);
123
124   /*
125     Line mode is default.
126    */
127   Axis axis = X_AXIS;
128
129   SCM a = ly_assoc_chain (ly_symbol2scm ("axis"), p);
130   if (gh_pair_p (a) && ly_axis_p (ly_cdr (a)))
131     axis = (Axis)gh_scm2int (ly_cdr (a));
132
133   Real baseline_skip = 0;
134   SCM b = ly_assoc_chain (ly_symbol2scm ("baseline-skip"), p);
135   if (gh_pair_p (b) && gh_number_p (ly_cdr (b)))
136     baseline_skip = gh_scm2double (ly_cdr (b)) * staff_space;
137   
138   Real kern[2] = {0,0};
139
140   SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
141   if (gh_pair_p (k) && gh_number_p (ly_cdr (k)))
142     kern[axis] = gh_scm2double (ly_cdr (k)) * staff_space;
143                              
144   Real raise = 0;
145   SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
146   if (gh_pair_p (r) && gh_number_p (ly_cdr (r)))
147     raise = gh_scm2double (ly_cdr (r)) * staff_space;
148   
149
150   Interval extent;
151   bool extent_b = false;
152   SCM e = ly_assoc_chain (ly_symbol2scm ("extent"), p);
153   if (gh_pair_p (e) && ly_number_pair_p (ly_cdr (e)))
154     {
155       extent = Interval (gh_scm2double (ly_cadr (e)) * staff_space,
156                        gh_scm2double (ly_cddr (e)) * staff_space);
157       extent_b = true;
158     }
159
160   Offset o (kern[X_AXIS], raise - kern[Y_AXIS]);
161   
162   Molecule mol = Lookup::filledbox (Box (Interval (0,0), Interval (0,0)));
163
164   SCM cp = ly_deep_copy (p);
165   if (raise)
166     {
167       SCM cr = ly_assoc_chain (ly_symbol2scm ("raise"), cp);
168       scm_set_cdr_x (cr, gh_int2scm (0));
169     }
170   
171   while (gh_pair_p (text))
172     {
173       Molecule m = text2molecule (me, ly_car (text), cp);
174
175       if (!m.empty_b ())
176         {
177           m.translate_axis (mol.extent (axis)[axis == X_AXIS ? RIGHT : DOWN]
178                             - (axis == Y_AXIS ? baseline_skip : 0),
179                             axis);
180           mol.add_molecule (m);
181         }
182       text = ly_cdr (text);
183     }
184   
185   
186   /* Set extend to markup requested value. */
187   if (extent_b)
188     {
189       Box b = mol.extent_box ();
190       SCM expr = mol.get_expr ();
191
192       b[axis] = extent;
193       mol = Molecule (b, expr);
194     }
195   
196   mol.translate (o);
197   
198   return mol;
199 }
200
201 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
202 SCM 
203 Text_item::brew_molecule (SCM smob)
204 {
205   Grob *me = unsmob_grob (smob);
206   
207   SCM text = me->get_grob_property ("text");
208
209   SCM properties = Font_interface::font_alist_chain (me);
210   Molecule mol = Text_item::text2molecule (me, text, properties);
211
212   SCM space = me->get_grob_property ("word-space");
213   if (gh_number_p (space))
214     {
215       Molecule m;
216       m.set_empty (false);
217       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
218                        * Staff_symbol_referencer::staff_space (me));
219     }
220   return mol.smobbed_copy (); 
221 }
222
223
224
225 ADD_INTERFACE (Text_item,"text-interface",
226   "A scheme markup text",
227   "text align baseline-skip lookup raise kern word-space magnify");