Skip to content

Commit 77e4ac6

Browse files
committed
Deprecate one of Canvas constructors
Remove references to deprecated constructor Add unit tests DEVSIX-3767
1 parent a3e8e68 commit 77e4ac6

File tree

10 files changed

+115
-15
lines changed

10 files changed

+115
-15
lines changed

forms/src/main/java/com/itextpdf/forms/fields/PdfFormField.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,7 @@ protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize,
24772477
x = rect.getWidth() / 2;
24782478
}
24792479

2480-
Canvas modelCanvas = new Canvas(canvas, getDocument(), new Rectangle(0, -height, 0, 2 * height));
2480+
Canvas modelCanvas = new Canvas(canvas, new Rectangle(0, -height, 0, 2 * height));
24812481
modelCanvas.setProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
24822482

24832483
Style paragraphStyle = new Style().setFont(font).setFontSize(fontSize);
@@ -2549,7 +2549,7 @@ protected void drawMultiLineTextAppearance(Rectangle rect, PdfFont font, String
25492549
canvas.beginVariableText();
25502550

25512551
Rectangle areaRect = new Rectangle(0, 0, width, height);
2552-
Canvas modelCanvas = new Canvas(canvas, getDocument(), areaRect);
2552+
Canvas modelCanvas = new Canvas(canvas, areaRect);
25532553
modelCanvas.setProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
25542554

25552555
Paragraph paragraph = createParagraphForTextFieldValue(value).setFont(font)
@@ -2606,7 +2606,7 @@ private void drawChoiceAppearance(Rectangle rect, float fontSize, String value,
26062606
clip().
26072607
endPath();
26082608

2609-
Canvas modelCanvas = new Canvas(canvas, getDocument(), new Rectangle(3, 0, Math.max(0, width - widthBorder), Math.max(0, height - heightBorder)));
2609+
Canvas modelCanvas = new Canvas(canvas, new Rectangle(3, 0, Math.max(0, width - widthBorder), Math.max(0, height - heightBorder)));
26102610
modelCanvas.setProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
26112611
for (int index = 0; index < strings.size(); index++) {
26122612
Boolean isFull = modelCanvas.getRenderer().getPropertyAsBoolean(Property.FULL);
@@ -2998,7 +2998,7 @@ protected void drawButton(PdfCanvas canvas, float x, float y, float width, float
29982998

29992999
Paragraph paragraph = new Paragraph(text).setFont(font).setFontSize(fontSize).setMargin(0).setMultipliedLeading(1).
30003000
setVerticalAlignment(VerticalAlignment.MIDDLE);
3001-
Canvas modelCanvas = new Canvas(canvas, getDocument(), new Rectangle(0, -height, width, 2 * height));
3001+
Canvas modelCanvas = new Canvas(canvas, new Rectangle(0, -height, width, 2 * height));
30023002
modelCanvas.setProperty(Property.APPEARANCE_STREAM_LAYOUT, true);
30033003
modelCanvas.showTextAligned(paragraph, width / 2, height / 2, TextAlignment.CENTER, VerticalAlignment.MIDDLE);
30043004
}

forms/src/test/java/com/itextpdf/forms/PdfFormFieldTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public void fillFormWithDefaultResourcesUpdateFont() throws IOException, Interru
614614
field.setFont(PdfFontFactory.createFont(StandardFonts.COURIER));
615615
field.setValue("New value size must be 8, but with different font.");
616616

617-
new Canvas(new PdfCanvas(pdfDoc.getFirstPage()), pdfDoc, new Rectangle(30, 500, 500, 200))
617+
new Canvas(new PdfCanvas(pdfDoc.getFirstPage()), new Rectangle(30, 500, 500, 200))
618618
.add(new Paragraph("The text font after modification it via PDF viewer (e.g. Acrobat) shall be preserved."));
619619

620620
pdfDoc.close();

layout/src/main/java/com/itextpdf/layout/Canvas.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,70 @@ public class Canvas extends RootElement<Canvas> {
9090
* @param rootArea the maximum area that the Canvas may write upon
9191
*/
9292
public Canvas(PdfPage page, Rectangle rootArea) {
93-
this(initPdfCanvasOrThrowIfPageIsFlushed(page), page.getDocument(), rootArea);
93+
this(initPdfCanvasOrThrowIfPageIsFlushed(page), rootArea);
9494
this.enableAutoTagging(page);
9595
this.isCanvasOfPage = true;
9696
}
9797

98+
/**
99+
* Creates a new Canvas to manipulate a specific content stream, which might be for example a page
100+
* or {@link PdfFormXObject} stream.
101+
*
102+
* @param pdfCanvas the low-level content stream writer
103+
* @param rootArea the maximum area that the Canvas may write upon
104+
*/
105+
public Canvas(PdfCanvas pdfCanvas, Rectangle rootArea) {
106+
super();
107+
this.pdfDocument = pdfCanvas.getDocument();
108+
this.pdfCanvas = pdfCanvas;
109+
this.rootArea = rootArea;
110+
}
111+
98112
/**
99113
* Creates a new Canvas to manipulate a specific document and content stream, which might be for example a page
100114
* or {@link PdfFormXObject} stream.
101115
*
102116
* @param pdfCanvas the low-level content stream writer
103117
* @param pdfDocument the document that the resulting content stream will be written to
104118
* @param rootArea the maximum area that the Canvas may write upon
119+
*
120+
* To be removed in 7.2
121+
* @deprecated use {@link Canvas#Canvas(PdfCanvas, Rectangle)} instead.
105122
*/
123+
@Deprecated
106124
public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea) {
107125
super();
108126
this.pdfDocument = pdfDocument;
109127
this.pdfCanvas = pdfCanvas;
110128
this.rootArea = rootArea;
111129
}
112130

131+
/**
132+
* Creates a new Canvas to manipulate a specific document and page.
133+
*
134+
* @param pdfCanvas The low-level content stream writer
135+
* @param rootArea The maximum area that the Canvas may write upon
136+
* @param immediateFlush Whether to flush the canvas immediately after operations, false otherwise
137+
*/
138+
public Canvas(PdfCanvas pdfCanvas, Rectangle rootArea, boolean immediateFlush) {
139+
this(pdfCanvas, rootArea);
140+
this.immediateFlush = immediateFlush;
141+
}
142+
113143
/**
114144
* Creates a new Canvas to manipulate a specific document and page.
115145
*
116146
* @param pdfCanvas The low-level content stream writer
117147
* @param pdfDocument The document that the resulting content stream will be written to
118148
* @param rootArea The maximum area that the Canvas may write upon
119149
* @param immediateFlush Whether to flush the canvas immediately after operations, false otherwise
150+
*
151+
* To be removed in 7.2
152+
* @deprecated use {@link Canvas#Canvas(PdfCanvas, Rectangle, boolean)} instead.
120153
*/
154+
@Deprecated
121155
public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea, boolean immediateFlush) {
122-
this(pdfCanvas, pdfDocument, rootArea);
156+
this(pdfCanvas, rootArea);
123157
this.immediateFlush = immediateFlush;
124158
}
125159

@@ -130,7 +164,7 @@ public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea,
130164
* @param pdfDocument the document that the resulting content stream will be written to
131165
*/
132166
public Canvas(PdfFormXObject formXObject, PdfDocument pdfDocument) {
133-
this(new PdfCanvas(formXObject, pdfDocument), pdfDocument, formXObject.getBBox().toRectangle());
167+
this(new PdfCanvas(formXObject, pdfDocument), formXObject.getBBox().toRectangle());
134168
}
135169

136170
/**

layout/src/test/java/com/itextpdf/layout/CanvasTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void canvasNoPageLinkTest() throws IOException, InterruptedException {
9090
pageSize.getWidth() - 72,
9191
50);
9292

93-
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
93+
Canvas canvas = new Canvas(pdfCanvas, rectangle);
9494
canvas.add(
9595
new Paragraph(
9696
new Link("Google link!", PdfAction.createURI("https://www.google.com"))
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2020 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.layout;
24+
25+
import com.itextpdf.io.source.ByteArrayOutputStream;
26+
import com.itextpdf.kernel.geom.Rectangle;
27+
import com.itextpdf.kernel.pdf.PdfDocument;
28+
import com.itextpdf.kernel.pdf.PdfPage;
29+
import com.itextpdf.kernel.pdf.PdfWriter;
30+
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
31+
import com.itextpdf.test.ExtendedITextTest;
32+
import com.itextpdf.test.annotations.type.UnitTest;
33+
34+
import org.junit.Assert;
35+
import org.junit.Test;
36+
import org.junit.experimental.categories.Category;
37+
38+
@Category(UnitTest.class)
39+
public class CanvasUnitTest extends ExtendedITextTest {
40+
41+
@Test
42+
public void canvasImmediateFlushConstructorTest() {
43+
PdfDocument pdf = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
44+
PdfPage page = pdf.addNewPage();
45+
PdfCanvas pdfCanvas = new PdfCanvas(page.getLastContentStream(), page.getResources(), pdf);
46+
Rectangle rectangle = new Rectangle(0, 0);
47+
Canvas canvas = new Canvas(pdfCanvas, rectangle, false);
48+
49+
Assert.assertEquals(pdfCanvas.getDocument(), canvas.getPdfDocument());
50+
Assert.assertFalse(canvas.immediateFlush);
51+
}
52+
53+
@Test
54+
//TODO remove test after deprecated constructor is removed
55+
public void canvasImmediateFlushDeprecatedConstructorTest() {
56+
PdfDocument pdf = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
57+
PdfPage page = pdf.addNewPage();
58+
Rectangle pageSize = page.getPageSize();
59+
PdfCanvas pdfCanvas = new PdfCanvas(page.getLastContentStream(), page.getResources(), pdf);
60+
Rectangle rectangle = new Rectangle(0, 0);
61+
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle, false);
62+
63+
Assert.assertEquals(pdfCanvas.getDocument(), canvas.getPdfDocument());
64+
Assert.assertFalse(canvas.immediateFlush);
65+
}
66+
}

layout/src/test/java/com/itextpdf/layout/FloatTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public void floatsOnCanvas() throws IOException, InterruptedException {
453453
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outFile)).setTagged();
454454
PdfPage page = pdfDoc.addNewPage();
455455
PdfCanvas pdfCanvas = new PdfCanvas(page);
456-
Canvas canvas = new Canvas(pdfCanvas, pdfDoc, page.getPageSize().applyMargins(36, 36, 36, 36, false));
456+
Canvas canvas = new Canvas(pdfCanvas, page.getPageSize().applyMargins(36, 36, 36, 36, false));
457457
canvas.enableAutoTagging(page);
458458

459459
Div div = new Div().setBackgroundColor(ColorConstants.RED);

layout/src/test/java/com/itextpdf/layout/ImageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ public void precisionTest01() throws IOException, InterruptedException {
902902
PdfCanvas currentPdfCanvas = new PdfCanvas(page);
903903

904904
Rectangle rc = new Rectangle(56.6929131f, 649.13385f, 481.889771f, 136.062988f);
905-
Canvas canvas = new Canvas(currentPdfCanvas, pdfDoc, rc);
905+
Canvas canvas = new Canvas(currentPdfCanvas, rc);
906906

907907
Table table = new Table(UnitValue.createPointArray(new float[]{158f}));
908908
table.setTextAlignment(TextAlignment.LEFT);

layout/src/test/java/com/itextpdf/layout/OverflowTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void overflowHiddenOnCanvasTest01() throws IOException, InterruptedExcept
217217
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
218218

219219
PdfPage page = pdfDocument.addNewPage();
220-
Canvas canvas = new Canvas(new PdfCanvas(page), pdfDocument, page.getPageSize().clone().applyMargins(36, 36, 36, 36, false));
220+
Canvas canvas = new Canvas(new PdfCanvas(page), page.getPageSize().clone().applyMargins(36, 36, 36, 36, false));
221221

222222
addParaWithImgSetOverflowX(canvas, OverflowPropertyValue.HIDDEN);
223223

@@ -251,7 +251,7 @@ public void overflowVisibleOnCanvasTest01() throws IOException, InterruptedExcep
251251
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
252252

253253
PdfPage page = pdfDocument.addNewPage();
254-
Canvas canvas = new Canvas(new PdfCanvas(page), pdfDocument, page.getPageSize().clone().applyMargins(36, 36, 36, 36, false));
254+
Canvas canvas = new Canvas(new PdfCanvas(page), page.getPageSize().clone().applyMargins(36, 36, 36, 36, false));
255255

256256
addParaWithImgSetOverflowX(canvas, OverflowPropertyValue.VISIBLE);
257257

pdfa/src/test/java/com/itextpdf/pdfa/PdfA2LayoutOcgTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void checkIfOcgForPdfA2Works() throws IOException, InterruptedException {
9494

9595
PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc, 1);
9696

97-
Canvas canvas1 = new Canvas(pdfCanvas, pdfDoc, new Rectangle(0, 0, 590, 420));
97+
Canvas canvas1 = new Canvas(pdfCanvas, new Rectangle(0, 0, 590, 420));
9898
PdfLayer imageLayer1 = new PdfLayer("*[email protected]", pdfDoc);
9999
imageLayer1.setOn(true);
100100
pdfCanvas.beginLayer(imageLayer1);

sign/src/main/java/com/itextpdf/signatures/PdfSignatureAppearance.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ private void addTextToCanvas(String text, PdfFont font, Rectangle dataRect) {
901901
PdfCanvas canvas;
902902
canvas = new PdfCanvas(n2, document);
903903
Paragraph paragraph = new Paragraph(text).setFont(font).setMargin(0).setMultipliedLeading(0.9f);
904-
Canvas layoutCanvas = new Canvas(canvas, document, dataRect);
904+
Canvas layoutCanvas = new Canvas(canvas, dataRect);
905905
paragraph.setFontColor(layer2FontColor);
906906
if (layer2FontSize == 0) {
907907
applyCopyFittingFontSize(paragraph, dataRect, layoutCanvas.getRenderer());

0 commit comments

Comments
 (0)