RichEdit control classes - printing support.

X-SVN-Rev: 1184
This commit is contained in:
Alan Liu 2000-04-20 17:44:33 +00:00
parent 5a7f23ea3d
commit 7dd3e16ec6
4 changed files with 299 additions and 0 deletions

View file

@ -0,0 +1,162 @@
/*
* @(#)$RCSfile: MConstTextPrintable.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:44:33 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.print;
import com.ibm.richtext.styledtext.MConstText;
import com.ibm.richtext.textformat.MFormatter;
import com.ibm.textlayout.attributes.AttributeMap;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Vector;
/**
* This class's interface is very close to that of the JDK 1.2 Printable
* interface, but can execute on JDK 1.1. On 1.2, this class is wrapped
* in a real Printable. On 1.1, the PrintContext class uses this class
* and a PrintJob for printing.
*
* Note that this class paginates the text in the first call to print,
* or to getPageCount.
* After construction, its page size is essentially fixed. This is not
* as flexible as the 1.2 classes allow, but it should suffice.
*/
final class MConstTextPrintable {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
static final int PAGE_EXISTS = 0;
static final int NO_SUCH_PAGE = 1;
private MConstText fText;
private AttributeMap fDefaultStyles;
private Rectangle fPageRect;
// If these two fields are null the text has not been paginated.
private MFormatter fFormatter;
private Vector fPageStarts;
/**
* Construct an MConstTextPrintable to print the given text. Each page will fit
* into pageRect.
*/
MConstTextPrintable(MConstText text,
AttributeMap defaultStyles,
Rectangle pageRect) {
fText = text;
fDefaultStyles = defaultStyles;
fPageRect = new Rectangle(pageRect);
}
private static boolean emptyParagraphAtEndOfText(MConstText text) {
if (text.length() > 0) {
char ch = text.at(text.length()-1);
return ch == '\n' || ch == '\u2029';
}
else {
return false;
}
}
private void paginate(Graphics graphics) {
if (fPageStarts == null) {
fFormatter = MFormatter.createFormatter(fText,
fDefaultStyles,
fPageRect.width,
true,
graphics);
fFormatter.formatToHeight(Integer.MAX_VALUE);
fFormatter.stopBackgroundFormatting();
fPageStarts = new Vector();
int lineCount = fFormatter.getLineCount();
if (emptyParagraphAtEndOfText(fText)) {
lineCount -= 1;
}
int startLine = 0;
fPageStarts.addElement(new Integer(startLine));
int startHeight = 0;
final int pageHeight = fPageRect.height;
while (startLine < lineCount) {
int nextStart = fFormatter.lineAtHeight(startHeight + pageHeight);
fPageStarts.addElement(new Integer(nextStart));
startHeight = fFormatter.lineGraphicStart(nextStart);
startLine = nextStart;
}
}
}
/**
* Print the given page in the given graphics. Page numbers are
* 0-based. The the return value indicates whether
* the page number is valid (as in JDK 1.2). Since you can get the page count
* directly, there's really no excuse for passing in an invalid page
* index.
* @param graphics the Graphics to print to
* @param pageNumber the 0-based page number. Should be nonnegative and
* less than getPageCount()
* @return PAGE_EXISTS if the page number is valid, or
* NO_SUCH_PAGE otherwise
*/
int print(Graphics graphics, int pageNumber) {
paginate(graphics);
if (pageNumber < getPageCount(graphics) && pageNumber >= 0) {
graphics.setColor(Color.black); // workaround for 1.2 printing bug
int startLine = ((Integer)fPageStarts.elementAt(pageNumber)).intValue();
int limitLine = ((Integer)fPageStarts.elementAt(pageNumber+1)).intValue();
int topOfPage = fFormatter.lineGraphicStart(startLine);
int pageHeight = fFormatter.lineGraphicStart(limitLine) - topOfPage;
Point origin = new Point(fPageRect.x, fPageRect.y - topOfPage);
Rectangle drawRect = new Rectangle(fPageRect);
drawRect.height = pageHeight;
fFormatter.draw(graphics, drawRect, origin);
return PAGE_EXISTS;
}
else {
return NO_SUCH_PAGE;
}
}
/**
* Return the number of pages that can be printed.
* @param graphics a Graphics instance representative of those
* which will be printed into
*/
int getPageCount(Graphics graphics) {
paginate(graphics);
return fPageStarts.size() - 1;
}
}

View file

@ -0,0 +1,81 @@
/*
* @(#)$RCSfile: PrintContext.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:44:33 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
// Requires Java2
package com.ibm.richtext.print;
import com.ibm.richtext.styledtext.MConstText;
import com.ibm.textlayout.attributes.AttributeMap;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PrinterException;
final class PrintContext implements Printable {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
private MConstTextPrintable fPrintable;
PrintContext(MConstText text, AttributeMap defaultStyles, PageFormat pf) {
int width = (int) Math.round(pf.getImageableWidth());
int height = (int) Math.round(pf.getImageableHeight());
int left = (((int)Math.round(pf.getWidth())) - width) / 2;
int top = (((int)Math.round(pf.getHeight())) - height) / 2;
Rectangle pageRect = new Rectangle(left, top, width, height);
fPrintable = new MConstTextPrintable(text, defaultStyles, pageRect);
}
public int print(Graphics graphics,
PageFormat format,
int pageIndex) throws PrinterException {
if (false)
throw new PrinterException("save trees");
if (fPrintable.print(graphics, pageIndex) == fPrintable.PAGE_EXISTS) {
return PAGE_EXISTS;
}
else {
return NO_SUCH_PAGE;
}
}
static void userPrintText(MConstText text,
AttributeMap defaultStyles,
Frame frame,
String jobTitle) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName(jobTitle);
if (job.printDialog()) {
job.setPrintable(new PrintContext(text, defaultStyles, job.defaultPage()));
try {
job.print();
}
catch(PrinterException e) {
System.out.println("Printer exception: " + e);
}
}
}
}

View file

@ -0,0 +1,51 @@
/*
* @(#)$RCSfile: PrintingUtils.java,v $ $Revision: 1.1 $ $Date: 2000/04/20 17:44:33 $
*
* (C) Copyright IBM Corp. 1998-1999. All Rights Reserved.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
package com.ibm.richtext.print;
import com.ibm.richtext.styledtext.MConstText;
import com.ibm.textlayout.attributes.AttributeMap;
import java.awt.Frame;
/**
* PrintingUtils contains a static method for printing styled text.
* @see com.ibm.richtext.styledtext.MConstText
*/
public final class PrintingUtils {
static final String COPYRIGHT =
"(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
// Keep it out of Javadoc...
private PrintingUtils() {
}
/**
* Print the given text. A Print dialog is presented to the user;
* unless the user cancels, the text is printed.
* @param text the text to print
* @param defaultStyles default values for unspecified attributes
* @param frame the parent of the Print dialog
* @param jobTitle the title of the PrintJob
*/
public static void userPrintText(MConstText text,
AttributeMap defaultStyles,
Frame frame,
String jobTitle) {
PrintContext.userPrintText(text, defaultStyles, frame, jobTitle);
}
}

View file

@ -0,0 +1,5 @@
<html>
<body bgcolor="white">
Provides printing for styled text.
</body>
</html>