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
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
|
/*
* 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.
*/
/* $Id$ */
package org.apache.fop.render.pcl;
//Java
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import org.w3c.dom.Document;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlgraphics.image.loader.ImageException;
import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageManager;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.xmlgraphics.image.loader.ImageSize;
import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
import org.apache.xmlgraphics.java2d.GraphicContext;
import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.area.Area;
import org.apache.fop.area.Block;
import org.apache.fop.area.BlockViewport;
import org.apache.fop.area.CTM;
import org.apache.fop.area.PageViewport;
import org.apache.fop.area.Trait;
import org.apache.fop.area.inline.AbstractTextArea;
import org.apache.fop.area.inline.ForeignObject;
import org.apache.fop.area.inline.Image;
import org.apache.fop.area.inline.InlineArea;
import org.apache.fop.area.inline.SpaceArea;
import org.apache.fop.area.inline.TextArea;
import org.apache.fop.area.inline.Viewport;
import org.apache.fop.area.inline.WordArea;
import org.apache.fop.datatypes.URISpecification;
import org.apache.fop.fo.extensions.ExtensionElementMapping;
import org.apache.fop.fonts.Font;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontMetrics;
import org.apache.fop.render.Graphics2DAdapter;
import org.apache.fop.render.PrintRenderer;
import org.apache.fop.render.RendererContext;
import org.apache.fop.render.RendererContextConstants;
import org.apache.fop.render.java2d.FontMetricsMapper;
import org.apache.fop.render.java2d.FontSetup;
import org.apache.fop.render.java2d.Java2DRenderer;
import org.apache.fop.render.pcl.extensions.PCLElementMapping;
import org.apache.fop.traits.BorderProps;
import org.apache.fop.util.QName;
import org.apache.fop.util.UnitConv;
/**
* Renderer for the PCL 5 printer language. It also uses HP GL/2 for certain graphic elements.
*/
public class PCLRenderer extends PrintRenderer {
/** logging instance */
private static Log log = LogFactory.getLog(PCLRenderer.class);
/** The MIME type for PCL */
public static final String MIME_TYPE = MimeConstants.MIME_PCL_ALT;
private static final QName CONV_MODE
= new QName(ExtensionElementMapping.URI, null, "conversion-mode");
private static final QName SRC_TRANSPARENCY
= new QName(ExtensionElementMapping.URI, null, "source-transparency");
/** The OutputStream to write the PCL stream to */
protected OutputStream out;
/** The PCL generator */
protected PCLGenerator gen;
private boolean ioTrouble = false;
private Stack graphicContextStack = new Stack();
private GraphicContext graphicContext = new GraphicContext();
private PCLPageDefinition currentPageDefinition;
private int currentPrintDirection = 0;
private GeneralPath currentPath = null;
private java.awt.Color currentFillColor = null;
/**
* Controls whether appearance is more important than speed. False can cause some FO feature
* to be ignored (like the advanced borders).
*/
private boolean qualityBeforeSpeed = false;
/**
* Controls whether all text should be painted as text. This is a fallback setting in case
* the mixture of native and bitmapped text does not provide the necessary quality.
*/
private boolean allTextAsBitmaps = false;
/**
* Controls whether an RGB canvas is used when converting Java2D graphics to bitmaps.
* This can be used to work around problems with Apache Batik, for example, but setting
* this to true will increase memory consumption.
*/
private boolean useColorCanvas = false;
/**
* Controls whether the generation of PJL commands gets disabled.
*/
private boolean disabledPJL = false;
/**
* Create the PCL renderer
*/
public PCLRenderer() {
}
/**
* Configures the renderer to trade speed for quality if desired. One example here is the way
* that borders are rendered.
* @param qualityBeforeSpeed true if quality is more important than speed
*/
public void setQualityBeforeSpeed(boolean qualityBeforeSpeed) {
this.qualityBeforeSpeed = qualityBeforeSpeed;
}
/**
* Controls whether PJL commands shall be generated by the PCL renderer.
* @param disable true to disable PJL commands
*/
public void setPJLDisabled(boolean disable) {
this.disabledPJL = disable;
}
/**
* Indicates whether PJL generation is disabled.
* @return true if PJL generation is disabled.
*/
public boolean isPJLDisabled() {
return this.disabledPJL;
}
/**
* {@inheritDoc}
*/
public void setupFontInfo(FontInfo inFontInfo) {
//Don't call super.setupFontInfo() here!
//The PCLRenderer uses the Java2D FontSetup which needs a special font setup
//create a temp Image to test font metrics on
fontInfo = inFontInfo;
BufferedImage fontImage = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = fontImage.createGraphics();
//The next line is important to get accurate font metrics!
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
FontSetup.setup(fontInfo, fontList, fontResolver, g);
}
/**
* Central exception handler for I/O exceptions.
* @param ioe IOException to handle
*/
protected void handleIOTrouble(IOException ioe) {
if (!ioTrouble) {
log.error("Error while writing to target file", ioe);
ioTrouble = true;
}
}
/** {@inheritDoc} */
public Graphics2DAdapter getGraphics2DAdapter() {
return new PCLGraphics2DAdapter();
}
/** @return the GraphicContext used to track coordinate system transformations */
public GraphicContext getGraphicContext() {
return this.graphicContext;
}
/** @return the target resolution */
protected int getResolution() {
int resolution = (int)Math.round(userAgent.getTargetResolution());
if (resolution <= 300) {
return 300;
} else {
return 600;
}
}
/**
* Sets the current font (NOTE: Hard-coded font mappings ATM!)
* @param name the font name (internal F* names for now)
* @param size the font size
* @param text the text to be rendered (used to determine if there are non-printable chars)
* @return true if the font can be mapped to PCL
* @throws IOException if an I/O problem occurs
*/
public boolean setFont(String name, float size, String text) throws IOException {
byte[] encoded = text.getBytes("ISO-8859-1");
for (int i = 0, c = encoded.length; i < c; i++) {
if (encoded[i] == 0x3F && text.charAt(i) != '?') {
return false;
}
}
int fontcode = 0;
if (name.length() > 1 && name.charAt(0) == 'F') {
try {
fontcode = Integer.parseInt(name.substring(1));
} catch (Exception e) {
log.error(e);
}
}
//Note "(ON" selects ISO 8859-1 symbol set as used by PCLGenerator
String formattedSize = gen.formatDouble2(size / 1000);
switch (fontcode) {
case 1: // F1 = Helvetica
// gen.writeCommand("(8U");
// gen.writeCommand("(s1p" + formattedSize + "v0s0b24580T");
// Arial is more common among PCL5 printers than Helvetica - so use Arial
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v0s0b16602T");
break;
case 2: // F2 = Helvetica Oblique
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v1s0b16602T");
break;
case 3: // F3 = Helvetica Bold
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v0s3b16602T");
break;
case 4: // F4 = Helvetica Bold Oblique
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v1s3b16602T");
break;
case 5: // F5 = Times Roman
// gen.writeCommand("(8U");
// gen.writeCommand("(s1p" + formattedSize + "v0s0b25093T");
// Times New is more common among PCL5 printers than Times - so use Times New
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v0s0b16901T");
break;
case 6: // F6 = Times Italic
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v1s0b16901T");
break;
case 7: // F7 = Times Bold
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v0s3b16901T");
break;
case 8: // F8 = Times Bold Italic
gen.writeCommand("(0N");
gen.writeCommand("(s1p" + formattedSize + "v1s3b16901T");
break;
case 9: // F9 = Courier
gen.writeCommand("(0N");
gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
+ "h0s0b4099T");
break;
case 10: // F10 = Courier Oblique
gen.writeCommand("(0N");
gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
+ "h1s0b4099T");
break;
case 11: // F11 = Courier Bold
gen.writeCommand("(0N");
gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
+ "h0s3b4099T");
break;
case 12: // F12 = Courier Bold Oblique
gen.writeCommand("(0N");
gen.writeCommand("(s0p" + gen.formatDouble2(120.01f / (size / 1000.00f))
+ "h1s3b4099T");
break;
case 13: // F13 = Symbol
return false;
//gen.writeCommand("(19M");
//gen.writeCommand("(s1p" + formattedSize + "v0s0b16686T");
// ECMA Latin 1 Symbol Set in Times Roman???
// gen.writeCommand("(9U");
// gen.writeCommand("(s1p" + formattedSize + "v0s0b25093T");
//break;
case 14: // F14 = Zapf Dingbats
return false;
//gen.writeCommand("(14L");
//gen.writeCommand("(s1p" + formattedSize + "v0s0b45101T");
//break;
default:
//gen.writeCommand("(0N");
//gen.writeCommand("(s" + formattedSize + "V");
return false;
}
return true;
}
/** {@inheritDoc} */
public void startRenderer(OutputStream outputStream) throws IOException {
log.debug("Rendering areas to PCL...");
this.out = outputStream;
this.gen = new PCLGenerator(out, getResolution());
if (!isPJLDisabled()) {
gen.universalEndOfLanguage();
gen.writeText("@PJL COMMENT Produced by " + userAgent.getProducer() + "\n");
if (userAgent.getTitle() != null) {
gen.writeText("@PJL JOB NAME = \"" + userAgent.getTitle() + "\"\n");
}
gen.writeText("@PJL SET RESOLUTION = " + getResolution() + "\n");
gen.writeText("@PJL ENTER LANGUAGE = PCL\n");
}
gen.resetPrinter();
gen.setUnitOfMeasure(getResolution());
gen.setRasterGraphicsResolution(getResolution());
}
/** {@inheritDoc} */
public void stopRenderer() throws IOException {
gen.separateJobs();
gen.resetPrinter();
if (!isPJLDisabled()) {
gen.universalEndOfLanguage();
}
}
/** {@inheritDoc} */
public String getMimeType() {
return MIME_TYPE;
}
/**
* {@inheritDoc}
*/
public void renderPage(PageViewport page) throws IOException, FOPException {
saveGraphicsState();
//Paper source
String paperSource = page.getForeignAttributeValue(
new QName(PCLElementMapping.NAMESPACE, null, "paper-source"));
if (paperSource != null) {
gen.selectPaperSource(Integer.parseInt(paperSource));
}
//Page size
final long pagewidth = Math.round(page.getViewArea().getWidth());
final long pageheight = Math.round(page.getViewArea().getHeight());
selectPageFormat(pagewidth, pageheight);
super.renderPage(page);
//Eject page
gen.formFeed();
restoreGraphicsState();
}
private void selectPageFormat(long pagewidth, long pageheight) throws IOException {
this.currentPageDefinition = PCLPageDefinition.getPageDefinition(
pagewidth, pageheight, 1000);
if (this.currentPageDefinition == null) {
this.currentPageDefinition = PCLPageDefinition.getDefaultPageDefinition();
log.warn("Paper type could not be determined. Falling back to: "
+ this.currentPageDefinition.getName());
}
log.debug("page size: " + currentPageDefinition.getPhysicalPageSize());
log.debug("logical page: " + currentPageDefinition.getLogicalPageRect());
if (this.currentPageDefinition.isLandscapeFormat()) {
gen.writeCommand("&l1O"); //Orientation
} else {
gen.writeCommand("&l0O"); //Orientation
}
gen.selectPageSize(this.currentPageDefinition.getSelector());
gen.clearHorizontalMargins();
gen.setTopMargin(0);
}
/** Saves the current graphics state on the stack. */
protected void saveGraphicsState() {
graphicContextStack.push(graphicContext);
graphicContext = (GraphicContext)graphicContext.clone();
}
/** Restores the last graphics state from the stack. */
protected void restoreGraphicsState() {
graphicContext = (GraphicContext)graphicContextStack.pop();
}
/**
* Clip an area. write a clipping operation given coordinates in the current
* transform. Coordinates are in points.
*
* @param x the x coordinate
* @param y the y coordinate
* @param width the width of the area
* @param height the height of the area
*/
protected void clipRect(float x, float y, float width, float height) {
//PCL cannot clip (only HP GL/2 can)
}
private Point2D transformedPoint(float x, float y) {
return transformedPoint(Math.round(x), Math.round(y));
}
private Point2D transformedPoint(int x, int y) {
AffineTransform at = graphicContext.getTransform();
if (log.isTraceEnabled()) {
log.trace("Current transform: " + at);
}
Point2D.Float orgPoint = new Point2D.Float(x, y);
Point2D.Float transPoint = new Point2D.Float();
at.transform(orgPoint, transPoint);
//At this point we have the absolute position in FOP's coordinate system
//Now get PCL coordinates taking the current print direction and the logical page
//into account.
Dimension pageSize = currentPageDefinition.getPhysicalPageSize();
Rectangle logRect = currentPageDefinition.getLogicalPageRect();
switch (currentPrintDirection) {
case 0:
transPoint.x -= logRect.x;
transPoint.y -= logRect.y;
break;
case 90:
float ty = transPoint.x;
transPoint.x = pageSize.height - transPoint.y;
transPoint.y = ty;
transPoint.x -= logRect.y;
transPoint.y -= logRect.x;
break;
case 180:
transPoint.x = pageSize.width - transPoint.x;
transPoint.y = pageSize.height - transPoint.y;
transPoint.x -= pageSize.width - logRect.x - logRect.width;
transPoint.y -= pageSize.height - logRect.y - logRect.height;
//The next line is odd and is probably necessary due to the default value of the
//Text Length command: "1/2 inch less than maximum text length"
//I wonder why this isn't necessary for the 90 degree rotation. *shrug*
transPoint.y -= UnitConv.in2mpt(0.5);
break;
case 270:
float tx = transPoint.y;
transPoint.y = pageSize.width - transPoint.x;
transPoint.x = tx;
transPoint.x -= pageSize.height - logRect.y - logRect.height;
transPoint.y -= pageSize.width - logRect.x - logRect.width;
break;
default:
throw new IllegalStateException("Illegal print direction: " + currentPrintDirection);
}
return transPoint;
}
private void changePrintDirection() {
AffineTransform at = graphicContext.getTransform();
int newDir;
try {
if (at.getScaleX() == 0 && at.getScaleY() == 0
&& at.getShearX() == 1 && at.getShearY() == -1) {
newDir = 90;
} else if (at.getScaleX() == -1 && at.getScaleY() == -1
&& at.getShearX() == 0 && at.getShearY() == 0) {
newDir = 180;
} else if (at.getScaleX() == 0 && at.getScaleY() == 0
&& at.getShearX() == -1 && at.getShearY() == 1) {
newDir = 270;
} else {
newDir = 0;
}
if (newDir != this.currentPrintDirection) {
this.currentPrintDirection = newDir;
gen.changePrintDirection(this.currentPrintDirection);
}
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
}
/**
* {@inheritDoc}
*/
protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
saveGraphicsState();
AffineTransform at = new AffineTransform(ctm.toArray());
graphicContext.transform(at);
changePrintDirection();
if (log.isDebugEnabled()) {
log.debug("startVPArea: " + at + " --> " + graphicContext.getTransform());
}
}
/**
* {@inheritDoc}
*/
protected void endVParea() {
restoreGraphicsState();
changePrintDirection();
if (log.isDebugEnabled()) {
log.debug("endVPArea() --> " + graphicContext.getTransform());
}
}
/**
* Handle block traits.
* The block could be any sort of block with any positioning
* so this should render the traits such as border and background
* in its position.
*
* @param block the block to render the traits
*/
protected void handleBlockTraits(Block block) {
int borderPaddingStart = block.getBorderAndPaddingWidthStart();
int borderPaddingBefore = block.getBorderAndPaddingWidthBefore();
float startx = currentIPPosition / 1000f;
float starty = currentBPPosition / 1000f;
float width = block.getIPD() / 1000f;
float height = block.getBPD() / 1000f;
startx += block.getStartIndent() / 1000f;
startx -= block.getBorderAndPaddingWidthStart() / 1000f;
width += borderPaddingStart / 1000f;
width += block.getBorderAndPaddingWidthEnd() / 1000f;
height += borderPaddingBefore / 1000f;
height += block.getBorderAndPaddingWidthAfter() / 1000f;
drawBackAndBorders(block, startx, starty, width, height);
}
/**
* {@inheritDoc}
*/
protected void renderText(final TextArea text) {
renderInlineAreaBackAndBorders(text);
String fontname = getInternalFontNameForArea(text);
final int fontsize = text.getTraitAsInteger(Trait.FONT_SIZE);
//Determine position
int saveIP = currentIPPosition;
final int rx = currentIPPosition + text.getBorderAndPaddingWidthStart();
int bl = currentBPPosition + text.getOffset() + text.getBaselineOffset();
try {
final Color col = (Color)text.getTrait(Trait.COLOR);
boolean pclFont = allTextAsBitmaps
? false
: setFont(fontname, fontsize, text.getText());
if (pclFont) {
//this.currentFill = col;
if (col != null) {
//useColor(ct);
gen.setTransparencyMode(true, false);
gen.selectGrayscale(col);
}
saveGraphicsState();
graphicContext.translate(rx, bl);
setCursorPos(0, 0);
gen.setTransparencyMode(true, true);
if (text.hasUnderline()) {
gen.writeCommand("&d0D");
}
super.renderText(text); //Updates IPD and renders words and spaces
if (text.hasUnderline()) {
gen.writeCommand("&d@");
}
restoreGraphicsState();
} else {
//Use Java2D to paint different fonts via bitmap
final Font font = getFontFromArea(text);
final int baseline = text.getBaselineOffset();
//for cursive fonts, so the text isn't clipped
int extraWidth = font.getFontSize() / 3;
final FontMetricsMapper mapper = (FontMetricsMapper)fontInfo.getMetricsFor(
font.getFontName());
int maxAscent = mapper.getMaxAscent(font.getFontSize()) / 1000;
final int additionalBPD = maxAscent - baseline;
Graphics2DAdapter g2a = getGraphics2DAdapter();
final Rectangle paintRect = new Rectangle(
rx, currentBPPosition + text.getOffset() - additionalBPD,
text.getIPD() + extraWidth, text.getBPD() + additionalBPD);
RendererContext rc = createRendererContext(paintRect.x, paintRect.y,
paintRect.width, paintRect.height, null);
Map atts = new java.util.HashMap();
atts.put(CONV_MODE, "bitmap");
atts.put(SRC_TRANSPARENCY, "true");
rc.setProperty(RendererContextConstants.FOREIGN_ATTRIBUTES, atts);
Graphics2DImagePainter painter = new Graphics2DImagePainter() {
public void paint(Graphics2D g2d, Rectangle2D area) {
g2d.setFont(mapper.getFont(font.getFontSize()));
g2d.translate(0, baseline + additionalBPD);
g2d.scale(1000, 1000);
g2d.setColor(col);
Java2DRenderer.renderText(text, g2d, font);
renderTextDecoration(g2d, mapper, fontsize, text, 0, 0);
}
public Dimension getImageSize() {
return paintRect.getSize();
}
};
g2a.paintImage(painter, rc,
paintRect.x, paintRect.y, paintRect.width, paintRect.height);
currentIPPosition = saveIP + text.getAllocIPD();
}
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
}
/**
* Paints the text decoration marks.
* @param g2d Graphics2D instance to paint to
* @param fm Current typeface
* @param fontsize Current font size
* @param inline inline area to paint the marks for
* @param baseline position of the baseline
* @param startx start IPD
*/
private static void renderTextDecoration(Graphics2D g2d,
FontMetrics fm, int fontsize, InlineArea inline,
int baseline, int startx) {
boolean hasTextDeco = inline.hasUnderline()
|| inline.hasOverline()
|| inline.hasLineThrough();
if (hasTextDeco) {
float descender = fm.getDescender(fontsize) / 1000f;
float capHeight = fm.getCapHeight(fontsize) / 1000f;
float lineWidth = (descender / -4f) / 1000f;
float endx = (startx + inline.getIPD()) / 1000f;
if (inline.hasUnderline()) {
Color ct = (Color) inline.getTrait(Trait.UNDERLINE_COLOR);
g2d.setColor(ct);
float y = baseline - descender / 2f;
g2d.setStroke(new BasicStroke(lineWidth));
g2d.draw(new Line2D.Float(startx / 1000f, y / 1000f,
endx, y / 1000f));
}
if (inline.hasOverline()) {
Color ct = (Color) inline.getTrait(Trait.OVERLINE_COLOR);
g2d.setColor(ct);
float y = (float)(baseline - (1.1 * capHeight));
g2d.setStroke(new BasicStroke(lineWidth));
g2d.draw(new Line2D.Float(startx / 1000f, y / 1000f,
endx, y / 1000f));
}
if (inline.hasLineThrough()) {
Color ct = (Color) inline.getTrait(Trait.LINETHROUGH_COLOR);
g2d.setColor(ct);
float y = (float)(baseline - (0.45 * capHeight));
g2d.setStroke(new BasicStroke(lineWidth));
g2d.draw(new Line2D.Float(startx / 1000f, y / 1000f,
endx, y / 1000f));
}
}
}
/**
* Sets the current cursor position. The coordinates are transformed to the absolute position
* on the logical PCL page and then passed on to the PCLGenerator.
* @param x the x coordinate (in millipoints)
* @param y the y coordinate (in millipoints)
*/
void setCursorPos(float x, float y) {
try {
Point2D transPoint = transformedPoint(x, y);
gen.setCursorPos(transPoint.getX(), transPoint.getY());
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
}
/** Clip using the current path. */
protected void clip() {
if (currentPath == null) {
throw new IllegalStateException("No current path available!");
}
//TODO Find a good way to do clipping. PCL itself cannot clip.
currentPath = null;
}
/**
* Closes the current subpath by appending a straight line segment from
* the current point to the starting point of the subpath.
*/
protected void closePath() {
currentPath.closePath();
}
/**
* Appends a straight line segment from the current point to (x, y). The
* new current point is (x, y).
* @param x x coordinate
* @param y y coordinate
*/
protected void lineTo(float x, float y) {
if (currentPath == null) {
currentPath = new GeneralPath();
}
currentPath.lineTo(x, y);
}
/**
* Moves the current point to (x, y), omitting any connecting line segment.
* @param x x coordinate
* @param y y coordinate
*/
protected void moveTo(float x, float y) {
if (currentPath == null) {
currentPath = new GeneralPath();
}
currentPath.moveTo(x, y);
}
/**
* Fill a rectangular area.
* @param x the x coordinate (in pt)
* @param y the y coordinate (in pt)
* @param width the width of the rectangle
* @param height the height of the rectangle
*/
protected void fillRect(float x, float y, float width, float height) {
try {
setCursorPos(x * 1000, y * 1000);
gen.fillRect((int)(width * 1000), (int)(height * 1000),
this.currentFillColor);
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
}
/**
* Sets the new current fill color.
* @param color the color
*/
protected void updateFillColor(java.awt.Color color) {
this.currentFillColor = color;
}
/**
* {@inheritDoc}
*/
protected void renderWord(WordArea word) {
//Font font = getFontFromArea(word.getParentArea());
String s = word.getWord();
try {
gen.writeText(s);
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
super.renderWord(word);
}
/**
* {@inheritDoc}
*/
protected void renderSpace(SpaceArea space) {
AbstractTextArea textArea = (AbstractTextArea)space.getParentArea();
String s = space.getSpace();
char sp = s.charAt(0);
Font font = getFontFromArea(textArea);
int tws = (space.isAdjustable()
? textArea.getTextWordSpaceAdjust()
+ 2 * textArea.getTextLetterSpaceAdjust()
: 0);
double dx = (font.getCharWidth(sp) + tws) / 100f;
try {
gen.writeCommand("&a+" + gen.formatDouble2(dx) + "H");
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
super.renderSpace(space);
}
/**
* Render an inline viewport.
* This renders an inline viewport by clipping if necessary.
* @param viewport the viewport to handle
* @todo Copied from AbstractPathOrientedRenderer
*/
public void renderViewport(Viewport viewport) {
float x = currentIPPosition / 1000f;
float y = (currentBPPosition + viewport.getOffset()) / 1000f;
float width = viewport.getIPD() / 1000f;
float height = viewport.getBPD() / 1000f;
// TODO: Calculate the border rect correctly.
float borderPaddingStart = viewport.getBorderAndPaddingWidthStart() / 1000f;
float borderPaddingBefore = viewport.getBorderAndPaddingWidthBefore() / 1000f;
float bpwidth = borderPaddingStart
+ (viewport.getBorderAndPaddingWidthEnd() / 1000f);
float bpheight = borderPaddingBefore
+ (viewport.getBorderAndPaddingWidthAfter() / 1000f);
drawBackAndBorders(viewport, x, y, width + bpwidth, height + bpheight);
if (viewport.getClip()) {
saveGraphicsState();
clipRect(x + borderPaddingStart, y + borderPaddingBefore, width, height);
}
super.renderViewport(viewport);
if (viewport.getClip()) {
restoreGraphicsState();
}
}
/**
* {@inheritDoc}
*/
protected void renderBlockViewport(BlockViewport bv, List children) {
// clip and position viewport if necessary
// save positions
int saveIP = currentIPPosition;
int saveBP = currentBPPosition;
//String saveFontName = currentFontName;
CTM ctm = bv.getCTM();
int borderPaddingStart = bv.getBorderAndPaddingWidthStart();
int borderPaddingBefore = bv.getBorderAndPaddingWidthBefore();
float x, y;
x = (float)(bv.getXOffset() + containingIPPosition) / 1000f;
y = (float)(bv.getYOffset() + containingBPPosition) / 1000f;
//This is the content-rect
float width = (float)bv.getIPD() / 1000f;
float height = (float)bv.getBPD() / 1000f;
if (bv.getPositioning() == Block.ABSOLUTE
|| bv.getPositioning() == Block.FIXED) {
currentIPPosition = bv.getXOffset();
currentBPPosition = bv.getYOffset();
//For FIXED, we need to break out of the current viewports to the
//one established by the page. We save the state stack for restoration
//after the block-container has been painted. See below.
List breakOutList = null;
if (bv.getPositioning() == Block.FIXED) {
breakOutList = breakOutOfStateStack();
}
CTM tempctm = new CTM(containingIPPosition, containingBPPosition);
ctm = tempctm.multiply(ctm);
//Adjust for spaces (from margin or indirectly by start-indent etc.
x += bv.getSpaceStart() / 1000f;
currentIPPosition += bv.getSpaceStart();
y += bv.getSpaceBefore() / 1000f;
currentBPPosition += bv.getSpaceBefore();
float bpwidth = (borderPaddingStart + bv.getBorderAndPaddingWidthEnd()) / 1000f;
float bpheight = (borderPaddingBefore + bv.getBorderAndPaddingWidthAfter()) / 1000f;
drawBackAndBorders(bv, x, y, width + bpwidth, height + bpheight);
//Now adjust for border/padding
currentIPPosition += borderPaddingStart;
currentBPPosition += borderPaddingBefore;
Rectangle2D clippingRect = null;
if (bv.getClip()) {
clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
bv.getIPD(), bv.getBPD());
}
startVParea(ctm, clippingRect);
currentIPPosition = 0;
currentBPPosition = 0;
renderBlocks(bv, children);
endVParea();
if (breakOutList != null) {
restoreStateStackAfterBreakOut(breakOutList);
}
currentIPPosition = saveIP;
currentBPPosition = saveBP;
} else {
currentBPPosition += bv.getSpaceBefore();
//borders and background in the old coordinate system
handleBlockTraits(bv);
//Advance to start of content area
currentIPPosition += bv.getStartIndent();
CTM tempctm = new CTM(containingIPPosition, currentBPPosition);
ctm = tempctm.multiply(ctm);
//Now adjust for border/padding
currentBPPosition += borderPaddingBefore;
Rectangle2D clippingRect = null;
if (bv.getClip()) {
clippingRect = new Rectangle(currentIPPosition, currentBPPosition,
bv.getIPD(), bv.getBPD());
}
startVParea(ctm, clippingRect);
currentIPPosition = 0;
currentBPPosition = 0;
renderBlocks(bv, children);
endVParea();
currentIPPosition = saveIP;
currentBPPosition = saveBP;
currentBPPosition += (int)(bv.getAllocBPD());
}
//currentFontName = saveFontName;
}
private List breakOutOfStateStack() {
log.debug("Block.FIXED --> break out");
List breakOutList = new java.util.ArrayList();
while (!this.graphicContextStack.empty()) {
breakOutList.add(0, this.graphicContext);
restoreGraphicsState();
}
return breakOutList;
}
private void restoreStateStackAfterBreakOut(List breakOutList) {
log.debug("Block.FIXED --> restoring context after break-out");
for (int i = 0, c = breakOutList.size(); i < c; i++) {
saveGraphicsState();
this.graphicContext = (GraphicContext)breakOutList.get(i);
}
}
/** {@inheritDoc} */
protected RendererContext createRendererContext(int x, int y, int width, int height,
Map foreignAttributes) {
RendererContext context = super.createRendererContext(
x, y, width, height, foreignAttributes);
context.setProperty(PCLRendererContextConstants.PCL_COLOR_CANVAS,
Boolean.valueOf(this.useColorCanvas));
return context;
}
/** {@inheritDoc} */
public void renderImage(Image image, Rectangle2D pos) {
drawImage(image.getURL(), pos, image.getForeignAttributes());
}
private static final ImageFlavor[] FLAVORS = new ImageFlavor[]
{ImageFlavor.GRAPHICS2D,
ImageFlavor.BUFFERED_IMAGE,
ImageFlavor.RENDERED_IMAGE,
ImageFlavor.XML_DOM};
/**
* Draw an image at the indicated location.
* @param uri the URI/URL of the image
* @param pos the position of the image
* @param foreignAttributes an optional Map with foreign attributes, may be null
*/
protected void drawImage(String uri, Rectangle2D pos, Map foreignAttributes) {
uri = URISpecification.getURL(uri);
Rectangle posInt = new Rectangle(
(int)pos.getX(),
(int)pos.getY(),
(int)pos.getWidth(),
(int)pos.getHeight());
Point origin = new Point(currentIPPosition, currentBPPosition);
int x = origin.x + posInt.x;
int y = origin.y + posInt.y;
ImageManager manager = getUserAgent().getFactory().getImageManager();
ImageInfo info = null;
try {
ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
info = manager.getImageInfo(uri, sessionContext);
//Only now fully load/prepare the image
Map hints = ImageUtil.getDefaultHints(sessionContext);
org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
info, FLAVORS, hints, sessionContext);
//...and process the image
if (img instanceof ImageGraphics2D) {
ImageGraphics2D imageG2D = (ImageGraphics2D)img;
RendererContext context = createRendererContext(
posInt.x, posInt.y,
posInt.width, posInt.height, foreignAttributes);
getGraphics2DAdapter().paintImage(imageG2D.getGraphics2DImagePainter(),
context, x, y, posInt.width, posInt.height);
} else if (img instanceof ImageRendered) {
ImageRendered imgRend = (ImageRendered)img;
RenderedImage ri = imgRend.getRenderedImage();
setCursorPos(x, y);
gen.paintBitmap(ri,
new Dimension(posInt.width, posInt.height),
false);
} else if (img instanceof ImageXMLDOM) {
ImageXMLDOM imgXML = (ImageXMLDOM)img;
renderDocument(imgXML.getDocument(), imgXML.getRootNamespace(),
pos, foreignAttributes);
} else {
throw new UnsupportedOperationException("Unsupported image type: " + img);
}
} catch (ImageException ie) {
log.error("Error while processing image: "
+ (info != null ? info.toString() : uri), ie);
} catch (FileNotFoundException fe) {
log.error(fe.getMessage());
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
}
/** {@inheritDoc} */
public void renderForeignObject(ForeignObject fo, Rectangle2D pos) {
Document doc = fo.getDocument();
String ns = fo.getNameSpace();
renderDocument(doc, ns, pos, fo.getForeignAttributes());
}
/**
* Common method to render the background and borders for any inline area.
* The all borders and padding are drawn outside the specified area.
* @param area the inline area for which the background, border and padding is to be
* rendered
* @todo Copied from AbstractPathOrientedRenderer
*/
protected void renderInlineAreaBackAndBorders(InlineArea area) {
float x = currentIPPosition / 1000f;
float y = (currentBPPosition + area.getOffset()) / 1000f;
float width = area.getIPD() / 1000f;
float height = area.getBPD() / 1000f;
float borderPaddingStart = area.getBorderAndPaddingWidthStart() / 1000f;
float borderPaddingBefore = area.getBorderAndPaddingWidthBefore() / 1000f;
float bpwidth = borderPaddingStart
+ (area.getBorderAndPaddingWidthEnd() / 1000f);
float bpheight = borderPaddingBefore
+ (area.getBorderAndPaddingWidthAfter() / 1000f);
if (height != 0.0f || bpheight != 0.0f && bpwidth != 0.0f) {
drawBackAndBorders(area, x, y - borderPaddingBefore
, width + bpwidth
, height + bpheight);
}
}
/**
* Draw the background and borders. This draws the background and border
* traits for an area given the position.
*
* @param area the area whose traits are used
* @param startx the start x position
* @param starty the start y position
* @param width the width of the area
* @param height the height of the area
*/
protected void drawBackAndBorders(Area area, float startx, float starty,
float width, float height) {
BorderProps bpsBefore = (BorderProps) area.getTrait(Trait.BORDER_BEFORE);
BorderProps bpsAfter = (BorderProps) area.getTrait(Trait.BORDER_AFTER);
BorderProps bpsStart = (BorderProps) area.getTrait(Trait.BORDER_START);
BorderProps bpsEnd = (BorderProps) area.getTrait(Trait.BORDER_END);
// draw background
Trait.Background back;
back = (Trait.Background) area.getTrait(Trait.BACKGROUND);
if (back != null) {
// Calculate padding rectangle
float sx = startx;
float sy = starty;
float paddRectWidth = width;
float paddRectHeight = height;
if (bpsStart != null) {
sx += bpsStart.width / 1000f;
paddRectWidth -= bpsStart.width / 1000f;
}
if (bpsBefore != null) {
sy += bpsBefore.width / 1000f;
paddRectHeight -= bpsBefore.width / 1000f;
}
if (bpsEnd != null) {
paddRectWidth -= bpsEnd.width / 1000f;
}
if (bpsAfter != null) {
paddRectHeight -= bpsAfter.width / 1000f;
}
if (back.getColor() != null) {
updateFillColor(back.getColor());
fillRect(sx, sy, paddRectWidth, paddRectHeight);
}
// background image
if (back.getImageInfo() != null) {
ImageSize imageSize = back.getImageInfo().getSize();
saveGraphicsState();
clipRect(sx, sy, paddRectWidth, paddRectHeight);
int horzCount = (int) ((paddRectWidth * 1000 / imageSize.getWidthMpt()) + 1.0f);
int vertCount = (int) ((paddRectHeight * 1000 / imageSize.getHeightMpt()) + 1.0f);
if (back.getRepeat() == EN_NOREPEAT) {
horzCount = 1;
vertCount = 1;
} else if (back.getRepeat() == EN_REPEATX) {
vertCount = 1;
} else if (back.getRepeat() == EN_REPEATY) {
horzCount = 1;
}
// change from points to millipoints
sx *= 1000;
sy *= 1000;
if (horzCount == 1) {
sx += back.getHoriz();
}
if (vertCount == 1) {
sy += back.getVertical();
}
for (int x = 0; x < horzCount; x++) {
for (int y = 0; y < vertCount; y++) {
// place once
Rectangle2D pos;
// Image positions are relative to the currentIP/BP
pos = new Rectangle2D.Float(
sx - currentIPPosition
+ (x * imageSize.getWidthMpt()),
sy - currentBPPosition
+ (y * imageSize.getHeightMpt()),
imageSize.getWidthMpt(),
imageSize.getHeightMpt());
drawImage(back.getURL(), pos, null);
}
}
restoreGraphicsState();
}
}
Rectangle2D.Float borderRect = new Rectangle2D.Float(startx, starty, width, height);
drawBorders(borderRect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
}
/**
* Draws borders.
* @param borderRect the border rectangle
* @param bpsBefore the border specification on the before side
* @param bpsAfter the border specification on the after side
* @param bpsStart the border specification on the start side
* @param bpsEnd the border specification on the end side
*/
protected void drawBorders(Rectangle2D.Float borderRect,
final BorderProps bpsBefore, final BorderProps bpsAfter,
final BorderProps bpsStart, final BorderProps bpsEnd) {
if (bpsBefore == null && bpsAfter == null && bpsStart == null && bpsEnd == null) {
return; //no borders to paint
}
if (qualityBeforeSpeed) {
drawQualityBorders(borderRect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
} else {
drawFastBorders(borderRect, bpsBefore, bpsAfter, bpsStart, bpsEnd);
}
}
/**
* Draws borders. Borders are drawn as shaded rectangles with no clipping.
* @param borderRect the border rectangle
* @param bpsBefore the border specification on the before side
* @param bpsAfter the border specification on the after side
* @param bpsStart the border specification on the start side
* @param bpsEnd the border specification on the end side
*/
protected void drawFastBorders(Rectangle2D.Float borderRect,
final BorderProps bpsBefore, final BorderProps bpsAfter,
final BorderProps bpsStart, final BorderProps bpsEnd) {
float startx = borderRect.x;
float starty = borderRect.y;
float width = borderRect.width;
float height = borderRect.height;
if (bpsBefore != null) {
float borderWidth = bpsBefore.width / 1000f;
updateFillColor(bpsBefore.color);
fillRect(startx, starty, width, borderWidth);
}
if (bpsAfter != null) {
float borderWidth = bpsAfter.width / 1000f;
updateFillColor(bpsAfter.color);
fillRect(startx, (starty + height - borderWidth),
width, borderWidth);
}
if (bpsStart != null) {
float borderWidth = bpsStart.width / 1000f;
updateFillColor(bpsStart.color);
fillRect(startx, starty, borderWidth, height);
}
if (bpsEnd != null) {
float borderWidth = bpsEnd.width / 1000f;
updateFillColor(bpsEnd.color);
fillRect((startx + width - borderWidth), starty, borderWidth, height);
}
}
/**
* Draws borders. Borders are drawn in-memory and painted as a bitmap.
* @param borderRect the border rectangle
* @param bpsBefore the border specification on the before side
* @param bpsAfter the border specification on the after side
* @param bpsStart the border specification on the start side
* @param bpsEnd the border specification on the end side
*/
protected void drawQualityBorders(Rectangle2D.Float borderRect,
final BorderProps bpsBefore, final BorderProps bpsAfter,
final BorderProps bpsStart, final BorderProps bpsEnd) {
Graphics2DAdapter g2a = getGraphics2DAdapter();
final Rectangle.Float effBorderRect = new Rectangle2D.Float(
borderRect.x - (currentIPPosition / 1000f),
borderRect.y - (currentBPPosition / 1000f),
borderRect.width,
borderRect.height);
final Rectangle paintRect = new Rectangle(
(int)Math.round(borderRect.x * 1000f),
(int)Math.round(borderRect.y * 1000f),
(int)Math.floor(borderRect.width * 1000f) + 1,
(int)Math.floor(borderRect.height * 1000f) + 1);
//Add one pixel wide safety margin around the paint area
int pixelWidth = (int)Math.round(UnitConv.in2mpt(1) / userAgent.getTargetResolution());
final int xoffset = (int)Math.round(-effBorderRect.x * 1000f) + pixelWidth;
final int yoffset = pixelWidth;
paintRect.x += xoffset;
paintRect.y += yoffset;
paintRect.width += 2 * pixelWidth;
paintRect.height += 2 * pixelWidth;
RendererContext rc = createRendererContext(paintRect.x, paintRect.y,
paintRect.width, paintRect.height, null);
Map atts = new java.util.HashMap();
atts.put(CONV_MODE, "bitmap");
atts.put(SRC_TRANSPARENCY, "true");
rc.setProperty(RendererContextConstants.FOREIGN_ATTRIBUTES, atts);
Graphics2DImagePainter painter = new Graphics2DImagePainter() {
public void paint(Graphics2D g2d, Rectangle2D area) {
g2d.translate(xoffset, yoffset);
g2d.scale(1000, 1000);
float startx = effBorderRect.x;
float starty = effBorderRect.y;
float width = effBorderRect.width;
float height = effBorderRect.height;
boolean[] b = new boolean[] {
(bpsBefore != null), (bpsEnd != null),
(bpsAfter != null), (bpsStart != null)};
if (!b[0] && !b[1] && !b[2] && !b[3]) {
return;
}
float[] bw = new float[] {
(b[0] ? bpsBefore.width / 1000f : 0.0f),
(b[1] ? bpsEnd.width / 1000f : 0.0f),
(b[2] ? bpsAfter.width / 1000f : 0.0f),
(b[3] ? bpsStart.width / 1000f : 0.0f)};
float[] clipw = new float[] {
BorderProps.getClippedWidth(bpsBefore) / 1000f,
BorderProps.getClippedWidth(bpsEnd) / 1000f,
BorderProps.getClippedWidth(bpsAfter) / 1000f,
BorderProps.getClippedWidth(bpsStart) / 1000f};
starty += clipw[0];
height -= clipw[0];
height -= clipw[2];
startx += clipw[3];
width -= clipw[3];
width -= clipw[1];
boolean[] slant = new boolean[] {
(b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3])};
if (bpsBefore != null) {
//endTextObject();
float sx1 = startx;
float sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
float ex1 = startx + width;
float ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
float outery = starty - clipw[0];
float clipy = outery + clipw[0];
float innery = outery + bw[0];
//saveGraphicsState();
Graphics2D g = (Graphics2D)g2d.create();
moveTo(sx1, clipy);
float sx1a = sx1;
float ex1a = ex1;
if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
sx1a -= clipw[3];
}
if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
ex1a += clipw[1];
}
lineTo(sx1a, outery);
lineTo(ex1a, outery);
}
lineTo(ex1, clipy);
lineTo(ex2, innery);
lineTo(sx2, innery);
closePath();
//clip();
g.clip(currentPath);
currentPath = null;
Rectangle2D.Float lineRect = new Rectangle2D.Float(
sx1a, outery, ex1a - sx1a, innery - outery);
Java2DRenderer.drawBorderLine(lineRect, true, true,
bpsBefore.style, bpsBefore.color, g);
//restoreGraphicsState();
}
if (bpsEnd != null) {
//endTextObject();
float sy1 = starty;
float sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
float ey1 = starty + height;
float ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
float outerx = startx + width + clipw[1];
float clipx = outerx - clipw[1];
float innerx = outerx - bw[1];
//saveGraphicsState();
Graphics2D g = (Graphics2D)g2d.create();
moveTo(clipx, sy1);
float sy1a = sy1;
float ey1a = ey1;
if (bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
sy1a -= clipw[0];
}
if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
ey1a += clipw[2];
}
lineTo(outerx, sy1a);
lineTo(outerx, ey1a);
}
lineTo(clipx, ey1);
lineTo(innerx, ey2);
lineTo(innerx, sy2);
closePath();
//clip();
g.setClip(currentPath);
currentPath = null;
Rectangle2D.Float lineRect = new Rectangle2D.Float(
innerx, sy1a, outerx - innerx, ey1a - sy1a);
Java2DRenderer.drawBorderLine(lineRect, false, false,
bpsEnd.style, bpsEnd.color, g);
//restoreGraphicsState();
}
if (bpsAfter != null) {
//endTextObject();
float sx1 = startx;
float sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
float ex1 = startx + width;
float ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
float outery = starty + height + clipw[2];
float clipy = outery - clipw[2];
float innery = outery - bw[2];
//saveGraphicsState();
Graphics2D g = (Graphics2D)g2d.create();
moveTo(ex1, clipy);
float sx1a = sx1;
float ex1a = ex1;
if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
sx1a -= clipw[3];
}
if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
ex1a += clipw[1];
}
lineTo(ex1a, outery);
lineTo(sx1a, outery);
}
lineTo(sx1, clipy);
lineTo(sx2, innery);
lineTo(ex2, innery);
closePath();
//clip();
g.setClip(currentPath);
currentPath = null;
Rectangle2D.Float lineRect = new Rectangle2D.Float(
sx1a, innery, ex1a - sx1a, outery - innery);
Java2DRenderer.drawBorderLine(lineRect, true, false,
bpsAfter.style, bpsAfter.color, g);
//restoreGraphicsState();
}
if (bpsStart != null) {
//endTextObject();
float sy1 = starty;
float sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
float ey1 = sy1 + height;
float ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
float outerx = startx - clipw[3];
float clipx = outerx + clipw[3];
float innerx = outerx + bw[3];
//saveGraphicsState();
Graphics2D g = (Graphics2D)g2d.create();
moveTo(clipx, ey1);
float sy1a = sy1;
float ey1a = ey1;
if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
sy1a -= clipw[0];
}
if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
ey1a += clipw[2];
}
lineTo(outerx, ey1a);
lineTo(outerx, sy1a);
}
lineTo(clipx, sy1);
lineTo(innerx, sy2);
lineTo(innerx, ey2);
closePath();
//clip();
g.setClip(currentPath);
currentPath = null;
Rectangle2D.Float lineRect = new Rectangle2D.Float(
outerx, sy1a, innerx - outerx, ey1a - sy1a);
Java2DRenderer.drawBorderLine(lineRect, false, false,
bpsStart.style, bpsStart.color, g);
//restoreGraphicsState();
}
}
public Dimension getImageSize() {
return paintRect.getSize();
}
};
try {
g2a.paintImage(painter, rc,
paintRect.x - xoffset, paintRect.y, paintRect.width, paintRect.height);
} catch (IOException ioe) {
handleIOTrouble(ioe);
}
}
/**
* Controls whether all text should be generated as bitmaps or only text for which there's
* no native font.
* @param allTextAsBitmaps true if all text should be painted as bitmaps
*/
public void setAllTextAsBitmaps(boolean allTextAsBitmaps) {
this.allTextAsBitmaps = allTextAsBitmaps;
}
}
|