List of usage examples for org.apache.pdfbox.pdmodel.font PDFont willBeSubset
public abstract boolean willBeSubset();
From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.TableDrawUtils.java
License:EUPL
private static void drawString(PDPage page, PDPageContentStream contentStream, float contentx, float contenty, float width, float height, float padding, PDFBoxTable abstractTable, PDDocument doc, Entry cell, float fontSize, float textHeight, String valign, String halign, String[] tlines, PDFont textFont, PDResources formResources, ISettings settings) throws PdfAsException { try {//from www . jav a 2 s. co m float ty = contenty - padding; float tx = contentx + padding; float innerHeight = height - (2 * padding); float innerWidth = width - (2 * padding); if (Style.BOTTOM.equals(valign)) { float bottom_offset = innerHeight - textHeight; ty -= bottom_offset; } else if (Style.MIDDLE.equals(valign)) { float bottom_offset = innerHeight - textHeight; bottom_offset = bottom_offset / 2.0f; ty -= bottom_offset; } float descent = 0; float ascent; float lineWidth = 0; float txNew = 0; if (tlines.length > 1 && Style.LINECENTER.equals(halign)) { //Calculate TXs ArrayList<Float> calculatedTXs = new ArrayList<>(); for (int k = 0; k < tlines.length; k++) { // if (textFont instanceof PDType1Font) { lineWidth = textFont.getStringWidth(tlines[k]) / 1000.0f * fontSize; txNew = (innerWidth - lineWidth) / 2.0f; logger.debug("calculatedTXNew in k-Loop: {} {}", k, txNew); calculatedTXs.add(tx + txNew); logger.debug("INNERWIDTH in drawString: " + innerWidth); logger.debug("TX in drawString: " + tx); ascent = textFont.getFontDescriptor().getAscent(); descent = textFont.getFontDescriptor().getDescent(); ascent = ascent / 1000.0f * fontSize; descent = descent / 1000.0f * fontSize; //ty = ty + (descent * (-1)); logger.debug("Text txNew {} ty {} lineWidth {} textHeight {}", txNew, ty, lineWidth, textHeight); logger.debug("Text ASCENT {} DESCENT {}", ascent, descent); logger.debug("Text TRANSFORMED ASCENT {} DESCENT {}", ascent, descent); drawDebugLineString(contentStream, txNew, ty, lineWidth, textHeight, descent, settings); } contentStream.beginText(); contentStream.setFont(textFont, fontSize); txNew = tx + calculatedTXs.get(0); logger.debug("Calculated TX0: " + txNew); //contentStream.newLineAtOffset(txNew, (ty - fontSize + (descent * (-1)))); /* if (formResources.getFont(COSName.getPDFName(textFont.getName())) != null) { String fontID = getFontID(textFont, formResources); logger.debug("Using Font: " + fontID); contentStream.appendRawCommands("/" + fontID + " " + fontSize + " Tf\n"); } else { contentStream.setFont(textFont, fontSize); } logger.debug("Writing: " + tx + " : " + (ty - fontSize + (descent * (-1))) + " as " + cell.getType()); contentStream.moveTextPositionByAmount(tx, (ty - fontSize + (descent * (-1)))); contentStream.appendRawCommands(fontSize + " TL\n"); */ for (int k = 0; k < tlines.length; k++) { float offset = calculatedTXs.get(k); if (k == 0) { contentStream.newLineAtOffset(offset, (ty - fontSize + (descent * (-1)))); } else { logger.debug("Calculated TX: {} {} ", k, offset); contentStream.newLineAtOffset(offset, -1 * fontSize); } //contentStream.appendRawCommands("T*\n"); contentStream.showText(tlines[k]); contentStream.newLineAtOffset(-1 * offset, 0); } contentStream.endText(); } else { // calculate the max with of the text content float maxWidth = 0; for (int k = 0; k < tlines.length; k++) { // 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) || Style.LINECENTER.equals(halign)) { float offset = innerWidth - maxWidth; if (offset > 0) { offset = offset / 2.0f; tx += offset; } } else if (Style.RIGHT.equals(halign)) { float offset = innerWidth - maxWidth; if (offset > 0) { tx += offset; } } ascent = textFont.getFontDescriptor().getAscent(); 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); logger.debug("Text TRANSFORMED ASCENT {} DESCENT {}", ascent, descent); drawDebugLineString(contentStream, tx, ty, maxWidth, textHeight, descent, settings); contentStream.beginText(); contentStream.setFont(textFont, fontSize); contentStream.newLineAtOffset(tx, (ty - fontSize + (descent * (-1)))); /* if (formResources.getFont(COSName.getPDFName(textFont.getName())) != null) { String fontID = getFontID(textFont, formResources); logger.debug("Using Font: " + fontID); contentStream.appendRawCommands("/" + fontID + " " + fontSize + " Tf\n"); } else { contentStream.setFont(textFont, fontSize); } logger.debug("Writing: " + tx + " : " + (ty - fontSize + (descent * (-1))) + " as " + cell.getType()); contentStream.moveTextPositionByAmount(tx, (ty - fontSize + (descent * (-1)))); contentStream.appendRawCommands(fontSize + " TL\n"); */ if (textFont.willBeSubset()) { logger.debug("Font will be subset!"); } for (int k = 0; k < tlines.length; k++) { contentStream.showText(tlines[k]); if (k < tlines.length - 1) { contentStream.newLineAtOffset(0, -1 * fontSize); //contentStream.appendRawCommands("T*\n"); } } contentStream.endText(); } } catch (IOException e) { logger.warn("IO Exception", e); throw new PdfAsException("Error", e); } }