]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
patch::: 1.3.126.jcn1
[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   FIXME:
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    use baselineskip for setting (lines ...)
55
56 */
57
58 Molecule
59 Text_item::text2molecule (Grob *me, SCM text, SCM alist_chain) 
60 {
61   if (gh_string_p (text))
62     return string2molecule (me, text, alist_chain);
63   else if (gh_list_p (text))
64     {
65       if (!gh_pair_p (gh_car (text)) && gh_string_p (gh_car (text)))
66         return string2molecule (me, gh_car (text), alist_chain);
67       else
68         return markup_sentence2molecule (me, text, alist_chain);
69     }
70   return Molecule ();
71 }
72              
73 Molecule
74 Text_item::string2molecule (Grob *me, SCM text, SCM alist_chain)
75 {
76   SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
77                               alist_chain);
78   if  (gh_pair_p (style) && gh_symbol_p (gh_cdr (style)))
79     alist_chain = Font_interface::add_style (me, gh_cdr(style), alist_chain);
80
81   Font_metric *fm = Font_interface::get_font (me, alist_chain);
82   
83   SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
84     
85   Molecule mol;
86   if (gh_pair_p (lookup) && gh_cdr (lookup) ==ly_symbol2scm ("name"))
87     mol = lookup_character (me, fm, text);
88   else
89     mol = lookup_text (me, fm, text);
90   
91   return mol;
92 }
93
94 Molecule
95 Text_item::lookup_character (Grob *, Font_metric*fm, SCM char_name)
96 {
97   return fm->find_by_name (ly_scm2string (char_name));
98 }
99
100
101 Molecule
102 Text_item::lookup_text (Grob *me, Font_metric*fm, SCM text)
103 {
104 #if 0
105   /*
106     Fixme; should be done differently, move to font-interface?
107
108     differently -- how/why?
109    */
110
111   SCM magnification = me->get_grob_property ("font-magnification");
112
113   Font_metric* metric = 0;
114   if (gh_number_p (magnification))
115     {
116
117       Real realmag = pow (1.2, gh_scm2int (magnification));
118       metric = all_fonts_global_p->find_scaled (ly_scm2string (font_name), realmag);
119
120       assert (false);
121     }
122 #else
123   SCM magnification = me->get_grob_property ("font-magnification");
124
125   if (gh_number_p (magnification) && gh_scm2double (magnification) > 1)
126     programming_error ("font-magnification disabled");
127 #endif
128   
129
130   SCM list = gh_list (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
131   list = fontify_atom (fm, list);
132   
133   return Molecule (fm->text_dimension (ly_scm2string (text)), list);
134 }
135
136 Molecule
137 Text_item::markup_sentence2molecule (Grob *me, SCM markup_sentence,
138                                      SCM alist_chain)
139 {
140   SCM sheet = me->paper_l ()->style_sheet_;
141   SCM f = gh_cdr (scm_assoc (ly_symbol2scm ("markup-to-properties"), sheet));
142   
143   SCM markup = gh_car (markup_sentence);
144   SCM sentence = gh_cdr (markup_sentence);
145   
146   SCM p = gh_cons  (gh_call2 (f, sheet, markup), alist_chain);
147
148   Axis align = X_AXIS;
149   SCM a = ly_assoc_chain (ly_symbol2scm ("align"), p);
150   if (gh_pair_p (a) && gh_number_p (gh_cdr (a)))
151     align = (Axis)gh_scm2int (gh_cdr (a));
152
153   Real staff_space = Staff_symbol_referencer::staff_space (me);
154   Real kern = 0;
155   SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
156   if (gh_pair_p (k) && gh_number_p (gh_cdr (k)))
157     kern = gh_scm2double (gh_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 (gh_cdr (r)))
162     raise = gh_scm2double (gh_cdr (r)) * staff_space;
163
164 #if 0
165   Offset o (align == X_AXIS ? kern : 0,
166             (align == Y_AXIS ? - kern : 0) + raise);
167 #else
168   Offset o (0, (align == Y_AXIS ? - kern : 0) + raise);
169 #endif
170  
171   Molecule mol;
172   while (gh_pair_p (sentence))
173     {
174       /* Ugh: this (kerning) only works if 'kern' is the first modifier of a
175          markup.  I guess the only solution is to rewrite markup definition,
176          see above. */
177       Molecule m = text2molecule (me, gh_car (sentence), p);
178       Real m_kern = 0;
179       SCM m_p = SCM_EOL;
180       if (gh_pair_p (gh_car (sentence)))
181         m_p = gh_cons  (gh_call2 (f, sheet, gh_caar (sentence)), alist_chain);
182       SCM m_k = ly_assoc_chain (ly_symbol2scm ("kern"), m_p);
183       if (gh_pair_p (m_k) && gh_number_p (gh_cdr (m_k)))
184         m_kern = gh_scm2double (gh_cdr (m_k)) * staff_space;
185
186       if (!m.empty_b ())
187         {
188           m.translate (o);
189           mol.add_at_edge (align, align == X_AXIS ? RIGHT : DOWN, m, m_kern);
190         }
191       sentence = gh_cdr (sentence);
192     }
193   return mol;
194 }
195
196 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
197 SCM 
198 Text_item::brew_molecule (SCM smob)
199 {
200   Grob *me = unsmob_grob (smob);
201   
202   SCM text = me->get_grob_property ("text");
203
204   SCM properties = Font_interface::font_alist_chain (me);
205   Molecule mol = Text_item::text2molecule (me, text, properties);
206
207   SCM space = me->get_grob_property ("word-space");
208   if (gh_number_p (space))
209     {
210       Molecule m;
211       m.set_empty (false);
212       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
213                        * Staff_symbol_referencer::staff_space (me));
214     }
215   return mol.smobbed_copy (); 
216 }
217