diff --git a/src/main/java/be/quodlibet/boxable/Row.java b/src/main/java/be/quodlibet/boxable/Row.java index 8a5de982..b5fb3e41 100644 --- a/src/main/java/be/quodlibet/boxable/Row.java +++ b/src/main/java/be/quodlibet/boxable/Row.java @@ -22,7 +22,7 @@ public class Row { private boolean headerRow = false; float height; private float lineSpacing = 1; - + Row(Table table, List> cells, float height) { this.table = table; this.cells = cells; @@ -41,13 +41,15 @@ public class Row { * alignment *

* - * @param width Absolute width in points or in % of table width - * @param value Cell's value (content) + * @param width + * Absolute width in points or in % of table width + * @param value + * Cell's value (content) * @return New {@link Cell} */ public Cell createCell(float width, String value) { Cell cell = new Cell(this, width, value, true); - if(headerRow){ + if (headerRow) { // set all cell as header cell cell.setHeaderCell(true); } @@ -97,10 +99,16 @@ public Cell createImageCell(float width, Image img, HorizontalAlignment align * {@link PDPage} where this table cell will be drawn * @param yStart * Y position from which table will be drawn - * @return + * @param pageTopMargin + * {@link TableCell}'s top margin + * @param pageBottomMargin + * {@link TableCell}'s bottom margin + * @return {@link TableCell} with provided width and table data */ - public TableCell createTableCell(float width, String tableData, PDDocument doc, PDPage page, float yStart, float pageTopMargin, float pageBottomMargin) { - TableCell cell = new TableCell(this, width, tableData, true, doc, page, yStart, pageTopMargin, pageBottomMargin); + public TableCell createTableCell(float width, String tableData, PDDocument doc, PDPage page, float yStart, + float pageTopMargin, float pageBottomMargin) { + TableCell cell = new TableCell(this, width, tableData, true, doc, page, yStart, pageTopMargin, + pageBottomMargin); setBorders(cell, cells.isEmpty()); cells.add(cell); return cell; @@ -112,15 +120,19 @@ public TableCell createTableCell(float width, String tableData, PDDocument do * alignment *

* - * @param width Absolute width in points or in % of table width - * @param value Cell's value (content) - * @param align Cell's {@link HorizontalAlignment} - * @param valign Cell's {@link VerticalAlignment} + * @param width + * Absolute width in points or in % of table width + * @param value + * Cell's value (content) + * @param align + * Cell's {@link HorizontalAlignment} + * @param valign + * Cell's {@link VerticalAlignment} * @return New {@link Cell} */ public Cell createCell(float width, String value, HorizontalAlignment align, VerticalAlignment valign) { Cell cell = new Cell(this, width, value, true, align, valign); - if(headerRow){ + if (headerRow) { // set all cell as header cell cell.setHeaderCell(true); } @@ -135,7 +147,8 @@ public Cell createCell(float width, String value, HorizontalAlignment align, * Creates a cell with the same width as the corresponding header cell *

* - * @param value Cell's value (content) + * @param value + * Cell's value (content) * @return new {@link Cell} */ public Cell createCell(String value) { @@ -152,8 +165,11 @@ public Cell createCell(String value) { * border. In most cases left border will be removed. *

* - * @param cell {@link Cell} - * @param leftBorder boolean for drawing cell's left border. If {@code true} then the left cell's border will be drawn. + * @param cell + * {@link Cell} + * @param leftBorder + * boolean for drawing cell's left border. If {@code true} then + * the left cell's border will be drawn. */ private void setBorders(final Cell cell, final boolean leftBorder) { if (!leftBorder) { @@ -172,7 +188,7 @@ void removeTopBorders() { cell.setTopBorderStyle(null); } } - + /** *

* Remove all borders of cells. @@ -180,10 +196,11 @@ void removeTopBorders() { */ void removeAllBorders() { for (final Cell cell : cells) { - cell.setBorderStyle(null);; + cell.setBorderStyle(null); + ; } } - + /** *

* Gets maximal height of the cells in current row therefore row's height. diff --git a/src/main/java/be/quodlibet/boxable/Table.java b/src/main/java/be/quodlibet/boxable/Table.java index 91d754bf..fc72b459 100644 --- a/src/main/java/be/quodlibet/boxable/Table.java +++ b/src/main/java/be/quodlibet/boxable/Table.java @@ -64,16 +64,27 @@ public abstract class Table { /** * @deprecated Use one of the constructors that pass a {@link PageProvider} - * @param yStart Y position where {@link Table} will start - * @param yStartNewPage Y position where possible new page of {@link Table} will start - * @param pageBottomMargin bottom margin of {@link Table} - * @param width {@link Table} width - * @param margin {@link Table} margin - * @param document {@link PDDocument} where {@link Table} will be drawn - * @param currentPage current page where {@link Table} will be drawn (some tables are big and can be through multiple pages) - * @param drawLines draw {@link Table}'s borders - * @param drawContent draw {@link Table}'s content - * @throws IOException if fonts are not loaded correctly + * @param yStart + * Y position where {@link Table} will start + * @param yStartNewPage + * Y position where possible new page of {@link Table} will start + * @param pageBottomMargin + * bottom margin of {@link Table} + * @param width + * {@link Table} width + * @param margin + * {@link Table} margin + * @param document + * {@link PDDocument} where {@link Table} will be drawn + * @param currentPage + * current page where {@link Table} will be drawn (some tables + * are big and can be through multiple pages) + * @param drawLines + * draw {@link Table}'s borders + * @param drawContent + * draw {@link Table}'s content + * @throws IOException + * if fonts are not loaded correctly */ @Deprecated public Table(float yStart, float yStartNewPage, float pageBottomMargin, float width, float margin, @@ -84,14 +95,22 @@ public Table(float yStart, float yStartNewPage, float pageBottomMargin, float wi /** * @deprecated Use one of the constructors that pass a {@link PageProvider} - * @param yStartNewPage Y position where possible new page of {@link Table} will start - * @param pageBottomMargin bottom margin of {@link Table} - * @param width {@link Table} width - * @param margin {@link Table} margin - * @param document {@link PDDocument} where {@link Table} will be drawn - * @param drawLines draw {@link Table}'s borders - * @param drawContent draw {@link Table}'s content - * @throws IOException if fonts are not loaded correctly + * @param yStartNewPage + * Y position where possible new page of {@link Table} will start + * @param pageBottomMargin + * bottom margin of {@link Table} + * @param width + * {@link Table} width + * @param margin + * {@link Table} margin + * @param document + * {@link PDDocument} where {@link Table} will be drawn + * @param drawLines + * draw {@link Table}'s borders + * @param drawContent + * draw {@link Table}'s content + * @throws IOException + * if fonts are not loaded correctly */ @Deprecated public Table(float yStartNewPage, float pageBottomMargin, float width, float margin, PDDocument document, @@ -164,7 +183,8 @@ public void drawTitle(String title, PDFont font, int fontSize, float tableWidth, } if (title == null) { - // if you don't have title just use the height of maxTextBox in your "row" + // if you don't have title just use the height of maxTextBox in your + // "row" yStart -= height; } else { PDPageContentStream articleTitle = createPdPageContentStream(); @@ -214,6 +234,8 @@ public Row createRow(List> cells, float height) { * * @return Y position of the table * @throws IOException + * if underlying stream has problem being written to. + * */ public float draw() throws IOException { ensureStreamIsOpen(); @@ -236,13 +258,14 @@ public float draw() throws IOException { } private void drawRow(Row row) throws IOException { - // if it is not header row or first row in the table then remove row's top border + // if it is not header row or first row in the table then remove row's + // top border if (row != header && row != rows.get(0)) { - if(!isEndOfPage(row)){ + if (!isEndOfPage(row)) { row.removeTopBorders(); } } - + // draw the bookmark if (row.getBookmark() != null) { PDPageXYZDestination bookmarkDestination = new PDPageXYZDestination(); @@ -254,9 +277,9 @@ private void drawRow(Row row) throws IOException { // we want to remove the borders as often as possible removeTopBorders = true; - + // check also if we want all borders removed - if(allBordersRemoved()) { + if (allBordersRemoved()) { row.removeAllBorders(); } @@ -273,10 +296,12 @@ private void drawRow(Row row) throws IOException { for (Row headerRow : header) { drawRow(headerRow); } - // after you draw all header rows on next page please keep removing top borders to avoid double border drawing + // after you draw all header rows on next page please keep + // removing top borders to avoid double border drawing removeTopBorders = true; } else { - // after a page break, we have to ensure that top borders get drawn + // after a page break, we have to ensure that top borders get + // drawn removeTopBorders = false; } } @@ -289,7 +314,8 @@ private void drawRow(Row row) throws IOException { row.removeTopBorders(); } - // if it is header row or first row in the table, we have to draw the top border + // if it is header row or first row in the table, we have to draw the + // top border if (row == rows.get(0)) { removeTopBorders = false; } @@ -348,7 +374,8 @@ private void drawCellContent(Row row) throws IOException { float cursorY; for (Cell cell : row.getCells()) { - // remember horizontal cursor position, so we can advance to the next cell easily later + // remember horizontal cursor position, so we can advance to the + // next cell easily later float cellStartX = cursorX; if (cell instanceof ImageCell) { final ImageCell imageCell = (ImageCell) cell; @@ -415,15 +442,21 @@ private void drawCellContent(Row row) throws IOException { this.tableContentStream.setFont(cell.getFont(), cell.getFontSize()); if (cell.isTextRotated()) { - // debugging mode - drawing (default!) padding of rotated cells - //left - // PDStreamUtils.rect(tableContentStream, cursorX, yStart, 5, cell.getHeight(), Color.GREEN); - //top - // PDStreamUtils.rect(tableContentStream, cursorX, yStart, cell.getWidth(), 5 , Color.GREEN); + // debugging mode - drawing (default!) padding of rotated + // cells + // left + // PDStreamUtils.rect(tableContentStream, cursorX, yStart, + // 5, cell.getHeight(), Color.GREEN); + // top + // PDStreamUtils.rect(tableContentStream, cursorX, yStart, + // cell.getWidth(), 5 , Color.GREEN); // bottom - // PDStreamUtils.rect(tableContentStream, cursorX, yStart - cell.getHeight(), cell.getWidth(), -5 , Color.GREEN); - //right - // PDStreamUtils.rect(tableContentStream, cursorX + cell.getWidth() - 5, yStart, 5, cell.getHeight(), Color.GREEN); + // PDStreamUtils.rect(tableContentStream, cursorX, yStart - + // cell.getHeight(), cell.getWidth(), -5 , Color.GREEN); + // right + // PDStreamUtils.rect(tableContentStream, cursorX + + // cell.getWidth() - 5, yStart, 5, cell.getHeight(), + // Color.GREEN); cursorY = yStart - cell.getInnerHeight() - cell.getTopPadding() - (cell.getTopBorder() != null ? cell.getTopBorder().getWidth() : 0); @@ -438,7 +471,8 @@ private void drawCellContent(Row row) throws IOException { cursorY += cell.getVerticalFreeSpace(); break; } - // respect left padding and descend by font height to get position of the base line + // respect left padding and descend by font height to get + // position of the base line cursorX += cell.getLeftPadding() + (cell.getLeftBorder() == null ? 0 : cell.getLeftBorder().getWidth()) + FontUtils.getHeight(cell.getFont(), cell.getFontSize()) @@ -457,17 +491,24 @@ private void drawCellContent(Row row) throws IOException { // make tokenize method just in case cell.getParagraph().getLines(); } else { - // debugging mode - drawing (default!) padding of rotated cells - //left - // PDStreamUtils.rect(tableContentStream, cursorX, yStart, 5, cell.getHeight(), Color.RED); - //top - // PDStreamUtils.rect(tableContentStream, cursorX, yStart, cell.getWidth(), 5 , Color.RED); + // debugging mode - drawing (default!) padding of rotated + // cells + // left + // PDStreamUtils.rect(tableContentStream, cursorX, yStart, + // 5, cell.getHeight(), Color.RED); + // top + // PDStreamUtils.rect(tableContentStream, cursorX, yStart, + // cell.getWidth(), 5 , Color.RED); // bottom - // PDStreamUtils.rect(tableContentStream, cursorX, yStart - cell.getHeight(), cell.getWidth(), -5 , Color.RED); - //right - // PDStreamUtils.rect(tableContentStream, cursorX + cell.getWidth() - 5, yStart, 5, cell.getHeight(), Color.RED); - - // position at top of current cell descending by font height - font descent, because we are + // PDStreamUtils.rect(tableContentStream, cursorX, yStart - + // cell.getHeight(), cell.getWidth(), -5 , Color.RED); + // right + // PDStreamUtils.rect(tableContentStream, cursorX + + // cell.getWidth() - 5, yStart, 5, cell.getHeight(), + // Color.RED); + + // position at top of current cell descending by font height + // - font descent, because we are // positioning the base line here cursorY = yStart - cell.getTopPadding() - FontUtils.getHeight(cell.getFont(), cell.getFontSize()) - FontUtils.getDescent(cell.getFont(), cell.getFontSize()) @@ -485,13 +526,13 @@ private void drawCellContent(Row row) throws IOException { PDStreamUtils.rect(tableContentStream, cursorX + cell.getWidth() - (cell.getRightBorder() == null ? 0 : cell.getRightBorder().getWidth()) , yStart - (cell.getTopBorder() == null ? 0 : cell.getTopBorder().getWidth()), -cell.getRightPadding(), cell.getHeight() - (cell.getTopBorder() == null ? 0 : cell.getTopBorder().getWidth()) - (cell.getBottomBorder() == null ? 0 : cell.getBottomBorder().getWidth()), Color.RED); // @formatter:on } - - + // respect left padding cursorX += cell.getLeftPadding() + (cell.getLeftBorder() == null ? 0 : cell.getLeftBorder().getWidth()); - // the widest text does not fill the inner width of the cell? no + // the widest text does not fill the inner width of the + // cell? no // problem, just add it ;) switch (cell.getAlign()) { case CENTER: @@ -514,15 +555,17 @@ private void drawCellContent(Row row) throws IOException { cursorY -= cell.getVerticalFreeSpace(); break; } - + } - // remember this horizontal position, as it is the anchor for each + // remember this horizontal position, as it is the anchor for + // each // new line float lineStartX = cursorX; float lineStartY = cursorY; - - // if it is head row or if it is header cell then please use bold font + + // if it is head row or if it is header cell then please use + // bold font if (row.equals(header) || cell.isHeaderCell()) { this.tableContentStream.setFont(cell.getParagraph().getFont(true, false), cell.getFontSize()); } @@ -537,7 +580,7 @@ private void drawCellContent(Row row) throws IOException { // calculate the width of this line float freeSpaceWithinLine = cell.getParagraph().getMaxLineWidth() - cell.getParagraph().getLineWidth(entry.getKey()); - //TODO: need to implemented rotated text yo! + // TODO: need to implemented rotated text yo! if (cell.isTextRotated()) { cursorY = lineStartY; switch (cell.getAlign()) { @@ -557,7 +600,8 @@ private void drawCellContent(Row row) throws IOException { cursorX += freeSpaceWithinLine / 2; break; case LEFT: - // it doesn't matter because X position is always the same + // it doesn't matter because X position is always + // the same // as row above break; case RIGHT: @@ -613,24 +657,28 @@ private void drawCellContent(Row row) throws IOException { case BULLET: if (cell.isTextRotated()) { - // move cursorX up because bullet needs to be in the middle of font height + // move cursorX up because bullet needs to be in + // the middle of font height cursorX += FontUtils.getHeight(currentFont, cell.getFontSize()) / 2; PDStreamUtils.rect(tableContentStream, cursorX, cursorY, currentFont.getStringWidth(token.getData()) / 1000 * cell.getFontSize(), currentFont.getStringWidth(" ") / 1000 * cell.getFontSize(), cell.getTextColor()); - // move cursorY for two characters (one for bullet, one for space after bullet) + // move cursorY for two characters (one for + // bullet, one for space after bullet) cursorY += 2 * currentFont.getStringWidth(" ") / 1000 * cell.getFontSize(); // return cursorY to his original place cursorX -= FontUtils.getHeight(currentFont, cell.getFontSize()) / 2; } else { - // move cursorY up because bullet needs to be in the middle of font height + // move cursorY up because bullet needs to be in + // the middle of font height cursorY += FontUtils.getHeight(currentFont, cell.getFontSize()) / 2; PDStreamUtils.rect(tableContentStream, cursorX, cursorY, currentFont.getStringWidth(token.getData()) / 1000 * cell.getFontSize(), currentFont.getStringWidth(" ") / 1000 * cell.getFontSize(), cell.getTextColor()); - // move cursorX for two characters (one for bullet, one for space after bullet) + // move cursorX for two characters (one for + // bullet, one for space after bullet) cursorX += 2 * currentFont.getStringWidth(" ") / 1000 * cell.getFontSize(); // return cursorY to his original place cursorY -= FontUtils.getHeight(currentFont, cell.getFontSize()) / 2; @@ -666,13 +714,14 @@ private void drawCellContent(Row row) throws IOException { } } if (cell.isTextRotated()) { - cursorX = cursorX + cell.getParagraph().getFontHeight()* cell.getLineSpacing(); + cursorX = cursorX + cell.getParagraph().getFontHeight() * cell.getLineSpacing(); } else { - cursorY = cursorY - cell.getParagraph().getFontHeight()* cell.getLineSpacing(); + cursorY = cursorY - cell.getParagraph().getFontHeight() * cell.getLineSpacing(); } } } - // set cursor to the start of this cell plus its width to advance to the next cell + // set cursor to the start of this cell plus its width to advance to + // the next cell cursorX = cellStartX + cell.getWidth(); } // Set Y position for next row @@ -833,7 +882,8 @@ public List getBookmarks() { * * @deprecated Use {@link #addHeaderRow(Row)} instead, as it supports * multiple header rows - * @param header row that will be set as table's header row + * @param header + * row that will be set as table's header row */ @Deprecated public void setHeader(Row header) { @@ -890,7 +940,8 @@ public float getMinimumHeight() { * Setting current row as table header row *

* - * @param row The row that would be added as table's header row + * @param row + * The row that would be added as table's header row */ public void addHeaderRow(Row row) { this.header.add(row); @@ -908,7 +959,7 @@ public Row getHeader() { if (header == null) { throw new IllegalArgumentException("Header Row not set on table"); } - + return header.get(header.size() - 1); } diff --git a/src/main/java/be/quodlibet/boxable/utils/FontUtils.java b/src/main/java/be/quodlibet/boxable/utils/FontUtils.java index cb57b385..f358e205 100644 --- a/src/main/java/be/quodlibet/boxable/utils/FontUtils.java +++ b/src/main/java/be/quodlibet/boxable/utils/FontUtils.java @@ -35,7 +35,7 @@ public FontMetrics(final float height, final float ascent, final float descent) this.descent = descent; } } - + /** *

* {@link HashMap} for caching {@link FontMetrics} for designated @@ -43,9 +43,8 @@ public FontMetrics(final float height, final float ascent, final float descent) * expensive to calculate and the results are only approximate. */ private static final Map fontMetrics = new HashMap<>(); - + private static final Map defaultFonts = new HashMap<>(); - private FontUtils() { } @@ -61,8 +60,6 @@ private FontUtils() { * @param fontPath * font path which will be loaded * @return The read {@link PDType0Font} - * @throws IOException - * If reading the font file fails */ public static final PDType0Font loadFont(PDDocument document, String fontPath) { try { @@ -72,7 +69,7 @@ public static final PDType0Font loadFont(PDDocument document, String fontPath) { return null; } } - + /** *

* Retrieving {@link String} width depending on current font size. The width @@ -162,12 +159,10 @@ public static float getHeight(final PDFont font, final float fontSize) { *

* * @param font - * The font from which calculation will be applied -<<<<<<< HEAD + * The font from which calculation will be applied <<<<<<< HEAD * @throws IOException - * If reading the font file fails -======= ->>>>>>> using FreeSans as default font and added new free fonts + * If reading the font file fails ======= >>>>>>> using FreeSans + * as default font and added new free fonts */ private static void createFontMetrics(final PDFont font) { final float base = font.getFontDescriptor().getXHeight() / 1000; @@ -175,19 +170,20 @@ private static void createFontMetrics(final PDFont font) { final float descent = font.getFontDescriptor().getDescent() / 1000; fontMetrics.put(font.getName(), new FontMetrics(base + ascent - descent, ascent, descent)); } - - public static void addDefaultFonts(final PDFont font,final PDFont fontBold,final PDFont fontItalic,final PDFont fontBoldItalic) { + + public static void addDefaultFonts(final PDFont font, final PDFont fontBold, final PDFont fontItalic, + final PDFont fontBoldItalic) { defaultFonts.put("font", font); defaultFonts.put("fontBold", fontBold); defaultFonts.put("fontItalic", fontItalic); defaultFonts.put("fontBoldItalic", fontBoldItalic); } - + public static Map getDefaultfonts() { return defaultFonts; } - - public static void setSansFontsAsDefault(PDDocument document){ + + public static void setSansFontsAsDefault(PDDocument document) { defaultFonts.put("font", loadFont(document, "fonts/FreeSans.ttf")); defaultFonts.put("fontBold", loadFont(document, "fonts/FreeSansBold.ttf")); defaultFonts.put("fontItalic", loadFont(document, "fonts/FreeSansOblique.ttf"));