]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-metric.cc
release: 1.3.71
[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--2000 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 "ly-smobs.icc"
15 #include "font-metric.hh"
16 #include "string.hh"
17
18 Box
19 Font_metric::text_dimension (String text) const
20 {
21   Interval ydims;
22   Real w=0.0;
23   
24   for (int i = 0; i < text.length_i (); i++) 
25     {
26       
27       switch (text[i]) 
28         {
29         case '\\':
30           for (i++; (i < text.length_i ()) && !isspace(text[i]) 
31                  && text[i]!='{' && text[i]!='}'; i++)
32             ;
33           // ugh.
34           i--; // Compensate for the increment in the outer loop!
35           break;
36         case '{':  // Skip '{' and '}'
37         case '}':
38           break;
39         
40         default: 
41           Box b = get_char ((unsigned char)text[i],false);
42           
43           // Ugh, use the width of 'x' for unknown characters
44           if (b[X_AXIS].length () == 0) 
45             b = get_char ((unsigned char)'x',false);
46           
47           w += b[X_AXIS].length ();
48           ydims.unite (b[Y_AXIS]);
49           break;
50         }
51     }
52   if (ydims.empty_b ())
53     ydims = Interval (0,0);
54
55   return Box(Interval (0, w), ydims);
56 }
57
58
59 Box
60 Scaled_font_metric::text_dimension (String t) const
61 {
62   Real realmag = pow (1.2, magstep_i_);
63   Box b (orig_l_->text_dimension (t));
64
65   return Box(b[X_AXIS]* realmag, b[Y_AXIS]*realmag);
66 }
67
68 Font_metric::~Font_metric ()
69 {
70   unsmobify_self ();
71 }
72
73 Font_metric::Font_metric ()
74 {
75   self_scm_ = SCM_EOL;
76   name_ = SCM_EOL;
77   smobify_self ();
78 }
79
80 Font_metric::Font_metric (Font_metric const &)
81 {
82 }
83
84
85 Box 
86 Font_metric::get_char (int, bool)const
87 {
88   return Box (Interval(0,0),Interval (0,0));
89 }
90
91 Scaled_font_metric::Scaled_font_metric (Font_metric* m, int s)
92 {
93   magstep_i_ = s;
94   orig_l_ = m;
95 }
96
97 SCM
98 Font_metric::description () const
99 {
100   return gh_cons (name_, gh_int2scm (0));
101 }
102
103
104 SCM
105 Scaled_font_metric::description () const
106 {
107   SCM od = orig_l_->description ();
108   gh_set_cdr_x (od, gh_int2scm (magstep_i_));
109   return od;
110 }
111
112
113 void
114 Font_metric::do_smobify_self ()
115 {
116 }
117
118 SCM
119 Font_metric::mark_smob (SCM s)
120 {
121   Font_metric * m = SMOB_TO_TYPE(Font_metric, s);
122   return m->name_;
123 }
124
125 int
126 Font_metric::print_smob (SCM s, SCM port, scm_print_state * )
127 {
128   Font_metric *m = unsmob_metrics (s);
129   scm_puts ("#<Font_metric ", port);
130   scm_display (m->name_, port);
131   scm_puts (">", port);
132   return 1;
133 }
134
135
136 IMPLEMENT_UNSMOB (Font_metric, metrics);
137 IMPLEMENT_SMOBS (Font_metric);
138