return buf.toString();
}
+ /**
+ * Append a double value to a string buffer suitable for PDF.
+ * In this method it is possible to set the maximum
+ * number of decimal places to output.
+ *
+ * @param doubleDown the Double value
+ * @param dec the number of decimal places to output
+ * @param buf the string buffer to which double is formatted (appended)
+ * @return the string buffer
+ */
+ public static StringBuffer doubleOut(double doubleDown, int dec, StringBuffer buf) {
+ if (dec < 0 || dec > 16) {
+ throw new IllegalArgumentException("Parameter dec must be between 1 and 16");
+ }
+ DoubleFormatUtil.formatDouble(doubleDown, dec, dec, buf);
+ return buf;
+ }
+
/** {@inheritDoc} */
protected String toPDFString() {
if (getNumber() == null) {
protected StreamCache data;
private transient Writer streamWriter;
+ private transient char[] charBuffer;
/**
* Create an empty stream object
}
}
+ /**
+ * Append data to the stream
+ *
+ * @param sb the string buffer of PDF to add
+ */
+ public void add(StringBuffer sb) {
+ try {
+ int nHave = sb.length();
+ if (charBuffer == null) {
+ charBuffer = new char [ nHave * 2 ];
+ } else {
+ int nAvail = charBuffer.length;
+ if (nAvail < nHave) {
+ int nAlloc = nAvail;
+ while (nAlloc < nHave) {
+ nAlloc *= 2;
+ }
+ charBuffer = new char [ nAlloc ];
+ }
+ }
+ sb.getChars(0, nHave, charBuffer, 0);
+ this.streamWriter.write(charBuffer, 0, nHave);
+ } catch (IOException ex) {
+ //TODO throw the exception and catch it elsewhere
+ ex.printStackTrace();
+ }
+ }
+
private void flush() throws IOException {
this.streamWriter.flush();
}
*/
protected abstract void write(String code);
+ /**
+ * Writes PDF code.
+ * @param code the PDF code to write
+ */
+ protected abstract void write(StringBuffer code);
+
private void writeAffineTransform(AffineTransform at, StringBuffer sb) {
double[] lt = new double[6];
at.getMatrix(lt);
- sb.append(PDFNumber.doubleOut(lt[0], DEC)).append(" ");
- sb.append(PDFNumber.doubleOut(lt[1], DEC)).append(" ");
- sb.append(PDFNumber.doubleOut(lt[2], DEC)).append(" ");
- sb.append(PDFNumber.doubleOut(lt[3], DEC)).append(" ");
- sb.append(PDFNumber.doubleOut(lt[4], DEC)).append(" ");
- sb.append(PDFNumber.doubleOut(lt[5], DEC));
+ PDFNumber.doubleOut(lt[0], DEC, sb);
+ sb.append(' ');
+ PDFNumber.doubleOut(lt[1], DEC, sb);
+ sb.append(' ');
+ PDFNumber.doubleOut(lt[2], DEC, sb);
+ sb.append(' ');
+ PDFNumber.doubleOut(lt[3], DEC, sb);
+ sb.append(' ');
+ PDFNumber.doubleOut(lt[4], DEC, sb);
+ sb.append(' ');
+ PDFNumber.doubleOut(lt[5], DEC, sb);
}
private static void writeChar(char ch, StringBuffer sb, boolean multibyte) {
case '(':
case ')':
case '\\':
- sb.append("\\");
+ sb.append('\\');
break;
default:
}
sb.append(ch);
}
} else {
- sb.append(PDFText.toUnicodeHex(ch));
+ PDFText.toUnicodeHex(ch, sb);
}
}
StringBuffer sb = new StringBuffer();
writeAffineTransform(at, sb);
sb.append(" cm\n");
- write(sb.toString());
+ write(sb);
}
}
*/
public void writeTf(String fontName, double fontSize) {
checkInTextObject();
- write("/" + fontName + " " + PDFNumber.doubleOut(fontSize) + " Tf\n");
-
+ StringBuffer sb = new StringBuffer();
+ sb.append('/');
+ sb.append(fontName);
+ sb.append(' ');
+ PDFNumber.doubleOut(fontSize,6,sb);
+ sb.append(" Tf\n");
+ write(sb);
this.startText = useMultiByte ? "<" : "(";
this.endText = useMultiByte ? ">" : ")";
}
StringBuffer sb = new StringBuffer();
writeAffineTransform(localTransform, sb);
sb.append(" Tm ");
- write(sb.toString());
+ write(sb);
}
/**
bufTJ = new StringBuffer();
}
if (bufTJ.length() == 0) {
- bufTJ.append("[");
+ bufTJ.append('[');
bufTJ.append(startText);
}
writeChar(codepoint, bufTJ);
bufTJ = new StringBuffer();
}
if (bufTJ.length() == 0) {
- bufTJ.append("[");
+ bufTJ.append('[');
} else {
bufTJ.append(endText);
- bufTJ.append(" ");
+ bufTJ.append(' ');
}
- bufTJ.append(PDFNumber.doubleOut(adjust, DEC - 4));
- bufTJ.append(" ");
+ PDFNumber.doubleOut(adjust, DEC - 4, bufTJ);
+ bufTJ.append(' ');
bufTJ.append(startText);
}
*/
public void writeTJ() {
if (isInString()) {
- bufTJ.append(endText).append("] TJ\n");
- write(bufTJ.toString());
+ bufTJ.append(endText);
+ bufTJ.append("] TJ\n");
+ write(bufTJ);
bufTJ.setLength(0);
}
}
*/
public void writeTd ( double x, double y ) {
StringBuffer sb = new StringBuffer();
- sb.append(PDFNumber.doubleOut(x, DEC));
+ PDFNumber.doubleOut(x, DEC, sb);
sb.append(' ');
- sb.append(PDFNumber.doubleOut(y, DEC));
- sb.append ( " Td\n" );
- write ( sb.toString() );
+ PDFNumber.doubleOut(y, DEC, sb);
+ sb.append(" Td\n");
+ write(sb);
}
/**
*/
public void writeTj ( char ch ) {
StringBuffer sb = new StringBuffer();
- sb.append ( '<' );
- writeChar ( ch, sb, true );
- sb.append ( '>' );
- sb.append ( " Tj\n" );
- write ( sb.toString() );
+ sb.append('<');
+ writeChar(ch, sb, true);
+ sb.append('>');
+ sb.append(" Tj\n");
+ write(sb);
}
}