Skip to content

Issue 143 #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Add this to your `pom.xml`:
<dependency>
<groupId>com.github.vandeseer</groupId>
<artifactId>easytable</artifactId>
<version>0.8.6-SNAPSHOT</version>
<version>0.9.0-SNAPSHOT</version>
</dependency>

Or checkout the repository and install it locally with maven (e.g. for the`develop` branch):
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.vandeseer</groupId>
<artifactId>easytable</artifactId>
<version>0.8.6-SNAPSHOT</version>
<version>0.9.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ protected float calculateInnerHeight() {


private float calculateYOffset(PDFont currentFont, int currentFontSize, int lineIndex) {
return PdfUtil.getFontHeight(currentFont, currentFontSize) // font height
+ (lineIndex > 0 ? PdfUtil.getFontHeight(currentFont, currentFontSize) * cell.getLineSpacing() : 0f); // line spacing
boolean backwardsCompatibleFontHeight = cell.getSettings().isBackwardsCompatibleFontHeight();
final float fontHeight = PdfUtil.getFontHeight(currentFont, currentFontSize, backwardsCompatibleFontHeight);
return fontHeight + (lineIndex > 0 ? fontHeight * cell.getLineSpacing() : 0f); // line spacing
}

static boolean isNotLastLine(List<String> lines, int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ public void drawContent(DrawingContext drawingContext) {
yOffset += (height - textHeight - cell.getPaddingTop() - cell.getPaddingBottom());
}

float xOffset = startX + cell.getPaddingLeft() - PdfUtil.getFontHeight(currentFont, currentFontSize);
float fontHeight = PdfUtil.getFontHeight(currentFont, currentFontSize, cell.getSettings().isBackwardsCompatibleFontHeight());
float xOffset = startX + cell.getPaddingLeft() - fontHeight;

float textWidth = (PdfUtil.getFontHeight(currentFont, currentFontSize) // font height
+ PdfUtil.getFontHeight(currentFont, currentFontSize) * cell.getLineSpacing()) * lines.size(); // line spacing;
float textWidth = (fontHeight // font height
+ fontHeight * cell.getLineSpacing()) * lines.size(); // line spacing;

if (cell.isHorizontallyAligned(CENTER)) {
xOffset = xOffset + ((cell.getWidth() - cell.getPaddingRight() - cell.getPaddingLeft()) / 2 - textWidth / 2);
Expand All @@ -84,8 +85,7 @@ public void drawContent(DrawingContext drawingContext) {
String line = lines.get(i);

xOffset += (
PdfUtil.getFontHeight(currentFont, currentFontSize) // font height
+ (i > 0 ? PdfUtil.getFontHeight(currentFont, currentFontSize) * cell.getLineSpacing() : 0f) // line spacing
fontHeight + (i > 0 ? fontHeight * cell.getLineSpacing() : 0f) // line spacing
);

drawText(line, currentFont, currentFontSize, currentTextColor, xOffset, yOffset, drawingContext.getContentStream());
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/vandeseer/easytable/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public class Settings {
@Setter(AccessLevel.NONE)
private Boolean wordBreak;

@Setter(AccessLevel.NONE)
private Boolean backwardsCompatibleFontHeight;

public boolean isBackwardsCompatibleFontHeight() {
return backwardsCompatibleFontHeight;
}

public void setBackwardsCompatibleFontHeight(boolean value) {
this.backwardsCompatibleFontHeight = value;
}

public boolean isWordBreak() {
return wordBreak != null && wordBreak;
}
Expand All @@ -60,6 +71,7 @@ public void fillingMergeBy(Settings settings) {
fillingMergeColorSettings(settings);
fillingMergeAlignmentSettings(settings);
fillingMergeWordBreakSetting(settings);
fillingMergeBackwardsCompatibleFontHeightSetting(settings);
}

private void fillingMergeWordBreakSetting(Settings settings) {
Expand All @@ -69,6 +81,13 @@ private void fillingMergeWordBreakSetting(Settings settings) {
}
}

private void fillingMergeBackwardsCompatibleFontHeightSetting(Settings settings) {
// Note that we use the boxed Boolean only here internally!
if (backwardsCompatibleFontHeight == null && settings.backwardsCompatibleFontHeight != null) {
backwardsCompatibleFontHeight = settings.isBackwardsCompatibleFontHeight();
}
}

private void fillingMergePaddingSettings(Settings settings) {
if (getPaddingBottom() == null && settings.getPaddingBottom() != null) {
paddingBottom = settings.getPaddingBottom();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/vandeseer/easytable/structure/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static class TableBuilder {
.paddingLeft(DEFAULT_PADDING)
.paddingRight(DEFAULT_PADDING)
.wordBreak(true)
.backwardsCompatibleFontHeight(false)
.build();

private Set<Point> rowSpanCells = new HashSet<>();
Expand Down Expand Up @@ -218,6 +219,11 @@ public TableBuilder wordBreak(Boolean wordBreak) {
return this;
}

public TableBuilder backwardsCompatibleFontHeight(boolean value) {
settings.setBackwardsCompatibleFontHeight(value);
return this;
}

public Table build() {
if (getNumberOfRegularCells() != getNumberOfSpannedCells()) {
throw new TableSetupException("Number of table cells does not match with table setup. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public float getTextHeight() {
return this.textHeight;
}

this.textHeight = PdfUtil.getFontHeight(getFont(), getFontSize());
this.textHeight = PdfUtil.getFontHeight(getFont(), getFontSize(), settings.isBackwardsCompatibleFontHeight());

if (settings.isWordBreak()) {

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/vandeseer/easytable/util/PdfUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -63,8 +64,17 @@ private static float getWidthOfStringWithoutNewlines(String text, PDFont font, i
* @param fontSize FontSize
* @return Height of font
*/
public static float getFontHeight(final PDFont font, final int fontSize) {
return font.getFontDescriptor().getCapHeight() * fontSize / 1000F;
static public float getFontHeight(final PDFont font, final int fontSize) {
return getFontHeight(font, fontSize, false);
}

static public float getFontHeight(final PDFont font, final int fontSize, boolean backwardsCompatible) {
if (backwardsCompatible) {
return font.getFontDescriptor().getCapHeight() * fontSize / 1000F;
} else {
final PDFontDescriptor fontDescriptor = font.getFontDescriptor();
return (fontDescriptor.getFontBoundingBox().getHeight() - fontDescriptor.getLeading()) / 1000.0f * fontSize;
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/vandeseer/MinimumWorkingExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void main(String[] args) throws IOException {

// Build the table
Table myTable = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(200, 200)
.padding(2)
.addRow(Row.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void getHeightShouldThrowExceptionIfNotYetRendered() {
@Test
public void getHeightShouldReturnValueIfTableIsBuilt() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(10, 10, 10)
.horizontalAlignment(CENTER)
.fontSize(10).font(HELVETICA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class TableTest {
@Test
public void getNumberOfColumns_tableBuilderWithThreeColumns() {
final TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnOfWidth(12)
.addColumnOfWidth(34)
.addColumnOfWidth(56);
Expand All @@ -26,6 +27,7 @@ public void getNumberOfColumns_tableBuilderWithThreeColumns() {
@Test
public void getWidth_tableBuilderWithTwoColumns() {
final TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnOfWidth(20)
.addColumnOfWidth(40);
final Table table = tableBuilder.build();
Expand All @@ -36,6 +38,7 @@ public void getWidth_tableBuilderWithTwoColumns() {
@Test
public void getRows_tableBuilderWithOneRow() {
final TableBuilder tableBuilder = Table.builder();
tableBuilder.backwardsCompatibleFontHeight(true);
tableBuilder.addColumnOfWidth(12)
.addColumnOfWidth(34);
final Row row = Row.builder()
Expand All @@ -51,6 +54,7 @@ public void getRows_tableBuilderWithOneRow() {
@Test
public void getHeight_twoRowsWithDifferentPaddings() {
final Table table = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnOfWidth(12)
.addColumnOfWidth(34)
.fontSize(12)
Expand All @@ -61,7 +65,7 @@ public void getHeight_twoRowsWithDifferentPaddings() {
.build();

// highest cell (50) + actual font height
final float actualFontHeight = PdfUtil.getFontHeight(table.getSettings().getFont(), 12);
final float actualFontHeight = PdfUtil.getFontHeight(table.getSettings().getFont(), 12, true);
assertThat(table.getHeight(), equalTo(50 + actualFontHeight));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void createDocumentWithTableOverMultiplePages() throws Exception {

private Table createTableWithCellColSpanning() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(300, 120, 70)
.fontSize(8)
.font(HELVETICA);
Expand Down Expand Up @@ -97,6 +98,7 @@ private Table createTableWithCellColSpanning() {

private Table createTableWithCellRowSpanning() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(300, 120, 70)
.fontSize(8)
.font(HELVETICA);
Expand Down Expand Up @@ -125,8 +127,9 @@ private Table createTableWithCellRowSpanning() {
return tableBuilder.build();
}

private Table createTableWithTwoCellRowSpannings() throws Exception {
private Table createTableWithTwoCellRowSpannings() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(300, 120, 70)
.fontSize(8)
.font(HELVETICA);
Expand Down Expand Up @@ -162,6 +165,7 @@ private Table createTableWithTwoCellRowSpannings() throws Exception {

private Table createTableWithSeveralRowSpannings() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(300, 120, 70)
.fontSize(8)
.font(HELVETICA);
Expand Down Expand Up @@ -201,6 +205,7 @@ private Table createTableWithSeveralRowSpannings() {

private Table createTableWithDifferentAlignmentsInSpannedCells() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(300, 120, 70)
.fontSize(8)
.font(HELVETICA);
Expand Down Expand Up @@ -241,6 +246,7 @@ private Table createTableWithDifferentAlignmentsInSpannedCells() {

private Table createTableWithSeveralRowAndCellSpannings() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(200, 120, 70, 100)
.fontSize(8)
.font(HELVETICA);
Expand Down Expand Up @@ -275,6 +281,7 @@ private Table createTableWithSeveralRowAndCellSpannings() {

private Table createTableWithImages() throws IOException {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(200, 120, 70, 100)
.horizontalAlignment(CENTER)
.verticalAlignment(MIDDLE)
Expand Down Expand Up @@ -311,6 +318,7 @@ private Table createTableWithImages() throws IOException {

private Table createVeryLargeTable() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(300, 120, 70)
.fontSize(12)
.font(HELVETICA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void createDocumentWithExcelLikeTables() throws IOException {
private Table createSimpleExampleTable() {

final TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(100, 50, 50, 50)
.fontSize(8)
.font(HELVETICA)
Expand Down Expand Up @@ -119,6 +120,7 @@ private Table createSimpleExampleTable() {
private Table createComplexExampleTable() throws IOException {

return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(50, 100, 40, 70, 120)
.borderColor(WHITE)
.textColor(DARK_GRAY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void testFinalY() throws IOException {

private static Table createHeaderTableForPage(int pageNumber) {
return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(100, 100, 100, 100)
.fontSize(8)
.font(HELVETICA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void createTwoPageTableWithRepeatedHeader() throws IOException {

private Table createTable() {
final Table.TableBuilder tableBuilder = Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnOfWidth(60)
.addColumnOfWidth(60);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void testParagraphCell() throws IOException {

private Table createParagraphTable() {
return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(200, 200)
.borderColor(WHITE)
.fontSize(8)
Expand Down Expand Up @@ -92,6 +93,7 @@ private Table createParagraphTable() {

private static Table createSimpleTable() {
return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(120, 120, 120, 120)
.fontSize(8)
.font(HELVETICA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void createDocumentWithTables() throws Exception {

private Table createRegularTable() {
return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(50, 50)
.addRow(Row.builder()
.add(TextCell.builder().borderWidth(1).text("1").build())
Expand All @@ -48,6 +49,7 @@ private Table createRegularTable() {

private Table createComplexTable1() {
return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(50, 50, 50, 50)
.addRow(Row.builder()
.add(TextCell.builder().borderWidth(1).text("1").build())
Expand Down Expand Up @@ -75,6 +77,7 @@ private Table createComplexTable1() {

private Table createComplexTable2() {
return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(50, 50, 50, 50)
.addRow(Row.builder()
.add(TextCell.builder().borderWidth(1).text("1").build())
Expand All @@ -101,6 +104,7 @@ private Table createComplexTable2() {

private Table createComplexTable3() {
return Table.builder()
.backwardsCompatibleFontHeight(true)
.addColumnsOfWidth(50, 50, 50, 50)
.addRow(Row.builder()
.add(TextCell.builder().borderWidth(1).text("1").build())
Expand Down
22 changes: 11 additions & 11 deletions src/test/java/org/vandeseer/integrationtest/StartPageTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package org.vandeseer.integrationtest;

import static junit.framework.TestCase.assertTrue;
import static junit.framework.TestCase.assertEquals;
import static org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode.APPEND;
import static org.vandeseer.TestUtils.getActualPdfFor;
import static org.vandeseer.TestUtils.getExpectedPdfFor;
import static org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA;

import java.io.IOException;

import de.redsix.pdfcompare.CompareResult;
import de.redsix.pdfcompare.PdfComparator;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
Expand All @@ -23,8 +16,14 @@
import org.vandeseer.easytable.structure.Table.TableBuilder;
import org.vandeseer.easytable.structure.cell.TextCell;

import de.redsix.pdfcompare.CompareResult;
import de.redsix.pdfcompare.PdfComparator;
import java.io.IOException;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode.APPEND;
import static org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA;
import static org.vandeseer.TestUtils.getActualPdfFor;
import static org.vandeseer.TestUtils.getExpectedPdfFor;

public class StartPageTest {

Expand Down Expand Up @@ -57,6 +56,7 @@ public void before() throws IOException {
content.close();

TableBuilder builder = Table.builder().addColumnsOfWidth(150, 150, 150).fontSize(25).font(HELVETICA)
.backwardsCompatibleFontHeight(true)
.padding(5).borderWidth(1)
.addRow(Row.builder().add(TextCell.builder().text("Header").build())
.add(TextCell.builder().text("of").build()).add(TextCell.builder().text("Table").build())
Expand Down
Loading