Skip to content

Commit 0928214

Browse files
author
Benoit Lagae
committed
Merge branch 'hotfix/fontstring' into develop
2 parents c544ffc + 19714d0 commit 0928214

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

io/src/main/java/com/itextpdf/io/LogMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public final class LogMessageConstant {
6969
public static final String FILE_CHANNEL_CLOSING_FAILED = "Closing of the file channel this source is based on failed.";
7070
public static final String FONT_HAS_INVALID_GLYPH = "Font {0} has invalid glyph: {1}";
7171
public static final String FORBID_RELEASE_IS_SET = "ForbidRelease flag is set and release is called. Releasing will not be performed.";
72+
public static final String FONT_PROPERTY_MUST_BE_PDF_FONT_OBJECT = "The Font Property must be a PdfFont object";
7273
public static final String FORM_FIELD_WAS_FLUSHED = "A form field was flushed. There's no way to create this field in the AcroForm dictionary.";
7374
public static final String IMAGE_HAS_AMBIGUOUS_SCALE = "The image cannot be auto scaled and scaled by a certain parameter simultaneously";
7475
public static final String IMAGE_HAS_JBIG2DECODE_FILTER = "Image cannot be inline if it has JBIG2Decode filter. It will be added as an ImageXObject";

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,14 @@ private boolean isGlyphPartOfWordForHyphenation(Glyph g) {
12651265

12661266
private void updateFontAndText() {
12671267
if (strToBeConverted != null) {
1268-
font = getPropertyAsFont(Property.FONT);
1268+
try {
1269+
font = getPropertyAsFont(Property.FONT);
1270+
}
1271+
catch (ClassCastException cce) {
1272+
font = resolveFirstPdfFont();
1273+
Logger logger = LoggerFactory.getLogger(TextRenderer.class);
1274+
logger.error(LogMessageConstant.FONT_PROPERTY_MUST_BE_PDF_FONT_OBJECT);
1275+
}
12691276
text = convertToGlyphLine(strToBeConverted);
12701277
otfFeaturesApplied = false;
12711278
strToBeConverted = null;

layout/src/test/java/com/itextpdf/layout/renderer/TextRendererTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,25 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.layout.renderer;
4444

45+
import com.itextpdf.io.LogMessageConstant;
4546
import com.itextpdf.kernel.geom.Rectangle;
4647
import com.itextpdf.kernel.pdf.ByteBufferOutputStream;
4748
import com.itextpdf.kernel.pdf.PdfDocument;
4849
import com.itextpdf.kernel.pdf.PdfWriter;
4950
import com.itextpdf.layout.Document;
51+
import com.itextpdf.layout.element.Paragraph;
5052
import com.itextpdf.layout.element.Text;
53+
import com.itextpdf.layout.font.FontProvider;
5154
import com.itextpdf.layout.layout.LayoutArea;
5255
import com.itextpdf.layout.layout.LayoutContext;
56+
import com.itextpdf.layout.layout.LayoutPosition;
5357
import com.itextpdf.layout.layout.LayoutResult;
58+
import com.itextpdf.layout.property.Property;
5459
import com.itextpdf.test.ExtendedITextTest;
60+
import com.itextpdf.test.annotations.LogMessage;
61+
import com.itextpdf.test.annotations.LogMessages;
5562
import com.itextpdf.test.annotations.type.UnitTest;
63+
5664
import org.junit.Assert;
5765
import org.junit.Test;
5866
import org.junit.experimental.categories.Category;
@@ -84,4 +92,42 @@ public void nextRendererTest() {
8492
Assert.assertEquals(result1.getOccupiedArea(), result2.getOccupiedArea());
8593
}
8694

95+
@Test
96+
@LogMessages(messages = {
97+
@LogMessage(messageTemplate = LogMessageConstant.FONT_PROPERTY_MUST_BE_PDF_FONT_OBJECT)
98+
})
99+
public void setTextException() {
100+
final String val = "other text";
101+
final String fontName = "Helvetica";
102+
TextRenderer rend = (TextRenderer) new Text("basic text").getRenderer();
103+
FontProvider fp = new FontProvider();
104+
fp.addFont(fontName);
105+
rend.setProperty(Property.FONT_PROVIDER, fp);
106+
rend.setProperty(Property.FONT, fontName);
107+
rend.setText(val);
108+
Assert.assertEquals(val, rend.getText().toString());
109+
}
110+
111+
/**
112+
* This test assumes that absolute positioning for {@link Text} elements is
113+
* not supported. Adding this support is the subject of DEVSIX-1393.
114+
*/
115+
@Test
116+
@LogMessages(messages = {
117+
@LogMessage(messageTemplate = LogMessageConstant.FONT_PROPERTY_MUST_BE_PDF_FONT_OBJECT)
118+
})
119+
public void setFontAsText() {
120+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(new ByteBufferOutputStream()));
121+
pdfDoc.addNewPage();
122+
Document doc = new Document(pdfDoc);
123+
Text txt = new Text("text");
124+
txt.setProperty(Property.POSITION, LayoutPosition.ABSOLUTE);
125+
txt.setProperty(Property.TOP, 5f);
126+
FontProvider fp = new FontProvider();
127+
fp.addFont("Helvetica");
128+
txt.setProperty(Property.FONT_PROVIDER, fp);
129+
txt.setFont("Helvetica");
130+
doc.add(new Paragraph().add(txt));
131+
doc.close();
132+
}
87133
}

0 commit comments

Comments
 (0)