aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml/src/main/java/org/apache/poi/xslf/util/DummyGraphics2d.java
blob: 1dfbc4c009093eb1290c572907fe38768ad3def1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */


package org.apache.poi.xslf.util;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ImageObserver;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.io.PrintStream;
import java.text.AttributedCharacterIterator;
import java.text.AttributedCharacterIterator.Attribute;
import java.text.CharacterIterator;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

public class DummyGraphics2d extends Graphics2D {
    private BufferedImage bufimg;
    private final Graphics2D g2D;
    private final PrintStream log;

    public DummyGraphics2d() {
        this(System.out);
    }

    public DummyGraphics2d(PrintStream log) {
        bufimg = new BufferedImage(1000, 1000, 2);
        g2D = (Graphics2D)bufimg.getGraphics();
        this.log = log;
    }

    public DummyGraphics2d(PrintStream log, Graphics2D g2D) {
        this.g2D = g2D;
        this.log = log;
    }

    @Override
    public void addRenderingHints(Map<?,?> hints) {
        String l =
            "addRenderingHinds(Map):" +
            "\n  hints = " + hints;
        log.println( l );
        g2D.addRenderingHints( hints );
    }

    @Override
    public void clip(Shape s) {
        String l =
            "clip(Shape):" +
            "\n  s = " + s;
        log.println( l );
        g2D.clip( s );
    }

    private void pathToString(StringBuilder sb, Path2D p) {
        sb.append("Path2D p = new Path2D.Double(").append(p.getWindingRule()).append(");\n");
        double[] coords = new double[6];

        for (PathIterator pi = p.getPathIterator(null); !pi.isDone(); pi.next()) {
            // The type will be SEG_LINETO, SEG_MOVETO, or SEG_CLOSE
            // Because the Area is composed of straight lines
            switch (pi.currentSegment(coords)) {
                case PathIterator.SEG_MOVETO:
                    sb.append("p.moveTo(").append(coords[0]).append(",").append(coords[1]).append(");\n");
                    break;
                case PathIterator.SEG_LINETO:
                    sb.append("p.lineTo(").append(coords[0]).append(",").append(coords[1]).append(");\n");
                    break;
                case PathIterator.SEG_QUADTO:
                    sb.append("p.quadTo(").append(coords[0]).append(",").append(coords[1]).append(",").append(coords[2]).append(",").append(coords[3]).append(");\n");
                    break;
                case PathIterator.SEG_CUBICTO:
                    sb.append("p.curveTo(").append(coords[0]).append(",").append(coords[1]).append(",").append(coords[2]).append(",").append(coords[3]).append(",").append(coords[4]).append(",").append(coords[5]).append(");\n");
                    break;
                case PathIterator.SEG_CLOSE:
                    sb.append("p.closePath();\n");
                    break;
            }
        }
    }

    @Override
    public void draw(Shape s) {
        if (s instanceof Path2D) {
            StringBuilder sb = new StringBuilder();
            pathToString(sb, (Path2D)s);
            sb.append("g.draw(p);");
            log.println(sb);
        } else {
            log.println( "g.draw("+ s + ")" );
        }
        g2D.draw( s );
    }

    @Override
    public void drawGlyphVector(GlyphVector g, float x, float y) {
        String l =
            "drawGlyphVector(GlyphVector, float, float):" +
            "\n  g = " + g +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.drawGlyphVector( g, x, y );
    }

    @Override
    public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
        String l =
            "drawImage(BufferedImage, BufferedImageOp, x, y):" +
            "\n  img = " + img +
            "\n  op = " + op +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.drawImage( img, op, x, y );
    }

    @Override
    public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
        String l =
            "drawImage(Image,AfflineTransform,ImageObserver):" +
            "\n  img = " + img +
            "\n  xform = " + xform +
            "\n  obs = " + obs;
        log.println( l );
        return g2D.drawImage( img, xform, obs );
    }

    @Override
    public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
        String l =
            "drawRenderableImage(RenderableImage, AfflineTransform):" +
            "\n  img = " + img +
            "\n  xform = " + xform;
        log.println( l );
        g2D.drawRenderableImage( img, xform );
    }

    @Override
    public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
        String l =
            "drawRenderedImage(RenderedImage, AffineTransform):" +
            "\n  img = " + img +
            "\n  xform = " + xform;
        log.println( l );
        g2D.drawRenderedImage( img, xform );
    }

    @Override
    public void drawString(String s, float x, float y) {
        String l =
            "drawString(s,x,y):" +
            "\n  s = " + s +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.drawString( s, x, y );
    }

    @Override
    public void fill(Shape s) {
        if (s instanceof Path2D) {
            StringBuilder sb = new StringBuilder();
            pathToString(sb, (Path2D)s);
            sb.append("g.fill(p);");
            log.println(sb);
        } else {
            log.println( "g.fill("+ s + ")" );
        }
        g2D.fill( s );
    }

    @Override
    public Color getBackground() {
        log.println( "getBackground():" );
        return g2D.getBackground();
    }

    @Override
    public Composite getComposite() {
        log.println( "getComposite():" );
        return g2D.getComposite();
    }

    @Override
    public GraphicsConfiguration getDeviceConfiguration() {
        log.println( "getDeviceConfiguration():" );
        return g2D.getDeviceConfiguration();
    }

    @Override
    public FontRenderContext getFontRenderContext() {
        log.println( "getFontRenderContext():" );
        return g2D.getFontRenderContext();
    }

    @Override
    public Paint getPaint() {
        log.println( "getPaint():" );
        return g2D.getPaint();
    }

    @Override
    public Object getRenderingHint(RenderingHints.Key hintKey) {
        log.println( "getRenderingHint(\""+hintKey+"\")" );
        return g2D.getRenderingHint( hintKey );
    }

    @Override
    public RenderingHints getRenderingHints() {
        log.println( "getRenderingHints():" );
        return g2D.getRenderingHints();
    }

    @Override
    public Stroke getStroke() {
        log.println( "getStroke():" );
        return g2D.getStroke();
    }

    @Override
    public AffineTransform getTransform() {
        log.println( "getTransform():" );
        return g2D.getTransform();
    }

    @Override
    public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
        String l =
            "hit(Rectangle, Shape, onStroke):" +
            "\n  rect = " + rect +
            "\n  s = " + s +
            "\n  onStroke = " + onStroke;
        log.println( l );
        return g2D.hit( rect, s, onStroke );
    }

    @Override
    public void rotate(double theta) {
        String l =
            "rotate(theta):" +
            "\n  theta = " + theta;
        log.println( l );
        g2D.rotate( theta );
    }

    @Override
    public void rotate(double theta, double x, double y) {
        String l =
            "rotate(double,double,double):" +
            "\n  theta = " + theta +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.rotate( theta, x, y );
    }

    @Override
    public void scale(double sx, double sy) {
        log.println( "g.scale("+sx+","+sy+");" );
        g2D.scale( sx, sy );
    }

    @Override
    public void setBackground(Color color) {
        log.printf(Locale.ROOT, "setBackground(new Color(0x%08X))%n", color.getRGB());
        g2D.setBackground( color );
    }

    private static final String[] COMPOSITE_RULES = {
        "CLEAR", "SRC", "SRC_OVER", "DST_OVER", "SRC_IN", "DST_IN", "SRC_OUT", "DST_OUT", "DST", "SRC_ATOP", "DST_ATOP", "XOR"
    };

    @Override
    public void setComposite(Composite comp) {
        String l = "g.setComposite(";
        if (comp instanceof AlphaComposite) {
            AlphaComposite ac = (AlphaComposite)comp;
            l += "AlphaComposite.getInstance(AlphaComposite."
              + COMPOSITE_RULES[Math.max(0,Math.min(COMPOSITE_RULES.length-1,ac.getRule()))]
              + ", " + ac.getAlpha() + "f));";
        } else {
            l += comp.toString() + ");";
        }
        log.println( l );
        g2D.setComposite( comp );
    }

    @Override
    public void setPaint( Paint paint ) {
        String l = "g.setPaint(";
        if (paint instanceof Color) {
            l += String.format(Locale.ROOT, "new Color(0x%08X));", ((Color)paint).getRGB());
        } else {
            l += paint.toString() + ");";
        }
        log.println( l );
        g2D.setPaint( paint );
    }

    @Override
    public void setRenderingHint(RenderingHints.Key hintKey, Object hintValue) {
        log.println( "g.setRenderingHint("+mapHint(hintKey)+", " + mapHint(hintValue) + ");" );
        g2D.setRenderingHint( hintKey, hintValue );
    }


    private static String mapHint(Object hint) {
        if (hint == null) {
            return "null";
        }
        if (hint instanceof AffineTransform) {
            return mapTransform((AffineTransform) hint);
        }
        for (int i=0; i<HINTS.length; i+=2) {
            if (hint == HINTS[i]) {
                return (String)HINTS[i+1];
            }
        }
        return "\"" + hint + "\"";
    }


    @Override
    public void setRenderingHints(Map<?,?> hints) {
        String l =
            "setRenderingHints(Map):" +
            "\n  hints = " + hints;
        log.println( l );
        g2D.setRenderingHints( hints );
    }

    @Override
    public void setStroke(Stroke s) {
        String l;
        if (s instanceof BasicStroke) {
            BasicStroke bs = (BasicStroke)s;
            String cap = new String[]{"BUTT","ROUND","SQUARE"}[bs.getEndCap()];
            String join = new String[]{"MITER","ROUND","BEVEL"}[bs.getLineJoin()];
            l = "g.setStroke(new BasicStroke(" + bs.getLineWidth() + "f, BasicStroke.CAP_" + cap + ", BasicStroke.JOIN_" + join + ", " +
                bs.getMiterLimit() + "f, " + Arrays.toString(bs.getDashArray()) + ", " + bs.getDashPhase() + "f));";
        } else {
            l = "g.setStroke(" + s + ");";
        }
        log.println( l );
        g2D.setStroke( s );
    }

    private static String mapTransform(AffineTransform tx) {
        return tx.isIdentity() ? "new AffineTransform()"
            : "new AffineTransform("+tx.getScaleX()+"f,"+tx.getShearY()+"f,"+tx.getShearX()+"f,"+tx.getScaleY()+"f,"+tx.getTranslateX()+"f,"+tx.getTranslateY()+"f)";
    }

    @Override
    public void setTransform(AffineTransform Tx) {
        log.println( "g.setTransform("+mapTransform(Tx)+");" );
        g2D.setTransform( Tx );
    }

    @Override
    public void shear(double shx, double shy) {
        String l =
            "shear(shx, dhy):" +
            "\n  shx = " + shx +
            "\n  shy = " + shy;
        log.println( l );
        g2D.shear( shx, shy );
    }

    @Override
    public void transform(AffineTransform Tx) {
        String l =
            "transform(AffineTransform):" +
            "\n  Tx = " + Tx;
        log.println( l );
        g2D.transform( Tx );
    }

    @Override
    public void translate(double tx, double ty) {
        String l =
            "translate(double, double):" +
            "\n  tx = " + tx +
            "\n  ty = " + ty;
        log.println( l );
        g2D.translate( tx, ty );
    }

    @Override
    public void clearRect(int x, int y, int width, int height) {
        String l =
            "clearRect(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        g2D.clearRect( x, y, width, height );
    }

    @Override
    public void clipRect(int x, int y, int width, int height) {
        String l =
            "clipRect(int, int, int, int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "height = " + height;
        log.println( l );
        g2D.clipRect( x, y, width, height );
    }

    @Override
    public void copyArea(int x, int y, int width, int height, int dx, int dy) {
        String l =
            "copyArea(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        g2D.copyArea( x, y, width, height, dx, dy );
    }

    @Override
    public Graphics create() {
        log.println( "create():" );
        return g2D.create();
    }

    @Override
    public Graphics create(int x, int y, int width, int height) {
        String l =
            "create(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        return g2D.create( x, y, width, height );
    }

    @Override
    public void dispose() {
        log.println( "dispose():" );
        g2D.dispose();
    }

    @Override
    public void draw3DRect(int x, int y, int width, int height, boolean raised) {
        String l =
            "draw3DRect(int,int,int,int,boolean):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height +
            "\n  raised = " + raised;
        log.println( l );
        g2D.draw3DRect( x, y, width, height, raised );
    }

    @Override
    public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
        String l =
            "drawArc(int,int,int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height +
            "\n  startAngle = " + startAngle +
            "\n  arcAngle = " + arcAngle;
        log.println( l );
        g2D.drawArc( x, y, width, height, startAngle, arcAngle );
    }

    @Override
    public void drawBytes(byte[] data, int offset, int length, int x, int y) {
        String l =
            "drawBytes(byte[],int,int,int,int):" +
            "\n  data = " + Arrays.toString(data) +
            "\n  offset = " + offset +
            "\n  length = " + length +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.drawBytes( data, offset, length, x, y );
    }

    @Override
    public void drawChars(char[] data, int offset, int length, int x, int y) {
        String l =
            "drawChars(data,int,int,int,int):" +
            "\n  data = " + Arrays.toString(data) +
            "\n  offset = " + offset +
            "\n  length = " + length +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.drawChars( data, offset, length, x, y );
    }

    @Override
    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) {
        String l =
            "drawImage(Image,int,int,int,int,int,int,int,int,ImageObserver):" +
            "\n  img = " + img +
            "\n  dx1 = " + dx1 +
            "\n  dy1 = " + dy1 +
            "\n  dx2 = " + dx2 +
            "\n  dy2 = " + dy2 +
            "\n  sx1 = " + sx1 +
            "\n  sy1 = " + sy1 +
            "\n  sx2 = " + sx2 +
            "\n  sy2 = " + sy2 +
            "\n  observer = " + observer;
        log.println( l );
        return g2D.drawImage( img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer );
    }

    @Override
    public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer) {
        String l =
            "drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver):" +
            "\n  img = " + img +
            "\n  dx1 = " + dx1 +
            "\n  dy1 = " + dy1 +
            "\n  dx2 = " + dx2 +
            "\n  dy2 = " + dy2 +
            "\n  sx1 = " + sx1 +
            "\n  sy1 = " + sy1 +
            "\n  sx2 = " + sx2 +
            "\n  sy2 = " + sy2 +
            "\n  bgcolor = " + bgcolor +
            "\n  observer = " + observer;
        log.println( l );
        return g2D.drawImage( img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor, observer );
    }

    @Override
    public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
        String l =
            "drawImage(Image,int,int,Color,ImageObserver):" +
            "\n  img = " + img +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  bgcolor = " + bgcolor +
            "\n  observer = " + observer;
        log.println( l );
        return g2D.drawImage( img, x, y, bgcolor, observer );
    }

    @Override
    public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
        String l =
            "drawImage(Image,int,int,observer):" +
            "\n  img = " + img +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  observer = " + observer;
        log.println( l );
        return g2D.drawImage( img, x, y, observer );
    }

    @Override
    public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
        String l =
            "drawImage(Image,int,int,int,int,Color,ImageObserver):" +
            "\n  img = " + img +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height +
            "\n  bgcolor = " + bgcolor +
            "\n  observer = " + observer;
        log.println( l );
        return g2D.drawImage( img, x, y, width, height, bgcolor, observer );
    }

    @Override
    public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
        String l =
            "drawImage(Image,int,int,width,height,observer):" +
            "\n  img = " + img +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height +
            "\n  observer = " + observer;
        log.println( l );
        return g2D.drawImage( img, x, y, width, height, observer );
    }

    @Override
    public void drawLine(int x1, int y1, int x2, int y2) {
        String l =
            "drawLine(int,int,int,int):" +
            "\n  x1 = " + x1 +
            "\n  y1 = " + y1 +
            "\n  x2 = " + x2 +
            "\n  y2 = " + y2;
        log.println( l );
        g2D.drawLine( x1, y1, x2, y2 );
    }

    @Override
    public void drawOval(int x, int y, int width, int height) {
        String l =
            "drawOval(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        g2D.drawOval( x, y, width, height );
    }

    @Override
    public void drawPolygon(Polygon p) {
        String l =
            "drawPolygon(Polygon):" +
            "\n  p = " + p;
        log.println( l );
        g2D.drawPolygon( p );
    }

    @Override
    public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
        String l =
            "drawPolygon(int[],int[],int):" +
            "\n  xPoints = " + Arrays.toString(xPoints) +
            "\n  yPoints = " + Arrays.toString(yPoints) +
            "\n  nPoints = " + nPoints;
        log.println( l );
        g2D.drawPolygon( xPoints, yPoints, nPoints );
    }

    @Override
    public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
        String l =
            "drawPolyline(int[],int[],int):" +
            "\n  xPoints = " + Arrays.toString(xPoints) +
            "\n  yPoints = " + Arrays.toString(yPoints) +
            "\n  nPoints = " + nPoints;
        log.println( l );
        g2D.drawPolyline( xPoints, yPoints, nPoints );
    }

    @Override
    public void drawRect(int x, int y, int width, int height) {
        String l =
            "drawRect(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        g2D.drawRect( x, y, width, height );
    }

    @Override
    public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
        String l =
            "drawRoundRect(int,int,int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height +
            "\n  arcWidth = " + arcWidth +
            "\n  arcHeight = " + arcHeight;
        log.println( l );
        g2D.drawRoundRect( x, y, width, height, arcWidth, arcHeight );
    }

    private static String mapAttribute(Object attr) {
        if (attr == null) {
            return "null";
        }
        if (attr instanceof Font) {
            Font f = (Font)attr;
            final String[] STYLE = { "Font.PLAIN", "Font.BOLD", "Font.ITALIC", "Font.BOLD | Font.ITALIC"  };
            return "new Font(\"" + f.getFamily(Locale.ROOT) + "\"," + STYLE[f.getStyle()] + "," + f.getSize() + ")";
        }
        if (attr instanceof Color) {
            return String.format(Locale.ROOT, "new Color(0x%08X)", ((Color)attr).getRGB());
        }
        for (int i=0; i<ATTRS.length; i+=2) {
            if (attr == ATTRS[i]) {
                return (String)ATTRS[i+1];
            }
        }
        return "\""+ attr +"\"";
    }

    @Override
    public void drawString(AttributedCharacterIterator iterator, float x, float y) {
        final int startIdx = iterator.getIndex();

        final Map<Attribute, Map<Integer,Object>> attMap = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        for (char ch = iterator.current(); ch != CharacterIterator.DONE; ch = iterator.next()) {
            sb.append(ch);
            iterator.getAttributes().forEach((k,v) ->
                 attMap.computeIfAbsent(k, (k2) -> new LinkedHashMap<>()).put(iterator.getIndex(), v)
            );
        }

        String l = "AttributedString as = new AttributedString(\""+sb+"\");\n";
        sb.setLength(0);
        sb.append(l);

        for (Map.Entry<Attribute, Map<Integer,Object>> me : attMap.entrySet()) {
            int startPos = -2, lastPos = -2;
            final Attribute at = me.getKey();

            Object lastObj = null;
            for (Map.Entry<Integer,Object> mo : me.getValue().entrySet()) {
                int pos = mo.getKey();
                Object obj = mo.getValue();
                if (lastPos < pos-1 || obj != lastObj) {
                    if (startPos >= 0) {
                        sb.append("as.addAttribute(").append(mapAttribute(at)).append(",").append(mapAttribute(lastObj)).append(",").append(startPos).append(",").append(lastPos + 1).append(");\n");
                    }
                    startPos = pos;
                }
                lastPos = pos;
                lastObj = obj;
            }
            if (lastObj != null) {
                sb.append("as.addAttribute(").append(mapAttribute(at)).append(",").append(mapAttribute(lastObj)).append(",").append(startPos).append(",").append(lastPos + 1).append(");\n");
            }
        }

        sb.append("g.drawString(as.getIterator(),").append(x).append("f,").append(y).append("f);");
        log.println(sb);

        iterator.setIndex(startIdx);
        g2D.drawString( iterator, x, y );
    }


    @Override
    public void drawString(AttributedCharacterIterator iterator, int x, int y) {
        drawString(iterator, (float)x, (float)y);
    }

    @Override
    public void drawString(String str, int x, int y) {
        String l =
            "drawString(str,int,int):" +
            "\n  str = " + str +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.drawString( str, x, y );
    }

    @Override
    public void fill3DRect(int x, int y, int width, int height, boolean raised) {
        String l =
            "fill3DRect(int,int,int,int,boolean):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height +
            "\n  raised = " + raised;
        log.println( l );
        g2D.fill3DRect( x, y, width, height, raised );
    }

    @Override
    public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
        String l =
            "fillArc(int,int,int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height +
            "\n  startAngle = " + startAngle +
            "\n  arcAngle = " + arcAngle;
        log.println( l );
        g2D.fillArc( x, y, width, height, startAngle, arcAngle );
    }

    @Override
    public void fillOval(int x, int y, int width, int height) {
        String l =
            "fillOval(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        g2D.fillOval( x, y, width, height );
    }

    @Override
    public void fillPolygon(Polygon p) {
        String l =
            "fillPolygon(Polygon):" +
            "\n  p = " + p;
        log.println( l );
        g2D.fillPolygon( p );
    }

    @Override
    public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
        String l =
            "fillPolygon(int[],int[],int):" +
            "\n  xPoints = " + Arrays.toString(xPoints) +
            "\n  yPoints = " + Arrays.toString(yPoints) +
            "\n  nPoints = " + nPoints;
        log.println( l );
        g2D.fillPolygon( xPoints, yPoints, nPoints );
    }

    @Override
    public void fillRect(int x, int y, int width, int height) {
        log.println( "g.fillRect(" + x + "," + y + "," + width + "," + height + ");" );
        g2D.fillRect( x, y, width, height );
    }

    @Override
    public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
        log.println( "fillRoundRect(" + x + "," + y + "," + width + "," + height + "," + arcWidth + "," + arcHeight + ")" );
        g2D.fillRoundRect( x, y, width, height, arcWidth, arcHeight );
    }

    @Override
    public Shape getClip() {
        log.println( "getClip():" );
        return g2D.getClip();
    }

    @Override
    public Rectangle getClipBounds() {
        log.println( "getClipBounds():" );
        return g2D.getClipBounds();
    }

    @Override
    public Rectangle getClipBounds(Rectangle r) {
        String l =
            "getClipBounds(Rectangle):" +
            "\n  r = " + r;
        log.println( l );
        return g2D.getClipBounds( r );
    }

    @Override
    public Color getColor() {
        log.println( "getColor():" );
        return g2D.getColor();
    }

    @Override
    public Font getFont() {
        log.println( "getFont():" );
        return g2D.getFont();
    }

    @Override
    public FontMetrics getFontMetrics() {
        log.println( "getFontMetrics():" );
        return g2D.getFontMetrics();
    }

    @Override
    public FontMetrics getFontMetrics(Font f) {
        log.println( "getFontMetrics():" );
        return g2D.getFontMetrics( f );
    }

    @Override
    public boolean hitClip(int x, int y, int width, int height) {
        String l =
            "hitClip(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        return g2D.hitClip( x, y, width, height );
    }

    @Override
    public void setClip(Shape clip) {
        String l =
            "setClip(Shape):" +
            "\n  clip = " + clip;
        log.println( l );
        g2D.setClip( clip );
    }

    @Override
    public void setClip(int x, int y, int width, int height) {
        String l =
            "setClip(int,int,int,int):" +
            "\n  x = " + x +
            "\n  y = " + y +
            "\n  width = " + width +
            "\n  height = " + height;
        log.println( l );
        g2D.setClip( x, y, width, height );
    }

    @Override
    public void setColor(Color c) {
        log.printf(Locale.ROOT, "g.setColor(new Color(0x%08X));%n", c.getRGB());
        g2D.setColor( c );
    }

    @Override
    public void setFont(Font font) {
        String l =
            "setFont(Font):" +
            "\n  font = " + font;
        log.println( l );
        g2D.setFont( font );
    }

    @Override
    public void setPaintMode() {
        log.println( "setPaintMode():" );
        g2D.setPaintMode();
    }

    @Override
    public void setXORMode(Color c1) {
        String l =
            "setXORMode(Color):" +
            "\n  c1 = " + c1;
        log.println( l );
        g2D.setXORMode( c1 );
    }

    public String toString() {
        log.println( "toString():" );
        return g2D.toString();
    }

    @Override
    public void translate(int x, int y) {
        String l =
            "translate(int,int):" +
            "\n  x = " + x +
            "\n  y = " + y;
        log.println( l );
        g2D.translate( x, y );
    }


    private static final Object[] HINTS = {
        RenderingHints.KEY_ANTIALIASING, "RenderingHints.KEY_ANTIALIASING",
        RenderingHints.VALUE_ANTIALIAS_ON, "RenderingHints.VALUE_ANTIALIAS_ON",
        RenderingHints.VALUE_ANTIALIAS_OFF, "RenderingHints.VALUE_ANTIALIAS_OFF",
        RenderingHints.VALUE_ANTIALIAS_DEFAULT, "RenderingHints.VALUE_ANTIALIAS_DEFAULT",
        RenderingHints.KEY_RENDERING, "RenderingHints.KEY_RENDERING",
        RenderingHints.VALUE_RENDER_SPEED, "RenderingHints.VALUE_RENDER_SPEED",
        RenderingHints.VALUE_RENDER_QUALITY, "RenderingHints.VALUE_RENDER_QUALITY",
        RenderingHints.VALUE_RENDER_DEFAULT, "RenderingHints.VALUE_RENDER_DEFAULT",
        RenderingHints.KEY_DITHERING, "RenderingHints.KEY_DITHERING",
        RenderingHints.VALUE_DITHER_DISABLE, "RenderingHints.VALUE_DITHER_DISABLE",
        RenderingHints.VALUE_DITHER_ENABLE, "RenderingHints.VALUE_DITHER_ENABLE",
        RenderingHints.VALUE_DITHER_DEFAULT, "RenderingHints.VALUE_DITHER_DEFAULT",
        RenderingHints.KEY_TEXT_ANTIALIASING, "RenderingHints.KEY_TEXT_ANTIALIASING",
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON, "RenderingHints.VALUE_TEXT_ANTIALIAS_ON",
        RenderingHints.VALUE_TEXT_ANTIALIAS_OFF, "RenderingHints.VALUE_TEXT_ANTIALIAS_OFF",
        RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT, "RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT",
        RenderingHints.VALUE_TEXT_ANTIALIAS_GASP, "RenderingHints.VALUE_TEXT_ANTIALIAS_GASP",
        RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB, "RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB",
        RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR, "RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR",
        RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB, "RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB",
        RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR, "RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR",
        RenderingHints.KEY_TEXT_LCD_CONTRAST, "RenderingHints.KEY_TEXT_LCD_CONTRAST",
        RenderingHints.KEY_FRACTIONALMETRICS, "RenderingHints.KEY_FRACTIONALMETRICS",
        RenderingHints.VALUE_FRACTIONALMETRICS_OFF, "RenderingHints.VALUE_FRACTIONALMETRICS_OFF",
        RenderingHints.VALUE_FRACTIONALMETRICS_ON, "RenderingHints.VALUE_FRACTIONALMETRICS_ON",
        RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT, "RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT",
        RenderingHints.KEY_INTERPOLATION, "RenderingHints.KEY_INTERPOLATION",
        RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR, "RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR",
        RenderingHints.VALUE_INTERPOLATION_BILINEAR, "RenderingHints.VALUE_INTERPOLATION_BILINEAR",
        RenderingHints.VALUE_INTERPOLATION_BICUBIC, "RenderingHints.VALUE_INTERPOLATION_BICUBIC",
        RenderingHints.KEY_ALPHA_INTERPOLATION, "RenderingHints.KEY_ALPHA_INTERPOLATION",
        RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED, "RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED",
        RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY, "RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY",
        RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT, "RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT",
        RenderingHints.KEY_COLOR_RENDERING, "RenderingHints.KEY_COLOR_RENDERING",
        RenderingHints.VALUE_COLOR_RENDER_SPEED, "RenderingHints.VALUE_COLOR_RENDER_SPEED",
        RenderingHints.VALUE_COLOR_RENDER_QUALITY, "RenderingHints.VALUE_COLOR_RENDER_QUALITY",
        RenderingHints.VALUE_COLOR_RENDER_DEFAULT, "RenderingHints.VALUE_COLOR_RENDER_DEFAULT",
        RenderingHints.KEY_STROKE_CONTROL, "RenderingHints.KEY_STROKE_CONTROL",
        RenderingHints.VALUE_STROKE_DEFAULT, "RenderingHints.VALUE_STROKE_DEFAULT",
        RenderingHints.VALUE_STROKE_NORMALIZE, "RenderingHints.VALUE_STROKE_NORMALIZE",
        RenderingHints.VALUE_STROKE_PURE, "RenderingHints.VALUE_STROKE_PURE"
    };

    private static final Object[] ATTRS = {
        TextAttribute.FAMILY, "TextAttribute.FAMILY",
        TextAttribute.WEIGHT, "TextAttribute.WEIGHT",
        TextAttribute.WEIGHT_EXTRA_LIGHT, "TextAttribute.WEIGHT_EXTRA_LIGHT",
        TextAttribute.WEIGHT_LIGHT, "TextAttribute.WEIGHT_LIGHT",
        TextAttribute.WEIGHT_DEMILIGHT, "TextAttribute.WEIGHT_DEMILIGHT",
        TextAttribute.WEIGHT_REGULAR, "TextAttribute.WEIGHT_REGULAR",
        TextAttribute.WEIGHT_SEMIBOLD, "TextAttribute.WEIGHT_SEMIBOLD",
        TextAttribute.WEIGHT_MEDIUM, "TextAttribute.WEIGHT_MEDIUM",
        TextAttribute.WEIGHT_DEMIBOLD, "TextAttribute.WEIGHT_DEMIBOLD",
        TextAttribute.WEIGHT_BOLD, "TextAttribute.WEIGHT_BOLD",
        TextAttribute.WEIGHT_HEAVY, "TextAttribute.WEIGHT_HEAVY",
        TextAttribute.WEIGHT_EXTRABOLD, "TextAttribute.WEIGHT_EXTRABOLD",
        TextAttribute.WEIGHT_ULTRABOLD, "TextAttribute.WEIGHT_ULTRABOLD",
        TextAttribute.WIDTH, "TextAttribute.WIDTH",
        TextAttribute.WIDTH_CONDENSED, "TextAttribute.WIDTH_CONDENSED",
        TextAttribute.WIDTH_SEMI_CONDENSED, "TextAttribute.WIDTH_SEMI_CONDENSED",
        TextAttribute.WIDTH_REGULAR, "TextAttribute.WIDTH_REGULAR",
        TextAttribute.WIDTH_SEMI_EXTENDED, "TextAttribute.WIDTH_SEMI_EXTENDED",
        TextAttribute.WIDTH_EXTENDED, "TextAttribute.WIDTH_EXTENDED",
        TextAttribute.POSTURE, "TextAttribute.POSTURE",
        TextAttribute.POSTURE_REGULAR, "TextAttribute.POSTURE_REGULAR",
        TextAttribute.POSTURE_OBLIQUE, "TextAttribute.POSTURE_OBLIQUE",
        TextAttribute.SIZE, "TextAttribute.SIZE",
        TextAttribute.TRANSFORM, "TextAttribute.TRANSFORM",
        TextAttribute.SUPERSCRIPT, "TextAttribute.SUPERSCRIPT",
        TextAttribute.SUPERSCRIPT_SUPER, "TextAttribute.SUPERSCRIPT_SUPER",
        TextAttribute.SUPERSCRIPT_SUB, "TextAttribute.SUPERSCRIPT_SUB",
        TextAttribute.FONT, "TextAttribute.FONT",
        TextAttribute.CHAR_REPLACEMENT, "TextAttribute.CHAR_REPLACEMENT",
        TextAttribute.FOREGROUND, "TextAttribute.FOREGROUND",
        TextAttribute.BACKGROUND, "TextAttribute.BACKGROUND",
        TextAttribute.UNDERLINE, "TextAttribute.UNDERLINE",
        TextAttribute.UNDERLINE_ON, "TextAttribute.UNDERLINE_ON",
        TextAttribute.STRIKETHROUGH, "TextAttribute.STRIKETHROUGH",
        TextAttribute.STRIKETHROUGH_ON, "TextAttribute.STRIKETHROUGH_ON",
        TextAttribute.RUN_DIRECTION, "TextAttribute.RUN_DIRECTION",
        TextAttribute.RUN_DIRECTION_LTR, "TextAttribute.RUN_DIRECTION_LTR",
        TextAttribute.RUN_DIRECTION_RTL, "TextAttribute.RUN_DIRECTION_RTL",
        TextAttribute.BIDI_EMBEDDING, "TextAttribute.BIDI_EMBEDDING",
        TextAttribute.JUSTIFICATION, "TextAttribute.JUSTIFICATION",
        TextAttribute.JUSTIFICATION_FULL, "TextAttribute.JUSTIFICATION_FULL",
        TextAttribute.JUSTIFICATION_NONE, "TextAttribute.JUSTIFICATION_NONE",
        TextAttribute.INPUT_METHOD_HIGHLIGHT, "TextAttribute.INPUT_METHOD_HIGHLIGHT",
        TextAttribute.INPUT_METHOD_UNDERLINE, "TextAttribute.INPUT_METHOD_UNDERLINE",
        TextAttribute.UNDERLINE_LOW_ONE_PIXEL, "TextAttribute.UNDERLINE_LOW_ONE_PIXEL",
        TextAttribute.UNDERLINE_LOW_TWO_PIXEL, "TextAttribute.UNDERLINE_LOW_TWO_PIXEL",
        TextAttribute.UNDERLINE_LOW_DOTTED, "TextAttribute.UNDERLINE_LOW_DOTTED",
        TextAttribute.UNDERLINE_LOW_GRAY, "TextAttribute.UNDERLINE_LOW_GRAY",
        TextAttribute.UNDERLINE_LOW_DASHED, "TextAttribute.UNDERLINE_LOW_DASHED",
        TextAttribute.SWAP_COLORS, "TextAttribute.SWAP_COLORS",
        TextAttribute.SWAP_COLORS_ON, "TextAttribute.SWAP_COLORS_ON",
        TextAttribute.NUMERIC_SHAPING, "TextAttribute.NUMERIC_SHAPING",
        TextAttribute.KERNING, "TextAttribute.KERNING",
        TextAttribute.KERNING_ON, "TextAttribute.KERNING_ON",
        TextAttribute.LIGATURES, "TextAttribute.LIGATURES",
        TextAttribute.LIGATURES_ON, "TextAttribute.LIGATURES_ON",
        TextAttribute.TRACKING, "TextAttribute.TRACKING",
        TextAttribute.TRACKING_TIGHT, "TextAttribute.TRACKING_TIGHT",
        TextAttribute.TRACKING_LOOSE, "TextAttribute.TRACKING_LOOSE"
    };
}