Skip to content

Commit

Permalink
* Added wrapping to text class.
Browse files Browse the repository at this point in the history
* Published calculate text width and height methods.
* PDFBox version 2.0.4 -> 2.0.21
  • Loading branch information
vkuzel committed Oct 11, 2020
1 parent e2fe36d commit 21fc72f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repositories {
}

dependencies {
compile("org.apache.pdfbox:pdfbox:2.0.4")
compile("org.apache.pdfbox:pdfbox:2.0.21")
testCompile("junit:junit:4.12")
}

59 changes: 57 additions & 2 deletions src/main/java/com/github/vkuzel/simplepdflayout/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Text<T extends Text<T>> extends Box<T> {
Expand All @@ -30,6 +32,40 @@ public T setText(String text) {
return getThis();
}

/**
* Sets a text and splits it by space character to multiple lines if it is
* longer than maxWidth value.
*
* @param text Text to print.
* @param maxWidth Points.
* @return This instance.
*/
public T setAndWrapText(String text, float maxWidth) {
float textWidth = calculateTextWidth(text);
if (textWidth < maxWidth) {
this.lines = Collections.singletonList(text);
} else {
String[] tokens = text.split(" ");
String buffer = "";
List<String> lines = new ArrayList<>();
for (String token : tokens) {
String potentialBuffer = buffer + (buffer.isEmpty() ? "" : " ") + token;
float bufferWidth = calculateTextWidth(potentialBuffer);
if (bufferWidth > maxWidth) {
lines.add(buffer);
buffer = token;
} else {
buffer = potentialBuffer;
}
}
if (!buffer.isEmpty()) {
lines.add(buffer);
}
this.lines = lines;
}
return getThis();
}

public T setFont(PDFont font) {
this.font = font;
return getThis();
Expand Down Expand Up @@ -77,7 +113,7 @@ protected void drawText(PDPageContentStream contentStream) {
float tx = contentTopLeft.getX();
float ty = contentTopLeft.getY() + (i + 1) * lineHeight;
if (alignment != Alignment.LEFT) {
float textWidth = (font.getStringWidth(line) / 1000f) * fontSize;
float textWidth = calculateTextWidth(line);
if (alignment == Alignment.RIGHT) {
tx += contentDimension.getWidth() - textWidth;
} else if (alignment == Alignment.CENTER) {
Expand Down Expand Up @@ -112,7 +148,26 @@ protected Dimension calculateDimension() {
return dimension;
}

protected float calculateTextHeight() {
public float calculateTextWidth() {
float width = 0;
for (String line : lines) {
float lineWidth = calculateTextWidth(line);
if (lineWidth > width) {
width = lineWidth;
}
}
return width;
}

public float calculateTextHeight() {
return lines.size() * lineHeight;
}

protected float calculateTextWidth(String text) {
try {
return font.getStringWidth(text) / 1000f * fontSize;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
21 changes: 21 additions & 0 deletions src/test/java/com/github/vkuzel/simplepdflayout/TextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.vkuzel.simplepdflayout;

import org.junit.Assert;
import org.junit.Test;

public class TextTest {

@Test
public void textIsProperlyWrapped() {
// given
float maxWidth = 60;
Text<?> text = new Text<>()
.setAndWrapText("XXX XXX XXX XXX XXX XXX", maxWidth);

// when
float width = text.calculateTextWidth();

// then
Assert.assertTrue(width <= maxWidth);
}
}

0 comments on commit 21fc72f

Please sign in to comment.