CommodorePrinter.java
package libsidplay.components.printer;
/** MPS-801/803 control codes, using the supplied 6 by 7 Commodore character ROM. */
public final class CommodorePrinter extends RasterPrinter {
private static final int CHAR_WIDTH = 6;
private static final int CHAR_HEIGHT = 7;
private final byte[] charset;
private final PrinterModel model;
private int secondary = 7;
private boolean graphic, reverse, quoted, enhanced, bitImage, escape, binaryPosition, beginLine;
private int addressBytes, addressHigh, repeat, repetitions;
public CommodorePrinter(byte[] charset, PrinterModel model) {
super(model.getWidth());
this.charset = charset;
this.model = model;
reset();
}
@Override
public void reset() {
super.reset();
graphic = secondary != 7;
reverse = quoted = enhanced = bitImage = escape = binaryPosition = false;
beginLine = true;
addressBytes = addressHigh = repeat = repetitions = 0;
}
public void setSecondary(int secondary) {
int address = secondary & 0x0f;
if (this.secondary != address) {
this.secondary = address;
graphic = address != 7;
}
}
@Override
public void put(byte data) {
int c = data & 0xff;
if (addressBytes != 0) {
if (--addressBytes == 1) {
addressHigh = c;
} else {
int position;
if (binaryPosition) {
position = addressHigh << 8 | c;
} else {
position = addressHigh >= '0' && addressHigh <= '9' ? addressHigh - '0' : 0;
if (c >= '0' && c <= '9') {
position = position * 10 + c - '0';
}
position *= CHAR_WIDTH;
}
if (position >= width) {
newLine();
} else {
x = position;
}
}
return;
}
if (repeat != 0) {
repetitions = c == 0 ? 256 : c;
repeat = 0;
return;
}
if (bitImage && (c & 0x80) != 0) {
int count = repetitions == 0 ? 1 : repetitions;
repetitions = 0;
for (int i = 0; i < count; i++) {
if (x >= width) {
newLine();
}
for (int row = 0; row < CHAR_HEIGHT; row++) {
if ((c & 1 << row) != 0) {
plot(x, row);
}
}
x++;
pending = true;
}
return;
}
if (beginLine) {
graphic = secondary != 7;
beginLine = false;
}
boolean escaped = escape;
escape = false;
if (c == 13) {
newLine();
reverse = quoted = false;
beginLine = true;
return;
}
if (!quoted || bitImage) {
switch (c) {
case 8:
bitImage = true;
return;
case 10:
newLine();
return;
case 14:
case 15:
enhanced = c == 14;
bitImage = false;
return;
case 16:
addressBytes = 2;
binaryPosition = escaped;
return;
case 17:
case 145:
graphic = c == 145;
return;
case 18:
case 146:
reverse = c == 18;
return;
case 26:
repeat = 1;
return;
case 27:
escape = true;
return;
case 141:
if (model == PrinterModel.MPS803) {
x = 0;
reverse = quoted = false;
graphic = secondary != 7;
}
return;
default:
break;
}
}
if (bitImage) {
return;
}
if (c == '"') {
quoted = !quoted;
}
boolean control = c < 32 || c >= 128 && c < 160;
if (control && !quoted) {
return;
}
if (control) {
c = c < 32 ? c + 64 : c - 32;
}
int scale = enhanced ? 2 : 1;
if (x + CHAR_WIDTH * scale > width) {
newLine();
}
// The ROM contains uppercase/graphics first, followed by business mode.
int glyph = (graphic ? 0 : 256) + c;
for (int row = 0; row < CHAR_HEIGHT; row++) {
for (int column = 0; column < CHAR_WIDTH; column++) {
boolean pixel = (charset[glyph * CHAR_HEIGHT + row] & 0x80 >> column) != 0;
if (pixel ^ (control || reverse)) {
for (int copy = 0; copy < scale; copy++) {
plot(x + column * scale + copy, row);
}
}
}
}
x += CHAR_WIDTH * scale;
pending = true;
}
private void newLine() {
feed(bitImage ? CHAR_HEIGHT : 10);
x = 0;
}
@Override
public void flush() {
flush(bitImage ? CHAR_HEIGHT : 10);
x = 0;
}
}