* UITypeEditor.cs: Add a table of special editors (types that can't use
[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;
33 using text = java.text;
34 using awt = java.awt;
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 {
70                         get {
71                                 return Width + (_lineIter.Margin*2);
72                         }
73                 }
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                 }
90
91                 internal float NativeY {
92                         get {
93                                 if (_lineIter.Format.IsVertical) {
94                                         float height = _lineIter.Height;
95                                         if (float.IsPositiveInfinity(height))
96                                                 height = 0;
97                                         switch (_lineIter.Format.Alignment) {
98                                                 case StringAlignment.Center:
99                                                         return (height - Width) / 2;
100                                                 case StringAlignment.Far:
101                                                         return height - _layout.getVisibleAdvance() - _lineIter.Margin;
102                                                 default:
103                                                         return _lineIter.Margin;
104                                         }
105                                 }
106                                 else
107                                         return AccumulatedHeight + Ascent;
108                         }
109                 }
110
111                 internal float NativeX {
112                         get {
113                                 float width = _lineIter.Width;
114                                 if (float.IsPositiveInfinity(width))
115                                         width = 0;
116                                 if (_lineIter.Format.IsVertical)
117                                         return (_lineIter.Format.IsRightToLeft) ?
118                                                 width - AccumulatedHeight - Ascent :
119                                                 AccumulatedHeight + Leading + Descent;                                  
120                                 else {
121                                         float xOffset;
122                                         switch ( _lineIter.Format.Alignment) {
123                                                 case StringAlignment.Center:
124                                                         xOffset = (width - Width) / 2;
125                                                         break;
126                                                 case StringAlignment.Far:
127                                                         if (_lineIter.Format.IsRightToLeft)
128                                                                 xOffset = _lineIter.Margin;
129                                                         else
130                                                                 xOffset = width - _layout.getVisibleAdvance() - _lineIter.Margin;
131                                                         break;
132                                                 default:
133                                                         if (_lineIter.Format.IsRightToLeft)
134                                                                 xOffset = width - _layout.getVisibleAdvance() - _lineIter.Margin;
135                                                         else
136                                                                 xOffset = _lineIter.Margin;
137                                                         break;
138                                         }
139
140                                         return xOffset;
141                                 }
142                         }
143                 }
144
145                 internal float Height {
146                         get {
147                                 return Ascent + Descent + Leading;
148                         }
149                 }
150
151                 internal float Width {
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) {
167                         if (_lineIter.Format.IsVertical) 
168                                 _layout.draw(g2d, y + NativeY, -(x + NativeX) );
169                         else
170                                 _layout.draw(g2d, x + NativeX, y + NativeY );
171                 }
172
173                 internal awt.Shape GetOutline(float x, float y) {
174                         geom.AffineTransform t = (geom.AffineTransform) _lineIter.Transform.clone();
175
176                         if (_lineIter.Format.IsVertical)
177                                 t.translate(y + NativeY, -(x + NativeX));
178                         else
179                                 t.translate(x + NativeX, y + NativeY);
180
181                         return _layout.getOutline(t);
182                 }
183
184                 #endregion
185         }
186
187 }