]> git.donarmstrong.com Git - lilypond.git/blob - lily/text-item.cc
patch::: 1.3.98.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
10 #include "debug.hh"
11 #include "text-item.hh"
12 #include "paper-def.hh"
13 #include "lookup.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "main.hh"
17 #include "all-font-metrics.hh"
18 #include "afm.hh"
19
20
21 /*
22     TEXT : STRING | (MARKUP SENTENCE)
23     MARKUP: PROPERTY | ABBREV
24     SENTENCE: TEXT | SENTENCE TEXT
25     PROPERTY: (key . value)
26     ABBREV: rows lines roman music bold italic named super sub text, or any font-style
27  */
28 Molecule
29 Text_item::text2molecule (Score_element *me, SCM text, SCM properties) 
30 {
31   if (gh_string_p (text))
32     return string2molecule (me, text, properties);
33   else if (gh_list_p (text))
34     {
35       if (!gh_pair_p (gh_car (text)) && gh_string_p (gh_car (text)))
36         return string2molecule (me, gh_car (text), properties);
37       else
38         return markup_sentence2molecule (me, text, properties);
39     }
40   return Molecule ();
41 }
42
43 static
44 SCM
45 get_elt_property (Score_element *me, char const *name)
46 {
47   SCM s = me->get_elt_property (name);
48   if (s == SCM_EOL)
49     error (_f ("No `%s' defined for %s", name, me->name ()));
50   return s;
51 }
52
53 Molecule
54 Text_item::string2molecule (Score_element *me, SCM text, SCM properties)
55 {
56   SCM style = scm_assoc (ly_symbol2scm ("font-style"), properties);
57   SCM paper = me->get_elt_property ("style-sheet");
58   if (paper == SCM_EOL)
59     paper = scm_string_to_symbol (me->paper_l ()->get_scmvar ("style_sheet"));
60
61   SCM font_name;
62   if (gh_pair_p (style))
63     {
64       SCM f = get_elt_property (me, "style-to-font-name");
65       font_name = gh_call2 (f, paper, gh_cdr (style));
66     }
67   else
68     {
69       SCM f = get_elt_property (me, "properties-to-font-name");
70       font_name = gh_call2 (f, paper, properties);
71     }
72    
73   // should move fallback to scm
74   if (!gh_string_p (font_name))
75     font_name = ly_str02scm ("cmr10");
76     
77   SCM lookup = scm_assoc (ly_symbol2scm ("lookup"), properties);
78
79   Molecule mol;
80   if (gh_pair_p (lookup) && ly_symbol2string (gh_cdr (lookup)) == "name")
81     mol = lookup_character (me, font_name, text);
82   else
83     mol = lookup_text (me, font_name, text);
84   
85   return mol;
86 }
87
88 /*
89   caching / use some form of Lookup without 'paper'?
90 */
91 Molecule
92 Text_item::lookup_character (Score_element *me, SCM font_name, SCM char_name)
93 {
94   Adobe_font_metric *afm = all_fonts_global_p->find_afm (ly_scm2string (font_name));
95
96   if (!afm)
97     {
98       warning (_f ("can't find font: `%s'", ly_scm2string (font_name)));
99       warning (_f ("(search path: `%s')", global_path.str ().ch_C()));
100       error (_ ("Aborting"));
101     }
102   
103   AFM_CharMetricInfo const *metric =
104     afm->find_char_metric (ly_scm2string (char_name), true);
105
106   if (!metric)
107     {
108       Molecule m;
109       m.set_empty (false);
110       return m;
111     }
112
113   SCM list = gh_list (ly_symbol2scm ("char"),
114                       gh_int2scm (metric->code),
115                       SCM_UNDEFINED);
116   
117   list = fontify_atom (afm, list);
118   return Molecule (afm_bbox_to_box (metric->charBBox), list);
119 }
120
121 Molecule
122 Text_item::lookup_text (Score_element *me, SCM font_name, SCM text)
123 {
124   SCM magnification = me->get_elt_property ("font-magnification");
125   Font_metric* metric = 0;
126   if (gh_number_p (magnification))
127     metric = all_fonts_global_p->find_scaled (ly_scm2string (font_name),
128                                               gh_scm2int (magnification));
129   else
130     metric = all_fonts_global_p->find_font (ly_scm2string (font_name));
131   
132   SCM list = gh_list (ly_symbol2scm ("text"), text, SCM_UNDEFINED);
133   list = fontify_atom (metric, list);
134   
135   return Molecule (metric->text_dimension (ly_scm2string (text)), list);
136 }
137
138 Molecule
139 Text_item::markup_sentence2molecule (Score_element *me, SCM markup_sentence,
140                                      SCM properties)
141 {
142   SCM markup = gh_car (markup_sentence);
143   SCM sentence = gh_cdr (markup_sentence);
144   SCM f = get_elt_property (me, "markup-to-properties");
145   SCM p = gh_append2 (gh_call1 (f, markup), properties);
146
147   Axis align = X_AXIS;
148   SCM a = scm_assoc (ly_symbol2scm ("align"), p);
149   if (gh_pair_p (a) && gh_number_p (gh_cdr (a)))
150     align = (Axis)gh_scm2int (gh_cdr (a));
151
152   Real staff_space = Staff_symbol_referencer::staff_space (me);
153   Real kern = 0;
154   SCM k = scm_assoc (ly_symbol2scm ("kern"), p);
155   if (gh_pair_p (k) && gh_number_p (gh_cdr (k)))
156     kern = gh_scm2double (gh_cdr (k)) * staff_space;
157                              
158   Real raise = 0;
159   SCM r = scm_assoc (ly_symbol2scm ("raise"), p);
160   if (gh_pair_p (r) && gh_number_p (gh_cdr (r)))
161     raise = gh_scm2double (gh_cdr (r)) * staff_space;
162
163   Offset o (align == X_AXIS ? kern : 0,
164             (align == Y_AXIS ? - kern : 0) + raise);
165
166   Molecule mol;
167   while (gh_pair_p (sentence))
168     {
169       Molecule m = text2molecule (me, gh_car (sentence), p);
170       if (!m.empty_b ())
171         {
172           m.translate (o);
173           mol.add_at_edge (align, align == X_AXIS ? RIGHT : DOWN, m, 0);
174         }
175       sentence = gh_cdr (sentence);
176     }
177   return mol;
178 }
179
180 MAKE_SCHEME_CALLBACK (Text_item, brew_molecule, 1);
181 SCM 
182 Text_item::brew_molecule (SCM smob)
183 {
184   Score_element *me = unsmob_element (smob);
185   
186   SCM text = me->get_elt_property ("text");
187
188   SCM properties = gh_append2 (me->immutable_property_alist_,
189                                me->mutable_property_alist_);
190
191   Molecule mol = Text_item::text2molecule (me, text, properties);
192
193   SCM space = me->get_elt_property ("word-space");
194   if (gh_number_p (space))
195     {
196       Molecule m;
197       m.set_empty (false);
198       mol.add_at_edge (X_AXIS, RIGHT, m, gh_scm2double (space)
199                        * Staff_symbol_referencer::staff_space (me));
200     }
201   return mol.create_scheme (); 
202 }
203