~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/awt/graphics.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2002, 2004, 2005, 2006, 2007 Jeroen Frijters
 
3
  Copyright (C) 2006 Active Endpoints, Inc.
 
4
  Copyright (C) 2006 - 2011 Volker Berlin (i-net software)
 
5
  Copyright (C) 2011 Karsten Heinrich (i-net software)
 
6
 
 
7
  This software is provided 'as-is', without any express or implied
 
8
  warranty.  In no event will the authors be held liable for any damages
 
9
  arising from the use of this software.
 
10
 
 
11
  Permission is granted to anyone to use this software for any purpose,
 
12
  including commercial applications, and to alter it and redistribute it
 
13
  freely, subject to the following restrictions:
 
14
 
 
15
  1. The origin of this software must not be misrepresented; you must not
 
16
     claim that you wrote the original software. If you use this software
 
17
     in a product, an acknowledgment in the product documentation would be
 
18
     appreciated but is not required.
 
19
  2. Altered source versions must be plainly marked as such, and must not be
 
20
     misrepresented as being the original software.
 
21
  3. This notice may not be removed or altered from any source distribution.
 
22
 
 
23
  Jeroen Frijters
 
24
  jeroen@frijters.net 
 
25
 
 
26
*/
 
27
 
 
28
using System;
 
29
using System.Drawing;
 
30
using System.Drawing.Drawing2D;
 
31
using System.Drawing.Text;
 
32
using System.Globalization;
 
33
using System.Windows.Forms;
 
34
using java.awt.image;
 
35
using java.util;
 
36
 
 
37
namespace ikvm.awt
 
38
{
 
39
 
 
40
    internal class BitmapGraphics : NetGraphics
 
41
    {
 
42
        private readonly Bitmap bitmap;
 
43
 
 
44
        internal BitmapGraphics(Bitmap bitmap, java.awt.Font font, Color fgcolor, Color bgcolor)
 
45
            : base(createGraphics(bitmap), font, fgcolor, bgcolor)
 
46
        {
 
47
            this.bitmap = bitmap;
 
48
        }
 
49
 
 
50
        internal BitmapGraphics(Bitmap bitmap)
 
51
            : base(createGraphics(bitmap), null, Color.White, Color.Black)
 
52
        {
 
53
            this.bitmap = bitmap;
 
54
        }
 
55
 
 
56
        protected override SizeF GetSize() {
 
57
            return bitmap.Size;
 
58
        }
 
59
 
 
60
        private static Graphics createGraphics(Bitmap bitmap)
 
61
        {
 
62
            // lock to prevent the exception
 
63
            // System.InvalidOperationException: Object is currently in use elsewhere
 
64
            lock (bitmap)
 
65
            {
 
66
                return Graphics.FromImage(bitmap);
 
67
            }
 
68
        }
 
69
 
 
70
        public override java.awt.Graphics create()
 
71
        {
 
72
            BitmapGraphics newGraphics = (BitmapGraphics)MemberwiseClone();
 
73
            newGraphics.init(createGraphics(bitmap));
 
74
            return newGraphics;
 
75
        }
 
76
 
 
77
        public override void copyArea(int x, int y, int width, int height, int dx, int dy)
 
78
                {
 
79
            Bitmap copy = new Bitmap(width, height);
 
80
            using (Graphics gCopy = Graphics.FromImage(copy))
 
81
            {
 
82
                gCopy.DrawImage(bitmap, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);
 
83
            }
 
84
            g.DrawImageUnscaled(copy, x + dx, y + dy);
 
85
                }
 
86
    }
 
87
 
 
88
    internal class ComponentGraphics : NetGraphics
 
89
    {
 
90
        private readonly Control control;
 
91
 
 
92
        internal ComponentGraphics(Control control, java.awt.Color fgColor, java.awt.Color bgColor, java.awt.Font font)
 
93
            : base(control.CreateGraphics(), font, J2C.ConvertColor(fgColor), J2C.ConvertColor(bgColor))
 
94
        {
 
95
            this.control = control;
 
96
        }
 
97
 
 
98
        protected override SizeF GetSize() {
 
99
            return control.Size;
 
100
        }
 
101
 
 
102
        public override java.awt.Graphics create()
 
103
        {
 
104
            ComponentGraphics newGraphics = (ComponentGraphics)MemberwiseClone();
 
105
            newGraphics.init(control.CreateGraphics());
 
106
            return newGraphics;
 
107
        }
 
108
 
 
109
        private Point getPointToScreenImpl(Point point)
 
110
        {
 
111
            return this.control.PointToScreen(point);
 
112
        }
 
113
 
 
114
        private Point getPointToScreen(Point point)
 
115
        {
 
116
            return (Point)this.control.Invoke(new Converter<Point,Point>(getPointToScreenImpl),point);
 
117
        }
 
118
 
 
119
                public override void copyArea(int x, int y, int width, int height, int dx, int dy)
 
120
                {
 
121
            Point src = getPointToScreen(new Point(x + (int)this.g.Transform.OffsetX, y + (int)this.g.Transform.OffsetY));
 
122
            Point dest = new Point(x + (int)this.g.Transform.OffsetX + dx, y + (int)this.g.Transform.OffsetY + dy);
 
123
            this.g.CopyFromScreen(src, dest, new Size(width, height));
 
124
                }
 
125
 
 
126
                public override void clip(java.awt.Shape shape)
 
127
                {
 
128
                        if (shape == null)
 
129
                        {
 
130
                                // the API specification says that this will clear
 
131
                                // the clip, but in fact the reference implementation throws a 
 
132
                                // NullPointerException - see the following entry in the bug parade:
 
133
                                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206189
 
134
                                throw new java.lang.NullPointerException();
 
135
                        }
 
136
                        base.clip(shape);
 
137
                }
 
138
    }
 
139
 
 
140
    internal class PrintGraphicsContext
 
141
    {
 
142
        internal PrintGraphics Current;
 
143
    }
 
144
 
 
145
    internal class PrintGraphics : NetGraphics
 
146
    {
 
147
        private NetGraphicsState myState;
 
148
        private PrintGraphicsContext baseContext;
 
149
        private bool disposed = false;
 
150
        private bool isBase = true;
 
151
 
 
152
        internal PrintGraphics(Graphics g)
 
153
            : base(g, null, Color.White, Color.Black)
 
154
        {
 
155
            baseContext = new PrintGraphicsContext();
 
156
            baseContext.Current = this;
 
157
        }
 
158
 
 
159
        public override java.awt.Graphics create()
 
160
        {
 
161
            checkState();
 
162
            myState = new NetGraphicsState();
 
163
            myState.saveGraphics(this);
 
164
            PrintGraphics newGraphics = (PrintGraphics)MemberwiseClone();
 
165
            newGraphics.myState = null;
 
166
            newGraphics.isBase = false;
 
167
            newGraphics.baseContext = baseContext;
 
168
            baseContext.Current = newGraphics; // since it is very likely that the next op will be on that graphics
 
169
            // this is similar to init
 
170
            myState.restoreGraphics(newGraphics);
 
171
            return newGraphics;
 
172
        }
 
173
 
 
174
        /// <summary>
 
175
        /// Checks whether the properties of this instance are set to the bse Graphics. If not, the context
 
176
        /// of the currently PrintGraphics is saved and the context if this instance is restored.
 
177
        /// </summary>
 
178
        private void checkState()
 
179
        {
 
180
            // this is required to simulate Graphics.create(), which is not possible in .NET
 
181
            // we simply call Save on create() an restore this state, if any method is called
 
182
            // on the current graphics. This will work for almost any use case of create()
 
183
            if (baseContext != null && baseContext.Current != this)
 
184
            {
 
185
                if (!baseContext.Current.disposed)
 
186
                {
 
187
                    if (baseContext.Current.myState == null)
 
188
                    {
 
189
                        baseContext.Current.myState = new NetGraphicsState(baseContext.Current);
 
190
                    }
 
191
                    else
 
192
                    {
 
193
                        baseContext.Current.myState.saveGraphics(baseContext.Current);
 
194
                    }
 
195
                }
 
196
                baseContext.Current = this;
 
197
                if (myState != null) // is only null, if this instance was already disposed
 
198
                {
 
199
                    myState.restoreGraphics(this);
 
200
                }
 
201
            }
 
202
        }
 
203
 
 
204
        public override void copyArea(int x, int y, int width, int height, int dx, int dy)
 
205
        {
 
206
            throw new NotImplementedException();
 
207
        }
 
208
 
 
209
        public override void clearRect(int x, int y, int width, int height)
 
210
        {
 
211
            checkState();
 
212
            base.clearRect(x, y, width, height);
 
213
        }
 
214
 
 
215
        public override void clipRect(int x, int y, int w, int h)
 
216
        {
 
217
            checkState();
 
218
            base.clearRect(x, y, w, h);
 
219
        }
 
220
 
 
221
        public override void clip(java.awt.Shape shape)
 
222
        {
 
223
            checkState();
 
224
            base.clip(shape);
 
225
        }
 
226
 
 
227
        public override void dispose()
 
228
        {            
 
229
            myState = null;
 
230
            if (pen != null) pen.Dispose();
 
231
            if (brush != null) brush.Dispose();
 
232
            disposed = true;
 
233
            if (!isBase)
 
234
            {
 
235
                // only dispose the underlying Graphics if this is the base PrintGraphics!
 
236
                return;
 
237
            }
 
238
            base.dispose();
 
239
        }
 
240
 
 
241
        public override void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
 
242
        {
 
243
            checkState();
 
244
            base.drawArc(x, y, width, height, startAngle, arcAngle);
 
245
        }
 
246
 
 
247
        public override void drawBytes(byte[] data, int offset, int length, int x, int y)
 
248
        {
 
249
            checkState();
 
250
            base.drawBytes(data, offset, length, x, y);
 
251
        }
 
252
 
 
253
        public override void drawChars(char[] data, int offset, int length, int x, int y)
 
254
        {
 
255
            checkState();
 
256
            base.drawChars(data, offset, length, x, y);
 
257
        }
 
258
 
 
259
        public override bool drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.Color color, java.awt.image.ImageObserver observer)
 
260
        {
 
261
            checkState();
 
262
            return base.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, color, observer);
 
263
        }
 
264
 
 
265
        public override bool drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.image.ImageObserver observer)
 
266
        {
 
267
            checkState();
 
268
            return base.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
 
269
        }
 
270
 
 
271
        public override bool drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)
 
272
        {
 
273
            checkState();
 
274
            return base.drawImage( img, x, y, width, height, bgcolor, observer);
 
275
        }
 
276
 
 
277
        public override bool drawImage(java.awt.Image img, int x, int y, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)
 
278
        {
 
279
            checkState();
 
280
            return base.drawImage(img, x, y, bgcolor, observer);
 
281
        }
 
282
 
 
283
        public override bool drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)
 
284
        {
 
285
            checkState();
 
286
            return base.drawImage( img, x, y, width, height, observer);
 
287
        }
 
288
 
 
289
        public override bool drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)
 
290
        {
 
291
            checkState();
 
292
            return base.drawImage(img, x, y, observer);
 
293
        }
 
294
 
 
295
        public override void drawLine(int x1, int y1, int x2, int y2)
 
296
        {
 
297
            checkState();
 
298
            base.drawLine(x1, y1, x2, y2);
 
299
        }
 
300
 
 
301
        public override void drawOval(int x, int y, int w, int h)
 
302
        {
 
303
            checkState();
 
304
            base.drawOval(x, y, w, h);
 
305
        }
 
306
 
 
307
        public override void drawPolygon(java.awt.Polygon polygon)
 
308
        {
 
309
            checkState();
 
310
            base.drawPolygon(polygon);
 
311
        }
 
312
 
 
313
        public override void drawPolygon(int[] aX, int[] aY, int aLength)
 
314
        {
 
315
            checkState();
 
316
            base.drawPolygon(aX, aY, aLength);
 
317
        }
 
318
 
 
319
        public override void drawPolyline(int[] aX, int[] aY, int aLength)
 
320
        {
 
321
            checkState();
 
322
            base.drawPolyline(aX, aY, aLength);
 
323
        }
 
324
 
 
325
        public override void drawRect(int x, int y, int width, int height)
 
326
        {
 
327
            checkState();
 
328
            base.drawRect(x, y, width, height);
 
329
        }
 
330
 
 
331
        public override void drawRoundRect(int x, int y, int w, int h, int arcWidth, int arcHeight)
 
332
        {
 
333
            checkState();
 
334
            base.drawRoundRect(x, y, w, h, arcWidth, arcHeight);
 
335
        }
 
336
 
 
337
        public override void fill3DRect(int x, int y, int width, int height, bool raised)
 
338
        {
 
339
            checkState();
 
340
            base.fill3DRect(x, y, width, height, raised);
 
341
        }
 
342
 
 
343
        public override void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
 
344
        {
 
345
            checkState();
 
346
            base.fillArc(x, y, width, height, startAngle, arcAngle);
 
347
        }
 
348
 
 
349
        public override void fillOval(int x, int y, int w, int h)
 
350
        {
 
351
            checkState();
 
352
            base.fillOval(x, y, w, h);
 
353
        }
 
354
 
 
355
        public override void fillPolygon(java.awt.Polygon polygon)
 
356
        {
 
357
            checkState();
 
358
            base.fillPolygon(polygon);
 
359
        }
 
360
 
 
361
        public override void fillPolygon(int[] aX, int[] aY, int aLength)
 
362
        {
 
363
            checkState();
 
364
            base.fillPolygon(aX, aY, aLength);
 
365
        }
 
366
 
 
367
        public override void fillRect(int x, int y, int width, int height)
 
368
        {
 
369
            checkState();
 
370
            base.fillRect(x, y, width, height);
 
371
        }
 
372
 
 
373
        public override void fillRoundRect(int x, int y, int w, int h, int arcWidth, int arcHeight)
 
374
        {
 
375
            checkState();
 
376
            base.fillRoundRect(x, y, w, h, arcWidth, arcHeight);
 
377
        }
 
378
 
 
379
        public override java.awt.Shape getClip()
 
380
        {
 
381
            checkState();
 
382
            return base.getClip();
 
383
        }
 
384
 
 
385
        public override java.awt.Rectangle getClipBounds(java.awt.Rectangle r)
 
386
        {
 
387
            checkState();
 
388
            return base.getClipBounds( r );
 
389
        }
 
390
 
 
391
        public override java.awt.Rectangle getClipBounds()
 
392
        {
 
393
            checkState();
 
394
            return base.getClipBounds();
 
395
        }
 
396
 
 
397
        [Obsolete]
 
398
        public override java.awt.Rectangle getClipRect()
 
399
        {
 
400
            checkState();
 
401
            return base.getClipRect();
 
402
        }
 
403
 
 
404
        public override java.awt.Color getColor()
 
405
        {
 
406
            checkState();
 
407
            return base.getColor();
 
408
        }
 
409
 
 
410
        public override java.awt.Font getFont()
 
411
        {
 
412
            checkState();
 
413
            return base.getFont();
 
414
        }
 
415
 
 
416
        public override java.awt.FontMetrics getFontMetrics(java.awt.Font f)
 
417
        {
 
418
            checkState();
 
419
            return base.getFontMetrics(f);
 
420
        }
 
421
 
 
422
        public override java.awt.FontMetrics getFontMetrics()
 
423
        {
 
424
            checkState();
 
425
            return base.getFontMetrics();
 
426
        }
 
427
 
 
428
        public override void setClip(int x, int y, int width, int height)
 
429
        {
 
430
            checkState();
 
431
            base.setClip(x,y,width,height);
 
432
        }
 
433
 
 
434
        public override void setClip(java.awt.Shape shape)
 
435
        {
 
436
            checkState();
 
437
            base.setClip(shape);
 
438
        }
 
439
 
 
440
        public override void setColor(java.awt.Color color)
 
441
        {
 
442
            checkState();
 
443
            base.setColor(color);
 
444
        }
 
445
 
 
446
        public override void setFont(java.awt.Font f)
 
447
        {
 
448
            checkState();
 
449
            base.setFont(f);
 
450
        }
 
451
 
 
452
        public override void setPaintMode()
 
453
        {
 
454
            checkState();
 
455
            base.setPaintMode();
 
456
        }
 
457
 
 
458
        public override void setXORMode(java.awt.Color param)
 
459
        {
 
460
            checkState();
 
461
            base.setXORMode(param);
 
462
        }
 
463
 
 
464
        public override void translate(int x, int y)
 
465
        {
 
466
            checkState();
 
467
            base.translate(x, y);
 
468
        }
 
469
 
 
470
        public override void draw(java.awt.Shape shape)
 
471
        {
 
472
            checkState();
 
473
            base.draw(shape);
 
474
        }
 
475
 
 
476
        public override bool drawImage(java.awt.Image img, java.awt.geom.AffineTransform xform, ImageObserver observer)
 
477
        {
 
478
            checkState();
 
479
            return base.drawImage(img, xform, observer);
 
480
        }
 
481
 
 
482
        public override void drawImage(java.awt.image.BufferedImage image, BufferedImageOp op, int x, int y)
 
483
        {
 
484
            checkState();
 
485
            base.drawImage(image, op, x, y);
 
486
        }
 
487
       
 
488
        public override void drawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)
 
489
        {
 
490
            checkState();
 
491
            base.drawRenderedImage(img, xform);
 
492
        }
 
493
 
 
494
        public override void drawRenderableImage(java.awt.image.renderable.RenderableImage image, java.awt.geom.AffineTransform xform)
 
495
        {
 
496
            checkState();
 
497
            base.drawRenderableImage(image, xform);
 
498
        }
 
499
 
 
500
        public override void drawString(string str, int x, int y)
 
501
        {
 
502
            checkState();
 
503
            base.drawString(str, x, y);
 
504
        }
 
505
 
 
506
        public override void drawString(string text, float x, float y)
 
507
        {
 
508
            checkState();
 
509
            base.drawString(text, x, y);
 
510
        }
 
511
 
 
512
        public override void drawString(java.text.AttributedCharacterIterator iterator, int x, int y)
 
513
        {
 
514
            checkState();
 
515
            base.drawString(iterator, x, y);
 
516
        }
 
517
 
 
518
        public override void drawString(java.text.AttributedCharacterIterator iterator, float x, float y)
 
519
        {
 
520
            checkState();
 
521
            base.drawString(iterator, x, y);
 
522
        }
 
523
 
 
524
        public override void fill(java.awt.Shape shape)
 
525
        {
 
526
            checkState();
 
527
            base.fill(shape);
 
528
        }
 
529
 
 
530
        public override bool hit(java.awt.Rectangle rect, java.awt.Shape s, bool onStroke)
 
531
        {
 
532
            checkState();
 
533
            return base.hit(rect, s, onStroke);
 
534
        }
 
535
 
 
536
        public override java.awt.GraphicsConfiguration getDeviceConfiguration()
 
537
        {
 
538
            // no check here, since invariant
 
539
            return base.getDeviceConfiguration();
 
540
        }
 
541
 
 
542
        public override void setComposite(java.awt.Composite comp)
 
543
        {
 
544
            checkState();
 
545
            base.setComposite(comp);
 
546
        }
 
547
 
 
548
        public override void setPaint(java.awt.Paint paint)
 
549
        {
 
550
            checkState();
 
551
            base.setPaint(paint);
 
552
        }
 
553
 
 
554
        public override void setStroke(java.awt.Stroke stroke)
 
555
        {
 
556
            checkState();
 
557
            base.setStroke(stroke);
 
558
        }
 
559
 
 
560
        public override void setRenderingHint(java.awt.RenderingHints.Key hintKey, Object hintValue)
 
561
        {
 
562
            checkState();
 
563
            base.setRenderingHint(hintKey, hintValue);
 
564
        }
 
565
 
 
566
        public override object getRenderingHint(java.awt.RenderingHints.Key hintKey)
 
567
        {
 
568
            checkState();
 
569
            return base.getRenderingHint(hintKey);
 
570
        }
 
571
 
 
572
        public override void setRenderingHints(java.util.Map hints)
 
573
        {
 
574
            checkState();
 
575
            base.setRenderingHints(hints);            
 
576
        }
 
577
 
 
578
        public override void addRenderingHints(java.util.Map hints)
 
579
        {
 
580
            checkState();
 
581
            base.addRenderingHints(hints);
 
582
        }
 
583
 
 
584
        public override java.awt.RenderingHints getRenderingHints()
 
585
        {
 
586
            checkState();
 
587
            return base.getRenderingHints();
 
588
        }
 
589
 
 
590
        public override void translate(double x, double y)
 
591
        {
 
592
            checkState();
 
593
            base.translate(x, y);
 
594
        }
 
595
 
 
596
        public override void rotate(double theta)
 
597
        {
 
598
            checkState();
 
599
            base.rotate(theta);
 
600
        }
 
601
 
 
602
        public override void rotate(double theta, double x, double y)
 
603
        {
 
604
            checkState();
 
605
            base.rotate(theta, x, y);
 
606
        }
 
607
 
 
608
        public override void scale(double scaleX, double scaleY)
 
609
        {
 
610
            checkState();
 
611
            base.scale(scaleX, scaleY);
 
612
        }
 
613
 
 
614
        public override void shear(double shearX, double shearY)
 
615
        {
 
616
            checkState();
 
617
            base.shear(shearX, shearY);
 
618
        }
 
619
 
 
620
        public override void transform(java.awt.geom.AffineTransform tx)
 
621
        {
 
622
            checkState();
 
623
            base.transform(tx);
 
624
        }
 
625
 
 
626
        public override void setTransform(java.awt.geom.AffineTransform tx)
 
627
        {
 
628
            checkState();
 
629
            base.setTransform(tx);
 
630
        }
 
631
 
 
632
        public override java.awt.geom.AffineTransform getTransform()
 
633
        {
 
634
            checkState();
 
635
            return base.getTransform();
 
636
        }
 
637
 
 
638
        public override java.awt.Paint getPaint()
 
639
        {
 
640
            checkState();
 
641
            return base.getPaint();
 
642
        }
 
643
 
 
644
        public override java.awt.Composite getComposite()
 
645
        {
 
646
            checkState();
 
647
            return base.getComposite();
 
648
        }
 
649
 
 
650
        public override void setBackground(java.awt.Color color)
 
651
        {
 
652
            checkState();
 
653
            base.setBackground(color);
 
654
        }
 
655
 
 
656
        public override java.awt.Color getBackground()
 
657
        {
 
658
            checkState();
 
659
            return base.getBackground();
 
660
        }
 
661
 
 
662
        public override java.awt.Stroke getStroke()
 
663
        {
 
664
            checkState();
 
665
            return base.getStroke();
 
666
        }
 
667
 
 
668
        public override java.awt.font.FontRenderContext getFontRenderContext()
 
669
        {
 
670
            checkState();
 
671
            return base.getFontRenderContext();
 
672
        }
 
673
 
 
674
        public override void drawGlyphVector(java.awt.font.GlyphVector gv, float x, float y)
 
675
        {
 
676
            checkState();
 
677
            base.drawGlyphVector(gv, x, y);
 
678
        }
 
679
    }
 
680
 
 
681
    /// <summary>
 
682
    /// State to store/restore the state of a NetGraphics/Graphics object
 
683
    /// </summary>
 
684
    internal class NetGraphicsState
 
685
    {
 
686
        private Brush brush;
 
687
        private Pen pen;
 
688
 
 
689
        // Graphics State
 
690
        private Matrix Transform;
 
691
        private Region Clip;
 
692
        private SmoothingMode SmoothingMode;
 
693
        private PixelOffsetMode PixelOffsetMode;
 
694
        private TextRenderingHint TextRenderingHint;
 
695
        private InterpolationMode InterpolationMode;
 
696
        private CompositingMode CompositingMode;
 
697
 
 
698
        private bool savedGraphics = false;
 
699
 
 
700
        public NetGraphicsState()
 
701
        {
 
702
        }
 
703
 
 
704
        public NetGraphicsState( NetGraphics netG )
 
705
        {
 
706
            saveGraphics(netG);
 
707
        }
 
708
 
 
709
        public void saveGraphics(NetGraphics netG)
 
710
        {
 
711
            if (netG == null )
 
712
            {
 
713
                return;
 
714
            }
 
715
            if (netG.g != null )
 
716
            {
 
717
                this.Transform = netG.g.Transform;
 
718
                this.Clip = netG.g.Clip;
 
719
                this.SmoothingMode = netG.g.SmoothingMode;
 
720
                this.PixelOffsetMode = netG.g.PixelOffsetMode;
 
721
                this.TextRenderingHint = netG.g.TextRenderingHint;
 
722
                this.InterpolationMode = netG.g.InterpolationMode;
 
723
                this.CompositingMode = netG.g.CompositingMode;
 
724
                savedGraphics = true;
 
725
            }
 
726
            if (netG.pen != null && netG.brush != null)
 
727
            {
 
728
                pen = (Pen)netG.pen.Clone();
 
729
                brush = (Brush)netG.brush.Clone();
 
730
            }
 
731
        }
 
732
 
 
733
        public void restoreGraphics(NetGraphics netG)
 
734
        {
 
735
            if (netG == null)
 
736
            {
 
737
                return;
 
738
            }
 
739
            if (netG.g != null)
 
740
            {
 
741
                if (savedGraphics)
 
742
                {
 
743
                    netG.g.Transform = Transform;
 
744
                    netG.g.Clip = Clip;
 
745
                    netG.g.SmoothingMode = SmoothingMode;
 
746
                    netG.g.PixelOffsetMode = PixelOffsetMode;
 
747
                    netG.g.TextRenderingHint = TextRenderingHint;
 
748
                    netG.g.InterpolationMode = InterpolationMode;
 
749
                    netG.g.CompositingMode = CompositingMode;
 
750
                }
 
751
                else
 
752
                {
 
753
                    // default values that Java used
 
754
                    netG.g.InterpolationMode = InterpolationMode.NearestNeighbor;
 
755
                }
 
756
            }
 
757
            if ( pen != null && brush != null )
 
758
            {
 
759
                netG.pen = (Pen)pen.Clone();
 
760
                netG.brush = (Brush)brush.Clone();
 
761
            }
 
762
            else
 
763
            {
 
764
                netG.pen = new Pen(netG.color);
 
765
                netG.brush = new SolidBrush(netG.color);
 
766
                netG.setRenderingHint(java.awt.RenderingHints.KEY_TEXT_ANTIALIASING, java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
 
767
            }
 
768
        }
 
769
    }
 
770
 
 
771
    internal abstract class NetGraphics : java.awt.Graphics2D
 
772
    {
 
773
        internal Graphics g;
 
774
        internal Graphics JGraphics { get { return g; } }
 
775
        private java.awt.Color javaColor;
 
776
        private java.awt.Paint javaPaint;
 
777
        internal Color color;
 
778
        private Color bgcolor;
 
779
        private java.awt.Font font;
 
780
        private java.awt.Stroke stroke;
 
781
        private static java.awt.BasicStroke defaultStroke = new java.awt.BasicStroke();
 
782
        private Font netfont;
 
783
        internal Brush brush;
 
784
        internal Pen pen;
 
785
        private CompositeHelper composite;
 
786
        private java.awt.Composite javaComposite = java.awt.AlphaComposite.SrcOver;
 
787
        private Object textAntialiasHint;
 
788
        private Object fractionalHint = java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT;
 
789
 
 
790
        protected NetGraphics(Graphics g, java.awt.Font font, Color fgcolor, Color bgcolor)
 
791
        {
 
792
            if (font == null)
 
793
            {
 
794
                font = new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12);
 
795
            }
 
796
            this.font = font;
 
797
            netfont = font.getNetFont();
 
798
                        this.color = fgcolor;
 
799
            this.bgcolor = bgcolor;
 
800
            composite = CompositeHelper.Create(javaComposite, g);
 
801
            init(g);
 
802
        }
 
803
 
 
804
        protected void init(Graphics graphics)
 
805
        {
 
806
            NetGraphicsState state = new NetGraphicsState();
 
807
            state.saveGraphics(this);
 
808
            g = graphics;
 
809
            state.restoreGraphics(this);            
 
810
        }
 
811
 
 
812
        /// <summary>
 
813
        /// Get the size of the graphics. This is used as a hind for some hacks.
 
814
        /// </summary>
 
815
        /// <returns></returns>
 
816
        protected virtual SizeF GetSize() {
 
817
            return g.ClipBounds.Size;
 
818
        }
 
819
 
 
820
        public override void clearRect(int x, int y, int width, int height)
 
821
        {
 
822
            using (Brush br = bgcolor != Color.Empty ? new SolidBrush(bgcolor) : brush)
 
823
            {
 
824
                CompositingMode tempMode = g.CompositingMode;
 
825
                g.CompositingMode = CompositingMode.SourceCopy;
 
826
                g.FillRectangle(br, x, y, width, height);
 
827
                g.CompositingMode = tempMode;
 
828
            }
 
829
        }
 
830
 
 
831
        public override void clipRect(int x, int y, int w, int h)
 
832
        {
 
833
            g.IntersectClip(new Rectangle(x, y, w, h));
 
834
        }
 
835
 
 
836
        public override void clip(java.awt.Shape shape)
 
837
        {
 
838
            if (shape == null)
 
839
            {
 
840
                                // note that ComponentGraphics overrides clip() to throw a NullPointerException when shape is null
 
841
                                g.ResetClip();
 
842
            }
 
843
            else
 
844
            {
 
845
                g.IntersectClip(new Region(J2C.ConvertShape(shape)));
 
846
            }
 
847
        }
 
848
 
 
849
        public override void dispose()
 
850
        {
 
851
            if (pen!=null) pen.Dispose();
 
852
            if (brush!=null) brush.Dispose();
 
853
            g.Dispose();
 
854
        }
 
855
 
 
856
        public override void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
 
857
        {
 
858
                        g.DrawArc(pen, x, y, width, height, 360 - startAngle - arcAngle, arcAngle);
 
859
        }
 
860
 
 
861
        public override void drawBytes(byte[] data, int offset, int length, int x, int y)
 
862
        {
 
863
            char[] chars = new char[length];
 
864
            for (int i = 0; i < length; i++)
 
865
            {
 
866
                chars[i] = (char)data[offset + i];
 
867
            }
 
868
            drawChars(chars, 0, length, x, y);
 
869
        }
 
870
 
 
871
        public override void drawChars(char[] data, int offset, int length, int x, int y)
 
872
        {
 
873
            drawString(new String(data, offset, length), x, y);
 
874
        }
 
875
 
 
876
        public override bool drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.Color color, java.awt.image.ImageObserver observer)
 
877
        {
 
878
            Image image = J2C.ConvertImage(img);
 
879
            if (image == null)
 
880
            {
 
881
                return false;
 
882
            }
 
883
            Rectangle destRect = new Rectangle(dx1, dy1, dx2 - dx1, dy2 - dy1);
 
884
            using (Brush brush = new SolidBrush(composite.GetColor(color))) {
 
885
                g.FillRectangle(brush, destRect);
 
886
            }
 
887
                        lock (image)
 
888
                        {
 
889
                g.DrawImage(image, destRect, sx1, sy1, sx2 - sx1, sy2 - sy1, GraphicsUnit.Pixel, composite.GetImageAttributes());
 
890
                        }
 
891
            return true;
 
892
        }
 
893
 
 
894
        public override bool drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.image.ImageObserver observer)
 
895
        {
 
896
            Image image = J2C.ConvertImage(img);
 
897
            if (image == null)
 
898
            {
 
899
                return false;
 
900
            }
 
901
            Rectangle destRect = new Rectangle(dx1, dy1, dx2 - dx1, dy2 - dy1);
 
902
                        lock (image)
 
903
                        {
 
904
                g.DrawImage(image, destRect, sx1, sy1, sx2 - sx1, sy2 - sy1, GraphicsUnit.Pixel, composite.GetImageAttributes());
 
905
                        }
 
906
            return true;
 
907
        }
 
908
 
 
909
        public override bool drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)
 
910
        {
 
911
                        Image image = J2C.ConvertImage(img);
 
912
                        if (image == null)
 
913
                        {
 
914
                                return false;
 
915
                        }
 
916
            using (Brush brush = new SolidBrush(composite.GetColor(bgcolor))) {
 
917
                g.FillRectangle(brush, x, y, width, height);
 
918
            }
 
919
                        lock (image)
 
920
                        {
 
921
                g.DrawImage(image, new Rectangle( x, y, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, composite.GetImageAttributes());
 
922
                        }
 
923
                        return true;
 
924
                }
 
925
 
 
926
        public override bool drawImage(java.awt.Image img, int x, int y, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)
 
927
        {
 
928
            if (img == null) {
 
929
                return false;
 
930
            }
 
931
            return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), bgcolor, observer);
 
932
                }
 
933
 
 
934
        public override bool drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)
 
935
        {
 
936
                        Image image = J2C.ConvertImage(img);
 
937
                        if (image == null)
 
938
                        {
 
939
                                return false;
 
940
                        }
 
941
                        lock (image)
 
942
                        {
 
943
                g.DrawImage(image, new Rectangle(x, y, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, composite.GetImageAttributes());
 
944
                        }
 
945
                        return true;
 
946
                }
 
947
 
 
948
        public override bool drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)
 
949
        {
 
950
            if (img == null) {
 
951
                return false;
 
952
            }
 
953
            return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), observer);
 
954
                }
 
955
 
 
956
        public override void drawLine(int x1, int y1, int x2, int y2)
 
957
        {
 
958
            // HACK DrawLine doesn't appear to draw the last pixel, so for single pixel lines, we have
 
959
            // a freaky workaround
 
960
            if (x1 == x2 && y1 == y2)
 
961
            {
 
962
                g.DrawLine(pen, x1, y1, x1 + 0.01f, y2 + 0.01f);
 
963
            }
 
964
            else
 
965
            {
 
966
                g.DrawLine(pen, x1, y1, x2, y2);
 
967
            }
 
968
        }
 
969
 
 
970
        public override void drawOval(int x, int y, int w, int h)
 
971
        {
 
972
            g.DrawEllipse(pen, x, y, w, h);
 
973
        }
 
974
 
 
975
        public override void drawPolygon(java.awt.Polygon polygon)
 
976
        {
 
977
            drawPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
 
978
        }
 
979
 
 
980
        public override void drawPolygon(int[] aX, int[] aY, int aLength)
 
981
        {
 
982
            Point[] points = new Point[aLength];
 
983
            for (int i = 0; i < aLength; i++)
 
984
            {
 
985
                points[i].X = aX[i];
 
986
                points[i].Y = aY[i];
 
987
            }
 
988
            g.DrawPolygon(pen, points);
 
989
        }
 
990
 
 
991
        /// <summary>
 
992
        /// Draw a sequence of connected lines
 
993
        /// </summary>
 
994
        /// <param name="aX">Array of x coordinates</param>
 
995
        /// <param name="aY">Array of y coordinates</param>
 
996
        /// <param name="aLength">Length of coordinate arrays</param>
 
997
        public override void drawPolyline(int[] aX, int[] aY, int aLength)
 
998
        {
 
999
            for (int i = 0; i < aLength - 1; i++)
 
1000
            {
 
1001
                Point point1 = new Point(aX[i], aY[i]);
 
1002
                Point point2 = new Point(aX[i + 1], aY[i + 1]);
 
1003
                g.DrawLine(pen, point1, point2);
 
1004
            }
 
1005
        }
 
1006
 
 
1007
        public override void drawRect(int x, int y, int width, int height)
 
1008
        {
 
1009
            g.DrawRectangle(pen, x, y, width, height);
 
1010
        }
 
1011
 
 
1012
        /// <summary>
 
1013
        /// Apparently there is no rounded rec function in .Net. Draw the
 
1014
        /// rounded rectangle by using lines and arcs.
 
1015
        /// </summary>
 
1016
                public override void drawRoundRect(int x, int y, int w, int h, int arcWidth, int arcHeight)
 
1017
        {
 
1018
            using (GraphicsPath gp = J2C.ConvertRoundRect(x, y, w, h, arcWidth, arcHeight))
 
1019
                g.DrawPath(pen, gp);
 
1020
        }
 
1021
 
 
1022
        public override void fill3DRect(int x, int y, int width, int height, bool raised)
 
1023
        {
 
1024
            java.awt.Paint p = getPaint();
 
1025
            java.awt.Color c = getColor();
 
1026
            java.awt.Color brighter = c.brighter();
 
1027
            java.awt.Color darker = c.darker();
 
1028
 
 
1029
            if( !raised ) {
 
1030
                setColor(darker);
 
1031
            } else if( p != c ) {
 
1032
                setColor(c);
 
1033
            }
 
1034
            fillRect(x + 1, y + 1, width - 2, height - 2);
 
1035
            setColor(raised ? brighter : darker);
 
1036
            fillRect(x, y, 1, height);
 
1037
            fillRect(x + 1, y, width - 2, 1);
 
1038
            setColor(raised ? darker : brighter);
 
1039
            fillRect(x + 1, y + height - 1, width - 1, 1);
 
1040
            fillRect(x + width - 1, y, 1, height - 1);
 
1041
            setPaint(p);
 
1042
        }
 
1043
 
 
1044
        public override void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
 
1045
        {
 
1046
                        g.FillPie(brush, x, y, width, height, 360 - startAngle - arcAngle, arcAngle);
 
1047
        }
 
1048
 
 
1049
        public override void fillOval(int x, int y, int w, int h)
 
1050
        {
 
1051
            g.FillEllipse(brush, x, y, w, h);
 
1052
        }
 
1053
 
 
1054
        public override void fillPolygon(java.awt.Polygon polygon)
 
1055
        {
 
1056
            fillPolygon(polygon.xpoints, polygon.ypoints, polygon.npoints);
 
1057
        }
 
1058
 
 
1059
        public override void fillPolygon(int[] aX, int[] aY, int aLength)
 
1060
        {
 
1061
            Point[] points = new Point[aLength];
 
1062
            for (int i = 0; i < aLength; i++)
 
1063
            {
 
1064
                points[i].X = aX[i];
 
1065
                points[i].Y = aY[i];
 
1066
            }
 
1067
            g.FillPolygon(brush, points);
 
1068
        }
 
1069
 
 
1070
        public override void fillRect(int x, int y, int width, int height)
 
1071
        {
 
1072
            g.FillRectangle(brush, x, y, width, height);
 
1073
        }
 
1074
 
 
1075
                public override void fillRoundRect(int x, int y, int w, int h, int arcWidth, int arcHeight)
 
1076
                {
 
1077
                        GraphicsPath gp = J2C.ConvertRoundRect(x, y, w, h, arcWidth, arcHeight);
 
1078
                        g.FillPath(brush, gp);
 
1079
                        gp.Dispose();
 
1080
                }
 
1081
 
 
1082
        public override java.awt.Shape getClip()
 
1083
        {
 
1084
            return getClipBounds();
 
1085
        }
 
1086
 
 
1087
        public override java.awt.Rectangle getClipBounds(java.awt.Rectangle r)
 
1088
        {
 
1089
            using (Region clip = g.Clip)
 
1090
            {
 
1091
                if (!clip.IsInfinite(g))
 
1092
                {
 
1093
                    RectangleF rec = clip.GetBounds(g);
 
1094
                    r.x = (int) rec.X;
 
1095
                    r.y = (int) rec.Y;
 
1096
                    r.width = (int) rec.Width;
 
1097
                    r.height = (int) rec.Height;
 
1098
                }
 
1099
                return r;
 
1100
            }
 
1101
        }
 
1102
 
 
1103
        public override java.awt.Rectangle getClipBounds()
 
1104
        {
 
1105
            using (Region clip = g.Clip)
 
1106
            {
 
1107
                if (clip.IsInfinite(g))
 
1108
                {
 
1109
                    return null;
 
1110
                }
 
1111
                RectangleF rec = clip.GetBounds(g);
 
1112
                return C2J.ConvertRectangle(rec);
 
1113
            }
 
1114
        }
 
1115
 
 
1116
        [Obsolete]
 
1117
        public override java.awt.Rectangle getClipRect()
 
1118
        {
 
1119
            return getClipBounds();
 
1120
        }
 
1121
 
 
1122
        public override java.awt.Color getColor()
 
1123
        {
 
1124
            if (javaColor == null)
 
1125
            {
 
1126
                javaColor = composite.GetColor(color);
 
1127
            }
 
1128
            return javaColor;
 
1129
        }
 
1130
 
 
1131
        public override java.awt.Font getFont()
 
1132
        {
 
1133
            return font;
 
1134
        }
 
1135
 
 
1136
        public override java.awt.FontMetrics getFontMetrics(java.awt.Font f)
 
1137
        {
 
1138
            return sun.font.FontDesignMetrics.getMetrics(f);
 
1139
        }
 
1140
 
 
1141
        public override java.awt.FontMetrics getFontMetrics()
 
1142
        {
 
1143
            return sun.font.FontDesignMetrics.getMetrics(font);
 
1144
        }
 
1145
 
 
1146
        public override void setClip(int x, int y, int width, int height)
 
1147
        {
 
1148
            g.Clip = new Region(new Rectangle(x, y, width, height));
 
1149
        }
 
1150
 
 
1151
        public override void setClip(java.awt.Shape shape)
 
1152
        {
 
1153
            if (shape == null)
 
1154
            {
 
1155
                Region clip = g.Clip;
 
1156
                clip.MakeInfinite();
 
1157
                g.Clip = clip;
 
1158
            }
 
1159
            else
 
1160
            {
 
1161
                g.Clip = new Region(J2C.ConvertShape(shape));
 
1162
            }
 
1163
        }
 
1164
 
 
1165
        public override void setColor(java.awt.Color color)
 
1166
        {
 
1167
            if (color == null || color == this.javaPaint)
 
1168
            {
 
1169
                // Does not change the color, if it is null like in SunGraphics2D
 
1170
                return;
 
1171
            }
 
1172
            this.javaPaint = this.javaColor = color;
 
1173
            this.color = composite.GetColor(color);
 
1174
            if (brush is SolidBrush)
 
1175
            {
 
1176
                ((SolidBrush)brush).Color = this.color;
 
1177
            }
 
1178
            else
 
1179
            {
 
1180
                brush.Dispose();
 
1181
                brush = new SolidBrush(this.color);
 
1182
            }
 
1183
            pen.Color = this.color;
 
1184
            pen.Brush = brush;
 
1185
        }
 
1186
 
 
1187
        public override void setFont(java.awt.Font f)
 
1188
        {
 
1189
            if (f != null && f != font)
 
1190
            {
 
1191
                netfont = f.getNetFont();
 
1192
                font = f;
 
1193
            }
 
1194
        }
 
1195
 
 
1196
        public override void setPaintMode()
 
1197
        {
 
1198
            throw new NotImplementedException();
 
1199
        }
 
1200
 
 
1201
        public override void setXORMode(java.awt.Color param)
 
1202
        {
 
1203
            if( param == null ) {
 
1204
                throw new java.lang.IllegalArgumentException("null XORColor");
 
1205
            }
 
1206
            throw new NotImplementedException();
 
1207
        }
 
1208
 
 
1209
        public override void translate(int x, int y)
 
1210
        {
 
1211
            Matrix transform = g.Transform;
 
1212
            transform.Translate(x, y);
 
1213
            g.Transform = transform;
 
1214
        }
 
1215
 
 
1216
        public override void draw(java.awt.Shape shape)
 
1217
        {
 
1218
            using (GraphicsPath gp = J2C.ConvertShape(shape))
 
1219
            {
 
1220
                g.DrawPath(pen, gp);
 
1221
            }
 
1222
        }
 
1223
 
 
1224
        public override bool drawImage(java.awt.Image img, java.awt.geom.AffineTransform xform, ImageObserver observer)
 
1225
        {
 
1226
            if (img == null) {
 
1227
                return true;
 
1228
            }
 
1229
     
 
1230
            if (xform == null || xform.isIdentity()) {
 
1231
                return drawImage(img, 0, 0, null, observer);
 
1232
            }
 
1233
 
 
1234
            NetGraphics clone = (NetGraphics)create();
 
1235
            clone.transform(xform);
 
1236
            bool rendered = clone.drawImage(img, 0, 0, null, observer);
 
1237
            clone.dispose();
 
1238
            return rendered;
 
1239
        }
 
1240
 
 
1241
        public override void drawImage(java.awt.image.BufferedImage image, BufferedImageOp op, int x, int y)
 
1242
        {
 
1243
 
 
1244
            if( op == null ) {
 
1245
                drawImage(image, x, y, null);
 
1246
            } else {
 
1247
                if( !(op is AffineTransformOp) ) {
 
1248
                    drawImage(op.filter(image, null), x, y, null);
 
1249
                } else {
 
1250
                    Console.WriteLine(new System.Diagnostics.StackTrace());
 
1251
                    throw new NotImplementedException();
 
1252
                }
 
1253
            }
 
1254
        }
 
1255
 
 
1256
        public override void drawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)
 
1257
        {
 
1258
            if (img == null) {
 
1259
                return;
 
1260
            }
 
1261
    
 
1262
            // BufferedImage case: use a simple drawImage call
 
1263
            if (img is BufferedImage) {
 
1264
                BufferedImage bufImg = (BufferedImage)img;
 
1265
                drawImage(bufImg,xform,null);
 
1266
                return;
 
1267
            }            
 
1268
            throw new NotImplementedException("drawRenderedImage not implemented for images which are not BufferedImages.");
 
1269
        }
 
1270
 
 
1271
        public override void drawRenderableImage(java.awt.image.renderable.RenderableImage image, java.awt.geom.AffineTransform xform)
 
1272
        {
 
1273
            throw new NotImplementedException();
 
1274
        }
 
1275
 
 
1276
        public override void drawString(string str, int x, int y)
 
1277
        {
 
1278
            drawString(str, (float)x, (float)y);
 
1279
        }
 
1280
 
 
1281
        public override void drawString(String text, float x, float y)
 
1282
        {
 
1283
            if (text.Length == 0)
 
1284
            {
 
1285
                return;
 
1286
            }
 
1287
            bool fractional = isFractionalMetrics();
 
1288
            StringFormat format = new StringFormat(StringFormat.GenericTypographic);
 
1289
            format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap | StringFormatFlags.FitBlackBox;
 
1290
            format.Trimming = StringTrimming.None;
 
1291
            if (fractional || !sun.font.StandardGlyphVector.isSimpleString(font, text))
 
1292
            {
 
1293
                g.DrawString(text, netfont, brush, x, y - font.getSize(), format);
 
1294
            }
 
1295
            else
 
1296
            {
 
1297
                // fixed metric for simple text, we position every character to simulate the Java behaviour
 
1298
                java.awt.font.FontRenderContext frc = new java.awt.font.FontRenderContext(null, isAntiAlias(), fractional);
 
1299
                sun.font.FontDesignMetrics metrics = sun.font.FontDesignMetrics.getMetrics(font, frc);
 
1300
                y -= font.getSize();
 
1301
                for (int i = 0; i < text.Length; i++)
 
1302
                {
 
1303
                    g.DrawString(text.Substring(i, 1), netfont, brush, x, y, format);
 
1304
                    x += metrics.charWidth(text[i]);
 
1305
                }
 
1306
            }
 
1307
        }
 
1308
 
 
1309
        public override void drawString(java.text.AttributedCharacterIterator iterator, int x, int y)
 
1310
        {
 
1311
            drawString(iterator, (float) x, (float) y);
 
1312
        }
 
1313
 
 
1314
        public override void drawString(java.text.AttributedCharacterIterator iterator, float x, float y)
 
1315
        {
 
1316
            if( iterator == null ) {
 
1317
                throw new java.lang.NullPointerException("AttributedCharacterIterator is null");
 
1318
            }
 
1319
            if( iterator.getBeginIndex() == iterator.getEndIndex() ) {
 
1320
                return; /* nothing to draw */
 
1321
            }
 
1322
            java.awt.font.TextLayout tl = new java.awt.font.TextLayout(iterator, getFontRenderContext());
 
1323
            tl.draw(this, x, y);
 
1324
        }
 
1325
 
 
1326
        public override void fill(java.awt.Shape shape)
 
1327
        {
 
1328
            g.FillPath(brush, J2C.ConvertShape(shape));
 
1329
        }
 
1330
 
 
1331
        public override bool hit(java.awt.Rectangle rect, java.awt.Shape s, bool onStroke)
 
1332
        {
 
1333
            if (onStroke)
 
1334
            {
 
1335
                //TODO use stroke
 
1336
                //s = stroke.createStrokedShape(s);
 
1337
            }
 
1338
            return s.intersects(rect);
 
1339
        }
 
1340
 
 
1341
        public override java.awt.GraphicsConfiguration getDeviceConfiguration()
 
1342
        {
 
1343
                        return new NetGraphicsConfiguration(Screen.PrimaryScreen);
 
1344
        }
 
1345
 
 
1346
        public override void setComposite(java.awt.Composite comp)
 
1347
        {
 
1348
            if (comp == null)
 
1349
            {
 
1350
                throw new java.lang.IllegalArgumentException("null Composite");
 
1351
            }
 
1352
            this.javaComposite = comp;
 
1353
            composite = CompositeHelper.Create(comp,g);
 
1354
            java.awt.Paint oldPaint = javaPaint;
 
1355
            javaPaint = null;
 
1356
            setPaint(oldPaint);
 
1357
        }
 
1358
 
 
1359
        public override void setPaint(java.awt.Paint paint)
 
1360
        {
 
1361
            if (paint is java.awt.Color)
 
1362
            {
 
1363
                setColor((java.awt.Color)paint);
 
1364
                return;
 
1365
            }
 
1366
 
 
1367
            if (paint == null || this.javaPaint == paint)
 
1368
            {
 
1369
                return;
 
1370
            }
 
1371
            this.javaPaint = paint;
 
1372
 
 
1373
            if (paint is java.awt.GradientPaint)
 
1374
            {
 
1375
                java.awt.GradientPaint gradient = (java.awt.GradientPaint)paint;
 
1376
                LinearGradientBrush linear;
 
1377
                if (gradient.isCyclic())
 
1378
                {
 
1379
                    linear = new LinearGradientBrush(
 
1380
                        J2C.ConvertPoint(gradient.getPoint1()),
 
1381
                        J2C.ConvertPoint(gradient.getPoint2()),
 
1382
                        composite.GetColor(gradient.getColor1()),
 
1383
                        composite.GetColor(gradient.getColor2()));
 
1384
                }
 
1385
                else
 
1386
                {
 
1387
                    //HACK because .NET does not support continue gradient like Java else Tile Gradient
 
1388
                    //that we receize the rectangle very large (factor z) and set 4 color values
 
1389
                    // a exact solution will calculate the size of the Graphics with the current transform
 
1390
                    Color color1 = composite.GetColor(gradient.getColor1());
 
1391
                    Color color2 = composite.GetColor(gradient.getColor2());
 
1392
                    float x1 = (float)gradient.getPoint1().getX();
 
1393
                    float x2 = (float)gradient.getPoint2().getX();
 
1394
                    float y1 = (float)gradient.getPoint1().getY();
 
1395
                    float y2 = (float)gradient.getPoint2().getY();
 
1396
                    float diffX = x2 - x1;
 
1397
                    float diffY = y2 - y1;
 
1398
                    const float z = 60; //HACK zoom factor, with a larger factor .NET will make the gradient wider.
 
1399
                    linear = new LinearGradientBrush(
 
1400
                        new PointF(x1 - z * diffX, y1 - z * diffY),
 
1401
                        new PointF(x2 + z * diffX, y2 + z * diffY),
 
1402
                        color1,
 
1403
                        color1);
 
1404
                    ColorBlend colorBlend = new ColorBlend(4);
 
1405
                    Color[] colors = colorBlend.Colors;
 
1406
                    colors[0] = colors[1] = color1;
 
1407
                    colors[2] = colors[3] = color2;
 
1408
                    float[] positions = colorBlend.Positions;
 
1409
                    positions[1] = z / (2 * z + 1);
 
1410
                    positions[2] = (z + 1) / (2 * z + 1);
 
1411
                    positions[3] = 1.0f;
 
1412
                    linear.InterpolationColors = colorBlend;
 
1413
                }
 
1414
                linear.WrapMode = WrapMode.TileFlipXY;
 
1415
                brush = linear;
 
1416
                pen.Brush = brush;
 
1417
                return;
 
1418
            }
 
1419
 
 
1420
            if (paint is java.awt.TexturePaint)
 
1421
            {
 
1422
                java.awt.TexturePaint texture = (java.awt.TexturePaint)paint;
 
1423
                brush = new TextureBrush(
 
1424
                    J2C.ConvertImage(texture.getImage()),
 
1425
                    J2C.ConvertRect(texture.getAnchorRect()),
 
1426
                    composite.GetImageAttributes());
 
1427
                pen.Brush = brush;
 
1428
                return;
 
1429
            }
 
1430
 
 
1431
            if (paint is java.awt.LinearGradientPaint) {
 
1432
                java.awt.LinearGradientPaint gradient = (java.awt.LinearGradientPaint)paint;
 
1433
                PointF start = J2C.ConvertPoint(gradient.getStartPoint());
 
1434
                PointF end = J2C.ConvertPoint(gradient.getEndPoint());
 
1435
 
 
1436
                java.awt.Color[] javaColors = gradient.getColors();
 
1437
                ColorBlend colorBlend;
 
1438
                Color[] colors;
 
1439
                bool noCycle = gradient.getCycleMethod() == java.awt.MultipleGradientPaint.CycleMethod.NO_CYCLE;
 
1440
                if (noCycle) {
 
1441
                    //HACK because .NET does not support continue gradient like Java else Tile Gradient
 
1442
                    //that we receize the rectangle very large (factor z) and set 2 additional color values
 
1443
                    //an exact solution will calculate the size of the Graphics with the current transform
 
1444
                    float diffX = end.X - start.X;
 
1445
                    float diffY = end.Y - start.Y;
 
1446
                    SizeF size = GetSize();
 
1447
                    //HACK zoom factor, with a larger factor .NET will make the gradient wider.
 
1448
                    float z = Math.Min(10, Math.Max(size.Width / diffX, size.Height / diffY));
 
1449
                    start.X -= z * diffX;
 
1450
                    start.Y -= z * diffY;
 
1451
                    end.X += z * diffX;
 
1452
                    end.Y += z * diffY;
 
1453
 
 
1454
                    colorBlend = new ColorBlend(javaColors.Length + 2);
 
1455
                    colors = colorBlend.Colors;
 
1456
                    float[] fractions = gradient.getFractions();
 
1457
                    float[] positions = colorBlend.Positions;
 
1458
                    for (int i = 0; i < javaColors.Length; i++) {
 
1459
                        colors[i + 1] = composite.GetColor(javaColors[i]);
 
1460
                        positions[i + 1] = (z + fractions[i]) / (2 * z + 1);
 
1461
                    }
 
1462
                    colors[0] = colors[1];
 
1463
                    colors[colors.Length - 1] = colors[colors.Length - 2];
 
1464
                    positions[positions.Length - 1] = 1.0f;
 
1465
                } else {
 
1466
                    colorBlend = new ColorBlend(javaColors.Length);
 
1467
                    colors = colorBlend.Colors;
 
1468
                    colorBlend.Positions = gradient.getFractions();
 
1469
                    for (int i = 0; i < javaColors.Length; i++) {
 
1470
                        colors[i] = composite.GetColor(javaColors[i]);
 
1471
                    }
 
1472
                }
 
1473
                LinearGradientBrush linear = new LinearGradientBrush(start, end, colors[0], colors[colors.Length - 1]);
 
1474
                linear.InterpolationColors = colorBlend;
 
1475
                switch (gradient.getCycleMethod().ordinal()) {
 
1476
                    case (int)java.awt.MultipleGradientPaint.CycleMethod.__Enum.NO_CYCLE:
 
1477
                    case (int)java.awt.MultipleGradientPaint.CycleMethod.__Enum.REFLECT:
 
1478
                        linear.WrapMode = WrapMode.TileFlipXY;
 
1479
                        break;
 
1480
                    case (int)java.awt.MultipleGradientPaint.CycleMethod.__Enum.REPEAT:
 
1481
                        linear.WrapMode = WrapMode.Tile;
 
1482
                        break;
 
1483
                }
 
1484
                brush = linear;
 
1485
                pen.Brush = brush;
 
1486
                return;
 
1487
            }
 
1488
 
 
1489
            throw new NotImplementedException("setPaint("+paint.GetType().FullName+")");
 
1490
        }
 
1491
 
 
1492
                public override void setStroke(java.awt.Stroke stroke)
 
1493
                {
 
1494
                        if (this.stroke != null && this.stroke.Equals(stroke))
 
1495
                        {
 
1496
                                return;
 
1497
                        }
 
1498
                        this.stroke = stroke;
 
1499
            if (stroke is java.awt.BasicStroke)
 
1500
            {
 
1501
                java.awt.BasicStroke s = (java.awt.BasicStroke)stroke;
 
1502
 
 
1503
                pen = new Pen(pen.Brush, s.getLineWidth());
 
1504
 
 
1505
                SetLineJoin(s);
 
1506
                SetLineDash(s);
 
1507
            }
 
1508
            else
 
1509
            {
 
1510
                Console.WriteLine("Unknown Stroke type: " + stroke.GetType().FullName);
 
1511
            }
 
1512
                }
 
1513
 
 
1514
        private void SetLineJoin(java.awt.BasicStroke s)
 
1515
        {
 
1516
            pen.MiterLimit = s.getMiterLimit();
 
1517
                        pen.LineJoin = J2C.ConvertLineJoin(s.getLineJoin());
 
1518
        }
 
1519
 
 
1520
        private void SetLineDash(java.awt.BasicStroke s)
 
1521
        {
 
1522
            float[] dash = s.getDashArray();
 
1523
            if (dash == null)
 
1524
            {
 
1525
                pen.DashStyle = DashStyle.Solid;
 
1526
            } else {
 
1527
                if (dash.Length % 2 == 1)
 
1528
                {
 
1529
                    int len = dash.Length;
 
1530
                    Array.Resize(ref dash, len * 2);
 
1531
                    Array.Copy(dash, 0, dash, len, len);
 
1532
                }
 
1533
                float lineWidth = s.getLineWidth();
 
1534
                if (lineWidth > 1) // for values < 0 there is no correctur needed
 
1535
                {
 
1536
                    for (int i = 0; i < dash.Length; i++)
 
1537
                    {
 
1538
                        //dividing by line thickness because of the representation difference
 
1539
                        dash[i] = dash[i] / lineWidth;
 
1540
                    }
 
1541
                }
 
1542
                // To fix the problem where solid style in Java can be represented at { 1.0, 0.0 }.
 
1543
                // In .NET, however, array can only have positive value
 
1544
                if (dash.Length == 2 && dash[dash.Length - 1] == 0)
 
1545
                {
 
1546
                    Array.Resize(ref dash, 1);
 
1547
                }
 
1548
 
 
1549
                float dashPhase = s.getDashPhase();
 
1550
                // correct the dash cap
 
1551
                switch (s.getEndCap())
 
1552
                {
 
1553
                    case java.awt.BasicStroke.CAP_BUTT:
 
1554
                        pen.DashCap = DashCap.Flat;
 
1555
                        break;
 
1556
                    case java.awt.BasicStroke.CAP_ROUND:
 
1557
                        pen.DashCap = DashCap.Round;
 
1558
                        break;
 
1559
                    case java.awt.BasicStroke.CAP_SQUARE:
 
1560
                        pen.DashCap = DashCap.Flat;
 
1561
                        // there is no equals DashCap in .NET, we need to emulate it
 
1562
                        dashPhase += lineWidth / 2;
 
1563
                        for (int i = 0; i < dash.Length; i++)
 
1564
                        {
 
1565
                            if (i % 2 == 0)
 
1566
                            {
 
1567
                                dash[i] += 1;
 
1568
                            }
 
1569
                            else
 
1570
                            {
 
1571
                                dash[i] = Math.Max(0.00001F, dash[i] - 1);
 
1572
                            }
 
1573
                        }
 
1574
                        break;
 
1575
                    default:
 
1576
                        Console.WriteLine("Unknown dash cap type:" + s.getEndCap());
 
1577
                        break;
 
1578
                }
 
1579
 
 
1580
                // calc the dash offset
 
1581
                if (lineWidth > 0)
 
1582
                {
 
1583
                    //dividing by line thickness because of the representation difference
 
1584
                    pen.DashOffset = dashPhase / lineWidth;
 
1585
                }
 
1586
                else
 
1587
                {
 
1588
                    // thickness == 0
 
1589
                    if (dashPhase > 0)
 
1590
                    {
 
1591
                        pen.Width = lineWidth = 0.001F; // hack to prevent a division with 0
 
1592
                        pen.DashOffset = dashPhase / lineWidth;
 
1593
                    }
 
1594
                    else
 
1595
                    {
 
1596
                        pen.DashOffset = 0;
 
1597
                    }
 
1598
                }
 
1599
 
 
1600
                // set the final dash pattern 
 
1601
                pen.DashPattern = dash;
 
1602
            }
 
1603
        }
 
1604
 
 
1605
        public override void setRenderingHint(java.awt.RenderingHints.Key hintKey, Object hintValue)
 
1606
        {
 
1607
            if (hintKey == java.awt.RenderingHints.KEY_ANTIALIASING)
 
1608
            {
 
1609
                if (hintValue == java.awt.RenderingHints.VALUE_ANTIALIAS_DEFAULT)
 
1610
                {
 
1611
                    g.SmoothingMode = SmoothingMode.Default;
 
1612
                    g.PixelOffsetMode = PixelOffsetMode.Default;
 
1613
                    return;
 
1614
                }
 
1615
                if (hintValue == java.awt.RenderingHints.VALUE_ANTIALIAS_OFF)
 
1616
                {
 
1617
                    g.SmoothingMode = SmoothingMode.None;
 
1618
                    g.PixelOffsetMode = PixelOffsetMode.Default;
 
1619
                    return;
 
1620
                }
 
1621
                if (hintValue == java.awt.RenderingHints.VALUE_ANTIALIAS_ON)
 
1622
                {
 
1623
                    g.SmoothingMode = SmoothingMode.AntiAlias;
 
1624
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
 
1625
                    return;
 
1626
                }
 
1627
                return;
 
1628
            }
 
1629
            if (hintKey == java.awt.RenderingHints.KEY_INTERPOLATION)
 
1630
            {
 
1631
                if (hintValue == java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR)
 
1632
                {
 
1633
                    g.InterpolationMode = InterpolationMode.HighQualityBilinear;
 
1634
                    return;
 
1635
                }
 
1636
                if (hintValue == java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC)
 
1637
                {
 
1638
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
 
1639
                    return;
 
1640
                }
 
1641
                if (hintValue == java.awt.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR)
 
1642
                {
 
1643
                    g.InterpolationMode = InterpolationMode.NearestNeighbor;
 
1644
                    return;
 
1645
                }
 
1646
                return;
 
1647
            }
 
1648
            if (hintKey == java.awt.RenderingHints.KEY_TEXT_ANTIALIASING)
 
1649
            {
 
1650
                if (hintValue == java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT ||
 
1651
                    hintValue == java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)
 
1652
                {
 
1653
                    g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
 
1654
                    textAntialiasHint = hintValue;
 
1655
                    return;
 
1656
                }
 
1657
                if (hintValue == java.awt.RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
 
1658
                {
 
1659
                    g.TextRenderingHint = TextRenderingHint.AntiAlias;
 
1660
                    textAntialiasHint = hintValue;
 
1661
                    return;
 
1662
                }
 
1663
                return;
 
1664
            }
 
1665
            if (hintKey == java.awt.RenderingHints.KEY_FRACTIONALMETRICS) 
 
1666
            {
 
1667
                if (hintValue == java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT || 
 
1668
                    hintValue == java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_OFF ||
 
1669
                    hintValue == java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_ON) 
 
1670
                {
 
1671
                    fractionalHint = hintValue;
 
1672
                }
 
1673
                return;
 
1674
            }
 
1675
 
 
1676
        }
 
1677
 
 
1678
        public override object getRenderingHint(java.awt.RenderingHints.Key hintKey)
 
1679
        {
 
1680
            return getRenderingHints().get(hintKey);
 
1681
        }
 
1682
 
 
1683
        public override void setRenderingHints(java.util.Map hints)
 
1684
        {
 
1685
            addRenderingHints(hints);
 
1686
            //TODO all not included values should reset to default, but was is default?
 
1687
        }
 
1688
 
 
1689
        public override void addRenderingHints(java.util.Map hints)
 
1690
        {
 
1691
            Iterator iterator = hints.entrySet().iterator();
 
1692
            while (iterator.hasNext())
 
1693
            {
 
1694
                java.util.Map.Entry entry = (java.util.Map.Entry)iterator.next();
 
1695
                setRenderingHint((java.awt.RenderingHints.Key)entry.getKey(), entry.getValue());
 
1696
            }
 
1697
        }
 
1698
 
 
1699
        public override java.awt.RenderingHints getRenderingHints()
 
1700
        {
 
1701
            java.awt.RenderingHints hints = new java.awt.RenderingHints(null);
 
1702
            switch (g.SmoothingMode)
 
1703
            {
 
1704
                case SmoothingMode.Default:
 
1705
                    hints.put(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_DEFAULT);
 
1706
                    break;
 
1707
                case SmoothingMode.None:
 
1708
                    hints.put(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_OFF);
 
1709
                    break;
 
1710
                case SmoothingMode.AntiAlias:
 
1711
                    hints.put(java.awt.RenderingHints.KEY_ANTIALIASING, java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
 
1712
                    break;
 
1713
            }
 
1714
 
 
1715
            switch (g.InterpolationMode)
 
1716
            {
 
1717
                case InterpolationMode.Bilinear:
 
1718
                case InterpolationMode.HighQualityBilinear:
 
1719
                    hints.put(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 
1720
                    break;
 
1721
                case InterpolationMode.Bicubic:
 
1722
                case InterpolationMode.HighQualityBicubic:
 
1723
                    hints.put(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC);
 
1724
                    break;
 
1725
                case InterpolationMode.NearestNeighbor:
 
1726
                    hints.put(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
 
1727
                    break;
 
1728
            }
 
1729
 
 
1730
            hints.put(java.awt.RenderingHints.KEY_TEXT_ANTIALIASING, textAntialiasHint);
 
1731
            hints.put(java.awt.RenderingHints.KEY_FRACTIONALMETRICS, fractionalHint);
 
1732
            return hints;
 
1733
        }
 
1734
 
 
1735
        public override void translate(double x, double y)
 
1736
        {
 
1737
            Matrix transform = g.Transform;
 
1738
            transform.Translate((float)x, (float)y);
 
1739
            g.Transform = transform;
 
1740
        }
 
1741
 
 
1742
                private static double RadiansToDegrees(double radians)
 
1743
                {
 
1744
                        return radians * (180 / Math.PI);
 
1745
                }
 
1746
 
 
1747
        public override void rotate(double theta)
 
1748
        {
 
1749
            Matrix transform = g.Transform;
 
1750
            transform.Rotate((float)RadiansToDegrees(theta));
 
1751
            g.Transform = transform;
 
1752
        }
 
1753
 
 
1754
        public override void rotate(double theta, double x, double y)
 
1755
        {
 
1756
            Matrix transform = g.Transform;
 
1757
            transform.Translate((float)x, (float)y);
 
1758
                        transform.Rotate((float)RadiansToDegrees(theta));
 
1759
            transform.Translate(-(float)x, -(float)y);
 
1760
            g.Transform = transform;
 
1761
        }
 
1762
 
 
1763
        public override void scale(double scaleX, double scaleY)
 
1764
        {
 
1765
            using (Matrix transform = g.Transform)
 
1766
            {
 
1767
                transform.Scale((float)scaleX, (float)scaleY);
 
1768
                g.Transform = transform;
 
1769
            }
 
1770
        }
 
1771
 
 
1772
        public override void shear(double shearX, double shearY)
 
1773
        {
 
1774
            using (Matrix transform = g.Transform)
 
1775
            {
 
1776
                transform.Shear((float)shearX, (float)shearY);
 
1777
                g.Transform = transform;
 
1778
            }
 
1779
        }
 
1780
 
 
1781
        public override void transform(java.awt.geom.AffineTransform tx)
 
1782
        {
 
1783
            using (Matrix transform = g.Transform,
 
1784
                matrix = J2C.ConvertTransform(tx))
 
1785
            {
 
1786
                transform.Multiply(matrix);
 
1787
                g.Transform = transform;
 
1788
            }
 
1789
        }
 
1790
 
 
1791
        public override void setTransform(java.awt.geom.AffineTransform tx)
 
1792
        {
 
1793
            g.Transform = J2C.ConvertTransform(tx);
 
1794
        }
 
1795
 
 
1796
        public override java.awt.geom.AffineTransform getTransform()
 
1797
        {
 
1798
            using (Matrix matrix = g.Transform)
 
1799
            {
 
1800
                return C2J.ConvertMatrix(matrix);
 
1801
            }
 
1802
        }
 
1803
 
 
1804
        public override java.awt.Paint getPaint()
 
1805
        {
 
1806
            if( javaPaint == null ) {
 
1807
                javaPaint = composite.GetColor(color);
 
1808
            }
 
1809
            return javaPaint;
 
1810
        }
 
1811
 
 
1812
        public override java.awt.Composite getComposite()
 
1813
        {
 
1814
            return javaComposite;
 
1815
        }
 
1816
 
 
1817
        public override void setBackground(java.awt.Color backcolor)
 
1818
        {
 
1819
            bgcolor = backcolor == null ? Color.Empty : Color.FromArgb(backcolor.getRGB());
 
1820
        }
 
1821
 
 
1822
        public override java.awt.Color getBackground()
 
1823
        {
 
1824
            return bgcolor == Color.Empty ? null : new java.awt.Color(color.ToArgb(), true);
 
1825
        }
 
1826
 
 
1827
        public override java.awt.Stroke getStroke()
 
1828
        {
 
1829
            if (stroke == null)
 
1830
            {
 
1831
                return defaultStroke;
 
1832
            }
 
1833
            return stroke; 
 
1834
        }
 
1835
 
 
1836
        private bool isAntiAlias()
 
1837
        {
 
1838
            switch (g.TextRenderingHint)
 
1839
            {
 
1840
                case TextRenderingHint.AntiAlias:
 
1841
                case TextRenderingHint.AntiAliasGridFit:
 
1842
                case TextRenderingHint.ClearTypeGridFit:
 
1843
                    return true;
 
1844
                default:
 
1845
                    return false;
 
1846
            }
 
1847
        }
 
1848
 
 
1849
        private bool isFractionalMetrics()
 
1850
        {
 
1851
            return fractionalHint == java.awt.RenderingHints.VALUE_FRACTIONALMETRICS_ON; 
 
1852
        }
 
1853
 
 
1854
        public override java.awt.font.FontRenderContext getFontRenderContext()
 
1855
        {
 
1856
            return new java.awt.font.FontRenderContext(getTransform(), isAntiAlias(), isFractionalMetrics());
 
1857
        }
 
1858
 
 
1859
        public override void drawGlyphVector(java.awt.font.GlyphVector gv, float x, float y)
 
1860
        {
 
1861
            java.awt.font.FontRenderContext frc = gv.getFontRenderContext();
 
1862
            Matrix currentMatrix = null;
 
1863
            Font currentFont = netfont;
 
1864
            TextRenderingHint currentHint = g.TextRenderingHint;
 
1865
            try
 
1866
            {
 
1867
                java.awt.Font javaFont = gv.getFont();
 
1868
                if (javaFont != null)
 
1869
                {
 
1870
                    netfont = javaFont.getNetFont();
 
1871
                }
 
1872
                if (frc.isAntiAliased()) {
 
1873
                    if( frc.usesFractionalMetrics() ){
 
1874
                        g.TextRenderingHint = TextRenderingHint.AntiAlias;
 
1875
                    } else {
 
1876
                        g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
 
1877
                    }
 
1878
                } else {
 
1879
                    if (frc.usesFractionalMetrics()) {
 
1880
                        g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
 
1881
                    } else {
 
1882
                        g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
 
1883
                    }
 
1884
                }
 
1885
                if (!frc.getTransform().equals(getTransform()))
 
1886
                {
 
1887
                    // save the old context and use the transformation from the renderContext
 
1888
                    currentMatrix = g.Transform;
 
1889
                    g.Transform = J2C.ConvertTransform(frc.getTransform());
 
1890
                }
 
1891
                drawString(J2C.ConvertGlyphVector(gv), x, y);
 
1892
            }
 
1893
            finally
 
1894
            {
 
1895
                // Restore the old context if needed
 
1896
                g.TextRenderingHint = currentHint;
 
1897
                netfont = currentFont;
 
1898
                if (currentMatrix != null)
 
1899
                {
 
1900
                    g.Transform = currentMatrix;
 
1901
                }
 
1902
            }
 
1903
        }
 
1904
    }
 
1905
 
 
1906
    sealed class NetGraphicsConfiguration : java.awt.GraphicsConfiguration
 
1907
    {
 
1908
        internal readonly Screen screen;
 
1909
 
 
1910
        public NetGraphicsConfiguration(Screen screen)
 
1911
        {
 
1912
            this.screen = screen;
 
1913
        }
 
1914
 
 
1915
        public override java.awt.image.BufferedImage createCompatibleImage(int width, int height, int transparency)
 
1916
        {
 
1917
            switch (transparency)
 
1918
            {
 
1919
                case java.awt.Transparency.__Fields.OPAQUE:
 
1920
                    return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
 
1921
                case java.awt.Transparency.__Fields.BITMASK:
 
1922
                    return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
 
1923
                case java.awt.Transparency.__Fields.TRANSLUCENT:
 
1924
                    return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
 
1925
                default:
 
1926
                    throw new java.lang.IllegalArgumentException("transparency:" + transparency);
 
1927
            }
 
1928
        }
 
1929
 
 
1930
        public override java.awt.image.BufferedImage createCompatibleImage(int width, int height)
 
1931
        {
 
1932
            return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
 
1933
        }
 
1934
 
 
1935
        public override java.awt.image.VolatileImage createCompatibleVolatileImage(int param1, int param2, java.awt.ImageCapabilities param3)
 
1936
        {
 
1937
            throw new NotImplementedException();
 
1938
        }
 
1939
 
 
1940
        public override java.awt.image.VolatileImage createCompatibleVolatileImage(int width, int height)
 
1941
        {
 
1942
            return new NetVolatileImage(width, height);
 
1943
        }
 
1944
 
 
1945
        public override java.awt.Rectangle getBounds()
 
1946
        {
 
1947
            System.Drawing.Rectangle bounds = screen.Bounds;
 
1948
            return new java.awt.Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height);
 
1949
        }
 
1950
 
 
1951
        public override java.awt.BufferCapabilities getBufferCapabilities()
 
1952
        {
 
1953
            throw new NotImplementedException();
 
1954
        }
 
1955
 
 
1956
        public override java.awt.image.ColorModel getColorModel(int transparency)
 
1957
        {
 
1958
            if (transparency == java.awt.Transparency.__Fields.TRANSLUCENT)
 
1959
            {
 
1960
                //we return the default ColorModel because this produce the fewest problems with convertions
 
1961
                return ColorModel.getRGBdefault();
 
1962
            }
 
1963
            else
 
1964
            {
 
1965
                return null;
 
1966
            }
 
1967
        }
 
1968
 
 
1969
        public override java.awt.image.ColorModel getColorModel()
 
1970
        {
 
1971
            //we return the default ColorModel because this produce the fewest problems with convertions
 
1972
            return ColorModel.getRGBdefault();
 
1973
        }
 
1974
 
 
1975
        public override java.awt.geom.AffineTransform getDefaultTransform()
 
1976
        {
 
1977
            return new java.awt.geom.AffineTransform();
 
1978
        }
 
1979
 
 
1980
        public override java.awt.GraphicsDevice getDevice()
 
1981
        {
 
1982
            return new NetGraphicsDevice(screen);
 
1983
        }
 
1984
 
 
1985
        public override java.awt.ImageCapabilities getImageCapabilities()
 
1986
        {
 
1987
            throw new NotImplementedException();
 
1988
        }
 
1989
 
 
1990
        public override java.awt.geom.AffineTransform getNormalizingTransform()
 
1991
        {
 
1992
            throw new NotImplementedException();
 
1993
        }
 
1994
 
 
1995
        public override VolatileImage createCompatibleVolatileImage(int width, int height, int transparency)
 
1996
        {
 
1997
            return new NetVolatileImage(width, height);
 
1998
        }
 
1999
 
 
2000
        public override VolatileImage createCompatibleVolatileImage(int width, int height, java.awt.ImageCapabilities caps, int transparency)
 
2001
        {
 
2002
            return new NetVolatileImage(width, height);
 
2003
        }
 
2004
 
 
2005
        public override bool isTranslucencyCapable()
 
2006
        {
 
2007
            return true;
 
2008
        }
 
2009
    }
 
2010
 
 
2011
    class NetGraphicsDevice : java.awt.GraphicsDevice
 
2012
    {
 
2013
        internal readonly Screen screen;
 
2014
 
 
2015
        internal NetGraphicsDevice(Screen screen)
 
2016
        {
 
2017
            this.screen = screen;
 
2018
        }
 
2019
 
 
2020
        public override java.awt.GraphicsConfiguration[] getConfigurations()
 
2021
        {
 
2022
            Screen[] screens = Screen.AllScreens;
 
2023
            NetGraphicsConfiguration[] configs = new NetGraphicsConfiguration[screens.Length];
 
2024
            for (int i = 0; i < screens.Length; i++)
 
2025
            {
 
2026
                configs[i] = new NetGraphicsConfiguration(screens[i]);
 
2027
            }
 
2028
            return configs;
 
2029
        }
 
2030
 
 
2031
        public override java.awt.GraphicsConfiguration getDefaultConfiguration()
 
2032
        {
 
2033
            return new NetGraphicsConfiguration(Screen.PrimaryScreen);
 
2034
        }
 
2035
 
 
2036
        public override string getIDstring()
 
2037
        {
 
2038
            return screen.DeviceName;
 
2039
        }
 
2040
 
 
2041
        public override int getType()
 
2042
        {
 
2043
            return TYPE_RASTER_SCREEN;
 
2044
        }
 
2045
    }
 
2046
 
 
2047
    public class NetGraphicsEnvironment : java.awt.GraphicsEnvironment
 
2048
    {
 
2049
        // Create a bitmap with the dimensions of the argument image. Then
 
2050
        // create a graphics objects from the bitmap. All paint operations will
 
2051
        // then paint the bitmap.
 
2052
                public override java.awt.Graphics2D createGraphics(BufferedImage bi)
 
2053
                {
 
2054
                        return new BitmapGraphics(bi.getBitmap());
 
2055
                }
 
2056
 
 
2057
        public override java.awt.Font[] getAllFonts()
 
2058
        {
 
2059
#if WINFX  
 
2060
            System.Collections.Generic.ICollection<Typeface> typefaces = System.Windows.Media.Fonts.SystemTypefaces;
 
2061
            java.awt.Font[] fonts = new java.awt.Font[typefaces.Count];
 
2062
            int i = 0;
 
2063
            foreach (Typeface face in typefaces)
 
2064
            {
 
2065
                FontFamily family = face.FontFamily;
 
2066
                fonts[i++] = new java.awt.Font(family.GetName(0), face.Style, 1);
 
2067
            }
 
2068
#else
 
2069
            String[] names = getAvailableFontFamilyNames();
 
2070
            java.awt.Font[] fonts = new java.awt.Font[names.Length];
 
2071
            for(int i=0; i<fonts.Length; i++)
 
2072
            {
 
2073
                fonts[i] = new java.awt.Font(names[i], 0, 1);
 
2074
            }
 
2075
            return fonts;
 
2076
#endif
 
2077
        }
 
2078
 
 
2079
        public override String[] getAvailableFontFamilyNames()
 
2080
        {
 
2081
            int language = CultureInfo.CurrentCulture.LCID;
 
2082
            return getAvailableFontFamilyNames(language);
 
2083
        }
 
2084
 
 
2085
        public override string[] getAvailableFontFamilyNames(Locale locale)
 
2086
        {
 
2087
            int language = CultureInfo.GetCultureInfo(locale.toString()).LCID;
 
2088
            return getAvailableFontFamilyNames(language);
 
2089
        }
 
2090
 
 
2091
        private String[] getAvailableFontFamilyNames(int language)
 
2092
        {
 
2093
                        FontFamily[] families = FontFamily.Families;
 
2094
            String[] results = new String[families.Length + 5];
 
2095
            int i = 0;
 
2096
            for (; i < families.Length; i++)
 
2097
            {
 
2098
                results[i] = families[i].GetName(language);
 
2099
            }
 
2100
            results[i++] = "Dialog";
 
2101
            results[i++] = "DialogInput";
 
2102
            results[i++] = "Serif";
 
2103
            results[i++] = "SansSerif";
 
2104
            results[i++] = "Monospaced";
 
2105
            Array.Sort(results);
 
2106
            return results;
 
2107
        }
 
2108
 
 
2109
        public override java.awt.GraphicsDevice getDefaultScreenDevice()
 
2110
        {
 
2111
            return new NetGraphicsDevice(Screen.PrimaryScreen);
 
2112
        }
 
2113
 
 
2114
        public override java.awt.GraphicsDevice[] getScreenDevices()
 
2115
        {
 
2116
            Screen[] screens = Screen.AllScreens;
 
2117
            NetGraphicsDevice[] devices = new NetGraphicsDevice[screens.Length];
 
2118
            for (int i = 0; i < screens.Length; i++)
 
2119
            {
 
2120
                devices[i] = new NetGraphicsDevice(screens[i]);
 
2121
            }
 
2122
            return devices;
 
2123
        }
 
2124
    }
 
2125
 
 
2126
}
 
 
b'\\ No newline at end of file'