lilypond-1.3.65
[lilypond.git] / lily / font-metric.cc
blob220eb2a469425cead0d7882e25b861e228ce13a3
1 /*
2 font-metric.cc -- implement Font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
9 */
11 #include <math.h>
12 #include <ctype.h>
14 #include "ly-smobs.icc"
15 #include "font-metric.hh"
17 Box
18 Font_metric::text_dimension (String text) const
20 Interval ydims;
21 Real w=0.0;
23 for (int i = 0; i < text.length_i (); i++)
26 switch (text[i])
28 case '\\':
29 for (i++; (i < text.length_i ()) && !isspace(text[i])
30 && text[i]!='{' && text[i]!='}'; i++)
32 // ugh.
33 i--; // Compensate for the increment in the outer loop!
34 break;
35 case '{': // Skip '{' and '}'
36 case '}':
37 break;
39 default:
40 Box b = get_char ((unsigned char)text[i],false);
42 // Ugh, use the width of 'x' for unknown characters
43 if (b[X_AXIS].length () == 0)
44 b = get_char ((unsigned char)'x',false);
46 w += b[X_AXIS].length ();
47 ydims.unite (b[Y_AXIS]);
48 break;
51 if (ydims.empty_b ())
52 ydims = Interval (0,0);
54 return Box(Interval (0, w), ydims);
58 Box
59 Scaled_font_metric::text_dimension (String t) const
61 Real realmag = pow (1.2, magstep_i_);
62 Box b (orig_l_->text_dimension (t));
64 return Box(b[X_AXIS]* realmag, b[Y_AXIS]*realmag);
67 Font_metric::~Font_metric ()
69 unsmobify_self ();
72 Font_metric::Font_metric ()
74 self_scm_ = SCM_EOL;
75 name_ = SCM_EOL;
76 smobify_self ();
79 Font_metric::Font_metric (Font_metric const &)
84 Box
85 Font_metric::get_char (int, bool)const
87 return Box (Interval(0,0),Interval (0,0));
90 Scaled_font_metric::Scaled_font_metric (Font_metric* m, int s)
92 magstep_i_ = s;
93 orig_l_ = m;
96 SCM
97 Font_metric::description () const
99 return gh_cons (name_, gh_int2scm (0));
104 Scaled_font_metric::description () const
106 SCM od = orig_l_->description ();
107 gh_set_cdr_x (od, gh_int2scm (magstep_i_));
108 return od;
112 void
113 Font_metric::do_smobify_self ()
118 Font_metric::mark_smob (SCM s)
120 Font_metric * m = SMOB_TO_TYPE(Font_metric, s);
121 return m->name_;
125 Font_metric::print_smob (SCM s, SCM port, scm_print_state * )
127 Font_metric *m = unsmob_metrics (s);
128 scm_puts ("#<Font_metric ", port);
129 scm_display (m->name_, port);
130 scm_puts (">", port);
131 return 1;
135 IMPLEMENT_UNSMOB (Font_metric, metrics);
136 IMPLEMENT_SMOBS (Font_metric);