Skip to content

Commit 5691451

Browse files
committed
Implement lang attribute processing
DEVSIX-3243
1 parent 9e01c5b commit 5691451

File tree

250 files changed

+1657
-205
lines changed

Some content is hidden

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

250 files changed

+1657
-205
lines changed

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/Html2PdfProperty.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,7 @@ public class Html2PdfProperty {
9191

9292
/** The Constant FORM_FIELD_SELECTED. */
9393
public static final int FORM_FIELD_LABEL = PROPERTY_START + 14;
94+
95+
/** The Constant FORM_ACCESSIBILITY_LANGUAGE. */
96+
public static final int FORM_ACCESSIBILITY_LANGUAGE = PROPERTY_START + 15;
9497
}

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/PageMarginBoxDummyElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public IAttributes getAttributes() {
7373

7474
@Override
7575
public String getAttribute(String key) {
76-
throw new UnsupportedOperationException();
76+
return null;
7777
}
7878

7979
@Override

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/element/FormField.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,4 @@ public <T1> T1 getDefaultProperty(int property) {
8989
return super.<T1>getDefaultProperty(property);
9090
}
9191
}
92-
9392
}

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/AbstractFormFieldRenderer.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.html2pdf.LogMessageConstant;
4646
import com.itextpdf.html2pdf.attach.impl.layout.Html2PdfProperty;
4747
import com.itextpdf.html2pdf.attach.impl.layout.form.element.IFormField;
48+
import com.itextpdf.html2pdf.attach.util.AccessiblePropHelper;
49+
import com.itextpdf.io.font.PdfEncodings;
4850
import com.itextpdf.kernel.geom.Rectangle;
51+
import com.itextpdf.kernel.pdf.PdfDocument;
52+
import com.itextpdf.kernel.pdf.PdfString;
53+
import com.itextpdf.kernel.pdf.tagging.PdfStructElem;
54+
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
55+
import com.itextpdf.kernel.pdf.tagutils.TagTreePointer;
56+
import com.itextpdf.layout.IPropertyContainer;
4957
import com.itextpdf.layout.layout.LayoutContext;
5058
import com.itextpdf.layout.layout.LayoutResult;
5159
import com.itextpdf.layout.layout.MinMaxWidthLayoutResult;
@@ -58,6 +66,9 @@ This file is part of the iText (R) project.
5866
import com.itextpdf.layout.renderer.DrawContext;
5967
import com.itextpdf.layout.renderer.ILeafElementRenderer;
6068
import com.itextpdf.layout.renderer.IRenderer;
69+
import com.itextpdf.layout.tagging.IAccessibleElement;
70+
71+
import java.util.List;
6172
import org.slf4j.LoggerFactory;
6273

6374
/**
@@ -126,6 +137,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
126137
setProperty(Property.FORCED_PLACEMENT, true);
127138
} else {
128139
flatRenderer = childRenderers.get(0);
140+
processLangAttribute();
129141
childRenderers.clear();
130142
childRenderers.add(flatRenderer);
131143
adjustFieldLayout(layoutContext);
@@ -142,6 +154,7 @@ public LayoutResult layout(LayoutContext layoutContext) {
142154
}
143155
if (!childRenderers.isEmpty()) {
144156
flatRenderer = childRenderers.get(0);
157+
processLangAttribute();
145158
childRenderers.clear();
146159
childRenderers.add(flatRenderer);
147160
adjustFieldLayout(layoutContext);
@@ -279,6 +292,15 @@ protected Float getContentWidth() {
279292
return super.retrieveWidth(0);
280293
}
281294

295+
/**
296+
* Gets the accessibility language.
297+
*
298+
* @return the accessibility language
299+
*/
300+
protected String getLang() {
301+
return this.<String>getProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE);
302+
}
303+
282304
//NOTE: should be removed in 3.0.0
283305
@Override
284306
protected Float retrieveWidth(float parentBoxWidth) {
@@ -292,4 +314,25 @@ protected Float retrieveWidth(float parentBoxWidth) {
292314
protected boolean isLayoutBasedOnFlatRenderer() {
293315
return true;
294316
}
317+
318+
protected void writeAcroFormFieldLangAttribute(PdfDocument pdfDoc) {
319+
if (pdfDoc.isTagged()) {
320+
TagTreePointer formParentPointer = pdfDoc.getTagStructureContext().getAutoTaggingPointer();
321+
List<String> kidsRoles = formParentPointer.getKidsRoles();
322+
int lastFormIndex = kidsRoles.lastIndexOf(StandardRoles.FORM);
323+
TagTreePointer formPointer = formParentPointer.moveToKid(lastFormIndex);
324+
325+
if (getLang() != null) {
326+
formPointer.getProperties().setLanguage(getLang());
327+
}
328+
formParentPointer.moveToParent();
329+
}
330+
}
331+
332+
private void processLangAttribute() {
333+
IPropertyContainer propertyContainer = flatRenderer.getModelElement();
334+
if (propertyContainer instanceof IAccessibleElement) {
335+
AccessiblePropHelper.trySetLangAttribute((IAccessibleElement) propertyContainer, getLang());
336+
}
337+
}
295338
}

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/AbstractSelectFieldRenderer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ public void drawChildren(DrawContext drawContext) {
144144
}
145145
}
146146

147+
/**
148+
* Gets the accessibility language.
149+
*
150+
* @return the accessibility language
151+
*/
152+
protected String getLang() {
153+
return this.<String>getProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE);
154+
}
155+
147156
protected abstract IRenderer createFlatRenderer();
148157

149158
protected abstract void applyAcroField(DrawContext drawContext);

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/ButtonContainerRenderer.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ This file is part of the iText (R) project.
5252
import com.itextpdf.kernel.pdf.PdfDocument;
5353
import com.itextpdf.kernel.pdf.PdfPage;
5454
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
55+
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
56+
import com.itextpdf.kernel.pdf.tagutils.TagTreePointer;
5557
import com.itextpdf.layout.property.Property;
5658
import com.itextpdf.layout.property.TransparentColor;
5759
import com.itextpdf.layout.property.UnitValue;
5860
import com.itextpdf.layout.renderer.BlockRenderer;
5961
import com.itextpdf.layout.renderer.DrawContext;
6062
import com.itextpdf.layout.renderer.IRenderer;
6163

64+
import java.util.List;
65+
6266
/**
6367
* The {@link AbstractOneLineTextFieldRenderer} implementation for buttons with kids.
6468
* @deprecated Will be renamed to {@code ButtonRenderer} in next major release.
@@ -99,6 +103,19 @@ public void draw(DrawContext drawContext) {
99103
if (forms.getField(name) == null) {
100104
forms.addField(button, page);
101105
}
106+
107+
if (doc.isTagged()) {
108+
TagTreePointer formParentPointer = doc.getTagStructureContext().getAutoTaggingPointer();
109+
List<String> kidsRoles = formParentPointer.getKidsRoles();
110+
int lastFormIndex = kidsRoles.lastIndexOf(StandardRoles.FORM);
111+
TagTreePointer formPointer = formParentPointer.moveToKid(lastFormIndex);
112+
113+
String lang = this.<String>getProperty(Html2PdfProperty.FORM_ACCESSIBILITY_LANGUAGE);
114+
if (lang != null) {
115+
formPointer.getProperties().setLanguage(lang);
116+
}
117+
formParentPointer.moveToParent();
118+
}
102119
}
103120
}
104121

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/ButtonRenderer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ protected void applyAcroField(DrawContext drawContext) {
155155
}
156156
applyDefaultFieldProperties(button);
157157
PdfAcroForm.getAcroForm(doc, true).addField(button, page);
158+
159+
writeAcroFormFieldLangAttribute(doc);
158160
}
159161

160162
/* (non-Javadoc)

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/CheckBoxRenderer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ protected void applyAcroField(DrawContext drawContext) {
133133
PdfPage page = doc.getPage(occupiedArea.getPageNumber());
134134
PdfButtonFormField checkBox = PdfFormField.createCheckBox(doc, area, name, isBoxChecked() ? "Yes" : "Off");
135135
PdfAcroForm.getAcroForm(doc, true).addField(checkBox, page);
136+
137+
writeAcroFormFieldLangAttribute(doc);
136138
}
137139

138140
@Override

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/InputFieldRenderer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ protected void applyAcroField(DrawContext drawContext) {
183183
}
184184
applyDefaultFieldProperties(inputField);
185185
PdfAcroForm.getAcroForm(doc, true).addField(inputField, page);
186+
187+
writeAcroFormFieldLangAttribute(doc);
186188
}
187189

188190
@Override

src/main/java/com/itextpdf/html2pdf/attach/impl/layout/form/renderer/RadioRenderer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,17 @@ protected void applyAcroField(DrawContext drawContext) {
151151
if (isBoxChecked()) {
152152
radioGroup.setValue(getModelId());
153153
}
154-
PdfFormField.createRadioButton(doc, area, radioGroup, getModelId())
155-
.setCheckType(PdfFormField.TYPE_CIRCLE);
154+
155+
PdfFormField radio = PdfFormField.createRadioButton(doc, area, radioGroup, getModelId());
156+
radio.setCheckType(PdfFormField.TYPE_CIRCLE);
157+
156158
if (addNew) {
157159
form.addField(radioGroup, page);
158160
} else {
159161
form.replaceField(getModelId(), radioGroup);
160162
}
163+
164+
writeAcroFormFieldLangAttribute(doc);
161165
}
162166

163167
@Override

0 commit comments

Comments
 (0)