📄 plaintextoutputtarget.java
字号:
/**
* ========================================
* JFreeReport : a free Java report library
* ========================================
*
* Project Info: http://www.jfree.org/jfreereport/index.html
* Project Lead: Thomas Morgner;
*
* (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
*
* This library 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 2.1 of the License, or (at your option) any later version.
*
* This library 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 this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* --------------------------
* PlainTextOutputTarget.java
* --------------------------
* (C)opyright 2003, by Thomas Morgner.
*
* Original Author: Thomas Morgner;
* Contributor(s): David Gilbert (for Simba Management Limited);
*
* $Id: PlainTextOutputTarget.java,v 1.12.2.1 2003/12/21 23:28:45 taqua Exp $
*
* Changes
* -------
* 29-Jan-2003 : Initial version
* 21-Feb-2003 : Fixed a bug with unclean calculations when writing strings.
*/
package org.jfree.report.modules.output.pageable.plaintext;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.io.IOException;
import org.jfree.report.DrawableContainer;
import org.jfree.report.ImageReference;
import org.jfree.report.content.ContentFactory;
import org.jfree.report.content.DefaultContentFactory;
import org.jfree.report.content.TextContentFactoryModule;
import org.jfree.report.layout.SizeCalculator;
import org.jfree.report.modules.output.pageable.base.LogicalPage;
import org.jfree.report.modules.output.pageable.base.OutputTarget;
import org.jfree.report.modules.output.pageable.base.OutputTargetException;
import org.jfree.report.modules.output.pageable.base.output.AbstractOutputTarget;
import org.jfree.report.modules.output.pageable.base.output.DummyOutputTarget;
import org.jfree.report.modules.output.pageable.base.physicals.PhysicalPage;
import org.jfree.report.style.FontDefinition;
import org.jfree.report.util.ReportConfiguration;
/**
* An outputtarget, that generates plaintext. The text can be enriched with
* escape sequences for Epson- or IBM-Compatible printers.
* <p>
* This target does not support images, shapes or different fonts.
* The output generation is needle-printer oriented, the pageformat is translated
* into a text page, graphics coordinates are aligned along the character grid
* of the text mode.
* <p>
* It is assumed that all characters have the same width, proportional printing
* is not supported.
* <p>
* The output mode is defined by supplying a suitable PrinterCommandSet. Depending
* on the target printer, you can supply several PrinterCommandSets:
* <ul>
* <li>EpsonPrinterCommandSet
* <p>Suitable for all Epson ESC/P compatible printers. The content will be formated
* with ESC/P escape sequences.
* <li>IBMPrinterCommandSet
* <p>Suitable for all IBM Compatible needle printers. The content will be formated using
* IBM escape sequences.
* <li>PrinterCommandSet
* <p>Suitable for unsupported printers and for text file output. The content will be
* written without any printer control sequences.
* </ul>
*
* @see org.jfree.report.modules.output.pageable.plaintext.PrinterCommandSet
* @see org.jfree.report.modules.output.pageable.plaintext.IBMPrinterCommandSet
* @see org.jfree.report.modules.output.pageable.plaintext.EpsonPrinterCommandSet
* @see org.jfree.report.modules.output.pageable.plaintext.PlainTextPage
*
* @author Thomas Morgner
*/
public strictfp class PlainTextOutputTarget extends AbstractOutputTarget
{
/** The configuration prefix for all properties. */
public static final String CONFIGURATION_PREFIX =
"org.jfree.report.modules.output.pageable.plaintext.";
/** The property to define the encoding of the text. */
public static final String ENCODING_PROPERTY = "Encoding";
/** The property to define the lines per inch of the text. */
public static final String LINES_PER_INCH = "LinesPerInch";
/** The property to define the characters per inch of the text. */
public static final String CHARS_PER_INCH = "CharsPerInch";
/** The 'XML encoding' property key. */
public static final String TEXT_OUTPUT_ENCODING
= CONFIGURATION_PREFIX + ENCODING_PROPERTY;
/** A default value of the 'XML encoding' property key. */
public static final String TEXT_OUTPUT_ENCODING_DEFAULT =
ReportConfiguration.getPlatformDefaultEncoding();
/**
* A state of a Graphics2D object. This does not store clipping regions or advanced
* properties.
*/
private static final class PlainTextState
{
/** The paint. */
private Paint mypaint;
/** The font. */
private FontDefinition myfont;
/** The stroke. */
private Stroke mystroke;
/**
* creates a new PlainTextState.
*
* @param source the source outputtarget.
*/
private PlainTextState(final OutputTarget source)
{
save(source);
}
/**
* Saves the state of the OutputTarget.
*
* @param source the OutputTarget.
*/
protected void save(final OutputTarget source)
{
mypaint = source.getPaint();
myfont = source.getFont();
mystroke = source.getStroke();
}
/**
* Copies the state back to the specified OutputTarget.
*
* @param target the OutputTarget.
* @throws OutputTargetException if restoring the output target state failed.
*/
protected void restore(final OutputTarget target)
throws OutputTargetException
{
target.setStroke(mystroke);
target.setFont(myfont);
target.setPaint(mypaint);
}
}
/**
* The PlainTextSizeCalculator is used to calculate the dimensions
* of an PlainText string.
*/
private static final class PlainTextSizeCalculator implements SizeCalculator
{
/** the current character width. */
private final float characterWidth;
/** the current character height. */
private final float characterHeight;
/**
* Creates a new PlainTextSizeCalculator.
*
* @param characterWidth the character width in points (1/72 inch).
* @param characterHeight the character height in points (1/72 inch).
*/
private PlainTextSizeCalculator(final float characterWidth, final float characterHeight)
{
this.characterWidth = characterWidth;
this.characterHeight = characterHeight;
}
/**
* Calculates the width of a <code>String<code> in the current <code>Graphics</code> context.
*
* @param text the text.
* @param lineStartPos the start position of the substring to be measured.
* @param endPos the position of the last character to be measured.
*
* @return the width of the string in Java2D units.
*/
public float getStringWidth(final String text, final int lineStartPos, final int endPos)
{
if (lineStartPos < 0)
{
throw new IllegalArgumentException("LineStart < 0");
}
if (endPos < lineStartPos)
{
throw new IllegalArgumentException("LineEnd < LineStart");
}
if (endPos > text.length())
{
throw new IllegalArgumentException("EndPos > TextLength");
}
return characterWidth * ((float) (endPos - lineStartPos));
}
/**
* Returns the line height. This includes the font's ascent, descent and leading.
*
* @return the line height.
*/
public float getLineHeight()
{
return characterHeight;
}
}
/** a flag indicating whether this OutputTarget is open. */
private boolean open;
/** the current font definition. */
private FontDefinition font;
/** the current paint, is not used. */
private Paint paint;
/** the current stroke, is not used. */
private Stroke stroke;
/** the current page width in CPI. */
private int currentPageWidth;
/** the current page height in LPI. */
private int currentPageHeight;
/** the character width in points. */
private float characterWidth;
/** the character height in points. */
private float characterHeight;
/** the current save state of this output target. */
private PlainTextState savedState;
/** the currently generated page. */
private PlainTextPage pageBuffer;
/** the current printer command set used to write and format the page. */
private PrinterCommandSet commandSet;
/**
* Creates a new PlainTextOutputTarget which uses the given command set to write
* the generated content.
*
* @param format the page format.
* @param commandSet the printer commandset used to write the generated content.
* @throws java.lang.NullPointerException if the printer command set is null
*/
public PlainTextOutputTarget(final PageFormat format, final PrinterCommandSet commandSet)
{
super(format);
if (commandSet == null)
{
throw new NullPointerException();
}
this.commandSet = commandSet;
}
/**
* Creates a new PlainTextOutputTarget which uses the given command set to write
* the generated content.
*
* @param commandSet the printer commandset used to write the generated content.
* @param logical the page format used by this target for layouting.
* @param physical the page format used by this target for printing.
* @throws java.lang.NullPointerException if the printer command set is null
*/
public PlainTextOutputTarget(final PageFormat logical, final PageFormat physical,
final PrinterCommandSet commandSet)
{
super(logical, physical);
if (commandSet == null)
{
throw new NullPointerException();
}
this.commandSet = commandSet;
}
/**
* Creates a new PlainTextOutputTarget which uses the given command set to write
* the generated content.
*
* @param commandSet the printer commandset used to write the generated content.
* @param logicalPage the page format used by this target for layouting.
* @throws java.lang.NullPointerException if the printer command set is null
*/
public PlainTextOutputTarget(final LogicalPage logicalPage, final PrinterCommandSet commandSet)
{
super(logicalPage);
if (commandSet == null)
{
throw new NullPointerException();
}
this.commandSet = commandSet;
}
/**
* Gets the printercommandset used to format the text.
* @return the printer command set.
*/
public PrinterCommandSet getCommandSet()
{
return commandSet;
}
/**
* Opens the target.
*
* @throws OutputTargetException if there is some problem opening the target.
*/
public void open() throws OutputTargetException
{
try
{
// 1 inch = 72 point
characterWidth = (72f / (float) commandSet.getDefaultCPI());
characterHeight = (72f / (float) commandSet.getDefaultLPI());
}
catch (Exception e)
{
throw new OutputTargetException("Failed to parse page format", e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -