]> git.donarmstrong.com Git - lilypond.git/blob - lily/virtual-font-metric.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / virtual-font-metric.cc
1 /*   
2   virtual-font-metric.cc --  implement Virtual_font_metric
3
4 source file of the GNU LilyPond music typesetter
5
6 (c) 2002--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8  */
9
10 #include "warn.hh"
11 #include "virtual-font-metric.hh"
12 #include "all-font-metrics.hh"
13 #include "stencil.hh"
14 #include "paper-def.hh"
15
16
17 /*
18   passing DEF is ughish. Should move into paperdef?
19   */
20 Virtual_font_metric::Virtual_font_metric (SCM font_list)
21 {
22   font_list_ = SCM_EOL;
23   SCM *tail = &font_list_;
24
25   SCM mag = SCM_EOL;
26   SCM name_list = SCM_EOL;
27   SCM *name_tail = &name_list;
28   
29   for (SCM s = font_list; ly_pair_p (s); s = ly_cdr (s))
30     {
31       if (Font_metric *fm = unsmob_metrics (ly_car (s)))
32         {
33           *tail =  scm_cons (ly_car (s),SCM_EOL);
34           tail = SCM_CDRLOC (*tail);
35
36           if (!ly_number_p (mag))
37             /* Ugh.  */
38             mag = ly_cdr (fm->description_);
39
40           *name_tail = scm_cons (ly_car (fm->description_), SCM_EOL);
41           name_tail = SCM_CDRLOC (*name_tail);
42         }
43     }
44
45   description_ = scm_cons (name_list, mag);
46 }
47
48 Real 
49 Virtual_font_metric::design_size () const
50 {
51   return unsmob_metrics (ly_car (font_list_))-> design_size ();
52 }
53
54 void
55 Virtual_font_metric::derived_mark ()const
56 {
57   scm_gc_mark (font_list_);
58 }
59
60 int
61 Virtual_font_metric::count () const
62 {
63   int k = 0;
64   for (SCM s = font_list_; ly_pair_p (s); s = ly_cdr (s))
65       k += unsmob_metrics (ly_car (s))->count ();
66   return k;
67 }
68
69 Stencil
70 Virtual_font_metric::find_by_name (String glyph) const
71 {
72   Stencil m;  
73   for (SCM s = font_list_; m.is_empty () && ly_pair_p (s); s = ly_cdr (s))
74     {
75       m = unsmob_metrics (ly_car (s))->find_by_name (glyph);
76     }
77
78   return m;
79 }
80
81 Box
82 Virtual_font_metric::get_ascii_char (int)  const
83 {
84   programming_error ("Virtual font metric cannot be indexed by ASCII.");
85   return Box ();
86 }
87
88 Stencil
89 Virtual_font_metric::get_ascii_char_stencil (int )  const
90 {
91   programming_error ("Virtual font metric cannot be indexed by ASCII.");
92   return Stencil ();
93 }
94
95 Offset
96 Virtual_font_metric::get_indexed_wxwy (int code)  const
97 {
98   int total = 0;
99   for (SCM s = font_list_; ly_pair_p (s); s = ly_cdr (s))
100     {
101       Font_metric *fm = unsmob_metrics (ly_car (s));
102       if (code < total + fm->count ())
103         return fm->get_indexed_wxwy (code - total);
104       total += fm->count ();
105     }
106   return Offset (0,0);
107 }
108
109 Box
110 Virtual_font_metric::get_indexed_char (int code)  const
111 {
112   int total = 0;
113   for (SCM s = font_list_; ly_pair_p (s); s = ly_cdr (s))
114     {
115       Font_metric *fm = unsmob_metrics (ly_car (s));
116       if (code < total + fm->count ())
117         return fm->get_indexed_char (code - total);
118       total += fm->count ();
119     }
120   return Box ();
121 }
122
123 int 
124 Virtual_font_metric::name_to_index (String glyph) const
125 {
126   Stencil m;
127   int total = 0; 
128   for (SCM s = font_list_; m.is_empty () && ly_pair_p (s); s = ly_cdr (s))
129     {
130       Font_metric *m =unsmob_metrics (ly_car (s));
131       int k = m->name_to_index (glyph);
132       if (k >= 0)
133         return total + k;
134
135       total += m->count ();
136     }
137   return -1;
138 }
139   
140 Stencil
141 Virtual_font_metric::get_indexed_char_stencil (int code)  const
142 {
143   Stencil  m ;  
144   int total = 0;
145   
146   for (SCM s = font_list_; ly_pair_p (s); s = ly_cdr (s))
147     {
148       Font_metric *fm = unsmob_metrics (ly_car (s));
149       if (code < total + fm->count ())
150         {
151           /* Ugh.  */
152           m = fm->get_indexed_char_stencil (code - total);
153           break; 
154         }
155       total += fm->count ();
156     }
157   return m;
158 }
159
160
161 SCM
162 Virtual_font_metric::get_font_list () const
163 {
164   return font_list_;
165 }
166
167 LY_DEFINE (ly_make_virtual_font, "ly:make-virtual-font", 0, 0, 1,
168            (SCM args),
169            "Make a virtual font metric from @var{args}, a list of font objects.")
170 {
171   Virtual_font_metric *fm = new  Virtual_font_metric (args);
172
173   return scm_gc_unprotect_object (fm->self_scm ());
174 }
175
176 String
177 Virtual_font_metric::coding_scheme () const
178 {
179   Font_metric *fm = unsmob_metrics (ly_car (font_list_));
180   return fm->coding_scheme ();
181 }