New test.
[mono.git] / mcs / class / System.Drawing / System.Drawing.Text / LineLayout.jvm.cs
1 //
2 // System.Drawing.Test.LineLayout.jvm.cs
3 //
4 // Author:
5 // Konstantin Triger <kostat@mainsoft.com>
6 //
7 // Copyright (C) 2005 Mainsoft Corporation, (http://www.mainsoft.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Drawing.Drawing2D;
31
32 using font = java.awt.font;\r
33 using text = java.text;
34 using awt = java.awt;\r
35 using geom = java.awt.geom;
36
37 namespace System.Drawing.Text {
38         
39         internal sealed class LineLayout {
40
41                 #region Fields
42
43                 readonly font.TextLayout _layout;
44
45                 readonly float _accumulatedHeight;
46                 readonly TextLineIterator _lineIter;
47
48                 #endregion
49
50                 #region ctor
51
52                 internal LineLayout(font.TextLayout layout,
53                         TextLineIterator lineIter,
54                         float accumulatedHeight) {
55
56                         _layout = layout;
57                         _lineIter = lineIter;
58                         _accumulatedHeight = accumulatedHeight;
59                 }
60
61                 #endregion
62
63                 #region Properties
64
65                 internal float AccumulatedHeight {
66                         get { return _accumulatedHeight; }
67                 }
68
69                 internal float MeasureWidth {\r
70                         get {\r
71                                 return Width + (_lineIter.Margin*2);\r
72                         }\r
73                 }\r
74
75                 internal int CharacterCount {
76                         get { return _layout.getCharacterCount(); }
77                 }
78
79                 internal float Ascent {
80                         get { return _layout.getAscent(); }
81                 }
82
83                 internal float Descent {
84                         get { return _layout.getDescent(); }
85                 }
86
87                 public float Leading {
88                         get { return _layout.getLeading(); }
89                 }\r
90 \r
91                 internal float NativeY {\r
92                         get {\r
93                                 if (_lineIter.Format.IsVertical) {\r
94                                         float height = _lineIter.Height;\r
95                                         if (float.IsPositiveInfinity(height))\r
96                                                 height = 0;\r
97                                         switch (_lineIter.Format.Alignment) {\r
98                                                 case StringAlignment.Center:\r
99                                                         return (height - Width) / 2;\r
100                                                 case StringAlignment.Far:\r
101                                                         return height - _layout.getVisibleAdvance() - _lineIter.Margin;\r
102                                                 default:\r
103                                                         return _lineIter.Margin;\r
104                                         }\r
105                                 }\r
106                                 else\r
107                                         return AccumulatedHeight + Ascent;\r
108                         }\r
109                 }\r
110 \r
111                 internal float NativeX {\r
112                         get {\r
113                                 float width = _lineIter.Width;\r
114                                 if (float.IsPositiveInfinity(width))\r
115                                         width = 0;\r
116                                 if (_lineIter.Format.IsVertical)\r
117                                         return (_lineIter.Format.IsRightToLeft) ?\r
118                                                 width - AccumulatedHeight - Ascent :\r
119                                                 AccumulatedHeight + Leading + Descent;                                  \r
120                                 else {\r
121                                         float xOffset;\r
122                                         switch ( _lineIter.Format.Alignment) {\r
123                                                 case StringAlignment.Center:\r
124                                                         xOffset = (width - Width) / 2;\r
125                                                         break;\r
126                                                 case StringAlignment.Far:\r
127                                                         if (_lineIter.Format.IsRightToLeft)\r
128                                                                 xOffset = _lineIter.Margin;\r
129                                                         else\r
130                                                                 xOffset = width - _layout.getVisibleAdvance() - _lineIter.Margin;\r
131                                                         break;\r
132                                                 default:\r
133                                                         if (_lineIter.Format.IsRightToLeft)\r
134                                                                 xOffset = width - _layout.getVisibleAdvance() - _lineIter.Margin;\r
135                                                         else\r
136                                                                 xOffset = _lineIter.Margin;\r
137                                                         break;\r
138                                         }\r
139 \r
140                                         return xOffset;\r
141                                 }\r
142                         }\r
143                 }
144
145                 internal float Height {\r
146                         get {\r
147                                 return Ascent + Descent + Leading;\r
148                         }\r
149                 }\r
150 \r
151                 internal float Width {\r
152                         get {
153                                 if (_lineIter.Format.MeasureTrailingSpaces)
154                                         if (!(_lineIter.Format.IsRightToLeft ^ 
155                                                 (_lineIter.Format.Alignment == StringAlignment.Far)))
156                                                 return _layout.getAdvance();
157                                         
158                                 return _layout.getVisibleAdvance();
159                         }
160                 }
161
162                 #endregion
163
164                 #region Methods
165
166                 internal void Draw(awt.Graphics2D g2d, float x, float y) {\r
167                         if (_lineIter.Format.IsVertical) \r
168                                 _layout.draw(g2d, y + NativeY, -(x + NativeX) );\r
169                         else\r
170                                 _layout.draw(g2d, x + NativeX, y + NativeY );\r
171                 }
172
173                 internal awt.Shape GetOutline(float x, float y) {\r
174                         geom.AffineTransform t = (geom.AffineTransform) _lineIter.Transform.MemberwiseClone ();\r
175 \r
176                         if (_lineIter.Format.IsVertical)\r
177                                 t.translate(y + NativeY, -(x + NativeX));\r
178                         else\r
179                                 t.translate(x + NativeX, y + NativeY);\r
180 \r
181                         return _layout.getOutline(t);\r
182                 }
183
184                 #endregion
185         }
186
187 }