Skip to content

Commit 4cdd4ff

Browse files
Utilize new LayoutTaggingHelper logic in layout classes
DEVSIX-1652
1 parent da9564b commit 4cdd4ff

18 files changed

+215
-296
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ This file is part of the iText (R) project.
5656
/**
5757
* This class is used for adding content directly onto a specified {@link PdfCanvas}.
5858
* {@link Canvas} does not know the concept of a page, so it can't reflow to a 'next' {@link Canvas}.
59-
*
59+
*
6060
* This class effectively acts as a bridge between the high-level <em>layout</em>
6161
* API and the low-level <em>kernel</em> API.
6262
*/
@@ -73,7 +73,7 @@ public class Canvas extends RootElement<Canvas> {
7373

7474
/**
7575
* Creates a new Canvas to manipulate a specific document and page.
76-
*
76+
*
7777
* @param pdfCanvas the low-level content stream writer
7878
* @param pdfDocument the document that the resulting content stream will be written to
7979
* @param rootArea the maximum area that the Canvas may write upon
@@ -100,7 +100,7 @@ public Canvas(PdfCanvas pdfCanvas, PdfDocument pdfDocument, Rectangle rootArea,
100100

101101
/**
102102
* Creates a new Canvas to manipulate a specific {@link PdfFormXObject}.
103-
*
103+
*
104104
* @param formXObject the form
105105
* @param pdfDocument the document that the resulting content stream will be written to
106106
*/
@@ -134,7 +134,7 @@ public PdfCanvas getPdfCanvas() {
134134

135135
/**
136136
* Sets the {@link IRenderer} for this Canvas.
137-
*
137+
*
138138
* @param canvasRenderer a renderer specific for canvas operations
139139
*/
140140
public void setRenderer(CanvasRenderer canvasRenderer) {
@@ -168,7 +168,7 @@ public boolean isAutoTaggingEnabled() {
168168
* Performs an entire recalculation of the element flow on the canvas,
169169
* taking into account all its current child elements. May become very
170170
* resource-intensive for large documents.
171-
*
171+
*
172172
* Do not use when you have set {@link #immediateFlush} to <code>true</code>.
173173
*/
174174
public void relayout() {
@@ -182,7 +182,7 @@ public void relayout() {
182182
}
183183
rootRenderer = (RootRenderer) nextRelayoutRenderer;
184184
for (IElement element : childElements) {
185-
rootRenderer.addChild(element.createRendererSubTree());
185+
createAndAddRendererSubTree(element);
186186
}
187187
}
188188

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void relayout() {
198198

199199
rootRenderer = (RootRenderer) nextRelayoutRenderer;
200200
for (IElement element : childElements) {
201-
rootRenderer.addChild(element.createRendererSubTree());
201+
createAndAddRendererSubTree(element);
202202
}
203203
}
204204

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ This file is part of the iText (R) project.
6464
import com.itextpdf.layout.property.VerticalAlignment;
6565
import com.itextpdf.layout.renderer.IRenderer;
6666
import com.itextpdf.layout.renderer.RootRenderer;
67+
import com.itextpdf.layout.tagging.LayoutTaggingHelper;
6768
import com.itextpdf.layout.splitting.DefaultSplitCharacters;
6869
import com.itextpdf.layout.splitting.ISplitCharacters;
6970

7071
import java.io.Closeable;
7172
import java.io.IOException;
7273
import java.util.ArrayList;
73-
import java.util.HashMap;
74+
import java.util.Collections;
7475
import java.util.List;
75-
import java.util.Map;
7676

7777
/**
7878
* A generic abstract root element for a PDF layout object hierarchy.
@@ -92,6 +92,8 @@ public abstract class RootElement<T extends IPropertyContainer> extends ElementP
9292

9393
protected RootRenderer rootRenderer;
9494

95+
private LayoutTaggingHelper defaultLayoutTaggingHelper;
96+
9597
/**
9698
* Adds an element to the root. The element is immediately placed in the contents.
9799
*
@@ -101,7 +103,7 @@ public abstract class RootElement<T extends IPropertyContainer> extends ElementP
101103
*/
102104
public T add(IBlockElement element) {
103105
childElements.add(element);
104-
ensureRootRendererNotNull().addChild(element.createRendererSubTree());
106+
createAndAddRendererSubTree(element);
105107
if (immediateFlush) {
106108
childElements.remove(childElements.size() - 1);
107109
}
@@ -117,7 +119,7 @@ public T add(IBlockElement element) {
117119
*/
118120
public T add(Image image) {
119121
childElements.add(image);
120-
ensureRootRendererNotNull().addChild(image.createRendererSubTree());
122+
createAndAddRendererSubTree(image);
121123
if (immediateFlush) {
122124
childElements.remove(childElements.size() - 1);
123125
}
@@ -188,6 +190,8 @@ public <T1> T1 getDefaultProperty(int property) {
188190
return (T1) (Object) defaultSplitCharacters;
189191
case Property.FONT_SIZE:
190192
return (T1) (Object) UnitValue.createPointValue(12);
193+
case Property.TAGGING_HELPER:
194+
return (T1) (Object) initTaggingHelperIfNeeded();
191195
case Property.TEXT_RENDERING_MODE:
192196
return (T1) (Object) PdfCanvasConstants.TextRenderingMode.FILL;
193197
case Property.TEXT_RISE:
@@ -363,4 +367,17 @@ public T showTextAligned(Paragraph p, float x, float y, int pageNumber, TextAlig
363367
}
364368

365369
protected abstract RootRenderer ensureRootRendererNotNull();
370+
371+
protected void createAndAddRendererSubTree(IElement element) {
372+
IRenderer rendererSubTreeRoot = element.createRendererSubTree();
373+
LayoutTaggingHelper taggingHelper = initTaggingHelperIfNeeded();
374+
if (taggingHelper != null) {
375+
taggingHelper.addKidsHint(pdfDocument.getTagStructureContext().getAutoTaggingPointer(), Collections.<IRenderer>singletonList(rendererSubTreeRoot));
376+
}
377+
ensureRootRendererNotNull().addChild(rendererSubTreeRoot);
378+
}
379+
380+
private LayoutTaggingHelper initTaggingHelperIfNeeded() {
381+
return defaultLayoutTaggingHelper == null && pdfDocument.isTagged() ? defaultLayoutTaggingHelper = new LayoutTaggingHelper(pdfDocument, immediateFlush) : defaultLayoutTaggingHelper;
382+
}
366383
}

layout/src/main/java/com/itextpdf/layout/element/AbstractElement.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ This file is part of the iText (R) project.
4343
*/
4444
package com.itextpdf.layout.element;
4545

46-
import com.itextpdf.kernel.pdf.PdfName;
4746
import com.itextpdf.kernel.pdf.action.PdfAction;
48-
import com.itextpdf.kernel.pdf.tagutils.IAccessibleElement;
4947
import com.itextpdf.layout.ElementPropertyContainer;
5048
import com.itextpdf.layout.Style;
5149
import com.itextpdf.layout.property.Property;
5250
import com.itextpdf.layout.renderer.IRenderer;
53-
5451
import java.util.ArrayList;
5552
import java.util.LinkedHashSet;
5653
import java.util.List;
@@ -59,7 +56,7 @@ This file is part of the iText (R) project.
5956
/**
6057
* Defines the most common properties that most {@link IElement} implementations
6158
* share.
62-
*
59+
*
6360
* @param <T> the type of the implementation
6461
*/
6562
public abstract class AbstractElement<T extends IElement> extends ElementPropertyContainer<T> implements IElement {
@@ -123,6 +120,7 @@ public <T1> T1 getProperty(int property) {
123120
/**
124121
* Add a new style to this element. A style can be used as an effective way
125122
* to define multiple equal properties to several elements.
123+
*
126124
* @param style the style to be added
127125
* @return this element
128126
*/
@@ -131,30 +129,18 @@ public T addStyle(Style style) {
131129
styles = new LinkedHashSet<>();
132130
}
133131
styles.add(style);
134-
return (T) (Object)this;
132+
return (T) (Object) this;
135133
}
136134

137135
/**
138136
* Gets the child elements of this elements
137+
*
139138
* @return a list of children
140139
*/
141140
public List<IElement> getChildren() {
142141
return childElements;
143142
}
144143

145-
protected abstract IRenderer makeNewRenderer();
146-
147-
/**
148-
* Marks all child elements as artifacts recursively.
149-
*/
150-
protected void propagateArtifactRoleToChildElements() {
151-
for (IElement child : childElements) {
152-
if (child instanceof IAccessibleElement) {
153-
((IAccessibleElement) child).setRole(PdfName.Artifact);
154-
}
155-
}
156-
}
157-
158144
/**
159145
* Returns <code>true</code> if this list contains no elements.
160146
*
@@ -168,7 +154,7 @@ public boolean isEmpty() {
168154
* Sets an action on this Element. An action is a general PDF concept that
169155
* signifies anything that makes the document interactive, e.g. a hyperlink
170156
* or a button.
171-
*
157+
*
172158
* @param action the {@link PdfAction} that should be performed
173159
* @return this Element
174160
*/
@@ -181,14 +167,16 @@ public T setAction(PdfAction action) {
181167
* Explicitly sets the page number this element should be put on. The location
182168
* on the page will be the same as if it were added at the end of the document,
183169
* but it will be located on the specified page.
184-
*
170+
* <p>
185171
* This method should be used very carefully in client code.
186-
*
172+
*
187173
* @param pageNumber the page number of the page this element should be placed on
188174
* @return this Element
189175
*/
190176
public T setPageNumber(int pageNumber) {
191177
setProperty(Property.PAGE_NUMBER, pageNumber);
192178
return (T) (Object) this;
193179
}
180+
181+
protected abstract IRenderer makeNewRenderer();
194182
}

layout/src/main/java/com/itextpdf/layout/element/Cell.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,6 @@ public PdfName getRole() {
223223
@Override
224224
public void setRole(PdfName role) {
225225
this.role = role;
226-
if (PdfName.Artifact.equals(role)) {
227-
propagateArtifactRoleToChildElements();
228-
}
229226
}
230227

231228
@Override

layout/src/main/java/com/itextpdf/layout/element/Div.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ This file is part of the iText (R) project.
5353
* A {@link Div} is a container object that defines a section in a document,
5454
* which will have some shared layout properties. Like all {@link BlockElement}
5555
* types, it will try to take up as much horizontal space as possible.
56-
*
56+
*
5757
* The concept is very similar to that of the div tag in HTML.
5858
*/
5959
public class Div extends BlockElement<Div> {
@@ -63,18 +63,18 @@ public class Div extends BlockElement<Div> {
6363

6464
/**
6565
* Adds any block element to the div's contents.
66-
*
66+
*
6767
* @param element a {@link BlockElement}
6868
* @return this Element
6969
*/
7070
public Div add(IBlockElement element) {
7171
childElements.add(element);
7272
return this;
7373
}
74-
74+
7575
/**
7676
* Adds an image to the div's contents.
77-
*
77+
*
7878
* @param element an {@link Image}
7979
* @return this Element
8080
*/
@@ -102,9 +102,6 @@ public PdfName getRole() {
102102
@Override
103103
public void setRole(PdfName role) {
104104
this.role = role;
105-
if (PdfName.Artifact.equals(role)) {
106-
propagateArtifactRoleToChildElements();
107-
}
108105
}
109106

110107
@Override

layout/src/main/java/com/itextpdf/layout/element/List.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public List setListSymbolAlignment(ListSymbolAlignment alignment) {
194194

195195
/**
196196
* Gets the indent offset of the {@link ListItem} symbols.
197-
*
197+
*
198198
* @return the indent offset as a <code>float</code>.
199199
*/
200200
public Float getSymbolIndent() {
@@ -203,7 +203,7 @@ public Float getSymbolIndent() {
203203

204204
/**
205205
* Sets the indent offset of the {@link ListItem} symbols.
206-
*
206+
*
207207
* @param symbolIndent the new indent offset.
208208
* @return this list.
209209
*/
@@ -252,9 +252,6 @@ public PdfName getRole() {
252252
@Override
253253
public void setRole(PdfName role) {
254254
this.role = role;
255-
if (PdfName.Artifact.equals(role)) {
256-
propagateArtifactRoleToChildElements();
257-
}
258255
}
259256

260257
@Override

layout/src/main/java/com/itextpdf/layout/element/Paragraph.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Paragraph addTabStops(java.util.List<TabStop> tabStops) {
150150
/**
151151
* Removes a tabstop position from the Paragraph, if it is present in the
152152
* {@link Property#TAB_STOPS} property.
153-
*
153+
*
154154
* @param tabStopPosition the {@link TabStop} position to be removed.
155155
* @return this Paragraph
156156
* @see TabStop
@@ -182,7 +182,7 @@ public <T1> T1 getDefaultProperty(int property) {
182182

183183
/**
184184
* Sets the indent value for the first line of the {@link Paragraph}.
185-
*
185+
*
186186
* @param indent the indent value that must be applied to the first line of
187187
* the Paragraph, as a <code>float</code>
188188
* @return this Paragraph
@@ -194,7 +194,7 @@ public Paragraph setFirstLineIndent(float indent) {
194194

195195
/**
196196
* Sets the leading value, using the {@link Leading#FIXED} strategy.
197-
*
197+
*
198198
* @param leading the new leading value
199199
* @return this Paragraph
200200
* @see Leading
@@ -206,7 +206,7 @@ public Paragraph setFixedLeading(float leading) {
206206

207207
/**
208208
* Sets the leading value, using the {@link Leading#MULTIPLIED} strategy.
209-
*
209+
*
210210
* @param leading the new leading value
211211
* @return this Paragraph
212212
* @see Leading
@@ -240,9 +240,6 @@ public PdfName getRole() {
240240
@Override
241241
public void setRole(PdfName role) {
242242
this.role = role;
243-
if (PdfName.Artifact.equals(role)) {
244-
propagateArtifactRoleToChildElements();
245-
}
246243
}
247244

248245
@Override

layout/src/main/java/com/itextpdf/layout/element/Table.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,6 @@ public PdfName getRole() {
802802
@Override
803803
public void setRole(PdfName role) {
804804
this.role = role;
805-
if (PdfName.Artifact.equals(role)) {
806-
propagateArtifactRoleToChildElements();
807-
}
808805
}
809806

810807
public Table setExtendBottomRow(boolean isExtended) {

0 commit comments

Comments
 (0)