]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-metric.cc
e125775587b71dadd70e277aa72efb181e58e8ff
[lilypond.git] / lily / font-metric.cc
1 /*   
2   font-metric.cc --  implement Font_metric
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8     Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
9  */
10
11 #include <math.h>
12 #include <ctype.h>
13
14 #include "virtual-methods.hh"
15 #include "warn.hh"
16 #include "stencil.hh"
17 #include "ly-smobs.icc"
18 #include "font-metric.hh"
19 #include "string.hh"
20
21 Box
22 Font_metric::text_dimension (String text) const
23 {
24   Interval ydims;
25   Real w=0.0;
26   
27   for (int i = 0; i < text.length (); i++) 
28     {
29       
30       switch (text[i]) 
31         {
32         case '\\':
33   // accent marks use width of base letter
34          if (i +1 < text.length ())
35            {
36              if (text[i+1]=='\'' || text[i+1]=='`' || text[i+1]=='"' ||
37                  text[i+1]=='^')
38                {
39                  i++;
40                  break;
41                }
42              // for string width \\ is a \ and \_ is a _.
43              if (text[i+1]=='\\' || text[i+1]=='_')        
44                {
45                  break;
46                }
47            }
48           
49           for (i++; (i < text.length ()) && !isspace (text[i]) 
50                  && text[i]!='{' && text[i]!='}'; i++)
51             ;
52           // ugh.
53           i--; // Compensate for the increment in the outer loop!
54           break;
55         case '{':  // Skip '{' and '}'
56         case '}':
57           break;
58         
59         default: 
60           Box b = get_ascii_char ((unsigned char)text[i]);
61           
62           // Ugh, use the width of 'x' for unknown characters
63           if (b[X_AXIS].length () == 0) 
64             b = get_ascii_char ((unsigned char)'x');
65           
66           w += b[X_AXIS].length ();
67           ydims.unite (b[Y_AXIS]);
68           break;
69         }
70     }
71   if (ydims.is_empty ())
72     ydims = Interval (0,0);
73
74   return Box (Interval (0, w), ydims);
75 }
76
77
78
79 Font_metric::~Font_metric ()
80 {
81 }
82
83 Font_metric::Font_metric ()
84 {
85   description_ = SCM_EOL;
86   self_scm_ = SCM_EOL;
87   smobify_self ();
88 }
89
90 Font_metric::Font_metric (Font_metric const &)
91 {
92 }
93
94 int
95 Font_metric::count () const
96 {
97   return 0;
98 }
99
100 Box 
101 Font_metric::get_ascii_char (int) const
102 {
103   return Box (Interval (0,0),Interval (0,0));
104 }
105
106 Box 
107 Font_metric::get_indexed_char (int k) const
108 {
109   return get_ascii_char (k);
110 }
111
112
113 int
114 Font_metric::name_to_index (String) const
115 {
116   return -1;
117 }
118
119 Offset
120 Font_metric::get_indexed_wxwy (int )const
121 {
122   return Offset (0,0);
123 }
124
125 void
126 Font_metric::derived_mark ()const
127 {
128   
129 }
130
131 SCM
132 Font_metric::mark_smob (SCM s)
133 {
134   Font_metric * m = (Font_metric*) SCM_CELL_WORD_1 (s);
135
136   m->derived_mark ();
137   return m->description_;
138 }
139
140 int
141 Font_metric::print_smob (SCM s, SCM port, scm_print_state *)
142 {
143   Font_metric *m = unsmob_metrics (s);
144   scm_puts ("#<", port);
145   scm_puts (classname (m), port);
146   scm_puts (" ", port);
147   scm_write (m->description_, port);
148   scm_puts (">", port);
149   return 1;
150 }
151
152
153
154 IMPLEMENT_SMOBS (Font_metric);
155 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric);
156 IMPLEMENT_TYPE_P (Font_metric, "ly:font-metric?");
157
158 Stencil
159 Font_metric::find_by_name (String) const
160 {
161   Stencil m ;
162   return m;
163 }
164
165 LY_DEFINE (ly_find_glyph_by_name, "ly:find-glyph-by-name", 2 , 0, 0,
166           (SCM font, SCM name),
167           "This function retrieves a Stencil for the glyph named @var{name} "
168            "in "
169            "@var{font}.  "
170            "The font must be available as an AFM file. If the glyph "
171            "is not found, @code{#f} is returned. ")
172 {
173   Font_metric *fm = unsmob_metrics (font);
174   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
175   SCM_ASSERT_TYPE (gh_string_p (name), name, SCM_ARG2, __FUNCTION__, "string");
176
177   Stencil m =  fm->find_by_name (ly_scm2string (name));
178
179   /*
180     TODO: make optional argument for default if not found.
181     
182    */
183   return m.smobbed_copy ();
184 }
185
186 LY_DEFINE (ly_get_glyph, "ly:get-glyph", 2 , 0, 0,
187           (SCM font, SCM index),
188           "This function retrieves a Stencil for the glyph numbered @var{index} in "
189 "@var{font}. ")
190 {
191   Font_metric *fm = unsmob_metrics (font);
192   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
193   SCM_ASSERT_TYPE (gh_number_p (index), index, SCM_ARG2, __FUNCTION__, "number");
194
195   return fm->get_ascii_char_stencil (gh_scm2int (index)).smobbed_copy ();
196 }
197
198 LY_DEFINE (ly_text_dimension,"ly:text-dimension", 2 , 0, 0,
199           (SCM font, SCM text),
200           "Given the font metric in @var{font} and the string @var{text}, compute "
201 "the extents of that text in that font. The return value is a pair of "
202 "number-pairs.")
203 {
204   Box b;
205   Font_metric *fm = unsmob_metrics (font);
206   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
207   SCM_ASSERT_TYPE (gh_string_p (text), text, SCM_ARG2, __FUNCTION__, "string");
208
209   b = fm->text_dimension (ly_scm2string (text));
210   
211   return gh_cons (ly_interval2scm (b[X_AXIS]), ly_interval2scm (b[Y_AXIS]));
212 }
213
214 Stencil
215 Font_metric::get_ascii_char_stencil (int code)  const
216 {
217   SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
218                        SCM_UNDEFINED);
219   at = fontify_atom (this, at);
220   Box b = get_ascii_char (code);
221   return Stencil (b, at);
222 }
223
224 Stencil
225 Font_metric::get_indexed_char_stencil (int code)  const
226 {
227   SCM at = scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code),
228                        SCM_UNDEFINED);
229   at = fontify_atom (this, at);
230   Box b = get_indexed_char (code);
231   return Stencil (b, at);
232 }