Hi,
I tried to find out how to submit a bug / patch for XML Graphics commons
without checking out and commiting the source myself but I could not
figure out how.
So here is a contribution by email. While using the PSGenerator
(org.apache.xmlgraphics.ps.PSGenerator) I found that the current
implementation of PSGenerator.escapeChar() is not working properly. Some
of the issues: the escaped chars are still Java chars instead of bytes,
when writing a char as an octal code it should have 3 digits, chars
below 32 are not escaped.
Below you find another method which does about the same task, it is not
a drop-in replacement but it solves most problems. There is a call in
this method to EPSGraphics2D.encode(), this one is intended to provide
translations for Unicode code points above 255 if they are supported by
the encoding vector of the font.
I hope this will help to improve the XML Graphics commons.
Kind regards,
Manuel Polling.
The code:
private static final char CHAR_BACKSPACE = '\b';
private static final char CHAR_CARRIAGE_RETURN = '\r';
private static final char CHAR_END_OF_LINE = '\n';
private static final char CHAR_ESCAPE = '\\';
private static final char CHAR_FORM_FEED = '\f';
private static final char CHAR_LEFT_PARENTHESIS = '(';
private static final char CHAR_RIGHT_PARENTHESIS = ')';
private static final char CHAR_TAB = '\t';
private static final char CHAR_SUBSTITUTE = '?';
/**
* TODO make this work for non iso-latin1 chars not present in our
own encoding (lookup chars in font),
* see org.apache.fop.render.ps.NativeTextHandler.
* @param text
* @param target
*/
private byte[] escapeText(final String text) {
final ByteArrayOutputStream asciiBuffer = new ByteArrayOutputStream(
text.length() * 2);
final int textLength = text.length();
for (int i = 0; i < textLength; i++) {
final char code = EPSGraphics2D.encode(text.charAt(i));
switch (code) {
case CHAR_LEFT_PARENTHESIS:
case CHAR_RIGHT_PARENTHESIS:
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write(code);
break;
case CHAR_CARRIAGE_RETURN:
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write('r');
break;
case CHAR_BACKSPACE:
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write('b');
break;
case CHAR_FORM_FEED:
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write('f');
break;
case CHAR_TAB:
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write('t');
break;
case CHAR_END_OF_LINE:
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write('n');
break;
case CHAR_ESCAPE:
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write(CHAR_ESCAPE);
break;
default:
if (code > 255) {
asciiBuffer.write(CHAR_SUBSTITUTE);
logger.warning(MessageFormat.format(
"unable to write codepoint ''{0}'' (0x{1}),
substituting ''{2}''.",
code, Integer.toString(code, 16), CHAR_SUBSTITUTE));
}
else if (code < 32 || code > 127) {
asciiBuffer.write(CHAR_ESCAPE);
asciiBuffer.write('0' + code / 64);
asciiBuffer.write('0' + (code / 8) % 8);
asciiBuffer.write('0' + code % 8);
}
else
asciiBuffer.write(code);
break;
}
}
return asciiBuffer.toByteArray();
}
--
drs. Manuel Polling
Consultant/Software Engineer
Edmond Research & Development B.V.
/The Document Solutions Specialist/
http://www.edmond.nl/