示例#1
0
 private void putBits(int bits, int length) {
   while (length > bit) {
     data |= bits >> (length - bit);
     length -= bit;
     outBuf.append((byte) data);
     data = 0;
     bit = 8;
   }
   data |= (bits & msbmask[length]) << (bit - length);
   bit -= length;
   if (bit == 0) {
     outBuf.append((byte) data);
     data = 0;
     bit = 8;
   }
 }
示例#2
0
 private void Fax4PostEncode() {
   putBits(EOL, 12);
   putBits(EOL, 12);
   if (bit != 8) {
     outBuf.append((byte) data);
     data = 0;
     bit = 8;
   }
 }
示例#3
0
 /**
  * Closes the encoder and returns the encoded data.
  *
  * @return the encoded data
  */
 public byte[] close() {
   Fax4PostEncode();
   return outBuf.toByteArray();
 }
示例#4
0
  /**
   * Outputs a <CODE>double</CODE> into a format suitable for the PDF.
   *
   * @param d a double
   * @param buf a ByteBuffer
   * @return 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>.
   */
  public static String formatDouble(double d, ByteBuffer buf) {
    if (HIGH_PRECISION) {
      DecimalFormat dn = new DecimalFormat("0.######", dfs);
      String sform = dn.format(d);
      if (buf == null) return sform;
      else {
        buf.append(sform);
        return null;
      }
    }
    boolean 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);

        StringBuffer res = new StringBuffer();
        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.charAt(cut) == '0') {
          --cut;
        }
        res.setLength(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 {
        StringBuffer res = new StringBuffer();
        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 {
      StringBuffer res = new StringBuffer();
      if (negative) res.append('-');
      d += 0.5;
      long v = (long) d;
      return res.append(v).toString();
    }
  }
示例#5
0
 void applyRotation(PdfDictionary pageN, ByteBuffer out) {
   if (!cstp.rotateContents) return;
   Rectangle page = reader.getPageSizeWithRotation(pageN);
   int rotation = page.getRotation();
   switch (rotation) {
     case 90:
       out.append(PdfContents.ROTATE90);
       out.append(page.getTop());
       out.append(' ').append('0').append(PdfContents.ROTATEFINAL);
       break;
     case 180:
       out.append(PdfContents.ROTATE180);
       out.append(page.getRight());
       out.append(' ');
       out.append(page.getTop());
       out.append(PdfContents.ROTATEFINAL);
       break;
     case 270:
       out.append(PdfContents.ROTATE270);
       out.append('0').append(' ');
       out.append(page.getRight());
       out.append(PdfContents.ROTATEFINAL);
       break;
   }
 }
示例#6
0
 public void alterContents() throws IOException {
   if (over == null && under == null) return;
   PdfArray ar = null;
   PdfObject content = PdfReader.getPdfObject(pageN.get(PdfName.CONTENTS), pageN);
   if (content == null) {
     ar = new PdfArray();
     pageN.put(PdfName.CONTENTS, ar);
   } else if (content.isArray()) {
     ar = (PdfArray) content;
   } else if (content.isStream()) {
     ar = new PdfArray();
     ar.add(pageN.get(PdfName.CONTENTS));
     pageN.put(PdfName.CONTENTS, ar);
   } else {
     ar = new PdfArray();
     pageN.put(PdfName.CONTENTS, ar);
   }
   ByteBuffer out = new ByteBuffer();
   if (under != null) {
     out.append(PdfContents.SAVESTATE);
     applyRotation(pageN, out);
     out.append(under.getInternalBuffer());
     out.append(PdfContents.RESTORESTATE);
   }
   if (over != null) out.append(PdfContents.SAVESTATE);
   PdfStream stream = new PdfStream(out.toByteArray());
   stream.flateCompress(cstp.getCompressionLevel());
   PdfIndirectReference ref1 = cstp.addToBody(stream).getIndirectReference();
   ar.addFirst(ref1);
   out.reset();
   if (over != null) {
     out.append(' ');
     out.append(PdfContents.RESTORESTATE);
     out.append(PdfContents.SAVESTATE);
     applyRotation(pageN, out);
     out.append(over.getInternalBuffer());
     out.append(PdfContents.RESTORESTATE);
     stream = new PdfStream(out.toByteArray());
     stream.flateCompress(cstp.getCompressionLevel());
     ar.add(cstp.addToBody(stream).getIndirectReference());
   }
   pageN.put(PdfName.RESOURCES, pageResources.getResources());
 }