Skip to content
New issue

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

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

Already on GitHub? # to your account

Updates boxable official #142

Merged
merged 27 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
39c6f85
bugfix: HTML ordered lists
Frulenzo Oct 28, 2016
51db846
bugfix: enabled numbering for <ol> items in centered and right aligne…
Frulenzo Nov 8, 2016
67b5ccd
bugfix: drawing bullets for unordered list in center and right aligne…
Frulenzo Nov 9, 2016
e7e07c8
Implemented basic table inside cell over HTML tags (table,tr,td)
Frulenzo Aug 22, 2016
13bf2ea
Added SampleTest8() as simple representation of table inside cell
Frulenzo Aug 22, 2016
64f3c61
supporting colspans
Frulenzo Aug 22, 2016
c865dbc
Added support for drawing header cells (<th>)
Frulenzo Aug 23, 2016
4664802
removing prettyPrint() from Jsoup document
Frulenzo Sep 2, 2016
4bd9a4d
using FreeSans as default font and added new free fonts
Frulenzo Sep 7, 2016
42a65fc
bugfix: tokenizing cells with rotated text
Frulenzo Dec 7, 2016
a7ab1bd
bugfix: preserve indent when <br> occur in <li> elements
Frulenzo Dec 16, 2016
740f01d
added initial indent for HTML lists
Frulenzo Dec 19, 2016
a6d30fb
drawing possible table title in TableCell
Frulenzo Dec 19, 2016
4e27363
making inner table title centered and font size as default cell font …
Frulenzo Dec 21, 2016
971cc89
supported outer text inside table cell
Frulenzo Jan 10, 2017
377b9dd
removing default font inside table cell
Frulenzo Jan 10, 2017
178a03d
bugfix: drawing inner table on current page
Frulenzo Jan 10, 2017
f288366
adding removeAllBorders() method in Row class
Frulenzo Sep 22, 2017
94e3621
implemented methods for possible borderless table
Frulenzo Sep 22, 2017
0f430aa
added SampleTest11() for showing borderless table
Frulenzo Sep 22, 2017
21a29e7
adding removeAllBorders() method in Row class
Frulenzo Sep 22, 2017
c7a9693
removing uneccessery method im Row
Frulenzo Sep 22, 2017
a1c5111
bugfix: closing text when ordering in TableCell
Frulenzo Dec 15, 2017
91953ad
add JSoup dependency for maven in pom.xml
dhorions Feb 1, 2018
f97aedd
removed unnecessary comments
dhorions Feb 1, 2018
0dfd689
added comments to test methods
dhorions Feb 1, 2018
53472f6
update version to 1.5
dhorions Feb 1, 2018
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
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ repositories {
}

dependencies {
compile 'org.apache.pdfbox:pdfbox:2.0.0-RC2'
compile 'org.slf4j:slf4j-api:1.7.10'
compile 'com.google.guava:guava:18.0'
compile 'org.apache.commons:commons-csv:1.2'
compile 'org.apache.pdfbox:pdfbox:2.0.3'
compile 'org.slf4j:slf4j-api:1.7.21'
compile 'com.google.guava:guava:20.0'
compile 'org.apache.commons:commons-csv:1.4'
compile group: 'org.jsoup', name: 'jsoup', version: '1.9.2'
testCompile 'junit:junit:4.12'
testCompile 'commons-io:commons-io:2.4'
}
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dhorions</groupId>
<artifactId>boxable</artifactId>
<version>1.4</version>
<version>1.5</version>
<packaging>jar</packaging>

<name>Boxable, a high-level API to creates table on top of Apache Pdfbox</name>
Expand Down Expand Up @@ -56,6 +56,12 @@
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
<type>jar</type>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/be/quodlibet/boxable/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
*/
package be.quodlibet.boxable;

import be.quodlibet.boxable.line.LineStyle;
import be.quodlibet.boxable.text.WrappingFunction;
import java.awt.Color;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import be.quodlibet.boxable.line.LineStyle;
import be.quodlibet.boxable.text.WrappingFunction;
import be.quodlibet.boxable.utils.FontUtils;

public class Cell<T extends PDPage> {

private float width;
Expand Down Expand Up @@ -103,6 +106,11 @@ public class Cell<T extends PDPage> {
throw new IllegalArgumentException(
"Cell Width=" + getWidth() + " can't be bigger than row width=" + row.getWidth());
}
//check if we have new default font
if(!FontUtils.getDefaultfonts().isEmpty()){
font = FontUtils.getDefaultfonts().get("font");
fontBold = FontUtils.getDefaultfonts().get("fontBold");
}
this.text = text == null ? "" : text;
this.align = align;
this.valign = valign;
Expand Down
84 changes: 55 additions & 29 deletions src/main/java/be/quodlibet/boxable/Paragraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ public Paragraph(String text, PDFont font, float fontSize, float width, final Ho
final Color color, final TextType textType, WrappingFunction wrappingFunction) {
this(text, font, fontSize, width, align, color, textType, wrappingFunction, 1);
}

public Paragraph(String text, PDFont font, float fontSize, float width, final HorizontalAlignment align,
final Color color, final TextType textType, WrappingFunction wrappingFunction, float lineSpacing) {
this.color = color;
this.text = text;
this.font = font;
// check if we have different default font for italic and bold text
if(!FontUtils.getDefaultfonts().isEmpty()){
fontBold = FontUtils.getDefaultfonts().get("fontBold");
fontBoldItalic = FontUtils.getDefaultfonts().get("fontBoldItalic");
fontItalic = FontUtils.getDefaultfonts().get("fontItalic");
}
this.fontSize = fontSize;
this.width = width;
this.textType = textType;
Expand Down Expand Up @@ -118,7 +124,7 @@ public List<String> getLines() {
if (token.getData().equals("ol")) {
numberOfOrderedLists++;
if(listLevel > 1){
stack.add(new HTMLListNode(orderListElement-1, stack.isEmpty() ? String.valueOf("1.") : stack.peek().getValue() + String.valueOf(orderListElement-1) + "."));
stack.add(new HTMLListNode(orderListElement-1, stack.isEmpty() ? String.valueOf(orderListElement-1)+"." : stack.peek().getValue() + String.valueOf(orderListElement-1) + "."));
}
orderListElement = 1;

Expand Down Expand Up @@ -163,8 +169,8 @@ public List<String> getLines() {
if (token.getData().equals("ol")) {
numberOfOrderedLists--;
// reset elements
orderListElement = stack.peek().getOrderingNumber()+1;
if(numberOfOrderedLists > 1){
if(numberOfOrderedLists>0){
orderListElement = stack.peek().getOrderingNumber()+1;
stack.pop();
}
}
Expand All @@ -177,9 +183,6 @@ public List<String> getLines() {
lineCounter++;
}
} else if (isListElement(token)) {
if(!getAlign().equals(HorizontalAlignment.LEFT)) {
listLevel = 0;
}
// wrap at last wrap point?
if (textInLine.width() + sinceLastWrapPoint.trimmedWidth() > width) {
// this is our line
Expand Down Expand Up @@ -265,11 +268,8 @@ public List<String> getLines() {
}
// wrapping at last wrap point
if (listElement) {
if(!getAlign().equals(HorizontalAlignment.LEFT)) {
listLevel = 0;
}
if (numberOfOrderedLists>0) {
String tab = getAlign().equals(HorizontalAlignment.LEFT) ? indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) : indentLevel(DEFAULT_TAB);
String tab = getAlign().equals(HorizontalAlignment.LEFT) ? indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) + indentLevel(DEFAULT_TAB) : indentLevel(DEFAULT_TAB);
String orderingNumber = stack.isEmpty() ? String.valueOf(orderListElement) + "." : stack.peek().getValue() + "." + String.valueOf(orderListElement-1) + ".";
try {
textInLine.push(currentFont, fontSize, new Token(TokenType.PADDING,
Expand All @@ -280,7 +280,7 @@ public List<String> getLines() {
} else {
try {
// if it's not left aligned then ignore list and list element and deal with it as normal text where <li> mimic <br> behavior
String tabBullet = getAlign().equals(HorizontalAlignment.LEFT) ? indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) + indentLevel(BULLET_SPACE) : indentLevel(DEFAULT_TAB);
String tabBullet = getAlign().equals(HorizontalAlignment.LEFT) ? indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) + indentLevel(DEFAULT_TAB_AND_BULLET) : indentLevel(DEFAULT_TAB);
textInLine.push(currentFont, fontSize, new Token(TokenType.PADDING,
String.valueOf(font.getStringWidth(tabBullet) / 1000 * getFontSize())));
} catch (IOException e) {
Expand Down Expand Up @@ -343,14 +343,11 @@ public List<String> getLines() {
lineCounter++;
}
} else if (isListElement(token)) {
if(!getAlign().equals(HorizontalAlignment.LEFT)) {
listLevel = 0;
}
listElement = true;
// token padding, token bullet
try {
// if it's not left aligned then ignore list and list element and deal with it as normal text where <li> mimic <br> behaviour
String tab = getAlign().equals(HorizontalAlignment.LEFT) ? indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) : indentLevel(DEFAULT_TAB);
String tab = getAlign().equals(HorizontalAlignment.LEFT) ? indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) + indentLevel(DEFAULT_TAB) : indentLevel(DEFAULT_TAB);
textInLine.push(currentFont, fontSize, new Token(TokenType.PADDING,
String.valueOf(font.getStringWidth(tab) / 1000 * getFontSize())));
if (numberOfOrderedLists>0) {
Expand All @@ -365,9 +362,7 @@ public List<String> getLines() {
orderListElement++;
} else {
// if it's unordered list then just move by bullet character (take care of alignment!)
if(getAlign().equals(HorizontalAlignment.LEFT)){
textInLine.push(currentFont, fontSize, new Token(TokenType.BULLET, " "));
}
textInLine.push(currentFont, fontSize, new Token(TokenType.BULLET, " "));
}
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -381,6 +376,32 @@ public List<String> getLines() {
maxLineWidth = Math.max(maxLineWidth, textInLine.trimmedWidth());
textInLine.reset();
lineCounter++;
if(listLevel>0){
// preserve current indent
try {
if (numberOfOrderedLists>0) {
String tab = getAlign().equals(HorizontalAlignment.LEFT) ? indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) : indentLevel(DEFAULT_TAB);
// if it's ordering list then move depending on your: ordering number + ". "
String orderingNumber;
if(listLevel > 1){
orderingNumber = stack.peek().getValue() + String.valueOf(orderListElement) + ". ";
} else {
orderingNumber = String.valueOf(orderListElement) + ". ";
}
String tabAndOrderingNumber = tab + orderingNumber;
textInLine.push(currentFont, fontSize, new Token(TokenType.PADDING, String.valueOf(font.getStringWidth(tabAndOrderingNumber) / 1000 * getFontSize())));
orderListElement++;
} else {
if(getAlign().equals(HorizontalAlignment.LEFT)){
String tab = indentLevel(DEFAULT_TAB*Math.max(listLevel - 1, 0)) + indentLevel(DEFAULT_TAB) + indentLevel(BULLET_SPACE);
textInLine.push(currentFont, fontSize, new Token(TokenType.PADDING,
String.valueOf(font.getStringWidth(tab) / 1000 * getFontSize())));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
break;
case TEXT:
Expand Down Expand Up @@ -493,15 +514,15 @@ private boolean isList(final Token token) {
}

private static String indentLevel(int numberOfSpaces) {
//String builder is efficient at concatenating strings together
// String builder is efficient at concatenating strings together
StringBuilder sb = new StringBuilder();

//Loop as many times as specified; each time add a space to the string
// Loop as many times as specified; each time add a space to the string
for (int i = 0; i < numberOfSpaces; i++) {
sb.append(" ");
}

//Return the string
// Return the string
return sb.toString();
}

Expand Down Expand Up @@ -575,10 +596,10 @@ public float write(final PDPageContentStream stream, float cursorX, float cursor
}

public float getHeight() {
if(getLines().size() == 0){
if (getLines().size() == 0) {
return 0;
} else {
return (getLines().size()-1)*getLineSpacing()*getFontHeight() + getFontHeight();
return (getLines().size() - 1) * getLineSpacing() * getFontHeight() + getFontHeight();
}
}

Expand All @@ -597,7 +618,8 @@ public float getFontWidth() {

/**
* @deprecated This method will be removed in a future release
* @param width Paragraph's width
* @param width
* Paragraph's width
* @return {@link Paragraph} with designated width
*/
@Deprecated
Expand All @@ -608,8 +630,10 @@ public Paragraph withWidth(int width) {

/**
* @deprecated This method will be removed in a future release
* @param font {@link PDFont} for {@link Paragraph}
* @param fontSize font size for {@link Paragraph}
* @param font
* {@link PDFont} for {@link Paragraph}
* @param fontSize
* font size for {@link Paragraph}
* @return {@link Paragraph} with designated font and font size
*/
@Deprecated
Expand All @@ -621,8 +645,10 @@ public Paragraph withFont(PDFont font, int fontSize) {

/**
* /**
*
* @deprecated This method will be removed in a future release
* @param color {@code int} rgb value for color
* @param color
* {@code int} rgb value for color
* @return Paragraph's {@link Color}
*/
@Deprecated
Expand All @@ -634,7 +660,7 @@ public Paragraph withColor(int color) {
/**
* @deprecated This method will be replaced by
* {@code public Color getColor()} in a future release
* @return Paragraph's {@link Color}
* @return Paragraph's {@link Color}
*/
@Deprecated
public int getColor() {
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/be/quodlibet/boxable/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;

Expand Down Expand Up @@ -58,7 +59,7 @@ public Cell<T> createCell(float width, String value) {

/**
* <p>
* Creates a image cell with provided width and {@link Image}
* Creates an image cell with provided width and {@link Image}
* </p>
*
* @param width
Expand All @@ -81,6 +82,30 @@ public Cell<T> createImageCell(float width, Image img, HorizontalAlignment align
return cell;
}

/**
* <p>
* Creates a table cell with provided width and table data
* </p>
*
* @param width
* Table width
* @param tableData
* Table's data (HTML table tags)
* @param doc
* {@link PDDocument} where this table will be drawn
* @param page
* {@link PDPage} where this table cell will be drawn
* @param yStart
* Y position from which table will be drawn
* @return
*/
public TableCell<T> createTableCell(float width, String tableData, PDDocument doc, PDPage page, float yStart, float pageTopMargin, float pageBottomMargin) {
TableCell<T> cell = new TableCell<T>(this, width, tableData, true, doc, page, yStart, pageTopMargin, pageBottomMargin);
setBorders(cell, cells.isEmpty());
cells.add(cell);
return cell;
}

/**
* <p>
* Creates a cell with provided width, cell value, horizontal and vertical
Expand Down Expand Up @@ -148,6 +173,17 @@ void removeTopBorders() {
}
}

/**
* <p>
* Remove all borders of cells.
* </p>
*/
void removeAllBorders() {
for (final Cell<T> cell : cells) {
cell.setBorderStyle(null);;
}
}

/**
* <p>
* Gets maximal height of the cells in current row therefore row's height.
Expand Down
Loading