Jump to content
We've recently updated our Privacy Statement, available here ×

Reason why Jasper prints slowly


ktrinad

Recommended Posts

By: Eugene D - eugenedruy

Reason why Jasper prints slowly

2003-11-26 11:33

I think I identified the reason why jasper prints

slowly . This problem was referred also at

https://sourceforge.net/forum/message.php?msg_id=2042142

https://sourceforge.net/forum/message.php?msg_id=1852388

 

JasperReports printing mechanism exports whole report to Graphics2D .

Each report element there is drawn as a graphic primitive ( Just want to say

that the flow does not create an image from the whole report and put this image

to the graphics.) All this happens in JRGraphics2DExporter class and text export particularly in

JRGraphics2DExporter.exportText (I attached it to this mail)

The text elements (which are supposed to be the majority of the rendered data)

are drawn using Sun's AttributedString , TextLayout and LineBreakMeasurer classes

LineBreakMeasurer iteratively breaks AttributedString to multiline text for given format width.

At each iteration it returns TextLayout object that later renders itself through

layout.draw method.

It looks like performance is affected by the way how TextLayout draws itself and

it only can be found by Sun's source investigation.

TextLayout draws each character as a glyphs (lines set) unless it finds out that

internal string can be optimized. If it is opimized it renders text.

Difference could be enormous even for one page of text.

I attached Print2DtoStream.java test where I print

2 pages of text to postscript and if optimized

the created ps file will be around 200k

non-optimized version produces 28M

The optimizability test is done in the constructor

TextLayout(AttributedCharacterIterator text, FontRenderContext frc)

After sequense of various tests that check optimazability of the string

it calls fastInit method and if any of those tests does not pass it goes

to standardInit method that creates "non-optimized" TextLayout object.

One can always verify whether created TextLayout is optimizible by checking

optInfo member of TextLayout object. If it is not null then TextLayout object

is optimizable and the layout object would render itself as text not as glyphs.

It turned out that LineBreakMeasurer instantiates TextLayout object

through some hidden package accessible constructor that is not running

optimizability tests at all.So TextLayout object returned from

LineBreakMeasurer is going to be always not optimized and draw itself as glyphs.

I consider it a Sun bug, that probably should be reported to them.

I implemented a workaround in jasperreports. You can see

JRGraphics2DExporter.java

my changes start after comment

// eugene

and end at

//eugene

What I did I still use LineBreakMeasurer to break the text , but I don't use

the returned TextLayout . Instead I instantiate TextLayout myself

with the same substring as in the TextLayout returned from LineBreakMeasurer.

 

To see the difference one can use Print2DtoSteram.

simply try the case 1

TextLayout layout = new TextLayout(paragraph, grx.getFontRenderContext());

layout.draw(grx, 10,y);

and case 4:

while (lineMeasurer.getPosition() < paragraphEnd && !isMaxHeightReached)

{

TextLayout layout;

layout = lineMeasurer.nextLayout(formatWidth);

 

layout.draw(

grx,

10,

drawPosY + y

);

drawPosY += layout.getDescent();

}

you can compare 2 created pspostscript files , you can also open them

and see the difference inside. In the first case ps file is going to have

strings inside , in the second case , the text will be rendered as lines

 

There is also a Sun bug that I found

http://developer.java.sun.com/developer/bugParade/bugs/4480930.html.

The bug is not regarding performance , but regarding quality, however

if one read evaluation section and following letters, there are some notes about

performance

 

I wonder what you think about that . Correct me please if I am wrong.

 

------------------JRGraphics2DExporter-----------

package dori.jasper.engine.export;

 

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.GraphicsEnvironment;

import java.awt.Image;

import java.awt.RenderingHints;

import java.awt.Stroke;

import java.awt.font.FontRenderContext;

import java.awt.font.LineBreakMeasurer;

import java.awt.font.TextLayout;

import java.awt.geom.AffineTransform;

import java.io.File;

import java.io.InputStream;

import java.net.URL;

import java.text.AttributedCharacterIterator;

import java.text.AttributedString;

import java.util.Collection;

import java.util.Iterator;

import java.util.Map;

import java.util.StringTokenizer;

 

import dori.jasper.engine.JRAbstractExporter;

import dori.jasper.engine.JRAlignment;

import dori.jasper.engine.JRElement;

import dori.jasper.engine.JRException;

import dori.jasper.engine.JRExporterParameter;

import dori.jasper.engine.JRFont;

import dori.jasper.engine.JRGraphicElement;

import dori.jasper.engine.JRImage;

import dori.jasper.engine.JRLine;

import dori.jasper.engine.JRPrintElement;

import dori.jasper.engine.JRPrintEllipse;

import dori.jasper.engine.JRPrintImage;

import dori.jasper.engine.JRPrintLine;

import dori.jasper.engine.JRPrintPage;

import dori.jasper.engine.JRPrintRectangle;

import dori.jasper.engine.JRPrintText;

import dori.jasper.engine.JRReportFont;

import dori.jasper.engine.JRTextElement;

import dori.jasper.engine.JasperPrint;

import dori.jasper.engine.design.JRDesignReportFont;

import dori.jasper.engine.util.JRImageLoader;

import dori.jasper.engine.util.JRLoader;

import dori.jasper.engine.util.JRStringUtil;

 

 

/**

*

*/

public class JRGraphics2DExporter extends JRAbstractExporter

{

 

 

/**

*

*/

private JasperPrint jasperPrint = null;

private Graphics2D grx = null;

private int pageIndex = 0;

private float zoom = 1f;

 

/**

*

*/

private JRReportFont defaultFont = null;

 

 

/**

*

*/

static

{

//FIXME is this a MUST when working with system fonts?

String envfonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

}

 

 

/**

*

*/

private JRReportFont getDefaultFont()

{

if (this.defaultFont == null)

{

this.defaultFont = jasperPrint.getDefaultFont();

if (this.defaultFont == null)

{

this.defaultFont = new JRDesignReportFont();

}

}

 

return this.defaultFont;

}

 

 

/**

*

*/

public void exportReport() throws JRException

{

this.jasperPrint = (JasperPrint)this.parameters.get(JRExporterParameter.JASPER_PRINT);

if (jasperPrint == null)

{

InputStream is = (InputStream)this.parameters.get(JRExporterParameter.INPUT_STREAM);

if (is != null)

{

this.jasperPrint = (JasperPrint)JRLoader.loadObject(is);

}

else

{

URL url = (URL)this.parameters.get(JRExporterParameter.INPUT_URL);

if (url != null)

{

this.jasperPrint = (JasperPrint)JRLoader.loadObject(url);

}

else

{

File file = (File)this.parameters.get(JRExporterParameter.INPUT_FILE);

if (file != null)

{

this.jasperPrint = (JasperPrint)JRLoader.loadObject(file);

}

else

{

String fileName = (String)this.parameters.get(JRExporterParameter.INPUT_FILE_NAME);

if (fileName != null)

{

this.jasperPrint = (JasperPrint)JRLoader.loadObject(fileName);

}

else

{

throw new JRException("No input source supplied to the exporter.");

}

}

}

}

}

 

grx = (Graphics2D)this.parameters.get(JRGraphics2DExporterParameter.GRAPHICS_2D);

if (grx == null)

{

throw new JRException("No output specified for the exporter. java.awt.Graphics2D object expected.");

}

 

int lastPageIndex = -1;

if (this.jasperPrint.getPages() != null)

{

lastPageIndex = this.jasperPrint.getPages().size() - 1;

}

 

Integer index = (Integer)this.parameters.get(JRExporterParameter.PAGE_INDEX);

if (index != null)

{

this.pageIndex = index.intValue();

if (this.pageIndex < 0 || this.pageIndex > lastPageIndex)

{

throw new JRException("Page index out of range : " + this.pageIndex + " of " + lastPageIndex);

}

}

 

Float zoomRatio = (Float)this.parameters.get(JRGraphics2DExporterParameter.ZOOM_RATIO);

if (zoomRatio != null)

{

this.zoom = zoomRatio.floatValue();

if (this.zoom <= 0)

{

throw new JRException("Invalid zoom ratio : " + this.zoom);

}

}

 

this.exportReportToGraphics2D();

}

 

 

/**

*

*/

public void exportReportToGraphics2D()

{

grx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

//grx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

grx.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

 

AffineTransform atrans = new AffineTransform();

atrans.scale(zoom, zoom);

grx.transform(atrans);

 

java.util.List pages = jasperPrint.getPages();

if (pages != null)

{

JRPrintPage page = (JRPrintPage)pages.get(this.pageIndex);

this.exportPage(page);

}

}

 

 

/**

*

*/

private void exportPage(JRPrintPage page)

{

grx.setColor(Color.white);

grx.fillRect(

0,

0,

jasperPrint.getPageWidth(),

jasperPrint.getPageHeight()

);

 

grx.setColor(Color.black);

grx.setStroke(new BasicStroke(1));

 

/* */

JRPrintElement element = null;

Collection elements = page.getElements();

if (elements != null && elements.size() > 0)

{

for(Iterator it = elements.iterator(); it.hasNext();)

{

element = (JRPrintElement)it.next();

 

if (element instanceof JRPrintLine)

{

exportLine((JRPrintLine)element);

}

else if (element instanceof JRPrintRectangle)

{

exportRectangle((JRPrintRectangle)element);

}

else if (element instanceof JRPrintEllipse)

{

exportEllipse((JRPrintEllipse)element);

}

else if (element instanceof JRPrintImage)

{

exportImage((JRPrintImage)element);

}

else if (element instanceof JRPrintText)

{

exportText((JRPrintText)element);

}

}

}

}

 

 

/**

*

*/

private void exportLine(JRPrintLine line)

{

grx.setColor(line.getForecolor());

 

Stroke stroke = null;

switch (line.getPen())

{

case JRGraphicElement.PEN_DOTTED :

{

stroke = new BasicStroke(

1f,

BasicStroke.CAP_BUTT,

BasicStroke.JOIN_BEVEL,

0f,

new float[]{5f, 3f},

0f

);

break;

}

case JRGraphicElement.PEN_4_POINT :

{

stroke = new BasicStroke(4f);

break;

}

case JRGraphicElement.PEN_2_POINT :

{

stroke = new BasicStroke(2f);

break;

}

case JRGraphicElement.PEN_THIN :

{

stroke = new BasicStroke(0.5f);

break;

}

case JRGraphicElement.PEN_NONE :

{

break;

}

case JRGraphicElement.PEN_1_POINT :

default :

{

stroke = new BasicStroke(1f);

break;

}

}

 

if (stroke != null)

{

grx.setStroke(stroke);

 

if (line.getDirection() == JRLine.DIRECTION_TOP_DOWN)

{

grx.drawLine(

line.getX(),

line.getY(),

line.getX() + line.getWidth() - 1,

line.getY() + line.getHeight() - 1

);

}

else

{

grx.drawLine(

line.getX(),

line.getY() + line.getHeight() - 1,

line.getX() + line.getWidth() - 1,

line.getY()

);

}

}

}

 

 

/**

*

*/

private void exportRectangle(JRPrintRectangle rectangle)

{

if (rectangle.getMode() == JRElement.MODE_OPAQUE)

{

grx.setColor(rectangle.getBackcolor());

grx.fillRoundRect(

rectangle.getX(),

rectangle.getY(),

rectangle.getWidth(),

rectangle.getHeight(),

2 * rectangle.getRadius(),

2 * rectangle.getRadius()

);

}

 

grx.setColor(rectangle.getForecolor());

 

Stroke stroke = null;

switch (rectangle.getPen())

{

case JRGraphicElement.PEN_DOTTED :

{

stroke = new BasicStroke(

1f,

BasicStroke.CAP_BUTT,

BasicStroke.JOIN_BEVEL,

0f,

new float[]{5f, 3f},

0f

);

break;

}

case JRGraphicElement.PEN_4_POINT :

{

stroke = new BasicStroke(4f);

break;

}

case JRGraphicElement.PEN_2_POINT :

{

stroke = new BasicStroke(2f);

break;

}

case JRGraphicElement.PEN_THIN :

{

stroke = new BasicStroke(0.5f);

break;

}

case JRGraphicElement.PEN_NONE :

{

break;

}

case JRGraphicElement.PEN_1_POINT :

default :

{

stroke = new BasicStroke(1f);

break;

}

}

 

if (stroke != null)

{

grx.setStroke(stroke);

 

grx.drawRoundRect(

rectangle.getX(),

rectangle.getY(),

rectangle.getWidth() - 1,

rectangle.getHeight() - 1,

2 * rectangle.getRadius(),

2 * rectangle.getRadius()

);

}

}

 

 

/**

*

*/

private void exportEllipse(JRPrintEllipse ellipse)

{

if (ellipse.getMode() == JRElement.MODE_OPAQUE)

{

grx.setColor(ellipse.getBackcolor());

grx.fillOval(

ellipse.getX(),

ellipse.getY(),

ellipse.getWidth(),

ellipse.getHeight()

);

}

 

grx.setColor(ellipse.getForecolor());

 

Stroke stroke = null;

switch (ellipse.getPen())

{

case JRGraphicElement.PEN_DOTTED :

{

stroke = new BasicStroke(

1f,

BasicStroke.CAP_BUTT,

BasicStroke.JOIN_BEVEL,

0f,

new float[]{5f, 3f},

0f

);

break;

}

case JRGraphicElement.PEN_4_POINT :

{

stroke = new BasicStroke(4f);

break;

}

case JRGraphicElement.PEN_2_POINT :

{

stroke = new BasicStroke(2f);

break;

}

case JRGraphicElement.PEN_THIN :

{

stroke = new BasicStroke(0.5f);

break;

}

case JRGraphicElement.PEN_NONE :

{

break;

}

case JRGraphicElement.PEN_1_POINT :

default :

{

stroke = new BasicStroke(1f);

break;

}

}

 

if (stroke != null)

{

grx.setStroke(stroke);

 

grx.drawOval(

ellipse.getX(),

ellipse.getY(),

ellipse.getWidth() - 1,

ellipse.getHeight() - 1

);

}

}

 

 

/**

*

*/

private void exportImage(JRPrintImage printImage)

{

if (printImage.getMode() == JRElement.MODE_OPAQUE)

{

grx.setColor(printImage.getBackcolor());

 

grx.fillRect(

printImage.getX(),

printImage.getY(),

printImage.getWidth(),

printImage.getHeight()

);

}

 

 

int borderOffset = 0;

Stroke stroke = null;

switch (printImage.getPen())

{

case JRGraphicElement.PEN_DOTTED :

{

borderOffset = 0;

stroke = new BasicStroke(

1f,

BasicStroke.CAP_BUTT,

BasicStroke.JOIN_BEVEL,

0f,

new float[]{5f, 3f},

0f

);

break;

}

case JRGraphicElement.PEN_4_POINT :

{

borderOffset = 2;

stroke = new BasicStroke(4f);

break;

}

case JRGraphicElement.PEN_2_POINT :

{

borderOffset = 1;

stroke = new BasicStroke(2f);

break;

}

case JRGraphicElement.PEN_NONE :

{

break;

}

case JRGraphicElement.PEN_THIN :

{

borderOffset = 0;

stroke = new BasicStroke(0.5f);

break;

}

case JRGraphicElement.PEN_1_POINT :

default :

{

borderOffset = 0;

stroke = new BasicStroke(1f);

break;

}

}

 

 

int availableImageWidth = printImage.getWidth() - 2 * borderOffset;

availableImageWidth = (availableImageWidth < 0)?0:availableImageWidth;

 

int availableImageHeight = printImage.getHeight() - 2 * borderOffset;

availableImageHeight = (availableImageHeight < 0)?0:availableImageHeight;

 

byte[] imageData = printImage.getImageData();

 

if (

availableImageWidth > 0 && availableImageHeight > 0 &&

imageData != null && imageData.length > 0

)

{

Image awtImage = JRImageLoader.loadImage( imageData );

 

int awtWidth = awtImage.getWidth(null);

int awtHeight = awtImage.getHeight(null);

 

float xalignFactor = 0f;

switch (printImage.getHorizontalAlignment())

{

case JRAlignment.HORIZONTAL_ALIGN_RIGHT :

{

xalignFactor = 1f;

break;

}

case JRAlignment.HORIZONTAL_ALIGN_CENTER :

{

xalignFactor = 0.5f;

break;

}

case JRAlignment.HORIZONTAL_ALIGN_LEFT :

default :

{

xalignFactor = 0f;

break;

}

}

 

float yalignFactor = 0f;

switch (printImage.getVerticalAlignment())

{

case JRAlignment.VERTICAL_ALIGN_BOTTOM :

{

yalignFactor = 1f;

break;

}

case JRAlignment.VERTICAL_ALIGN_MIDDLE :

{

yalignFactor = 0.5f;

break;

}

case JRAlignment.VERTICAL_ALIGN_TOP :

default :

{

yalignFactor = 0f;

break;

}

}

 

switch (printImage.getScaleImage())// FIXME maybe put this in JRFiller

{

case JRImage.SCALE_IMAGE_CLIP :

{

int xoffset = (int)(xalignFactor * (availableImageWidth - awtWidth));

int yoffset = (int)(yalignFactor * (availableImageHeight - awtHeight));

 

grx.setClip(

printImage.getX() + borderOffset,

printImage.getY() + borderOffset,

availableImageWidth,

availableImageHeight

);

grx.drawImage(

awtImage,

printImage.getX() + xoffset + borderOffset,

printImage.getY() + yoffset + borderOffset,

awtWidth,

awtHeight,

//Color.red,

null

);

grx.setClip(

0,

0,

jasperPrint.getPageWidth(),

jasperPrint.getPageHeight()

);

 

break;

}

case JRImage.SCALE_IMAGE_FILL_FRAME :

{

grx.drawImage(

awtImage,

printImage.getX() + borderOffset,

printImage.getY() + borderOffset,

availableImageWidth,

availableImageHeight,

//Color.red,

null

);

 

break;

}

case JRImage.SCALE_IMAGE_RETAIN_SHAPE :

default :

{

if (printImage.getHeight() > 0)

{

double ratio = (double)awtWidth / (double)awtHeight;

 

if( ratio > (double)availableImageWidth / (double)availableImageHeight )

{

awtWidth = availableImageWidth;

awtHeight = (int)(availableImageWidth / ratio);

}

else

{

awtWidth = (int)(availableImageHeight * ratio);

awtHeight = availableImageHeight;

}

 

int xoffset = (int)(xalignFactor * (availableImageWidth - awtWidth));

int yoffset = (int)(yalignFactor * (availableImageHeight - awtHeight));

 

grx.drawImage(

awtImage,

printImage.getX() + xoffset + borderOffset,

printImage.getY() + yoffset + borderOffset,

awtWidth,

awtHeight,

//Color.red,

null

);

}

 

break;

}

}

}

 

if (stroke != null)

{

grx.setColor(printImage.getForecolor());

 

grx.setStroke(stroke);

 

grx.drawRect(

printImage.getX(),

printImage.getY(),

printImage.getWidth() - 1,

printImage.getHeight() - 1

);

}

}

 

 

/**

*

*/

private void exportText(JRPrintText text)

{

String allText = text.getText();

 

if (allText == null)

{

return;

}

 

if (text.getMode() == JRElement.MODE_OPAQUE)

{

grx.setColor(text.getBackcolor());

grx.fillRect(

text.getX(),

text.getY(),

text.getWidth(),

text.getHeight()

);

}

else

{

/*

grx.setColor(text.getForecolor());

grx.setStroke(new BasicStroke(1));

grx.drawRect(

text.getX(),

text.getY(),

text.getWidth(),

text.getHeight()

);

*/

}

 

if (allText.length() == 0)

{

return;

}

 

grx.setColor(text.getForecolor());

 

allText = JRStringUtil.treatNewLineChars(allText);

 

float formatWidth = (float) text.getWidth();

 

float verticalOffset = 0f;

switch (text.getVerticalAlignment())

{

case JRTextElement.VERTICAL_ALIGN_TOP :

{

verticalOffset = 0f;

break;

}

case JRTextElement.VERTICAL_ALIGN_MIDDLE :

{

verticalOffset = ((float)text.getHeight() - text.getTextHeight()) / 2f;

break;

}

case JRTextElement.VERTICAL_ALIGN_BOTTOM :

{

verticalOffset = (float)text.getHeight() - text.getTextHeight();

break;

}

default :

{

verticalOffset = 0f;

}

}

 

float lineSpacing = 1f;

switch (text.getLineSpacing())

{

case JRTextElement.LINE_SPACING_SINGLE :

{

lineSpacing = 1f;

break;

}

case JRTextElement.LINE_SPACING_1_1_2 :

{

lineSpacing = 1.5f;

break;

}

case JRTextElement.LINE_SPACING_DOUBLE :

{

lineSpacing = 2f;

break;

}

default :

{

lineSpacing = 1f;

}

}

 

int maxHeight = text.getHeight();

//FontRenderContext fontRenderContext = new FontRenderContext(new AffineTransform(), true, true);

FontRenderContext fontRenderContext = grx.getFontRenderContext();

JRFont font = text.getFont();

if (font == null)

{

font = this.getDefaultFont();

}

Map fontAttributes = font.getAttributes();

 

AttributedString atext;

AttributedCharacterIterator paragraph;

int paragraphStart;

int paragraphEnd;

LineBreakMeasurer lineMeasurer;

 

float drawPosY = 0;

float drawPosX = 0;

 

String paragr_text = "";

boolean isMaxHeightReached = false;

 

StringTokenizer tkzer = new StringTokenizer(allText, "n");

 

while(tkzer.hasMoreTokens() && !isMaxHeightReached)

{

paragr_text = tkzer.nextToken();

 

atext = new AttributedString(paragr_text, fontAttributes);

paragraph = atext.getIterator();

paragraphStart = paragraph.getBeginIndex();

paragraphEnd = paragraph.getEndIndex();

lineMeasurer = new LineBreakMeasurer(paragraph,fontRenderContext);

lineMeasurer.setPosition(paragraphStart);

 

TextLayout layout = null;

while (lineMeasurer.getPosition() < paragraphEnd && !isMaxHeightReached)

{

// eugene

int startIndex, endIndex;

startIndex = lineMeasurer.getPosition();

//eugene

 

layout = lineMeasurer.nextLayout(formatWidth);

// eugene

int charCount = layout.getCharacterCount();

endIndex = startIndex + charCount;

AttributedString tmpText = new AttributedString(paragraph, startIndex, endIndex);

TextLayout prevLayout = layout;

layout = new TextLayout(tmpText.getIterator(), fontRenderContext);

//eugene

 

drawPosY += layout.getLeading() + lineSpacing * layout.getAscent();

 

if (drawPosY + layout.getDescent() <= maxHeight)

{

switch (text.getTextAlignment())

{

case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED :

{

if (layout.isLeftToRight())

{

drawPosX = 0;

}

else

{

drawPosX = formatWidth - layout.getAdvance();

}

if (lineMeasurer.getPosition() < paragraphEnd)

{

layout = layout.getJustifiedLayout(formatWidth);

}

 

break;

}

case JRAlignment.HORIZONTAL_ALIGN_RIGHT :

{

if (layout.isLeftToRight())

{

drawPosX = formatWidth - layout.getAdvance();

}

else

{

drawPosX = formatWidth;

}

break;

}

case JRAlignment.HORIZONTAL_ALIGN_CENTER :

{

drawPosX = (formatWidth - layout.getAdvance()) / 2;

break;

}

case JRAlignment.HORIZONTAL_ALIGN_LEFT :

default :

{

if (layout.isLeftToRight())

{

drawPosX = 0;

}

else

{

drawPosX = formatWidth - layout.getAdvance();

}

}

}

 

layout.draw(

grx,

drawPosX + text.getX(),

drawPosY + text.getY() + verticalOffset

);

drawPosY += layout.getDescent();

}

else

{

drawPosY -= layout.getLeading() + lineSpacing * layout.getAscent();

isMaxHeightReached = true;

}

}

}

}

 

 

}

----------------------Print2DtoStream---------

package tests;

 

 

import sun.awt.font.FontResolver;

 

import java.io.*;

import java.awt.*;

import java.awt.font.FontRenderContext;

import java.awt.font.LineBreakMeasurer;

import java.awt.font.TextLayout;

import java.awt.font.TextAttribute;

import java.awt.print.*;

import java.text.AttributedString;

import java.text.AttributedCharacterIterator;

import java.util.Map;

import javax.print.*;

import javax.print.attribute.*;

import javax.print.attribute.standard.*;

 

/*

* Use the Java Print Service API to locate a service which can export

* 2D graphics to a stream as Postscript. This may be spooled to a

* Postscript printer, or used in a postscript viewer.

*/

public class Print2DtoStream implements Printable{

 

public Print2DtoStream() {

 

/* Use the pre-defined flavor for a Printable from an InputStream */

DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;

 

/* Specify the type of the output stream */

String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();

 

/* Locate factory which can export a GIF image stream as Postscript */

StreamPrintServiceFactory[] factories =

StreamPrintServiceFactory.lookupStreamPrintServiceFactories(

flavor, psMimeType);

if (factories.length == 0) {

System.err.println("No suitable factories");

System.exit(0);

}

 

try {

/* Create a file for the exported postscript */

FileOutputStream fos = new FileOutputStream("out.ps");

 

/* Create a Stream printer for Postscript */

StreamPrintService sps = factories[0].getPrintService(fos);

 

/* Create and call a Print Job */

DocPrintJob pj = sps.createPrintJob();

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

 

Doc doc = new SimpleDoc(this, flavor, null);

 

pj.print(doc, aset);

fos.close();

 

} catch (PrintException pe) {

System.err.println(pe);

} catch (IOException ie) {

System.err.println(ie);

}

}

 

private static final String mText =

"<html>Four score and seven years ago our fathers brought forth on this "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last full measure of devotion--that we here highly "

+ "resolve that these dead shall not have died in vain, that this "

+ "nation under God shall have a new birth of freedom, and that "

+ "government of the people, by the people, for the people shall "

+ "continent a new nation, conceived in liberty and dedicated to the "

+ "proposition that all men are created equal. Now we are engaged in "

+ "a great civil war, testing whether that nation or any nation so "

+ "conceived and so dedicated can long endure. We are met on a great "

+ "battlefield of that war. We have come to dedicate a portion of "

+ "that field as a final resting-place for those who here gave their "

+ "lives that that nation might live. It is altogether fitting and "

+ "proper that we should do this. But in a larger sense, we cannot "

+ "dedicate, we cannot consecrate, we cannot hallow this ground."

+ "The brave men, living and dead who struggled here have consecrated "

+ "it far above our poor power to add or detract. The world will "

+ "little note nor long remember what we say here, but it can never "

+ "forget what they did here. It is for us the living rather to be "

+ "dedicated here to the unfinished work which they who fought here "

+ "have thus far so nobly advanced. It is rather for us to be here "

+ "dedicated to the great task remaining before us--that from these "

+ "honored dead we take increased devotion to that cause for which "

+ "they gave the last

Link to comment
Share on other sites

  • 1 year later...
  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

I have noticed that this change is implemented it the latest release. But I have a very simple line based report and I have the MinimizePrinterJobSize flag set correctly. I have even checked to be sure Eugene's changes is being executed, but my Post Script files are still large. A 10 page report of simple lines of text is 20+ megs. This is on a linux system with jasper reports 2.0.2.

 

Any advice on how to get my post script files down to a reasonable size?

Link to comment
Share on other sites

  • 1 year later...

Continuation of the original discussion on sourceforge.net (truncated in the first post):

... full measure of devotion--that we here highly " 
+ "resolve that these dead shall not have died in vain, that this " 
+ "nation under God shall have a new birth of freedom, and that " 
+ "government of the people, by the people, for the people shall " 
+ "not perish from the earth.</html>"; 
 
 
public int print(Graphics g,PageFormat pf,int pageIndex) { 
 
if (pageIndex == 0) { 
/* 
Graphics2D g2d= (Graphics2D)g; 
g2d.translate(pf.getImageableX(), pf.getImageableY()); 
g2d.setColor(Color.black); 
g2d.drawString("example string", 250, 250); 
g2d.fillRect(0, 0, 200, 200); 
*/ 
 
Graphics2D grx= (Graphics2D)g; 
int y = 10; 
int chunkSize = 65; 
for(int i = 0; i < mText.length() - chunkSize - 1; i+= chunkSize) 

//grx.drawString(mText.substring(i, i +chunkSize), 10, y); 
 
String allText = mText.substring(i, i +chunkSize); 
 
if (allText == null) 

return PAGE_EXISTS; 

 
 
if (allText.length() == 0) 

return PAGE_EXISTS; 

 
 
 
float formatWidth = 600; 
 
float verticalOffset = y; 
 
float lineSpacing = 1f; 
 
int maxHeight = 1000; 
//FontRenderContext fontRenderContext = new FontRenderContext(new AffineTransform(), true, true); 
FontRenderContext fontRenderContext = grx.getFontRenderContext(); 
 
AttributedString atext; 
AttributedCharacterIterator paragraph; 
int paragraphStart; 
int paragraphEnd; 
LineBreakMeasurer lineMeasurer; 
 
float drawPosY = 0; 
float drawPosX = 0; 
 
String paragr_text = ""; 
boolean isMaxHeightReached = false; 
 

 
atext = new AttributedString(allText, grx.getFont().getAttributes()); 
paragraph = atext.getIterator(); 
paragraphStart = paragraph.getBeginIndex(); 
paragraphEnd = paragraph.getEndIndex(); 
lineMeasurer = new LineBreakMeasurer(paragraph,fontRenderContext); 
lineMeasurer.setPosition(paragraphStart); 
 
/* 
* sun\'s optimizability test extracted from TextLayout constructor 
 
int start = paragraph.getBeginIndex(); 
int limit = paragraph.getEndIndex(); 
if (start == limit) { 
throw new IllegalArgumentException("Zero length iterator passed to TextLayout constructor."); 

 
int len = limit - start; 
paragraph.first(); 
char[] chars = new char[len]; 
int n = 0; 
for (char c = paragraph.first(); c != paragraph.DONE; c = paragraph.next()) { 
chars[n++] = c; 

 
paragraph.first(); 
if (paragraph.getRunLimit() == limit) { 
 
Map attributes = paragraph.getAttributes(); 
Font font = singleFont(chars, 0, len, attributes); 
if (font != null) { 
//fastInit(chars, font, attributes, frc); 
//return; 


*/ 
 
// 1- uncomment following to see what happens 
// layout created from the constructor is printed 
//TextLayout layout = new TextLayout(paragraph, grx.getFontRenderContext()); 
//layout.draw(grx, 10,y); 
// 2- uncomment the following line to see how atrributed string is printed 
//grx.drawString(paragraph, 10, y); 
// 3- uncomment the following line to see how string is printed 
//grx.drawString(allText, 10, y); 
// 4- uncomment the following to see how textLayout returned from 
// lineMeasurer draws itself 
/* 
while (lineMeasurer.getPosition() < paragraphEnd && !isMaxHeightReached) 

TextLayout layout; 
layout = lineMeasurer.nextLayout(formatWidth); 
 
layout.draw( 
grx, 
10, 
drawPosY + y 
); 
drawPosY += layout.getDescent(); 

*/ 

y += 10; 

 
 
 
 
return Printable.PAGE_EXISTS; 
} else { 
return Printable.NO_SUCH_PAGE; 


 
// part of optimizability test from TextLayout class 
private static Font singleFont(char[] text, 
int start, 
int limit, 
Map attributes) { 
 
if (attributes.get(TextAttribute.CHAR_REPLACEMENT) != null) { 
return null; 

 
Font font = (Font)attributes.get(TextAttribute.FONT); 
if (font == null) { 
if (attributes.get(TextAttribute.FAMILY) != null) { 
font = Font.getFont(attributes); 
if (font.canDisplayUpTo(text, start, limit) != -1) { 
return null; 


else { 
FontResolver resolver = FontResolver.getInstance(); 
int fontIndex = resolver.getFontIndex(text[start]); 
Font fnt = resolver.getFont(fontIndex, null); 
for (int i=start+1; i<limit; i++) { 
if (resolver.getFontIndex(text) != fontIndex) { 
return null; 


font = resolver.getFont(fontIndex, attributes); 


 
/* 
if (sameBaselineUpTo(font, text, start, limit) != limit) { 
return null; 

*/ 
return font; 

 
 
public static void main(String args[]) { 
Print2DtoStream sp = new Print2DtoStream(); 

}

 

 

 

By: Boris Klug - bklug
RE: Reason why Jasper prints slowly
2003-12-08 13:47

Hey - good work! Hope this fix will be included in future versions soon.

 

 

 

By: Teodor Danciu - teodord
RE: Reason why Jasper prints slowly
2003-12-08 23:22

Hi, 
 
This seems to be indeed one of the best  
JasperReports patches ever. 
 
If everything is OK with you, Eugene, it will be part 
of the next version, to be released before the end of 
this year or in early January. 
 
This problem was around from sometime now : 
 
https://sourceforge.net/tracker/index.php?func=detail&aid=568143&group_id=36382&atid=416703 
 
Thank you for your time and effort. 
Teodor

 

 

 

By: Eugene D - eugenedruy
RE: Reason why Jasper prints slowly
2003-12-09 17:29

Hey Teodor, 
 
Go ahead with that.  
I am really happy that the patch will be in the next release. 
Yesterday I submitted a bug report to Sun. 
I wonder whether they are going to improve 
their TextLayout optimazability approach 
in the future releases of JDK

 

 

 

By: Teodor Danciu - teodord
RE: Reason why Jasper prints slowly
2003-12-09 21:11

Hi, 
 
I have added the fix to the CVS version. 
 
Thank you, 
Teodor 

 

 

By: Eugene D - eugenedruy
RE: Reason why Jasper prints slowly
2004-01-22 22:23

Just wanted to let everybody know that I got a reply to my bug report from Phil Race from Sun: 
------------------------------------------------- 
4480930 is fixed in JDK 1.5 - beta available in a couple of weeks. 
The printed output for TextLayout is better quality AND the spool 
files are much smaller. 
 
Regards, 
Phil 
------------------------------------------------- 
It lloks like for jdks earlier than 1.5 one has to have  
the workaround.  
I haven\'t had a chance to take a look at TextLayout 
implementation in 1.5, so  
I can just hope that whatever they did  
in 1.5 for TextLayout printing optimization is good enough.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...