Skip to content

Commit 46a22c1

Browse files
BezrukovMiText-CI
authored andcommitted
Add background image support for text elements for layout module
DEVSIX-4142
1 parent 3f85b3c commit 46a22c1

File tree

4 files changed

+46
-35
lines changed

4 files changed

+46
-35
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ public void drawBackground(DrawContext drawContext) {
506506
if (isTagged) {
507507
drawContext.getCanvas().openTag(new CanvasArtifact());
508508
}
509-
Rectangle backgroundArea = applyMargins(bBox, false);
509+
Rectangle backgroundArea = getBackgroundArea(applyMargins(bBox, false));
510510
if (backgroundArea.getWidth() <= 0 || backgroundArea.getHeight() <= 0) {
511511
Logger logger = LoggerFactory.getLogger(AbstractRenderer.class);
512512
logger.warn(MessageFormatUtil.format(LogMessageConstant.RECTANGLE_HAS_NEGATIVE_OR_ZERO_SIZES, "background"));
@@ -590,6 +590,16 @@ public PdfFormXObject createXObject(AbstractLinearGradientBuilder linearGradient
590590
return xObject;
591591
}
592592

593+
/**
594+
* Evaluate the actual background
595+
*
596+
* @param occupiedAreaWithMargins the current occupied area with applied margins
597+
* @return the actual background area
598+
*/
599+
protected Rectangle getBackgroundArea(Rectangle occupiedAreaWithMargins) {
600+
return occupiedAreaWithMargins;
601+
}
602+
593603
protected boolean clipBorderArea(DrawContext drawContext, Rectangle outerBorderBox) {
594604
return clipArea(drawContext, outerBorderBox, true, true, false, true);
595605
}

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

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -856,37 +856,6 @@ public boolean accept(Glyph glyph) {
856856
}
857857
}
858858

859-
@Override
860-
public void drawBackground(DrawContext drawContext) {
861-
Background background = this.<Background>getProperty(Property.BACKGROUND);
862-
Float textRise = this.getPropertyAsFloat(Property.TEXT_RISE);
863-
Rectangle bBox = getOccupiedAreaBBox();
864-
Rectangle backgroundArea = applyMargins(bBox, false);
865-
float bottomBBoxY = backgroundArea.getY();
866-
float leftBBoxX = backgroundArea.getX();
867-
if (background != null) {
868-
boolean isTagged = drawContext.isTaggingEnabled();
869-
PdfCanvas canvas = drawContext.getCanvas();
870-
if (isTagged) {
871-
canvas.openTag(new CanvasArtifact());
872-
}
873-
boolean backgroundAreaIsClipped = clipBackgroundArea(drawContext, backgroundArea);
874-
canvas.saveState().setFillColor(background.getColor());
875-
TransparentColor backgroundColor = new TransparentColor(background.getColor(), background.getOpacity());
876-
backgroundColor.applyFillTransparency(drawContext.getCanvas());
877-
canvas.rectangle(leftBBoxX - background.getExtraLeft(), bottomBBoxY + (float) textRise - background.getExtraBottom(),
878-
backgroundArea.getWidth() + background.getExtraLeft() + background.getExtraRight(),
879-
backgroundArea.getHeight() - (float) textRise + background.getExtraTop() + background.getExtraBottom());
880-
canvas.fill().restoreState();
881-
if (backgroundAreaIsClipped) {
882-
drawContext.getCanvas().restoreState();
883-
}
884-
if (isTagged) {
885-
canvas.closeTag();
886-
}
887-
}
888-
}
889-
890859
/**
891860
* Trims any whitespace characters from the start of the {@link GlyphLine}
892861
* to be rendered.
@@ -1116,6 +1085,12 @@ private boolean hasOtfFont() {
11161085
return font instanceof PdfType0Font && font.getFontProgram() instanceof TrueTypeFont;
11171086
}
11181087

1088+
@Override
1089+
protected Rectangle getBackgroundArea(Rectangle occupiedAreaWithMargins) {
1090+
float textRise = (float) this.getPropertyAsFloat(Property.TEXT_RISE);
1091+
return occupiedAreaWithMargins.moveUp(textRise).decreaseHeight(textRise);
1092+
}
1093+
11191094
@Override
11201095
protected Float getFirstYLineRecursively() {
11211096
return getYLine();

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ This file is part of the iText (R) project.
5858
import com.itextpdf.kernel.utils.CompareTool;
5959
import com.itextpdf.layout.element.Div;
6060
import com.itextpdf.layout.element.Paragraph;
61+
import com.itextpdf.layout.element.Text;
6162
import com.itextpdf.layout.property.BackgroundImage;
6263
import com.itextpdf.layout.property.Property;
63-
import com.itextpdf.layout.property.Transform;
64-
import com.itextpdf.layout.property.Transform.SingleTransform;
65-
import com.itextpdf.layout.property.UnitValue;
6664
import com.itextpdf.test.ExtendedITextTest;
6765
import com.itextpdf.test.LogLevelConstants;
6866
import com.itextpdf.test.annotations.LogMessage;
@@ -122,6 +120,34 @@ public void backgroundImageWithLinearGradientAndTransformTest() throws IOExcepti
122120
backgroundImageGenericTest("backgroundImageWithLinearGradientAndTransform", backgroundImage, Math.PI / 4);
123121
}
124122

123+
@Test
124+
public void backgroundImageForText() throws IOException, InterruptedException {
125+
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(sourceFolder + "itis.jpg"));
126+
BackgroundImage backgroundImage = new BackgroundImage(xObject);
127+
128+
Assert.assertTrue(backgroundImage.isRepeatX());
129+
Assert.assertTrue(backgroundImage.isRepeatY());
130+
131+
Assert.assertTrue(backgroundImage.isBackgroundSpecified());
132+
133+
String outFileName = destinationFolder + "backgroundImageForText.pdf";
134+
String cmpFileName = sourceFolder + "cmp_backgroundImageForText.pdf";
135+
136+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileOutputStream(outFileName)))) {
137+
Document doc = new Document(pdfDocument);
138+
139+
Text textElement = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
140+
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ");
141+
textElement.setProperty(Property.BACKGROUND_IMAGE, backgroundImage);
142+
textElement.setFontSize(50);
143+
144+
doc.add(new Paragraph(textElement));
145+
146+
}
147+
148+
Assert.assertNull(new CompareTool().compareByContent(outFileName, cmpFileName, destinationFolder, "diff"));
149+
}
150+
125151
@Test
126152
public void backgroundImageWithoutRepeatX() throws IOException, InterruptedException {
127153
PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.create(sourceFolder + "itis.jpg"));

0 commit comments

Comments
 (0)