/// <summary>Outputs a <CODE>double</CODE> into a format suitable for the PDF.</summary> /// <param name="d">a double</param> /// <param name="buf">a ByteBufferOutputStream</param> /// <returns> /// the <CODE>String</CODE> representation of the <CODE>double</CODE> if /// <CODE>buf</CODE> is <CODE>null</CODE>. If <CODE>buf</CODE> is <B>not</B> <CODE>null</CODE>, /// then the double is appended directly to the buffer and this methods returns <CODE>null</CODE>. /// </returns> public static String FormatDouble(double d, iText.Kernel.Pdf.ByteBufferOutputStream buf) { if (HIGH_PRECISION) { String sform = DecimalFormatUtil.FormatNumber(d, "0.######"); if (buf == null) { return(sform); } else { buf.Append(sform); return(null); } } bool negative = false; if (Math.Abs(d) < 0.000015) { if (buf != null) { buf.Append(ZERO); return(null); } else { return("0"); } } if (d < 0) { negative = true; d = -d; } if (d < 1.0) { d += 0.000005; if (d >= 1) { if (negative) { if (buf != null) { buf.Append((byte)'-'); buf.Append((byte)'1'); return(null); } else { return("-1"); } } else { if (buf != null) { buf.Append((byte)'1'); return(null); } else { return("1"); } } } if (buf != null) { int v = (int)(d * 100000); if (negative) { buf.Append((byte)'-'); } buf.Append((byte)'0'); buf.Append((byte)'.'); buf.Append((byte)(v / 10000 + ZERO)); if (v % 10000 != 0) { buf.Append((byte)((v / 1000) % 10 + ZERO)); if (v % 1000 != 0) { buf.Append((byte)((v / 100) % 10 + ZERO)); if (v % 100 != 0) { buf.Append((byte)((v / 10) % 10 + ZERO)); if (v % 10 != 0) { buf.Append((byte)((v) % 10 + ZERO)); } } } } return(null); } else { int x = 100000; int v = (int)(d * x); StringBuilder res = new StringBuilder(); if (negative) { res.Append('-'); } res.Append("0."); while (v < x / 10) { res.Append('0'); x /= 10; } res.Append(v); int cut = res.Length - 1; while (res[cut] == '0') { --cut; } res.Length = cut + 1; return(res.ToString()); } } else { if (d <= 32767) { d += 0.005; int v = (int)(d * 100); if (v < byteCacheSize && byteCache[v] != null) { if (buf != null) { if (negative) { buf.Append((byte)'-'); } buf.Append(byteCache[v]); return(null); } else { String tmp = PdfEncodings.ConvertToString(byteCache[v], null); if (negative) { tmp = "-" + tmp; } return(tmp); } } if (buf != null) { if (v < byteCacheSize) { //create the cachebyte[] byte[] cache; int size = 0; if (v >= 1000000) { //the original number is >=10000, we need 5 more bytes size += 5; } else { if (v >= 100000) { //the original number is >=1000, we need 4 more bytes size += 4; } else { if (v >= 10000) { //the original number is >=100, we need 3 more bytes size += 3; } else { if (v >= 1000) { //the original number is >=10, we need 2 more bytes size += 2; } else { if (v >= 100) { //the original number is >=1, we need 1 more bytes size += 1; } } } } } //now we must check if we have a decimal number if (v % 100 != 0) { //yes, do not forget the "." size += 2; } if (v % 10 != 0) { size++; } cache = new byte[size]; int add = 0; if (v >= 1000000) { cache[add++] = bytes[(v / 1000000)]; } if (v >= 100000) { cache[add++] = bytes[(v / 100000) % 10]; } if (v >= 10000) { cache[add++] = bytes[(v / 10000) % 10]; } if (v >= 1000) { cache[add++] = bytes[(v / 1000) % 10]; } if (v >= 100) { cache[add++] = bytes[(v / 100) % 10]; } if (v % 100 != 0) { cache[add++] = (byte)'.'; cache[add++] = bytes[(v / 10) % 10]; if (v % 10 != 0) { cache[add++] = bytes[v % 10]; } } byteCache[v] = cache; } if (negative) { buf.Append((byte)'-'); } if (v >= 1000000) { buf.Append(bytes[(v / 1000000)]); } if (v >= 100000) { buf.Append(bytes[(v / 100000) % 10]); } if (v >= 10000) { buf.Append(bytes[(v / 10000) % 10]); } if (v >= 1000) { buf.Append(bytes[(v / 1000) % 10]); } if (v >= 100) { buf.Append(bytes[(v / 100) % 10]); } if (v % 100 != 0) { buf.Append((byte)'.'); buf.Append(bytes[(v / 10) % 10]); if (v % 10 != 0) { buf.Append(bytes[v % 10]); } } return(null); } else { StringBuilder res = new StringBuilder(); if (negative) { res.Append('-'); } if (v >= 1000000) { res.Append(chars[(v / 1000000)]); } if (v >= 100000) { res.Append(chars[(v / 100000) % 10]); } if (v >= 10000) { res.Append(chars[(v / 10000) % 10]); } if (v >= 1000) { res.Append(chars[(v / 1000) % 10]); } if (v >= 100) { res.Append(chars[(v / 100) % 10]); } if (v % 100 != 0) { res.Append('.'); res.Append(chars[(v / 10) % 10]); if (v % 10 != 0) { res.Append(chars[v % 10]); } } return(res.ToString()); } } else { d += 0.5; long v = (long)d; if (negative) { return("-" + System.Convert.ToString(v)); } else { return(System.Convert.ToString(v)); } } } }
/// <summary>Appends another <CODE>ByteBufferOutputStream</CODE> to this buffer.</summary> /// <param name="buf">the <CODE>ByteBufferOutputStream</CODE> to be appended</param> /// <returns>a reference to this <CODE>ByteBufferOutputStream</CODE> object</returns> public virtual iText.Kernel.Pdf.ByteBufferOutputStream Append(iText.Kernel.Pdf.ByteBufferOutputStream buf) { return(Append(buf.buf, 0, buf.count)); }