Java Reference
In-Depth Information
Example 12−3: HardcopyWriter.java (continued)
x0 = (int)(leftmargin * pagedpi);
y0 = (int)(topmargin * pagedpi);
width = pagesize.width - (int)((leftmargin + rightmargin) * pagedpi);
height = pagesize.height - (int)((topmargin + bottommargin) * pagedpi);
// Get body font and font size
font = new Font("Monospaced", Font.PLAIN, fontsize);
metrics = frame.getFontMetrics(font);
lineheight = metrics.getHeight();
lineascent = metrics.getAscent();
charwidth = metrics.charWidth('0'); // Assumes a monospaced font!
// Now compute columns and lines will fit inside the margins
chars_per_line = width / charwidth;
lines_per_page = height / lineheight;
// Get header font information
// And compute baseline of page header: 1/8" above the top margin
headerfont = new Font("SansSerif", Font.ITALIC, fontsize);
headermetrics = frame.getFontMetrics(headerfont);
headery = y0 - (int)(0.125 * pagedpi) -
headermetrics.getHeight() + headermetrics.getAscent();
// Compute the date/time string to display in the page header
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.SHORT);
df.setTimeZone(TimeZone.getDefault());
time = df.format(new Date());
this.jobname = jobname;
// save name
this.fontsize = fontsize;
// save font size
}
/**
* This is the write() method of the stream. All Writer subclasses
* implement this. All other versions of write() are variants of this one
**/
public void write(char[] buffer, int index, int len) {
synchronized(this.lock) { // For thread safety
// Loop through all the characters passed to us
for(int i = index; i < index + len; i++) {
// If we haven't begun a page (or a new page), do that now.
if (page == null) newpage();
// If the character is a line terminator, then begin new line,
// unless it is a \n immediately after a \r.
if (buffer[i] == '\n') {
if (!last_char_was_return) newline();
continue;
}
if (buffer[i] == '\r') {
newline();
last_char_was_return = true;
continue;
}
else last_char_was_return = false;
// If it some other non-printing character, ignore it.
Search WWH ::




Custom Search