Skip to content

Commit b69be93

Browse files
ars18wrwiText-CI
authored andcommitted
Support a caption for a table. Add some new tests.
DEVSIX-982
1 parent dc601c1 commit b69be93

File tree

11 files changed

+441
-36
lines changed

11 files changed

+441
-36
lines changed

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

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ This file is part of the iText (R) project.
4444
package com.itextpdf.layout.element;
4545

4646
import com.itextpdf.kernel.PdfException;
47+
import com.itextpdf.kernel.colors.ColorConstants;
4748
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
4849
import com.itextpdf.kernel.pdf.tagutils.AccessibilityProperties;
4950
import com.itextpdf.kernel.pdf.tagutils.DefaultAccessibilityProperties;
5051
import com.itextpdf.layout.Document;
5152
import com.itextpdf.layout.borders.Border;
5253
import com.itextpdf.layout.property.BorderCollapsePropertyValue;
54+
import com.itextpdf.layout.property.CaptionSide;
5355
import com.itextpdf.layout.property.Property;
5456
import com.itextpdf.layout.property.UnitValue;
5557
import com.itextpdf.layout.renderer.IRenderer;
@@ -87,6 +89,7 @@ public class Table extends BlockElement<Table> implements ILargeElement {
8789
private int rowWindowStart = 0;
8890
private Document document;
8991
private Cell[] lastAddedRow;
92+
private Div caption;
9093

9194
/**
9295
* Constructs a {@code Table} with the preferable column widths.
@@ -513,6 +516,54 @@ public Table setSkipLastFooter(boolean skipLastFooter) {
513516
return this;
514517
}
515518

519+
/** Sets the table's caption.
520+
*
521+
* If there is no {@link Property#CAPTION_SIDE} set (note that it's an inheritable property),
522+
* {@link CaptionSide#TOP} will be used.
523+
* Also the {@link StandardRoles#CAPTION} will be set on the element.
524+
*
525+
* @param caption The element to be set as a caption.
526+
* @return this element
527+
*/
528+
529+
public Table setCaption(Div caption) {
530+
this.caption = caption;
531+
if (null != caption) {
532+
ensureCaptionPropertiesAreSet();
533+
}
534+
return this;
535+
}
536+
537+
/**
538+
* Sets the table's caption and its caption side.
539+
*
540+
* Also the {@link StandardRoles#CAPTION} will be set on the element.
541+
*
542+
* @param caption The element to be set as a caption.
543+
* @param side The caption side to be set on the caption.
544+
** @return this element
545+
*/
546+
public Table setCaption(Div caption, CaptionSide side) {
547+
if (null != caption) {
548+
caption.setProperty(Property.CAPTION_SIDE, side);
549+
}
550+
setCaption(caption);
551+
return this;
552+
}
553+
554+
private void ensureCaptionPropertiesAreSet() {
555+
this.caption.getAccessibilityProperties().setRole(StandardRoles.CAPTION);
556+
}
557+
558+
/**
559+
* Gets the table's caption.
560+
*
561+
* @return the table's caption.
562+
*/
563+
public Div getCaption() {
564+
return caption;
565+
}
566+
516567
/**
517568
* Starts new row. This mean that next cell will be added at the beginning of next line.
518569
*
@@ -798,7 +849,7 @@ public Table setBorderCollapse(BorderCollapsePropertyValue collapsePropertyValue
798849
}
799850
return this;
800851
}
801-
852+
802853
public Table setHorizontalBorderSpacing(float spacing) {
803854
setProperty(Property.HORIZONTAL_BORDER_SPACING, spacing);
804855
if (null != header) {
@@ -820,7 +871,7 @@ public Table setVerticalBorderSpacing(float spacing) {
820871
}
821872
return this;
822873
}
823-
874+
824875
@Override
825876
public AccessibilityProperties getAccessibilityProperties() {
826877
if (tagProperties == null) {
@@ -915,10 +966,10 @@ private void ensureHeaderIsInitialized() {
915966
header.setBorderCollapse((BorderCollapsePropertyValue) this.<BorderCollapsePropertyValue>getProperty(Property.BORDER_COLLAPSE));
916967
}
917968
if (hasOwnProperty(Property.HORIZONTAL_BORDER_SPACING)) {
918-
header.setHorizontalBorderSpacing((float)this.<Float>getProperty(Property.HORIZONTAL_BORDER_SPACING));
969+
header.setHorizontalBorderSpacing((float) this.<Float>getProperty(Property.HORIZONTAL_BORDER_SPACING));
919970
}
920971
if (hasOwnProperty(Property.VERTICAL_BORDER_SPACING)) {
921-
header.setVerticalBorderSpacing((float)this.<Float>getProperty(Property.VERTICAL_BORDER_SPACING));
972+
header.setVerticalBorderSpacing((float) this.<Float>getProperty(Property.VERTICAL_BORDER_SPACING));
922973
}
923974
}
924975
}
@@ -933,10 +984,10 @@ private void ensureFooterIsInitialized() {
933984
footer.setBorderCollapse((BorderCollapsePropertyValue) this.<BorderCollapsePropertyValue>getProperty(Property.BORDER_COLLAPSE));
934985
}
935986
if (hasOwnProperty(Property.HORIZONTAL_BORDER_SPACING)) {
936-
footer.setHorizontalBorderSpacing((float)this.<Float>getProperty(Property.HORIZONTAL_BORDER_SPACING));
987+
footer.setHorizontalBorderSpacing((float) this.<Float>getProperty(Property.HORIZONTAL_BORDER_SPACING));
937988
}
938989
if (hasOwnProperty(Property.VERTICAL_BORDER_SPACING)) {
939-
footer.setVerticalBorderSpacing((float)this.<Float>getProperty(Property.VERTICAL_BORDER_SPACING));
990+
footer.setVerticalBorderSpacing((float) this.<Float>getProperty(Property.VERTICAL_BORDER_SPACING));
940991
}
941992
}
942993
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2018 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.layout.property;
44+
45+
/**
46+
* A specialized enum containing the potential caption side values for a {@link
47+
* com.itextpdf.layout.element.Table}'s caption.
48+
*/
49+
public enum CaptionSide {
50+
BOTTOM,
51+
TOP
52+
}

layout/src/main/java/com/itextpdf/layout/property/Property.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private Property() {
7878
public static final int BORDER_TOP_RIGHT_RADIUS = 111;
7979
public static final int BOTTOM = 14;
8080
public static final int BOX_SIZING = 105;
81+
public static final int CAPTION_SIDE = 119;
8182
public static final int CHARACTER_SPACING = 15;
8283
public static final int CLEAR = 100;
8384
public static final int COLLAPSING_MARGINS = 89;
@@ -213,14 +214,15 @@ private Property() {
213214
* related to textual operations. Indicates whether or not this type of property is inheritable.
214215
*/
215216
private static final boolean[] INHERITED_PROPERTIES;
216-
private static final int MAX_INHERITED_PROPERTY_ID = 118;
217+
private static final int MAX_INHERITED_PROPERTY_ID = 119;
217218

218219
static {
219220
INHERITED_PROPERTIES = new boolean[MAX_INHERITED_PROPERTY_ID + 1];
220221

221222
INHERITED_PROPERTIES[Property.APPEARANCE_STREAM_LAYOUT] = true;
222223
INHERITED_PROPERTIES[Property.BASE_DIRECTION] = true;
223224
INHERITED_PROPERTIES[Property.BOLD_SIMULATION] = true;
225+
INHERITED_PROPERTIES[Property.CAPTION_SIDE] = true;
224226
INHERITED_PROPERTIES[Property.CHARACTER_SPACING] = true;
225227
INHERITED_PROPERTIES[Property.COLLAPSING_MARGINS] = true;
226228
INHERITED_PROPERTIES[Property.FIRST_LINE_INDENT] = true;

layout/src/main/java/com/itextpdf/layout/renderer/BlockFormattingContextUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public static boolean isRendererCreateBfc(IRenderer renderer) {
7474
|| FloatingHelper.isRendererFloating(renderer)
7575
|| isAbsolutePosition(renderer)
7676
|| isFixedPosition(renderer)
77+
|| isCaption(renderer)
7778
|| AbstractRenderer.isOverflowProperty(OverflowPropertyValue.HIDDEN, renderer, Property.OVERFLOW_X)
7879
|| AbstractRenderer.isOverflowProperty(OverflowPropertyValue.HIDDEN, renderer, Property.OVERFLOW_Y);
7980
}
@@ -92,4 +93,9 @@ private static boolean isFixedPosition(IRenderer renderer) {
9293
Integer positioning = NumberUtil.asInteger(renderer.<Object>getProperty(Property.POSITION));
9394
return Integer.valueOf(LayoutPosition.FIXED).equals(positioning);
9495
}
96+
97+
private static boolean isCaption(IRenderer renderer) {
98+
return renderer.getParent() instanceof TableRenderer
99+
&& (renderer instanceof DivRenderer);
100+
}
95101
}

0 commit comments

Comments
 (0)