diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-24 14:53:40 +0200 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-10-24 14:53:40 +0200 |
commit | e3f81b5376e639aa1954441f0ad0e749773a6dff (patch) | |
tree | d5753f913fed104f0af4441c771694d6a76b82a3 /pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl | |
parent | 89e2fa9b385c37d2aefdccb01c777f87d3e096a2 (diff) | |
download | pdf-as-4-e3f81b5376e639aa1954441f0ad0e749773a6dff.tar.gz pdf-as-4-e3f81b5376e639aa1954441f0ad0e749773a6dff.tar.bz2 pdf-as-4-e3f81b5376e639aa1954441f0ad0e749773a6dff.zip |
start Font adjusting
Diffstat (limited to 'pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl')
2 files changed, 34 insertions, 389 deletions
diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java index 927469b1..826124ee 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java @@ -23,7 +23,6 @@ ******************************************************************************/ package at.gv.egiz.pdfas.lib.impl.stamping.pdfbox; -import java.awt.Color; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; @@ -33,7 +32,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -48,8 +46,6 @@ import org.apache.pdfbox.pdmodel.PDResources; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.common.PDStream; import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; -import org.apache.pdfbox.pdmodel.font.PDFont; -import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg; import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap; import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm; @@ -68,401 +64,18 @@ import at.gv.egiz.pdfas.common.exceptions.PdfAsException; import at.gv.egiz.pdfas.common.settings.ISettings; import at.gv.egiz.pdfas.common.utils.ImageUtils; import at.knowcenter.wag.egov.egiz.table.Entry; -import at.knowcenter.wag.egov.egiz.table.Style; public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder { private static final Logger logger = LoggerFactory .getLogger(PDFAsVisualSignatureBuilder.class); - private void drawDebugLine(PDPageContentStream contentStream, float x, - float y, float width, float height) { - try { - contentStream.setStrokingColor(Color.RED); - contentStream.drawLine(x, y, x + width, y); - contentStream.setStrokingColor(Color.BLUE); - contentStream.drawLine(x, y, x, y - height); - contentStream.setStrokingColor(Color.GREEN); - contentStream.drawLine(x + width, y, x + width, y - height); - contentStream.setStrokingColor(Color.ORANGE); - contentStream.drawLine(x, y - height, x + width, y - height); - - contentStream.setStrokingColor(Color.BLACK); - } catch (Throwable e) { - e.printStackTrace(); - } - } - - private void drawDebugPadding(PDPageContentStream contentStream, float x, - float y, float padding, float width, float height) { - try { - contentStream.setStrokingColor(Color.RED); - contentStream.drawLine(x, y, x + padding, y - padding); - contentStream.drawLine(x + width, y, x + width - padding, y - - padding); - contentStream.drawLine(x + width, y - height, x + width - padding, - y - height + padding); - contentStream.drawLine(x, y - height, x + padding, y - height - + padding); - contentStream.setStrokingColor(Color.BLACK); - } catch (Throwable e) { - e.printStackTrace(); - } - } - - private void drawTable(PDPage page, PDPageContentStream contentStream, - float x, float y, float width, float height, - PDFBoxTable abstractTable, PDDocument doc, boolean subtable) - throws IOException, PdfAsException { - - final int rows = abstractTable.getRowCount(); - final int cols = abstractTable.getColCount(); - float[] colsSizes = abstractTable.getColsRelativeWith(); - int max_cols = abstractTable.getColCount(); - float padding = abstractTable.getPadding(); - float fontSize = PDFBoxFont.defaultFontSize; - PDFont textFont = PDFBoxFont.defaultFont; - if (colsSizes == null) { - colsSizes = new float[max_cols]; - // set the column ratio for all columns to 1 - for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) { - colsSizes[cols_idx] = 1; - } - } - - logger.debug("Drawing Table:"); - abstractTable.dumpTable(); - - if (abstractTable.getBGColor() != null) { - contentStream.setNonStrokingColor(abstractTable.getBGColor()); - contentStream.fillRect(x, y, abstractTable.getWidth(), - abstractTable.getHeight()); - contentStream.setNonStrokingColor(Color.BLACK); - } - float total = 0; - - for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) { - total += colsSizes[cols_idx]; - } - - for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) { - colsSizes[cols_idx] = (colsSizes[cols_idx] / total) - * abstractTable.getWidth(); - } - - for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) { - logger.debug("Col: " + cols_idx + " : " + colsSizes[cols_idx]); - } - - float border = abstractTable.style.getBorder(); - contentStream.setLineWidth(border); - - float tableHeight = abstractTable.getHeight(); - float tableWidth = abstractTable.getWidth(); - final float colWidth = tableWidth / (float) cols; - - // draw if boarder > 0 - if (border != 0) { - - float nexty = y + tableHeight; - float lasty = nexty; - for (int i = 0; i < rows; i++) { - ArrayList<Entry> row = abstractTable.getRow(i); - // Draw row border! - logger.debug("ROW LINE: {} {} {} {}", x, nexty, x + width, - nexty); - contentStream.drawLine(x, nexty, x + width, nexty); - lasty = nexty; - if (i < abstractTable.getRowHeights().length) { - // nexty -= abstractTable.getRowHeights()[i] + padding * 2; - nexty -= abstractTable.getRowHeights()[i]; - } - - // if (subtable && i + 1 == - // abstractTable.getRowHeights().length) { - // nexty -= padding; - // } - - float nextx = x; - float ypos = y; - float yheight = y + abstractTable.getHeight(); - if (subtable) { - // ypos -= padding; - yheight = y + abstractTable.getHeight(); - } - - for (int j = 0; j < row.size(); j++) { - Entry cell = (Entry) row.get(j); - - if (subtable && j == cols) { - continue; - } - logger.debug("COL LINE: {} {} {} {}", nextx, ypos, nextx, - yheight); - contentStream.drawLine(nextx, lasty, nextx, nexty); - for (int k = 0; k < cell.getColSpan(); k++) { - if (k + j < colsSizes.length) { - nextx += (colsSizes != null) ? colsSizes[k + j] - : colWidth; - } - } - } - if (!subtable) { - contentStream.drawLine(nextx, lasty, nextx, nexty); - } - } - - contentStream.drawLine(x, nexty, x + tableWidth, nexty); - - } - - float textx = x; - float texty = y + tableHeight; - for (int i = 0; i < abstractTable.getRowCount(); i++) { - ArrayList<Entry> row = abstractTable.getRow(i); - for (int j = 0; j < row.size(); j++) { - Entry cell = (Entry) row.get(j); - - Style inherit_style = Style.doInherit(cell.getStyle(), - abstractTable.style); - cell.setStyle(inherit_style); - - // if(subtable) { - drawDebugPadding(contentStream, textx, texty, padding, - ((colsSizes != null) ? colsSizes[j] : colWidth), - abstractTable.getRowHeights()[i]); - // } - // if(true) { - // textx += (colsSizes != null) ? colsSizes[j] : colWidth; - // continue; - // } - - if (cell.getType() == Entry.TYPE_CAPTION - || cell.getType() == Entry.TYPE_VALUE) { - - if (cell.getType() == Entry.TYPE_CAPTION) { - textFont = abstractTable.getFont().getFont(doc); - fontSize = abstractTable.getFont().getFontSize(); - } else if (cell.getType() == Entry.TYPE_VALUE) { - textFont = abstractTable.getValueFont().getFont(doc); - fontSize = abstractTable.getValueFont().getFontSize(); - } - - String text = (String) cell.getValue(); - - // COSName name = COSName.getPDFName("ANDI_TAG!"); - // contentStream.beginMarkedContentSequence(COSName.ALT, - // name); - String fontName = textFont.equals(PDType1Font.COURIER) ? "COURIER" - : "HELVETICA"; - - float fheight = textFont.getFontDescriptor().getCapHeight() - / 1000 * fontSize; - - String[] tlines = text.split("\n"); - float textHeight = fontSize * tlines.length; - - Style cellStyle = cell.getStyle(); - String valign = null; - String halign = null; - - if (cell.getType() == Entry.TYPE_CAPTION - && cellStyle != null) { - valign = cellStyle.getVAlign(); - halign = cellStyle.getHAlign(); - } else if (cell.getType() == Entry.TYPE_VALUE - && cellStyle != null) { - valign = cellStyle.getValueVAlign(); - halign = cellStyle.getValueHAlign(); - } - float ty = texty - padding; - float tx = textx + padding; - if (Style.BOTTOM.equals(valign)) { - float bottom_offset = abstractTable.getRowHeights()[i] - - textHeight; - ty -= bottom_offset; - } else if (Style.MIDDLE.equals(valign)) { - float bottom_offset = abstractTable.getRowHeights()[i] - - textHeight; - bottom_offset = bottom_offset / 2.0f; - ty -= bottom_offset; - } - - float columnWidth = (colsSizes != null) ? colsSizes[j] - : colWidth; - float maxWidth = 0; - for (int k = 0; k < tlines.length; k++) { - float lineWidth; - if (textFont instanceof PDType1Font) { - lineWidth = textFont.getStringWidth(tlines[k]) - / 1000.0f * fontSize; - } else { - float fwidth = textFont - .getStringWidth("abcdefghijklmnopqrstuvwxyz ") - / 1000.0f * fontSize; - fwidth = fwidth - / (float) "abcdefghijklmnopqrstuvwxyz" - .length(); - lineWidth = tlines[k].length() * fwidth; - } - - if (maxWidth < lineWidth) { - maxWidth = lineWidth; - } - } - - if (Style.CENTER.equals(halign)) { - float offset = columnWidth - maxWidth - 2 * padding; - if (offset > 0) { - offset = offset / 2.0f; - tx += offset; - } - } else if (Style.RIGHT.equals(halign)) { - float offset = columnWidth - maxWidth - 2 * padding; - if (offset > 0) { - tx += offset; - } - } - - drawDebugLine(contentStream, tx, ty, maxWidth, textHeight); - - contentStream.beginText(); - - if (innerFormResources.getFonts().containsValue(textFont)) { - String fontID = getFontID(textFont); - logger.debug("Using Font: " + fontID); - contentStream.appendRawCommands("/" + fontID + " " - + fontSize + " Tf\n"); - } else { - contentStream.setFont(textFont, fontSize); - } - - logger.debug("Writing: " + tx + " : " + (ty - fheight) - + " = " + text + " as " + cell.getType() + " w " - + fontName); - contentStream.moveTextPositionByAmount(tx, (ty - fheight)); - - if (text.contains("\n")) { - String[] lines = text.split("\n"); - contentStream.appendRawCommands(fontSize + " TL\n"); - for (int k = 0; k < lines.length; k++) { - contentStream.drawString(lines[k]); - if (k < lines.length - 1) { - contentStream.appendRawCommands("T*\n"); - } - } - } else { - contentStream.drawString(text); - } - contentStream.endText(); - // contentStream.endMarkedContentSequence(); - } else if (cell.getType() == Entry.TYPE_IMAGE) { - String img_ref = (String) cell.getValue(); - if (!images.containsKey(img_ref)) { - logger.error("Image not prepared! : " + img_ref); - throw new PdfAsException("Image not prepared! : " - + img_ref); - } - ImageObject image = images.get(img_ref); - PDXObjectImage pdImage = image.getImage(); - - float imgx = textx + padding; - float hoffset = ((colsSizes != null) ? colsSizes[j] - : colWidth) - image.getWidth(); - if (cell.getStyle().getImageVAlign() != null - && cell.getStyle().getImageVAlign() - .equals(Style.CENTER)) { - hoffset = hoffset / 2.0f; - imgx += hoffset; - } else if (cell.getStyle().getImageHAlign() != null - && cell.getStyle().getImageHAlign() - .equals(Style.RIGHT)) { - imgx += hoffset; - } - - float imgy = texty - padding; - float voffset = abstractTable.getRowHeights()[i] - - image.getHeight(); - if (cell.getStyle().getImageVAlign() != null - && cell.getStyle().getImageVAlign() - .equals(Style.MIDDLE)) { - voffset = voffset / 2.0f; - imgy -= voffset; - } else if (cell.getStyle().getImageVAlign() != null - && cell.getStyle().getImageVAlign() - .equals(Style.BOTTOM)) { - imgy -= voffset; - } - - drawDebugLine(contentStream, imgx, imgy, image.getWidth(), - image.getHeight()); - - logger.debug("Image: " + imgx + " : " - + (imgy - image.getHeight())); - contentStream.drawXObject(pdImage, imgx, - imgy - image.getHeight(), image.getWidth(), - image.getHeight()); - // contentStream.endMarkedContentSequence(); - - } else if (cell.getType() == Entry.TYPE_TABLE) { - - float tableY = texty - abstractTable.getRowHeights()[i]; - float tableX = textx; - // texty = texty - padding; - PDFBoxTable tbl_value = (PDFBoxTable) cell.getValue(); - - Style inherit_styletab = Style.doInherit( - abstractTable.style, cell.getStyle()); - tbl_value.table.setStyle(inherit_styletab); - - logger.debug("Table: " + tableX + " : " + tableY); - // logger.debug("Table height: " + ); - TableDrawUtils.drawTable(page, contentStream, tableX, - tableY, (colsSizes != null) ? colsSizes[j] - : colWidth, - abstractTable.getRowHeights()[i], tbl_value, doc, - true, innerFormResources, images, settings); - } - textx += (colsSizes != null) ? colsSizes[j] : colWidth; - } - // if (i + 1 < abstractTable.getRowHeights().length) { - logger.debug("Row {} from {} - {} - {} = {}", i, texty, - abstractTable.getRowHeights()[i], padding * 2, texty - - (abstractTable.getRowHeights()[i])); - texty -= abstractTable.getRowHeights()[i]; - // texty = texty - abstractTable.getRowHeights()[i + 1] - padding - // * 2; - // texty = texty - abstractTable.getRowHeights()[i] - padding - // * 2; - // } - textx = x; - } - } - - private void drawTable2(PDPage page, PDPageContentStream contentStream, - float x, float y, float width, float height, - PDFBoxTable abstractTable, PDDocument doc, boolean subtable) { - - } - private PDFAsVisualSignatureProperties properties; private PDFAsVisualSignatureDesigner designer; private ISettings settings; private PDResources innerFormResources; private Map<String, ImageObject> images = new HashMap<String, ImageObject>(); - private String getFontID(PDFont font) { - Iterator<java.util.Map.Entry<String, PDFont>> it = innerFormResources - .getFonts().entrySet().iterator(); - while (it.hasNext()) { - java.util.Map.Entry<String, PDFont> entry = it.next(); - if (entry.getValue().equals(font)) { - return entry.getKey(); - } - } - return ""; - } - public PDFAsVisualSignatureBuilder( PDFAsVisualSignatureProperties properties, ISettings settings, PDFAsVisualSignatureDesigner designer) { diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java index 88eb798a..3572ad54 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/TableDrawUtils.java @@ -209,11 +209,21 @@ public class TableDrawUtils { tx += offset; } } - + float ascent = textFont.getFontDescriptor().getAscent(); + float descent = textFont.getFontDescriptor().getDescent(); + + ascent = ascent / 1000.0f * fontSize; + descent = descent / 1000.0f * fontSize; + + ty = ty + (descent * (-1)); + logger.debug("Text tx {} ty {} maxWidth {} textHeight {}", tx, ty, maxWidth, textHeight); + logger.debug("Text ASCENT {} DESCENT {}", ascent, descent); - drawDebugLine(contentStream, tx, ty, maxWidth, textHeight, settings); + logger.debug("Text TRANSFORMED ASCENT {} DESCENT {}", ascent, descent); + + drawDebugLineString(contentStream, tx, ty, maxWidth, textHeight, descent, settings); contentStream.beginText(); @@ -528,6 +538,28 @@ public class TableDrawUtils { } } } + + private static void drawDebugLineString(PDPageContentStream contentStream, + float x, float y, float width, float height, float descent, ISettings settings) { + if ("true".equals(settings.getValue(TABLE_DEBUG))) { + try { + contentStream.setStrokingColor(Color.RED); + contentStream.drawLine(x, y, x + width, y); + contentStream.setStrokingColor(Color.BLUE); + contentStream.drawLine(x, y, x, y - height); + contentStream.setStrokingColor(Color.GREEN); + contentStream.drawLine(x + width, y, x + width, y - height); + contentStream.setStrokingColor(Color.ORANGE); + contentStream.drawLine(x, y - height, x + width, y - height); + contentStream.setStrokingColor(Color.MAGENTA); + contentStream.drawLine(x, y + (descent * (-1)) - height, x + width, y + (descent * (-1)) - height); + + contentStream.setStrokingColor(Color.BLACK); + } catch (Throwable e) { + e.printStackTrace(); + } + } + } private static void drawDebugPadding(PDPageContentStream contentStream, float x, float y, float padding, float width, float height, |