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
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
|
/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed 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.
*/
/* $Id$ */
package org.apache.fop.render.ps;
//Java
import java.text.AttributedCharacterIterator;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
/* java.awt.Font is not imported to avoid confusion with
org.apache.fop.fonts.Font */
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.TexturePaint;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.ImageObserver;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.RenderableImage;
import java.io.IOException;
//Batik
import org.apache.batik.ext.awt.g2d.AbstractGraphics2D;
import org.apache.batik.ext.awt.g2d.GraphicContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//FOP
import org.apache.fop.fonts.Font;
import org.apache.fop.image.FopImage;
import org.apache.fop.apps.Document;
/**
* This concrete implementation of <tt>AbstractGraphics2D</tt> is a
* simple help to programmers to get started with their own
* implementation of <tt>Graphics2D</tt>.
* <tt>DefaultGraphics2D</tt> implements all the abstract methods
* is <tt>AbstractGraphics2D</tt> and makes it easy to start
* implementing a <tt>Graphic2D</tt> piece-meal.
*
* @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
* @version $Id: PSGraphics2D.java,v 1.11 2003/03/11 08:42:24 jeremias Exp $
* @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D
*/
public class PSGraphics2D extends AbstractGraphics2D {
/** the logger for this class */
protected Log log = LogFactory.getLog(PSTextPainter.class);
/** the PostScript generator being created */
protected PSGenerator gen;
private boolean clippingDisabled = true;
/** Currently valid FontState */
protected Font font;
/** Overriding FontState */
protected Font overrideFont = null;
/** the current (internal) font name */
protected String currentFontName;
/** the current font size in millipoints */
protected int currentFontSize;
/**
* the current colour for use in svg
*/
protected Color currentColour = new Color(0, 0, 0);
/** FontInfo containing all available fonts */
protected Document document;
/**
* Create a new Graphics2D that generates PostScript code.
* @param textAsShapes True if text should be rendered as graphics
* @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
*/
public PSGraphics2D(boolean textAsShapes) {
super(textAsShapes);
}
/**
* Create a new Graphics2D that generates PostScript code.
* @param textAsShapes True if text should be rendered as graphics
* @param gen PostScript generator to use for output
* @see org.apache.batik.ext.awt.g2d.AbstractGraphics2D#AbstractGraphics2D(boolean)
*/
public PSGraphics2D(boolean textAsShapes, PSGenerator gen) {
this(textAsShapes);
setPSGenerator(gen);
}
/**
* Constructor for creating copies
* @param g parent PostScript Graphics2D
*/
public PSGraphics2D(PSGraphics2D g) {
super(g);
}
/**
* Sets the PostScript generator
* @param gen the PostScript generator
*/
public void setPSGenerator(PSGenerator gen) {
this.gen = gen;
}
/**
* Sets the GraphicContext
* @param c GraphicContext to use
*/
public void setGraphicContext(GraphicContext c) {
gc = c;
}
/**
* Creates a new <code>Graphics</code> object that is
* a copy of this <code>Graphics</code> object.
* @return a new graphics context that is a copy of
* this graphics context.
*/
public Graphics create() {
return new PSGraphics2D(this);
}
/**
* Central handler for IOExceptions for this class.
* @param ioe IOException to handle
*/
protected void handleIOException(IOException ioe) {
ioe.printStackTrace();
}
/**
* This method is used by AbstractPSDocumentGraphics2D to prepare a new page if
* necessary.
*/
protected void preparePainting() {
//nop, used by AbstractPSDocumentGraphics2D
}
/**
* Draws as much of the specified image as is currently available.
* The image is drawn with its top-left corner at
* (<i>x</i>, <i>y</i>) in this graphics context's coordinate
* space. Transparent pixels in the image do not affect whatever
* pixels are already there.
* <p>
* This method returns immediately in all cases, even if the
* complete image has not yet been loaded, and it has not been dithered
* and converted for the current output device.
* <p>
* If the image has not yet been completely loaded, then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the specified image observer.
* @param img the specified image to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param observer object to be notified as more of
* the image is converted.
* @return True if the image has been fully drawn/loaded
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
*/
public boolean drawImage(Image img, int x, int y,
ImageObserver observer) {
preparePainting();
log.debug("drawImage: " + x + ", " + y + " " + img.getClass().getName());
final int width = img.getWidth(observer);
final int height = img.getHeight(observer);
if (width == -1 || height == -1) {
return false;
}
Dimension size = new Dimension(width, height);
BufferedImage buf = buildBufferedImage(size);
java.awt.Graphics2D g = buf.createGraphics();
g.setComposite(AlphaComposite.SrcOver);
g.setBackground(new Color(1, 1, 1, 0));
g.setPaint(new Color(1, 1, 1, 0));
g.fillRect(0, 0, width, height);
g.clip(new Rectangle(0, 0, buf.getWidth(), buf.getHeight()));
if (!g.drawImage(img, 0, 0, observer)) {
return false;
}
g.dispose();
final byte[] result = new byte[buf.getWidth() * buf.getHeight() * 3];
//final byte[] mask = new byte[buf.getWidth() * buf.getHeight()];
Raster raster = buf.getData();
DataBuffer bd = raster.getDataBuffer();
int count = 0;
//int maskpos = 0;
switch (bd.getDataType()) {
case DataBuffer.TYPE_INT:
int[][] idata = ((DataBufferInt)bd).getBankData();
for (int i = 0; i < idata.length; i++) {
for (int j = 0; j < idata[i].length; j++) {
// mask[maskpos++] = (byte)((idata[i][j] >> 24) & 0xFF);
if (((idata[i][j] >> 24) & 0xFF) != 255) {
result[count++] = (byte)0xFF;
result[count++] = (byte)0xFF;
result[count++] = (byte)0xFF;
} else {
result[count++] = (byte)((idata[i][j] >> 16) & 0xFF);
result[count++] = (byte)((idata[i][j] >> 8) & 0xFF);
result[count++] = (byte)((idata[i][j]) & 0xFF);
}
}
}
break;
default:
// error
break;
}
try {
FopImage fopimg = new TempImage(width, height, result, null);
AffineTransform at = getTransform();
gen.saveGraphicsState();
Shape imclip = getClip();
writeClip(imclip);
gen.concatMatrix(at);
PSImageUtils.renderFopImage(fopimg,
1000 * x, 1000 * y, 1000 * width, 1000 * height, gen);
gen.restoreGraphicsState();
} catch (IOException ioe) {
handleIOException(ioe);
}
return true;
}
/**
* Creates a buffered image.
* @param size dimensions of the image to be created
* @return the buffered image
*/
public BufferedImage buildBufferedImage(Dimension size) {
return new BufferedImage(size.width, size.height,
BufferedImage.TYPE_INT_ARGB);
}
class TempImage implements FopImage {
private int height;
private int width;
private int bitsPerPixel;
private ColorSpace colorSpace;
private int bitmapSiye;
private byte[] bitmaps;
private byte[] mask;
private Color transparentColor;
TempImage(int width, int height, byte[] bitmaps,
byte[] mask) {
this.height = height;
this.width = width;
this.bitsPerPixel = 8;
this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
this.bitmaps = bitmaps;
this.mask = mask;
}
public String getMimeType() {
return "application/octet-stream";
}
/**
* @see org.apache.fop.image.FopImage#load(int, org.apache.commons.logging.Log)
*/
public boolean load(int type, Log logger) {
switch (type) {
case FopImage.DIMENSIONS: break;
case FopImage.BITMAP: break;
case FopImage.ORIGINAL_DATA: break;
default: throw new RuntimeException("Unknown load type: " + type);
}
return true;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public ColorSpace getColorSpace() {
return this.colorSpace;
}
public ICC_Profile getICCProfile() {
return null;
}
public int getBitsPerPixel() {
return this.bitsPerPixel;
}
// For transparent images
public boolean isTransparent() {
return getTransparentColor() != null;
}
public Color getTransparentColor() {
return this.transparentColor;
}
public boolean hasSoftMask() {
return this.mask != null;
}
public byte[] getSoftMask() {
return this.mask;
}
public byte[] getBitmaps() {
return this.bitmaps;
}
// width * (bitsPerPixel / 8) * height, no ?
public int getBitmapsSize() {
return getWidth() * getHeight() * 3; //Assumes RGB!
}
// get compressed image bytes
// I don't know if we really need it, nor if it
// should be changed...
public byte[] getRessourceBytes() {
return null;
}
public int getRessourceBytesSize() {
return 0;
}
}
/**
* Draws as much of the specified image as has already been scaled
* to fit inside the specified rectangle.
* <p>
* The image is drawn inside the specified rectangle of this
* graphics context's coordinate space, and is scaled if
* necessary. Transparent pixels do not affect whatever pixels
* are already there.
* <p>
* This method returns immediately in all cases, even if the
* entire image has not yet been scaled, dithered, and converted
* for the current output device.
* If the current output representation is not yet complete, then
* <code>drawImage</code> returns <code>false</code>. As more of
* the image becomes available, the process that draws the image notifies
* the image observer by calling its <code>imageUpdate</code> method.
* <p>
* A scaled version of an image will not necessarily be
* available immediately just because an unscaled version of the
* image has been constructed for this output device. Each size of
* the image may be cached separately and generated from the original
* data in a separate image production sequence.
* @param img the specified image to be drawn.
* @param x the <i>x</i> coordinate.
* @param y the <i>y</i> coordinate.
* @param width the width of the rectangle.
* @param height the height of the rectangle.
* @param observer object to be notified as more of
* the image is converted.
* @return True if the image has been fully loaded/drawn
* @see java.awt.Image
* @see java.awt.image.ImageObserver
* @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int)
*/
public boolean drawImage(Image img, int x, int y, int width, int height,
ImageObserver observer) {
preparePainting();
log.warn("NYI: drawImage");
return true;
}
/**
* Disposes of this graphics context and releases
* any system resources that it is using.
* A <code>Graphics</code> object cannot be used after
* <code>dispose</code>has been called.
* <p>
* When a Java program runs, a large number of <code>Graphics</code>
* objects can be created within a short time frame.
* Although the finalization process of the garbage collector
* also disposes of the same system resources, it is preferable
* to manually free the associated resources by calling this
* method rather than to rely on a finalization process which
* may not run to completion for a long period of time.
* <p>
* Graphics objects which are provided as arguments to the
* <code>paint</code> and <code>update</code> methods
* of components are automatically released by the system when
* those methods return. For efficiency, programmers should
* call <code>dispose</code> when finished using
* a <code>Graphics</code> object only if it was created
* directly from a component or another <code>Graphics</code> object.
* @see java.awt.Graphics#finalize
* @see java.awt.Component#paint
* @see java.awt.Component#update
* @see java.awt.Component#getGraphics
* @see java.awt.Graphics#create
*/
public void dispose() {
this.gen = null;
this.font = null;
this.currentColour = null;
this.document = null;
}
/**
* Processes a path iterator generating the nexessary painting operations.
* @param iter PathIterator to process
* @throws IOException In case of an I/O problem.
*/
public void processPathIterator(PathIterator iter) throws IOException {
double[] vals = new double[6];
while (!iter.isDone()) {
int type = iter.currentSegment(vals);
switch (type) {
case PathIterator.SEG_CUBICTO:
gen.writeln(gen.formatDouble(1000 * vals[0]) + " "
+ gen.formatDouble(1000 * vals[1]) + " "
+ gen.formatDouble(1000 * vals[2]) + " "
+ gen.formatDouble(1000 * vals[3]) + " "
+ gen.formatDouble(1000 * vals[4]) + " "
+ gen.formatDouble(1000 * vals[5])
+ " curveto");
break;
case PathIterator.SEG_LINETO:
gen.writeln(gen.formatDouble(1000 * vals[0]) + " "
+ gen.formatDouble(1000 * vals[1])
+ " lineto");
break;
case PathIterator.SEG_MOVETO:
gen.writeln(gen.formatDouble(1000 * vals[0]) + " "
+ gen.formatDouble(1000 * vals[1])
+ " M");
break;
case PathIterator.SEG_QUADTO:
gen.writeln(gen.formatDouble(1000 * vals[0]) + " "
+ gen.formatDouble(1000 * vals[1]) + " "
+ gen.formatDouble(1000 * vals[2]) + " "
+ gen.formatDouble(1000 * vals[3]) + " QUADTO ");
break;
case PathIterator.SEG_CLOSE:
gen.writeln("closepath");
break;
default:
break;
}
iter.next();
}
}
/**
* Strokes the outline of a <code>Shape</code> using the settings of the
* current <code>Graphics2D</code> context. The rendering attributes
* applied include the <code>Clip</code>, <code>Transform</code>,
* <code>Paint</code>, <code>Composite</code> and
* <code>Stroke</code> attributes.
* @param s the <code>Shape</code> to be rendered
* @see #setStroke
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see #transform
* @see #setTransform
* @see #clip
* @see #setClip
* @see #setComposite
*/
public void draw(Shape s) {
preparePainting();
try {
gen.saveGraphicsState();
Shape imclip = getClip();
writeClip(imclip);
establishColor(getColor());
applyPaint(getPaint(), false);
applyStroke(getStroke());
gen.writeln("newpath");
PathIterator iter = s.getPathIterator(getTransform());
processPathIterator(iter);
doDrawing(false, true, false);
gen.restoreGraphicsState();
} catch (IOException ioe) {
handleIOException(ioe);
}
}
/**
* Establishes a clipping region
* @param s Shape defining the clipping region
*/
protected void writeClip(Shape s) {
if (s == null) {
return;
}
if (!this.clippingDisabled) {
preparePainting();
try {
gen.writeln("newpath");
PathIterator iter = s.getPathIterator(getTransform());
processPathIterator(iter);
// clip area
gen.writeln("clippath");
} catch (IOException ioe) {
handleIOException(ioe);
}
}
}
/**
* Applies a new Paint object.
* @param paint Paint object to use
* @param fill True if to be applied for filling
*/
protected void applyPaint(Paint paint, boolean fill) {
preparePainting();
if (paint instanceof GradientPaint) {
log.warn("NYI: Gradient paint");
} else if (paint instanceof TexturePaint) {
log.warn("NYI: texture paint");
}
}
/**
* Applies a new Stroke object.
* @param stroke Stroke object to use
*/
protected void applyStroke(Stroke stroke) {
preparePainting();
try {
if (stroke instanceof BasicStroke) {
BasicStroke bs = (BasicStroke)stroke;
float[] da = bs.getDashArray();
if (da != null) {
gen.write("[");
for (int count = 0; count < da.length; count++) {
gen.write("" + (1000 * (int)da[count]));
if (count < da.length - 1) {
gen.write(" ");
}
}
gen.write("] ");
float offset = bs.getDashPhase();
gen.writeln((1000 * (int)offset) + " setdash");
}
int ec = bs.getEndCap();
switch (ec) {
case BasicStroke.CAP_BUTT:
gen.writeln("0 setlinecap");
break;
case BasicStroke.CAP_ROUND:
gen.writeln("1 setlinecap");
break;
case BasicStroke.CAP_SQUARE:
gen.writeln("2 setlinecap");
break;
default: log.warn("Unsupported line cap: " + ec);
}
int lj = bs.getLineJoin();
switch (lj) {
case BasicStroke.JOIN_MITER:
gen.writeln("0 setlinejoin");
break;
case BasicStroke.JOIN_ROUND:
gen.writeln("1 setlinejoin");
break;
case BasicStroke.JOIN_BEVEL:
gen.writeln("2 setlinejoin");
break;
default: log.warn("Unsupported line join: " + lj);
}
float lw = bs.getLineWidth();
gen.writeln(gen.formatDouble(1000 * lw) + " setlinewidth");
float ml = bs.getMiterLimit();
gen.writeln(gen.formatDouble(1000 * ml) + " setmiterlimit");
}
} catch (IOException ioe) {
handleIOException(ioe);
}
}
/**
* Renders a {@link RenderedImage},
* applying a transform from image
* space into user space before drawing.
* The transformation from user space into device space is done with
* the current <code>Transform</code> in the <code>Graphics2D</code>.
* The specified transformation is applied to the image before the
* transform attribute in the <code>Graphics2D</code> context is applied.
* The rendering attributes applied include the <code>Clip</code>,
* <code>Transform</code>, and <code>Composite</code> attributes. Note
* that no rendering is done if the specified transform is
* noninvertible.
* @param img the image to be rendered
* @param xform the transformation from image space into user space
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
preparePainting();
log.warn("NYI: drawRenderedImage");
}
/**
* Renders a
* {@link RenderableImage},
* applying a transform from image space into user space before drawing.
* The transformation from user space into device space is done with
* the current <code>Transform</code> in the <code>Graphics2D</code>.
* The specified transformation is applied to the image before the
* transform attribute in the <code>Graphics2D</code> context is applied.
* The rendering attributes applied include the <code>Clip</code>,
* <code>Transform</code>, and <code>Composite</code> attributes. Note
* that no rendering is done if the specified transform is
* noninvertible.
* <p>
* Rendering hints set on the <code>Graphics2D</code> object might
* be used in rendering the <code>RenderableImage</code>.
* If explicit control is required over specific hints recognized by a
* specific <code>RenderableImage</code>, or if knowledge of which hints
* are used is required, then a <code>RenderedImage</code> should be
* obtained directly from the <code>RenderableImage</code>
* and rendered using
* {@link #drawRenderedImage(RenderedImage, AffineTransform) drawRenderedImage}.
* @param img the image to be rendered
* @param xform the transformation from image space into user space
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
* @see #drawRenderedImage
*/
public void drawRenderableImage(RenderableImage img,
AffineTransform xform) {
preparePainting();
log.warn("NYI: drawRenderableImage");
}
/**
* Establishes the given color in the PostScript interpreter.
* @param c the color to set
* @throws IOException In case of an I/O problem
*/
protected void establishColor(Color c) throws IOException {
StringBuffer p = new StringBuffer();
float[] comps = c.getColorComponents(null);
if (c.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
// according to pdfspec 12.1 p.399
// if the colors are the same then just use the g or G operator
boolean same = (comps[0] == comps[1]
&& comps[0] == comps[2]);
// output RGB
if (same) {
p.append(gen.formatDouble(comps[0]));
} else {
for (int i = 0; i < c.getColorSpace().getNumComponents(); i++) {
if (i > 0) {
p.append(" ");
}
p.append(gen.formatDouble(comps[i]));
}
}
if (same) {
p.append(" setgray");
} else {
p.append(" setrgbcolor");
}
} else if (c.getColorSpace().getType() == ColorSpace.TYPE_CMYK) {
// colorspace is CMYK
for (int i = 0; i < c.getColorSpace().getNumComponents(); i++) {
if (i > 0) {
p.append(" ");
}
p.append(gen.formatDouble(comps[i]));
}
p.append(" setcmykcolor");
} else {
// means we're in DeviceGray or Unknown.
// assume we're in DeviceGray, because otherwise we're screwed.
p.append(gen.formatDouble(comps[0]));
p.append(" setgray");
}
gen.writeln(p.toString());
}
/**
* Renders the text specified by the specified <code>String</code>,
* using the current <code>Font</code> and <code>Paint</code> attributes
* in the <code>Graphics2D</code> context.
* The baseline of the first character is at position
* (<i>x</i>, <i>y</i>) in the User Space.
* The rendering attributes applied include the <code>Clip</code>,
* <code>Transform</code>, <code>Paint</code>, <code>Font</code> and
* <code>Composite</code> attributes. For characters in script systems
* such as Hebrew and Arabic, the glyphs can be rendered from right to
* left, in which case the coordinate supplied is the location of the
* leftmost character on the baseline.
* @param s the <code>String</code> to be rendered
* @param x the x-coordinate where the <code>String</code>
* should be rendered
* @param y the y-coordinate where the <code>String</code>
* should be rendered
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see java.awt.Graphics#setFont
* @see #setTransform
* @see #setComposite
* @see #setClip
*/
public void drawString(String s, float x, float y) {
if (this.textAsShapes) {
drawStringAsShapes(s, x, y);
} else {
drawStringAsText(s, x, y);
}
}
/**
* Draw a string to the PostScript document. The text is painted as shapes.
* @param s the string to draw
* @param x the x position
* @param y the y position
*/
public void drawStringAsShapes(String s, float x, float y) {
java.awt.Font awtFont = super.getFont();
FontRenderContext frc = super.getFontRenderContext();
GlyphVector gv = awtFont.createGlyphVector(frc, s);
Shape glyphOutline = gv.getOutline(x, y);
fill(glyphOutline);
}
/**
* Draw a string to the PostScript document. The text is painted using
* text operations.
* @param s the string to draw
* @param x the x position
* @param y the y position
*/
public void drawStringAsText(String s, float x, float y) {
preparePainting();
log.trace("drawString('" + s + "', " + x + ", " + y + ")");
try {
if (this.overrideFont == null) {
java.awt.Font awtFont = getFont();
this.font = createFont(awtFont);
} else {
this.font = this.overrideFont;
this.overrideFont = null;
}
//Color and Font state
establishColor(getColor());
establishCurrentFont();
//Clip
Shape imclip = getClip();
writeClip(imclip);
gen.saveGraphicsState();
//Prepare correct transformation
AffineTransform trans = getTransform();
gen.writeln("[" + toArray(trans) + "] concat");
gen.writeln(gen.formatDouble(1000 * x) + " "
+ gen.formatDouble(1000 * y) + " moveto ");
gen.writeln("1 -1 scale");
StringBuffer sb = new StringBuffer("(");
escapeText(s, sb);
sb.append(") t ");
gen.writeln(sb.toString());
gen.restoreGraphicsState();
} catch (IOException ioe) {
handleIOException(ioe);
}
}
/**
* Converts an AffineTransform to a value array.
* @param at AffineTransform to convert
* @return a String (array of six space-separated values)
*/
protected String toArray(AffineTransform at) {
final double[] vals = new double[6];
at.getMatrix(vals);
return gen.formatDouble5(vals[0]) + " "
+ gen.formatDouble5(vals[1]) + " "
+ gen.formatDouble5(vals[2]) + " "
+ gen.formatDouble5(vals[3]) + " "
+ gen.formatDouble(1000 * vals[4]) + " "
+ gen.formatDouble(1000 * vals[5]);
}
private void escapeText(final String text, StringBuffer target) {
final int l = text.length();
for (int i = 0; i < l; i++) {
final char ch = text.charAt(i);
final char mch = this.font.mapChar(ch);
PSGenerator.escapeChar(mch, target);
}
}
private Font createFont(java.awt.Font f) {
String fontFamily = f.getFamily();
if (fontFamily.equals("sanserif")) {
fontFamily = "sans-serif";
}
int fontSize = 1000 * f.getSize();
String style = f.isItalic() ? "italic" : "normal";
int weight = f.isBold() ? Font.BOLD : Font.NORMAL;
String fontKey = this.document.getFontInfo().findAdjustWeight(fontFamily, style, weight);
if (fontKey == null) {
fontKey = this.document.getFontInfo().findAdjustWeight("sans-serif", style, weight);
}
return new Font(fontKey,
this.document.getFontInfo().getMetricsFor(fontKey),
fontSize);
}
private void establishCurrentFont() throws IOException {
if ((currentFontName != this.font.getFontName())
|| (currentFontSize != this.font.getFontSize())) {
gen.writeln(this.font.getFontName() + " " + this.font.getFontSize() + " F");
currentFontName = this.font.getFontName();
currentFontSize = this.font.getFontSize();
}
}
/**
* Renders the text of the specified iterator, using the
* <code>Graphics2D</code> context's current <code>Paint</code>. The
* iterator must specify a font
* for each character. The baseline of the
* first character is at position (<i>x</i>, <i>y</i>) in the
* User Space.
* The rendering attributes applied include the <code>Clip</code>,
* <code>Transform</code>, <code>Paint</code>, and
* <code>Composite</code> attributes.
* For characters in script systems such as Hebrew and Arabic,
* the glyphs can be rendered from right to left, in which case the
* coordinate supplied is the location of the leftmost character
* on the baseline.
* @param iterator the iterator whose text is to be rendered
* @param x the x-coordinate where the iterator's text is to be
* rendered
* @param y the y-coordinate where the iterator's text is to be
* rendered
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see #setTransform
* @see #setComposite
* @see #setClip
*/
public void drawString(AttributedCharacterIterator iterator, float x,
float y) {
preparePainting();
log.warn("NYI: drawString(AttributedCharacterIterator)");
/*
try {
gen.writeln("BT");
Shape imclip = getClip();
writeClip(imclip);
establishColor(getColor());
AffineTransform trans = getTransform();
trans.translate(x, y);
double[] vals = new double[6];
trans.getMatrix(vals);
for (char ch = iterator.first(); ch != CharacterIterator.DONE;
ch = iterator.next()) {
//Map attr = iterator.getAttributes();
gen.writeln(gen.formatDouble(vals[0]) + " "
+ gen.formatDouble(vals[1]) + " "
+ gen.formatDouble(vals[2]) + " "
+ gen.formatDouble(vals[3]) + " "
+ gen.formatDouble(vals[4]) + " "
+ gen.formatDouble(vals[5]) + " "
+ gen.formatDouble(vals[6]) + " Tm [" + ch
+ "]");
}
gen.writeln("ET");
} catch (IOException ioe) {
handleIOException(ioe);
}*/
}
/**
* Fills the interior of a <code>Shape</code> using the settings of the
* <code>Graphics2D</code> context. The rendering attributes applied
* include the <code>Clip</code>, <code>Transform</code>,
* <code>Paint</code>, and <code>Composite</code>.
* @param s the <code>Shape</code> to be filled
* @see #setPaint
* @see java.awt.Graphics#setColor
* @see #transform
* @see #setTransform
* @see #setComposite
* @see #clip
* @see #setClip
*/
public void fill(Shape s) {
preparePainting();
try {
gen.saveGraphicsState();
Shape imclip = getClip();
writeClip(imclip);
establishColor(getColor());
applyPaint(getPaint(), true);
gen.writeln("newpath");
PathIterator iter = s.getPathIterator(getTransform());
processPathIterator(iter);
doDrawing(true, false,
iter.getWindingRule() == PathIterator.WIND_EVEN_ODD);
gen.restoreGraphicsState();
} catch (IOException ioe) {
handleIOException(ioe);
}
}
/**
* Commits a painting operation.
* @param fill filling
* @param stroke stroking
* @param nonzero ???
* @exception IOException In case of an I/O problem
*/
protected void doDrawing(boolean fill, boolean stroke, boolean nonzero)
throws IOException {
preparePainting();
if (fill) {
if (stroke) {
if (!nonzero) {
gen.writeln("stroke");
} else {
gen.writeln("stroke");
}
} else {
if (!nonzero) {
gen.writeln("fill");
} else {
gen.writeln("fill");
}
}
} else {
// if(stroke)
gen.writeln("stroke");
}
}
/**
* Returns the device configuration associated with this
* <code>Graphics2D</code>.
* @return the device configuration
*/
public GraphicsConfiguration getDeviceConfiguration() {
return GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
}
/**
* Used to create proper font metrics
*/
private Graphics2D fmg;
{
BufferedImage bi = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_ARGB);
fmg = bi.createGraphics();
}
/**
* Sets the overriding font.
* @param font Font to set
*/
public void setOverrideFont(Font font) {
this.overrideFont = font;
}
/**
* Gets the font metrics for the specified font.
* @return the font metrics for the specified font.
* @param f the specified font
* @see java.awt.Graphics#getFont
* @see java.awt.FontMetrics
* @see java.awt.Graphics#getFontMetrics()
*/
public java.awt.FontMetrics getFontMetrics(java.awt.Font f) {
return fmg.getFontMetrics(f);
}
/**
* Sets the paint mode of this graphics context to alternate between
* this graphics context's current color and the new specified color.
* This specifies that logical pixel operations are performed in the
* XOR mode, which alternates pixels between the current color and
* a specified XOR color.
* <p>
* When drawing operations are performed, pixels which are the
* current color are changed to the specified color, and vice versa.
* <p>
* Pixels that are of colors other than those two colors are changed
* in an unpredictable but reversible manner; if the same figure is
* drawn twice, then all pixels are restored to their original values.
* @param c1 the XOR alternation color
*/
public void setXORMode(Color c1) {
log.warn("NYI: setXORMode");
}
/**
* Copies an area of the component by a distance specified by
* <code>dx</code> and <code>dy</code>. From the point specified
* by <code>x</code> and <code>y</code>, this method
* copies downwards and to the right. To copy an area of the
* component to the left or upwards, specify a negative value for
* <code>dx</code> or <code>dy</code>.
* If a portion of the source rectangle lies outside the bounds
* of the component, or is obscured by another window or component,
* <code>copyArea</code> will be unable to copy the associated
* pixels. The area that is omitted can be refreshed by calling
* the component's <code>paint</code> method.
* @param x the <i>x</i> coordinate of the source rectangle.
* @param y the <i>y</i> coordinate of the source rectangle.
* @param width the width of the source rectangle.
* @param height the height of the source rectangle.
* @param dx the horizontal distance to copy the pixels.
* @param dy the vertical distance to copy the pixels.
*/
public void copyArea(int x, int y, int width, int height, int dx,
int dy) {
log.warn("NYI: copyArea");
}
/* --- for debugging
public void transform(AffineTransform tx) {
System.out.println("transform(" + toArray(tx) + ")");
super.transform(zx);
}
public void scale(double sx, double sy) {
System.out.println("scale(" + sx + ", " + sy + ")");
super.scale(sx, sy);
}
public void translate(double tx, double ty) {
System.out.println("translate(double " + tx + ", " + ty + ")");
super.translate(tx, ty);
}
public void translate(int tx, int ty) {
System.out.println("translate(int " + tx + ", " + ty + ")");
super.translate(tx, ty);
}
*/
}
|