All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.nervalreports.generators.PDFTextSentence Maven / Gradle / Ivy

Go to download

This is the PDF generator package of NervalReports (a lightweight report creation library), used to generate a report directly to a .pdf file.

There is a newer version: 1.2
Show newest version
/** This file is part of nervalreports.
 *
 * nervalreports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nervalreports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with nervalreports.  If not, see . */
package net.sf.nervalreports.generators;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import net.sf.nervalreports.core.ReportColor;

/** Text construction info for {@link PDFReportGenerator}.  
 * @author farrer */
/* default */ class PDFTextSentence extends PDFSentence {
	
	enum PDFTextSentenceType {
		/** Just a defined fixed text. */
		NORMAL,
		/** Text that represents the current page number. */
		CURRENT_PAGE,
		/** Text that represents the total document pages. */
		PAGE_COUNT
	}
	
	/** The sentence itself. */
	private String text;
	
	/** String builder to use, when text was added (instead of just a single text). */
	private StringBuilder stringBuilder;
	
	/** Type of the textual sentence. */
	private final PDFTextSentenceType type;
	
	/** Color to use for the sentence */
	private final ReportColor color;
	
	/** Font size of the sentence. */
	private final int fontSize;
	
	/** Font used for the sentence. */
	private final PDType1Font font;
	
	/** Y correction factor for the sentence. */
	private final float[] correctionFactor = new float[2];
	
	/** Default constructor.
	 * @param type type of the textual sentence.
	 * @param text text of the sentence.
	 * @param color color to use.
	 * @param font font to use.
	 * @param fontSize size of the font. 
	 * @param width width to display the sentence (as having a final space).
	 * @throws IOException in case of error retrieving the bounding box from font.
	 * @TODO add support for TrueType fonts with the PDType0Font PDFBox 2.0 class. */
	PDFTextSentence(PDFTextSentenceType type, String text, ReportColor color, PDType1Font font, int fontSize, float width) throws IOException {
		this.text = text;
		this.fontSize = fontSize;
		this.color = color;
		this.font = font;
		this.type = type;
		setWidth(width);
		setHeight(fontSize);
		this.stringBuilder = null;
		this.correctionFactor[0] = Math.abs((font.getBoundingBox().getLowerLeftY() / 1000) * fontSize);
		this.correctionFactor[1] = Math.abs((font.getBoundingBox().getUpperRightY() / 1000) * fontSize);
	}
	
	/** Add text to the current sentence.
	 * @param text text to add.
	 * @param @param widthForText width of the text we are adding (as with a final space).. */
	void addText(String text, float widthForText) {
		if (stringBuilder == null) {
			/* Yet no string builder, let's create it, appending current defined text */
			stringBuilder = new StringBuilder();
			stringBuilder.append(this.text);
		}
		/* Append new text, prefixed with space */
		stringBuilder.append(" ");
		stringBuilder.append(text);
		setWidth(getWidth() + widthForText);
		/* Make sure will recalculate the text from StringBuilder when needed. */
		this.text = null;
	}
	
	/** @return if current sentence is compatible with another one. */
	boolean isCompatibleWith(PDFTextSentenceType type, ReportColor color, PDType1Font font, int fontSize) {
		return this.type.equals(type) && this.color.equals(color) && this.font.equals(font) && (this.fontSize == fontSize);
	}
	
	/** @return {@link #text}. */
	String getText() {
		if ((text == null) && (stringBuilder != null)) {
			/* Text must be built from builder. */
			text = stringBuilder.toString();
		}
		return text;
	}
	
	/** Replace the current {@link #text}, keeping its style.  */
	void replaceText(String text, float width) {
		this.text = text;
		this.stringBuilder = null;
		setWidth(width);
	}
	
	/** @return {@link #color}. */
	ReportColor getColor() {
		return color;
	}
	
	/** @return {@link #fontSize}. */
	int getFontSize() {
		return fontSize;
	}
	
	/** @return {@link #font}. */
	PDType1Font getFont() {
		return font;
	}
	
	/** @return {@link #type}. */
	PDFTextSentenceType getType() {
		return type;
	}
	
	/** As the font draw is from bottom to top, the initial Y position must be corrected when, for example,
	 * drawing backgrounds. this function will return the amount one should correct for current cell. 
	 * @return float[2], representing absolute (ie: without signal) correction factor to Y coordinates,
	 *         according to font bounding box. [0] is LowerLeftY and [1] is UpperRightY. 
	 * @throws IOException while recovering font bounding box. */
	float[] getYCorrectionFactor() {
		return correctionFactor;
	}

	/** {@inheritDoc} */
	@Override
	void flush(PDPageContentStream pageContentStream, float x, float y) throws Exception {
		pageContentStream.beginText();
		pageContentStream.setFont(getFont(), getFontSize());
		pageContentStream.setNonStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
		pageContentStream.newLineAtOffset(x, y);
		pageContentStream.showText(getText());
		pageContentStream.endText();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy