]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
release: 1.3.146
[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--2001 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
24   TEXT: STRING
25         | (MARKUP? TEXT+)
26         ;
27
28   HEAD: MARKUP-ITEM | (MARKUP-ITEM+)
29
30   MARKUP-ITEM: PROPERTY | ABBREV | FONT-STYLE
31   PROPERTY: (key . value)
32   ABBREV: rows lines roman music bold italic named super sub text
33    
34 */
35
36 Molecule
37 Text_item::text2molecule (Grob *me, SCM text, SCM alist_chain) 
38 {
39   if (gh_string_p (text))
40     return string2molecule (me, text, alist_chain);
41   else if (gh_list_p (text) && text != SCM_EOL && text != SCM_UNDEFINED)
42     {
43       if (!gh_pair_p (gh_car (text)) && gh_string_p (gh_car (text)))
44         return string2molecule (me, gh_car (text), alist_chain);
45       else
46         return markup_text2molecule (me, text, alist_chain);
47     }
48   return Molecule ();
49 }
50              
51 Molecule
52 Text_item::string2molecule (Grob *me, SCM text, SCM alist_chain)
53 {
54   SCM style = ly_assoc_chain (ly_symbol2scm ("font-style"),
55                               alist_chain);
56   if (gh_pair_p (style) && gh_symbol_p (gh_cdr (style)))
57     alist_chain = Font_interface::add_style (me, gh_cdr (style), alist_chain);
58
59   Font_metric *fm = Font_interface::get_font (me, alist_chain);
60   
61   SCM lookup = ly_assoc_chain (ly_symbol2scm ("lookup"), alist_chain);
62     
63   Molecule mol;
64   if (gh_pair_p (lookup) && gh_cdr (lookup) ==ly_symbol2scm ("name"))
65     mol = lookup_character (me, fm, text);
66   else
67     mol = lookup_text (me, fm, text);
68   
69   return mol;
70 }
71
72 Molecule
73 Text_item::lookup_character (Grob *, Font_metric*fm, SCM char_name)
74 {
75   return fm->find_by_name (ly_scm2string (char_name));
76 }
77
78
79 Molecule
80 Text_item::lookup_text (Grob *me, Font_metric*fm, SCM text)
81 {
82 #if 0
83   /*
84     Fixme; should be done differently, move to font-interface?
85
86     differently -- how/why?
87    */
88
89   SCM magnification = me->get_grob_property ("font-magnification");
90
91   Font_metric* metric = 0;
92   if (gh_number_p (magnification))
93     {
94
95       Real realmag = pow (1.2, gh_scm2int (magnification));
96       metric = all_fonts_global_p->find_scaled (ly_scm2string (font_name), realmag);
97
98       assert (false);
99     }
100 #else
101   SCM magnification = me->get_grob_property ("font-magnification");
102
103   if (gh_number_p (magnification) && gh_scm2double (magnification) > 1)
104     programming_error ("font-magnification disabled");
105 #endif
106   
107
108   SCM list = gh_list (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
109   list = fontify_atom (fm, list);
110   
111   return Molecule (fm->text_dimension (ly_scm2string (text)), list);
112 }
113
114 Molecule
115 Text_item::markup_text2molecule (Grob *me, SCM markup_text,
116                                SCM alist_chain)
117 {
118   SCM sheet = me->paper_l ()->style_sheet_;
119   SCM f = gh_cdr (scm_assoc (ly_symbol2scm ("markup-to-properties"), sheet));
120   
121   SCM markup = gh_car (markup_text);
122   SCM text = gh_cdr (markup_text);
123
124 #if 1
125   SCM p = gh_cons (gh_call2 (f, sheet, markup), alist_chain);
126 #else
127   SCM pp = gh_call2 (f, sheet, markup);
128   gh_newline ();
129   scm_write (pp, scm_current_error_port ());
130   gh_newline ();
131   SCM p = gh_cons (pp, alist_chain);
132 #endif
133
134   Real staff_space = Staff_symbol_referencer::staff_space (me);
135
136   Axis align = X_AXIS;
137   SCM a = ly_assoc_chain (ly_symbol2scm ("align"), p);
138   if (gh_pair_p (a) && gh_number_p (gh_cdr (a)))
139     align = (Axis)gh_scm2int (gh_cdr (a));
140
141   Real baseline_skip = 0;
142   SCM b = ly_assoc_chain (ly_symbol2scm ("baseline-skip"), p);
143   if (gh_pair_p (b) && gh_number_p (gh_cdr (b)))
144     baseline_skip = gh_scm2double (gh_cdr (b)) * staff_space;
145   
146   Array<Real> kern (2);
147   kern[0] = 0; // zucht
148   kern[1] = 0;
149   SCM k = ly_assoc_chain (ly_symbol2scm ("kern"), p);
150   if (gh_pair_p (k) && gh_number_p (gh_cdr (k)))
151     kern[align] = gh_scm2double (gh_cdr (k)) * staff_space;
152                              
153   Real raise = 0;
154   SCM r = ly_assoc_chain (ly_symbol2scm ("raise"), p);
155   if (gh_pair_p (r) && gh_number_p (gh_cdr (r)))
156     raise = gh_scm2double (gh_cdr (r)) * staff_space;
157
158   Offset o (0, (align == Y_AXIS ? - kern[align] : 0) + raise);
159    
160   Molecule mol;
161   while (gh_pair_p (text))
162     {
163       Molecule m = text2molecule (me, gh_car (text), p);
164       SCM m_p = SCM_EOL;
165       if (gh_pair_p (gh_car (text)))
166         m_p = gh_cons (gh_call2 (f, sheet, gh_caar (text)), alist_chain);
167       SCM m_k = ly_assoc_chain (ly_symbol2scm ("kern"), m_p);
168       Real m_kern = kern[align];
169       if (gh_pair_p (m_k) && gh_number_p (gh_cdr (m_k)))
170         m_kern = gh_scm2double (gh_cdr (m_k)) * staff_space;
171
172       if (!m.empty_b ())
173         {
174           m.translate (o);
175           if (align == Y_AXIS && baseline_skip)
176             m_kern += baseline_skip - m.extent (Y_AXIS)[UP];
177           mol.add_at_edge (align, align == X_AXIS ? RIGHT : DOWN, m, m_kern);
178         }
179       text = gh_cdr (text);
180     }
181   return mol;
182 }
183
184 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
185 SCM 
186 Text_item::brew_molecule (SCM smob)
187 {
188   Grob *me = unsmob_grob (smob);
189   
190   SCM text = me->get_grob_property ("text");
191
192   SCM properties = Font_interface::font_alist_chain (me);
193   Molecule mol = Text_item::text2molecule (me, text, properties);
194
195   SCM space = me->get_grob_property ("word-space");
196   if (gh_number_p (space))
197     {
198       Molecule m;
199       m.set_empty (false);
200       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
201                        * Staff_symbol_referencer::staff_space (me));
202     }
203   return mol.smobbed_copy (); 
204 }
205