Skip to content

Instantly share code, notes, and snippets.

@christoph-jerolimov
Created November 25, 2013 00:51
Show Gist options
  • Save christoph-jerolimov/7634637 to your computer and use it in GitHub Desktop.
Save christoph-jerolimov/7634637 to your computer and use it in GitHub Desktop.
PdfBox code... Dump before i realized that this is "just another apache project" i wouldn't use.
import java.io.IOException;
public interface ContentWidth {
public float getContentWidth(String text) throws IOException;
}
import java.io.IOException;
import org.apache.pdfbox.pdmodel.font.PDFont;
public class FontBasedContentWidth implements ContentWidth {
private PDFont font;
private float fontSize;
public FontBasedContentWidth(PDFont font, float fontSize) {
this.font = font;
this.fontSize = fontSize;
}
@Override
public float getContentWidth(String text) throws IOException {
// System.out.println(
// "string width is " + font.getStringWidth(text) +
// " with font " + font +
// " for text " + text);
return font.getStringWidth(text) / 1000 * fontSize;
}
}
import static org.junit.Assert.*;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.junit.BeforeClass;
import org.junit.Test;
public class FontBasedContentWidthTest {
private static PDFont helvetica;
@BeforeClass
public static void loadFont() {
// Ensure that we already load PDFBox when running the tests.
helvetica = PDType1Font.HELVETICA;
}
@Test
public void testEmptyContent() throws IOException {
PDFont font = helvetica;
float fontSize = 16;
FontBasedContentWidth contentWidth = new FontBasedContentWidth(font, fontSize);
assertEquals(0, contentWidth.getContentWidth(""), 0);
}
@Test
public void testSimpleContent() throws IOException {
PDFont font = helvetica;
float fontSize = 16;
FontBasedContentWidth contentWidth = new FontBasedContentWidth(font, fontSize);
assertEquals(80, contentWidth.getContentWidth("Hello World"), 5);
assertEquals(40, contentWidth.getContentWidth("iiiiiiiiiii"), 10);
assertEquals(140, contentWidth.getContentWidth("MMMMMMMMMMM"), 10);
}
}
public class StringBasedContentWidth implements ContentWidth {
private float multiplicator;
public StringBasedContentWidth() {
this.multiplicator = 1;
}
public StringBasedContentWidth(float multiplicator) {
this.multiplicator = multiplicator;
}
@Override
public float getContentWidth(String text) {
return text.length() * multiplicator;
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class StringBasedContentWidthTest {
@Test
public void testEmptyContent() {
StringBasedContentWidth contentWidth = new StringBasedContentWidth();
assertEquals(0, contentWidth.getContentWidth(""), 1);
}
@Test
public void testSimpleContent() {
StringBasedContentWidth contentWidth = new StringBasedContentWidth();
assertEquals(11, contentWidth.getContentWidth("Hello World"), 1);
assertEquals(11, contentWidth.getContentWidth("iiiiiiiiiii"), 1);
assertEquals(11, contentWidth.getContentWidth("MMMMMMMMMMM"), 1);
}
@Test
public void testSimpleContentWithMultiplicator() {
StringBasedContentWidth contentWidth = new StringBasedContentWidth(5);
assertEquals(55, contentWidth.getContentWidth("Hello World"), 1);
assertEquals(55, contentWidth.getContentWidth("iiiiiiiiiii"), 1);
assertEquals(55, contentWidth.getContentWidth("MMMMMMMMMMM"), 1);
}
}
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TextBlock {
private static final Pattern LINE_PATTERN = Pattern.compile("(\r\n|\r|\n|\t| )");
private float maximumWidth;
private ContentWidth contentWidth;
private LinkedList<String> lines = new LinkedList<>();
public TextBlock(float maximumWidth, ContentWidth contentWidth) {
this.maximumWidth = maximumWidth;
this.contentWidth = contentWidth;
}
public void addBlock(String text) throws IOException {
StringBuilder currentLine = new StringBuilder();
String nextWord;
int startText = 0;
Matcher matcher = LINE_PATTERN.matcher(text);
while (matcher.find()) {
int endText = matcher.start();
nextWord = text.substring(startText, endText);
startText = matcher.end();
addWordToLine(nextWord, currentLine);
// If we found an newline we will create it also.
String group = matcher.group();
if (group.equals("\r\n") || group.equals("\r") || group.equals("\n")) {
lines.addLast(currentLine.toString());
currentLine.delete(0, currentLine.length());
}
}
// If there is a string behind the last match
nextWord = text.substring(startText);
addWordToLine(nextWord, currentLine);
// If we have still some data we add them too.
if (currentLine.length() > 0) {
lines.addLast(currentLine.toString());
}
}
private void addWordToLine(String nextWord, StringBuilder currentLine) throws IOException {
// If the new line would be to long for the current line we use next as a new line
if (currentLine.length() > 0 && contentWidth.getContentWidth(currentLine + " " + nextWord) > maximumWidth) {
lines.addLast(currentLine.toString());
currentLine.delete(0, currentLine.length());
}
// Append the current word
if (currentLine.length() > 0) {
currentLine.append(" ");
}
currentLine.append(nextWord);
}
public List<String> getLines() {
return lines;
}
}
package com.grandcentrix.axa.bkv.pdfhelper;
import java.io.IOException;
import java.util.List;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
public class TextBlockTest {
private static PDFont helvetica;
@BeforeClass
public static void loadFont() {
// Ensure that we already load PDFBox when running the tests.
helvetica = PDType1Font.HELVETICA;
}
@Test
public void testSimpleBlock() throws IOException {
String text = "Hallo das ist ein Test.\nWarum?\r\n\nTrennen wir hier?";
TextBlock textBlock = new TextBlock(20, new StringBasedContentWidth(1));
textBlock.addBlock(text);
List<String> lines = textBlock.getLines();
assertEquals("Hallo das ist ein", lines.get(0));
assertEquals("Test.", lines.get(1));
assertEquals("Warum?", lines.get(2));
assertEquals("", lines.get(3));
assertEquals("Trennen wir hier?", lines.get(4));
assertEquals(5, lines.size());
}
@Test
public void testTwoTextBlocks() throws IOException {
String text = "Hallo das ist ein Test.\nWarum?\r\n\nTrennen wir hier?";
TextBlock textBlock = new TextBlock(20, new StringBasedContentWidth(1));
textBlock.addBlock(text);
textBlock.addBlock(text);
List<String> lines = textBlock.getLines();
assertEquals("Hallo das ist ein", lines.get(0));
assertEquals("Test.", lines.get(1));
assertEquals("Warum?", lines.get(2));
assertEquals("", lines.get(3));
assertEquals("Trennen wir hier?", lines.get(4));
assertEquals("Hallo das ist ein", lines.get(5));
assertEquals("Test.", lines.get(6));
assertEquals("Warum?", lines.get(7));
assertEquals("", lines.get(8));
assertEquals("Trennen wir hier?", lines.get(9));
assertEquals(10, lines.size());
}
@Test
public void testFontBasedContentBlock() throws IOException {
String loremipsum = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
PDFont font = helvetica;
float fontSize = 16;
TextBlock textBlock = new TextBlock(600, new FontBasedContentWidth(font, fontSize));
textBlock.addBlock(loremipsum);
List<String> lines = textBlock.getLines();
assertEquals("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod", lines.get(0));
assertEquals("tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero", lines.get(1));
assertEquals("eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea", lines.get(2));
assertEquals("takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,", lines.get(3));
assertEquals("consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et", lines.get(4));
assertEquals("dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo", lines.get(5));
assertEquals("duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est", lines.get(6));
assertEquals("Lorem ipsum dolor sit amet.", lines.get(7));
assertEquals(8, lines.size());
}
@Test
public void testDoubledFontSizeTextBlock() throws IOException {
String loremipsum = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.";
PDFont font = helvetica;
float fontSize = 32;
TextBlock textBlock = new TextBlock(600, new FontBasedContentWidth(font, fontSize));
textBlock.addBlock(loremipsum);
List<String> lines = textBlock.getLines();
assertEquals("Lorem ipsum dolor sit amet, consetetur", lines.get(0));
assertEquals("sadipscing elitr, sed diam nonumy eirmod", lines.get(1));
assertEquals("tempor invidunt ut labore et dolore magna", lines.get(2));
assertEquals("aliquyam erat, sed diam voluptua.", lines.get(3));
assertEquals(4, lines.size());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment