]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-interface.cc
patch::: 1.3.122.mb1
[lilypond.git] / lily / font-interface.cc
1 /*   
2   font-interface.cc --  implement Font_interface
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "all-font-metrics.hh"
11 #include "font-metric.hh"
12 #include "font-interface.hh"
13 #include "grob.hh"
14 #include "paper-def.hh"
15 #include "warn.hh"
16
17
18 SCM
19 Font_interface::font_alist_chain (Grob *me)
20 {
21   SCM defaults = gh_cdr (scm_assoc (ly_symbol2scm ("font-defaults"),
22                                     me->paper_l ()->style_sheet_ ));
23
24   SCM ch = gh_list (me->mutable_property_alist_,
25                     me->immutable_property_alist_,
26                     defaults,
27                     SCM_UNDEFINED);
28
29   return ch;
30 }
31
32 /*
33   todo: split up this func, reuse in text_item? 
34  */
35 Font_metric *
36 Font_interface::get_default_font (Grob*me)
37 {
38   Font_metric * fm =  unsmob_metrics (me->get_grob_property ("font"));
39   if (fm)
40     return fm;
41
42   fm = get_font (me,  font_alist_chain (me));
43   me->set_grob_property ("font", fm->self_scm ());
44   return fm;
45 }
46
47 Font_metric *
48 Font_interface::get_font (Grob *me, SCM chain)
49 {
50   
51   SCM ss = me->paper_l ()->style_sheet_;
52
53   SCM proc = gh_cdr (scm_assoc (ly_symbol2scm ("properties-to-font"),
54                                 ss));
55
56   SCM fonts = gh_cdr (scm_assoc (ly_symbol2scm ("fonts"), ss));
57
58   assert (gh_procedure_p (proc));
59   SCM font_name = gh_call2 (proc, fonts, chain);
60
61   Font_metric *fm = me->paper_l ()->find_font (font_name, 1.0);
62
63   return fm;
64 }
65
66 SCM
67 Font_interface::add_style (Grob* me, SCM style, SCM chain)
68 {
69   assert (gh_symbol_p (style));
70   
71   SCM sheet = me->paper_l ()->style_sheet_;
72       
73   SCM style_alist = gh_cdr (scm_assoc (ly_symbol2scm ("style-alist"), sheet));
74   SCM entry = scm_assoc (style, style_alist);
75   if (gh_pair_p (entry))
76     {
77       chain = gh_cons (gh_cdr (entry), chain);
78     }
79   return chain;
80 }
81
82 /*
83 SCM routines:  
84
85 Interpreting music...
86 MIDI output to wtk1-fugue2.midi...
87 Track ... 
88
89 real    0m31.862s
90 user    0m29.110s
91 sys     0m0.260s
92
93 real    0m26.964s
94 user    0m24.850s
95 sys     0m0.280s
96
97
98 so a 14% speedup.
99
100 */
101
102 static SCM name_sym, shape_sym, family_sym, series_sym, rel_sz_sym, pt_sz_sym, wild_sym;
103
104
105 static void
106 init_syms ()
107 {
108   name_sym = scm_permanent_object (ly_symbol2scm ("font-name"));
109   shape_sym  = scm_permanent_object (ly_symbol2scm ("font-shape"));
110   family_sym = scm_permanent_object (ly_symbol2scm ("font-family"));
111   series_sym = scm_permanent_object (ly_symbol2scm ("font-series"));
112   rel_sz_sym = scm_permanent_object (ly_symbol2scm ("font-relative-size"));
113   pt_sz_sym = scm_permanent_object (ly_symbol2scm ("font-point-size"));
114   wild_sym = scm_permanent_object (ly_symbol2scm ("*"));
115 }
116
117 bool
118 Font_interface::wild_compare(SCM field_val, SCM val)
119 {
120   return (val == SCM_BOOL_F || field_val == wild_sym || field_val == val);
121 }
122
123 ADD_SCM_INIT_FUNC(Font_interface_syms,init_syms);
124
125
126 MAKE_SCHEME_CALLBACK(Font_interface,properties_to_font_name,2);
127 SCM
128 Font_interface::properties_to_font_name (SCM fonts, SCM alist_chain)
129 {
130   SCM name = ly_assoc_chain (name_sym, alist_chain);
131
132   SCM shape = SCM_BOOL_F;
133   SCM family = SCM_BOOL_F;
134   SCM series = SCM_BOOL_F;
135
136   
137   SCM point_sz = ly_assoc_chain (pt_sz_sym, alist_chain);
138   SCM rel_sz = SCM_BOOL_F;
139
140   if (!gh_pair_p (name))
141     {
142        shape = ly_assoc_chain (shape_sym, alist_chain);
143        family = ly_assoc_chain (family_sym, alist_chain);
144        series = ly_assoc_chain (series_sym, alist_chain);
145
146        if (gh_pair_p (shape))
147          shape = gh_cdr (shape);
148        if (gh_pair_p (family))
149          family = gh_cdr (family);
150        if (gh_pair_p (series))
151          series = gh_cdr (series);
152     }
153   else
154     name = gh_cdr (name);
155
156
157   if (gh_pair_p (point_sz))
158     point_sz = gh_cdr (point_sz);
159   else
160     {
161       rel_sz = ly_assoc_chain (rel_sz_sym, alist_chain);
162       if (gh_pair_p (rel_sz))
163         rel_sz = gh_cdr (rel_sz);
164     }
165
166   for (SCM s = fonts ; gh_pair_p (s); s = gh_cdr (s))
167     {
168       SCM qlist = gh_caar (s);
169
170       if (name != SCM_BOOL_F)
171         {
172           if (!wild_compare(scm_list_ref (qlist, gh_int2scm (4)), name))
173             continue;
174         }
175       else
176         {
177           if (!wild_compare(scm_list_ref (qlist, gh_int2scm (1)), series))
178             continue;
179           if (!wild_compare(scm_list_ref (qlist, gh_int2scm (2)), shape))
180             continue;
181           if (!wild_compare(scm_list_ref (qlist, gh_int2scm (3)), family))
182             continue;
183         }
184   
185       if (point_sz != SCM_BOOL_F)
186         {
187           // This if statement will always be true since name must 
188           // be SCM_BOOL_F here, right?  /MB
189           if (!wild_compare(scm_list_ref (qlist, gh_int2scm (4)), name))
190             continue;
191         }
192       else
193         {
194           if (!wild_compare(gh_car (qlist), rel_sz))
195             continue;
196         }
197
198       
199       SCM qname = gh_cdar (s);
200       return qname;
201     }
202
203   warning (_("couldn't find any font satisfying ") );
204   scm_write (gh_list (name, point_sz, shape, series , family, rel_sz, SCM_UNDEFINED), scm_current_error_port ());
205   scm_flush(scm_current_error_port ());
206
207   return ly_str02scm ("cmr10");
208   
209 }