Skip to content

Commit 8ab16e4

Browse files
committed
Improve checkbox rendering and remove old methods
DEVSIX-7443
1 parent 1b610a2 commit 8ab16e4

File tree

63 files changed

+180
-333
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+180
-333
lines changed

commons/src/main/java/com/itextpdf/commons/utils/ExperimentalFeatures.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ This file is part of the iText (R) project.
2727
*/
2828
// TODO Shall be removed in the scope of DEVSIX-7385
2929
public final class ExperimentalFeatures {
30-
/**
31-
* Determines, whether the old or the new checkbox form field drawing logic will be used.
32-
*/
33-
public static boolean ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING = false;
3430

3531
/**
3632
* Determines, whether the old or the new text form field drawing logic will be used.

commons/src/test/java/com/itextpdf/commons/utils/ExperimentalFeaturesTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public class ExperimentalFeaturesTest extends ExtendedITextTest {
3434

3535
@Test
3636
public void testDefaults() {
37-
Assert.assertFalse(ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING);
3837
Assert.assertTrue(ExperimentalFeatures.ENABLE_EXPERIMENTAL_TEXT_FORM_RENDERING);
3938
}
4039
}

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.forms.fields;
2424

25-
import com.itextpdf.commons.utils.ExperimentalFeatures;
2625
import com.itextpdf.forms.fields.properties.CheckBoxType;
2726
import com.itextpdf.kernel.pdf.PdfDocument;
2827
import com.itextpdf.kernel.pdf.PdfName;
@@ -90,21 +89,8 @@ public PdfButtonFormField createCheckBox() {
9089
check.put(PdfName.V, new PdfName(PdfFormAnnotation.OFF_STATE_VALUE));
9190

9291
if (getWidgetRectangle() != null) {
93-
//TODO DEVSIX-7443 remove flag
94-
if (ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING) {
95-
check.getFirstFormAnnotation()
96-
.drawCheckBoxAndSaveAppearanceExperimental(PdfFormAnnotation.ON_STATE_VALUE);
97-
setPageToField(check);
98-
return check;
99-
}
100-
//TODO DEVSIX-7443 remove from here till end
101-
if (getConformanceLevel() == null) {
102-
check.getFirstFormAnnotation().drawCheckAppearance(getWidgetRectangle().getWidth(),
103-
getWidgetRectangle().getHeight(), PdfFormAnnotation.ON_STATE_VALUE);
104-
} else {
105-
check.getFirstFormAnnotation().drawPdfA2CheckAppearance(getWidgetRectangle().getWidth(),
106-
getWidgetRectangle().getHeight(), PdfFormAnnotation.ON_STATE_VALUE, checkType);
107-
}
92+
check.getFirstFormAnnotation()
93+
.drawCheckBoxAndSaveAppearance(PdfFormAnnotation.ON_STATE_VALUE);
10894
setPageToField(check);
10995
}
11096

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

Lines changed: 45 additions & 205 deletions
Large diffs are not rendered by default.

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ This file is part of the iText (R) project.
5454
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
5555
import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation;
5656
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
57-
import com.itextpdf.layout.properties.HorizontalAlignment;
5857
import com.itextpdf.layout.properties.TextAlignment;
5958

6059
import java.io.ByteArrayOutputStream;
@@ -113,7 +112,6 @@ public class PdfFormField extends AbstractPdfFormField {
113112
*/
114113
private static final Set<PdfName> FORM_FIELD_KEYS = new HashSet<>();
115114

116-
private static final String[] CHECKBOX_TYPE_ZAPFDINGBATS_CODE = {"4", "l", "8", "u", "n", "H"};
117115

118116
private static final Logger LOGGER = LoggerFactory.getLogger(PdfFormField.class);
119117

@@ -1068,7 +1066,6 @@ public PdfFormField setCheckType(CheckBoxType checkType) {
10681066
checkType = CheckBoxType.CROSS;
10691067
}
10701068
this.checkType = checkType;
1071-
text = CHECKBOX_TYPE_ZAPFDINGBATS_CODE[checkType.ordinal()];
10721069
if (getPdfAConformanceLevel() != null) {
10731070
return this;
10741071
}

forms/src/main/java/com/itextpdf/forms/form/renderer/CheckBoxRenderer.java

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.forms.form.renderer;
2424

25-
import com.itextpdf.commons.utils.ExperimentalFeatures;
2625
import com.itextpdf.forms.PdfAcroForm;
2726
import com.itextpdf.forms.fields.CheckBoxFormFieldBuilder;
2827
import com.itextpdf.forms.fields.PdfButtonFormField;
@@ -34,21 +33,19 @@ This file is part of the iText (R) project.
3433
import com.itextpdf.forms.form.renderer.checkboximpl.ICheckBoxRenderingStrategy;
3534
import com.itextpdf.forms.form.renderer.checkboximpl.PdfACheckBoxRenderingStrategy;
3635
import com.itextpdf.forms.form.renderer.checkboximpl.PdfCheckBoxRenderingStrategy;
37-
import com.itextpdf.kernel.colors.Color;
38-
import com.itextpdf.kernel.colors.ColorConstants;
3936
import com.itextpdf.kernel.geom.Rectangle;
4037
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
4138
import com.itextpdf.kernel.pdf.PdfDocument;
4239
import com.itextpdf.kernel.pdf.PdfPage;
4340
import com.itextpdf.layout.borders.Border;
44-
import com.itextpdf.layout.borders.SolidBorder;
4541
import com.itextpdf.layout.element.Paragraph;
4642
import com.itextpdf.layout.layout.LayoutContext;
4743
import com.itextpdf.layout.properties.Background;
4844
import com.itextpdf.layout.properties.BoxSizingPropertyValue;
4945
import com.itextpdf.layout.properties.HorizontalAlignment;
5046
import com.itextpdf.layout.properties.Property;
5147
import com.itextpdf.layout.properties.RenderingMode;
48+
import com.itextpdf.layout.properties.TextAlignment;
5249
import com.itextpdf.layout.properties.UnitValue;
5350
import com.itextpdf.layout.properties.VerticalAlignment;
5451
import com.itextpdf.layout.renderer.DrawContext;
@@ -63,18 +60,18 @@ public class CheckBoxRenderer extends AbstractFormFieldRenderer {
6360

6461
// 1px
6562
public static final float DEFAULT_BORDER_WIDTH = 0.75F;
66-
private static final Color DEFAULT_BORDER_COLOR = ColorConstants.DARK_GRAY;
67-
private static final Color DEFAULT_BACKGROUND_COLOR = ColorConstants.WHITE;
6863
// 11px
6964
private static final float DEFAULT_SIZE = 8.25F;
7065

66+
7167
/**
7268
* Creates a new {@link CheckBoxRenderer} instance.
7369
*
7470
* @param modelElement the model element
7571
*/
7672
public CheckBoxRenderer(CheckBox modelElement) {
7773
super(modelElement);
74+
this.setProperty(Property.VERTICAL_ALIGNMENT, VerticalAlignment.MIDDLE);
7875
}
7976

8077
/* (non-Javadoc)
@@ -141,25 +138,16 @@ public ICheckBoxRenderingStrategy createCheckBoxRenderStrategy() {
141138

142139
@Override
143140
public void drawBackground(DrawContext drawContext) {
144-
if (!ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING) {
145-
super.drawBackground(drawContext);
146-
}
147141
// draw background in child
148142
}
149143

150144
@Override
151145
public void drawBorder(DrawContext drawContext) {
152-
if (!ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING) {
153-
super.drawBorder(drawContext);
154-
}
155146
//draw border in child
156147
}
157148

158149
@Override
159150
protected Rectangle applyBorderBox(Rectangle rect, Border[] borders, boolean reverse) {
160-
if (!ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING) {
161-
return super.applyBorderBox(rect, borders, reverse);
162-
}
163151
// Do not apply borders here, they will be applied in flat renderer
164152
return rect;
165153
}
@@ -173,12 +161,14 @@ public boolean isBoxChecked() {
173161
return Boolean.TRUE.equals(this.<Boolean>getProperty(FormProperty.FORM_FIELD_CHECKED));
174162
}
175163

176-
/* (non-Javadoc)
177-
* @see com.itextpdf.html2pdf.attach.impl.layout.form.renderer.AbstractFormFieldRenderer#adjustFieldLayout()
164+
/**
165+
* Adjusts the field layout.
166+
*
167+
* @param layoutContext layout context
178168
*/
179169
@Override
180170
protected void adjustFieldLayout(LayoutContext layoutContext) {
181-
this.setProperty(Property.BACKGROUND, null);
171+
//we don't need any layout adjustments
182172
}
183173

184174
/**
@@ -188,20 +178,25 @@ protected void adjustFieldLayout(LayoutContext layoutContext) {
188178
*/
189179
@Override
190180
public IRenderer createFlatRenderer() {
191-
if (!ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING) {
192-
final Paragraph paragraph = new Paragraph().setWidth(DEFAULT_SIZE).setHeight(DEFAULT_SIZE)
193-
.setBorder(new SolidBorder(DEFAULT_BORDER_COLOR, DEFAULT_BORDER_WIDTH))
194-
.setBackgroundColor(DEFAULT_BACKGROUND_COLOR).setHorizontalAlignment(HorizontalAlignment.CENTER);
195-
return new FlatParagraphRenderer(paragraph);
196-
}
197181
final UnitValue heightUV = getPropertyAsUnitValue(Property.HEIGHT);
198182
final UnitValue widthUV = getPropertyAsUnitValue(Property.WIDTH);
199183

200-
final float height = null == heightUV ? DEFAULT_SIZE : heightUV.getValue();
201-
final float width = null == widthUV ? DEFAULT_SIZE : widthUV.getValue();
184+
// if it is a percentage value, we need to calculate the actual value but we
185+
// don't have the parent's width yet, so we will take the default value
186+
float height = DEFAULT_SIZE;
187+
if (heightUV != null && heightUV.isPointValue()) {
188+
height = heightUV.getValue();
189+
}
190+
191+
float width = DEFAULT_SIZE;
192+
if (widthUV != null && widthUV.isPointValue()) {
193+
width = widthUV.getValue();
194+
}
202195

203196
final Paragraph paragraph = new Paragraph().setWidth(width).setHeight(height).setMargin(0)
204-
.setVerticalAlignment(VerticalAlignment.MIDDLE).setHorizontalAlignment(HorizontalAlignment.CENTER);
197+
.setVerticalAlignment(VerticalAlignment.MIDDLE)
198+
.setHorizontalAlignment(HorizontalAlignment.CENTER)
199+
.setTextAlignment(TextAlignment.CENTER);
205200

206201
paragraph.setProperty(Property.BOX_SIZING, this.<BoxSizingPropertyValue>getProperty(Property.BOX_SIZING));
207202
modelElement.setProperty(Property.RENDERING_MODE, this.<RenderingMode>getProperty(Property.RENDERING_MODE));
@@ -225,20 +220,20 @@ protected void applyAcroField(DrawContext drawContext) {
225220
final PdfPage page = doc.getPage(occupiedArea.getPageNumber());
226221
final CheckBoxFormFieldBuilder builder = new CheckBoxFormFieldBuilder(doc, name).setWidgetRectangle(area)
227222
.setConformanceLevel(this.<PdfAConformanceLevel>getProperty(FormProperty.FORM_CONFORMANCE_LEVEL));
223+
228224
if (this.hasProperty(FormProperty.FORM_CHECKBOX_TYPE)) {
229225
builder.setCheckType((CheckBoxType) this.<CheckBoxType>getProperty(FormProperty.FORM_CHECKBOX_TYPE));
230226
}
231227
final PdfButtonFormField checkBox = builder.createCheckBox();
232-
if (ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING) {
233-
final Border border = this.<Border>getProperty(Property.BORDER);
234-
if (border != null) {
235-
checkBox.getFirstFormAnnotation().setBorderColor(border.getColor());
236-
checkBox.getFirstFormAnnotation().setBorderWidth(border.getWidth());
237-
}
238-
final Background background = this.modelElement.<Background>getProperty(Property.BACKGROUND);
239-
if (background != null) {
240-
checkBox.getFirstFormAnnotation().setBackgroundColor(background.getColor());
241-
}
228+
checkBox.getFirstFormAnnotation().setRenderingMode(this.getRenderingMode());
229+
final Border border = this.<Border>getProperty(Property.BORDER);
230+
if (border != null) {
231+
checkBox.getFirstFormAnnotation().setBorderColor(border.getColor());
232+
checkBox.getFirstFormAnnotation().setBorderWidth(border.getWidth());
233+
}
234+
final Background background = this.modelElement.<Background>getProperty(Property.BACKGROUND);
235+
if (background != null) {
236+
checkBox.getFirstFormAnnotation().setBackgroundColor(background.getColor());
242237
}
243238

244239
checkBox.setValue(PdfFormAnnotation.ON_STATE_VALUE);

forms/src/main/java/com/itextpdf/forms/form/renderer/checkboximpl/PdfCheckBoxRenderingStrategy.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,26 @@ This file is part of the iText (R) project.
3838
import com.itextpdf.layout.properties.Property;
3939
import com.itextpdf.layout.renderer.DrawContext;
4040

41+
import java.util.Collections;
4142
import java.util.HashMap;
43+
import java.util.Map;
4244

4345
/**
4446
* This class is used to draw a checkBox icon in PDF mode this is the default strategy for drawing a checkBox.
4547
*/
4648
public final class PdfCheckBoxRenderingStrategy implements ICheckBoxRenderingStrategy {
47-
private static final HashMap<CheckBoxType, String> CHECKBOX_TYPE_ZAPFDINGBATS_CODE = new HashMap<>();
49+
public static final Map<CheckBoxType, String> CHECKBOX_TYPE_ZAPFDINGBATS_CODE;
4850

4951
static {
50-
CHECKBOX_TYPE_ZAPFDINGBATS_CODE.put(CheckBoxType.CHECK, "4");
51-
CHECKBOX_TYPE_ZAPFDINGBATS_CODE.put(CheckBoxType.CIRCLE, "l");
52-
CHECKBOX_TYPE_ZAPFDINGBATS_CODE.put(CheckBoxType.CROSS, "8");
53-
CHECKBOX_TYPE_ZAPFDINGBATS_CODE.put(CheckBoxType.DIAMOND, "u");
54-
CHECKBOX_TYPE_ZAPFDINGBATS_CODE.put(CheckBoxType.SQUARE, "n");
55-
CHECKBOX_TYPE_ZAPFDINGBATS_CODE.put(CheckBoxType.STAR, "H");
52+
Map<CheckBoxType, String> initialMap = new HashMap<>();
53+
initialMap.put(CheckBoxType.CHECK, "4");
54+
initialMap.put(CheckBoxType.CIRCLE, "l");
55+
initialMap.put(CheckBoxType.CROSS, "8");
56+
initialMap.put(CheckBoxType.DIAMOND, "u");
57+
initialMap.put(CheckBoxType.SQUARE, "n");
58+
initialMap.put(CheckBoxType.STAR, "H");
59+
CHECKBOX_TYPE_ZAPFDINGBATS_CODE = Collections.unmodifiableMap(initialMap);
60+
5661
}
5762

5863
/**

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.forms;
2424

25-
import com.itextpdf.commons.utils.ExperimentalFeatures;
2625
import com.itextpdf.forms.fields.CheckBoxFormFieldBuilder;
2726
import com.itextpdf.forms.fields.NonTerminalFormFieldBuilder;
2827
import com.itextpdf.forms.fields.PdfFormField;
@@ -38,9 +37,7 @@ This file is part of the iText (R) project.
3837
import com.itextpdf.test.annotations.type.IntegrationTest;
3938

4039
import java.io.IOException;
41-
import org.junit.After;
4240
import org.junit.Assert;
43-
import org.junit.Before;
4441
import org.junit.BeforeClass;
4542
import org.junit.Test;
4643
import org.junit.experimental.categories.Category;
@@ -60,19 +57,6 @@ public static void beforeClass() {
6057
createDestinationFolder(DESTINATION_DIR);
6158
}
6259

63-
private boolean experimentalCheckboxRendering;
64-
65-
@Before
66-
public void before() {
67-
experimentalCheckboxRendering = ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING;
68-
ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING = false;
69-
}
70-
71-
@After
72-
public void after() {
73-
ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING = experimentalCheckboxRendering;
74-
}
75-
7660
@Test
7761
public void addFieldTest() throws IOException, InterruptedException {
7862
String outputFile = "addFieldTest.pdf";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ This file is part of the iText (R) project.
2929
import com.itextpdf.forms.fields.NonTerminalFormFieldBuilder;
3030
import com.itextpdf.forms.fields.PdfButtonFormField;
3131
import com.itextpdf.forms.fields.PdfChoiceFormField;
32-
import com.itextpdf.forms.fields.PdfFormField;
3332
import com.itextpdf.forms.fields.PdfFormAnnotation;
33+
import com.itextpdf.forms.fields.PdfFormField;
3434
import com.itextpdf.forms.fields.PdfTextFormField;
3535
import com.itextpdf.forms.fields.PushButtonFormFieldBuilder;
3636
import com.itextpdf.forms.fields.RadioFormFieldBuilder;

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.forms;
2424

25-
import com.itextpdf.commons.utils.ExperimentalFeatures;
2625
import com.itextpdf.forms.xfdf.XfdfObject;
2726
import com.itextpdf.forms.xfdf.XfdfObjectFactory;
2827
import com.itextpdf.io.logs.IoLogMessageConstant;
@@ -39,9 +38,7 @@ This file is part of the iText (R) project.
3938
import java.io.FileOutputStream;
4039
import java.io.IOException;
4140
import javax.xml.parsers.ParserConfigurationException;
42-
import org.junit.After;
4341
import org.junit.Assert;
44-
import org.junit.Before;
4542
import org.junit.BeforeClass;
4643
import org.junit.Test;
4744
import org.junit.experimental.categories.Category;
@@ -60,20 +57,6 @@ public static void beforeClass() {
6057
createDestinationFolder(destinationFolder);
6158
}
6259

63-
64-
private boolean experimentalCheckboxRendering;
65-
66-
@Before
67-
public void before() {
68-
experimentalCheckboxRendering = ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING;
69-
ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING = false;
70-
}
71-
72-
@After
73-
public void after() {
74-
ExperimentalFeatures.ENABLE_EXPERIMENTAL_CHECKBOX_RENDERING = experimentalCheckboxRendering;
75-
}
76-
7760
@Test
7861
@LogMessages(messages = @LogMessage(messageTemplate =
7962
IoLogMessageConstant.XFDF_HREF_ATTRIBUTE_AND_PDF_DOCUMENT_NAME_ARE_DIFFERENT))

0 commit comments

Comments
 (0)