001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.print.text;
010:
011: import java.awt.Color;
012: import java.awt.Font;
013: import java.awt.FontMetrics;
014: import java.awt.Graphics;
015: import java.awt.print.*;
016: import org.acm.seguin.print.PagePrinter;
017: import org.acm.seguin.print.PrintingSettings;
018:
019: /**
020: * Handles printing the page
021: *
022: *@author Chris Seguin
023: *@created August 6, 1999
024: */
025: public class TextPagePrinter extends PagePrinter {
026: private String filename;
027: private LineSet lineSet;
028: private LinePrinter linePrinter;
029: private int textFontSize = 10;
030: private int textSkip = 2;
031:
032: private static int linesPerPage = -1;
033:
034: /**
035: * Constructor for the UMLPagePrinter object
036: *
037: *@param initFilename Description of Parameter
038: *@param init Description of Parameter
039: *@param printer Description of Parameter
040: */
041: public TextPagePrinter(String initFilename, String init,
042: LinePrinter printer) {
043: lineSet = new LineSet(init);
044: linePrinter = printer;
045: filename = initFilename;
046: }
047:
048: /**
049: * Sets the TextFontSize attribute of the TextPagePrinter object
050: *
051: *@param value The new TextFontSize value
052: */
053: public void setTextFontSize(int value) {
054: textFontSize = value;
055: }
056:
057: /**
058: * Sets the BetweenLineSpacing attribute of the TextPagePrinter object
059: *
060: *@param value The new BetweenLineSpacing value
061: */
062: public void setBetweenLineSpacing(int value) {
063: textSkip = value;
064: }
065:
066: /**
067: * Guess the number of pages
068: *
069: *@param pf Description of Parameter
070: *@return Description of the Returned Value
071: */
072: public int calculatePageCount(PageFormat pf) {
073: int pageHeight = (int) pf.getImageableHeight();
074: int pageWidth = (int) pf.getImageableWidth();
075:
076: int pagesHigh;
077: int lpp = linesPerPage;
078: int lineCount = lineSet.size();
079: if (linesPerPage == -1) {
080: PrintingSettings ps = new PrintingSettings();
081: lpp = ps.getLinesPerPage();
082: }
083:
084: pagesHigh = lineCount / lpp;
085: if (lineCount % lpp != 0) {
086: pagesHigh++;
087: }
088:
089: return pagesHigh;
090: }
091:
092: /**
093: * Print the page
094: *
095: *@param g the graphics object
096: *@param pf the page format
097: *@param pageNumber the page number
098: *@return Whether there is more pages or not
099: */
100: public int print(Graphics g, PageFormat pf, int pageNumber) {
101: int pageCount = calculatePageCount(pf);
102: if (pageNumber > pageCount) {
103: return Printable.NO_SUCH_PAGE;
104: }
105:
106: linePrinter.setFontSize(textFontSize);
107: int high = linePrinter.getLineHeight(g) + textSkip;
108: if (linesPerPage == -1) {
109: int pageHeight = (int) pf.getImageableHeight()
110: - headerHeight;
111: linesPerPage = pageHeight / high;
112: PrintingSettings ps = new PrintingSettings();
113: ps.setLinesPerPage(linesPerPage);
114: }
115:
116: int startIndex = pageNumber * linesPerPage;
117:
118: int xOffset = (int) pf.getImageableX();
119: int yOffset = (int) pf.getImageableY() + headerHeight;
120:
121: printHeader(g, filename, "" + (1 + pageNumber), "" + pageCount);
122:
123: linePrinter.init(g);
124: for (int ndx = 0; ndx < linesPerPage; ndx++) {
125: int index = ndx + pageNumber * linesPerPage;
126: String line = lineSet.getLine(index);
127: if (line == null) {
128: break;
129: }
130:
131: linePrinter.print(g, line, xOffset, yOffset + (1 + ndx)
132: * high, lineSet, index);
133: }
134:
135: return Printable.PAGE_EXISTS;
136: }
137: }
|