Example #1
0
  /**
   * create a new TrueTypeFont object based on a description of the font from the PDF file. If the
   * description happens to contain an in-line true-type font file (under key "FontFile2"), use the
   * true type font. Otherwise, parse the description for key information and use that to generate
   * an appropriate font.
   */
  public TTFFont(String baseFont, PDFObject fontObj, PDFFontDescriptor descriptor)
      throws IOException {
    super(baseFont, fontObj, descriptor);

    String fontName = descriptor.getFontName();
    PDFObject ttfObj = descriptor.getFontFile2();

    // try {
    //    byte[] fontData = ttfObj.getStream();
    //    java.io.FileOutputStream fis = new java.io.FileOutputStream("/tmp/" + fontName + ".ttf");
    //    fis.write(fontData);
    //    fis.flush();
    //    fis.close();
    // } catch (Exception ex) {
    //    ex.printStackTrace();
    // }
    if (ttfObj != null) {
      this.font = TrueTypeFont.parseFont(ttfObj.getStreamBuffer());
      // read the units per em from the head table
      HeadTable head = (HeadTable) this.font.getTable("head");
      this.unitsPerEm = head.getUnitsPerEm();
    } else {
      this.font = null;
    }
    //        System.out.println ("TTFFont: ttfObj: " + ttfObj + ", fontName: " + fontName);

  }
Example #2
0
  /**
   * create a new NativeFont object based on a description of the font from the PDF file. If the
   * description happens to contain an in-line true-type font file (under key "FontFile2"), use the
   * true type font. Otherwise, parse the description for key information and use that to generate
   * an appropriate font.
   */
  public NativeFont(String baseFont, PDFObject fontObj, PDFFontDescriptor descriptor)
      throws IOException {
    super(baseFont, fontObj, descriptor);

    String fontName = descriptor.getFontName();

    PDFObject ttf = descriptor.getFontFile2();
    if (ttf != null) {
      byte[] fontdata = ttf.getStream();

      try {
        setFont(fontdata);
      } catch (FontFormatException ffe) {
        throw new PDFParseException("Font format exception: " + ffe);
      }
    } else {
      int flags = descriptor.getFlags();
      int style = ((flags & PDFFontDescriptor.FORCEBOLD) != 0) ? Font.BOLD : Font.PLAIN;

      if (fontName.indexOf("Bold") > 0) {
        style |= Font.BOLD;
      }
      if (descriptor.getItalicAngle() != 0) {
        style |= Font.ITALIC;
      }
      if ((flags & PDFFontDescriptor.FIXED_PITCH) != 0) { // fixed width
        setFont(new Font("Monospaced", style, 1));
      } else if ((flags & PDFFontDescriptor.SERIF) != 0) { // serif font
        setFont(new Font("Serif", style, 1));
      } else {
        setFont(new Font("Sans-serif", style, 1));
      }
    }
  }
  /**
   * create a new Type1CFont based on a font data stream and a descriptor
   *
   * @param baseFont the postscript name of this font
   * @param src a stream containing the font
   * @param descriptor the descriptor for this font
   */
  public Type1CFont(String baseFont, PDFObject src, PDFFontDescriptor descriptor)
      throws IOException {
    super(baseFont, src, descriptor);

    if (!PDFFont.sUseFontSubstitution) {
      PDFObject dataObj = descriptor.getFontFile3();
      data = dataObj.getStream();
    }
    pos = 0;
    if (!PDFFont.sUseFontSubstitution) {
      parse();
    }

    // TODO: free up (set to null) unused structures (data, subrs, stack)
  }
Example #4
0
  /**
   * create a new BuiltingFont object based on a description of the font from the PDF file. Parse
   * the description for key information and use that to generate an appropriate font.
   */
  public BuiltinFont(String baseFont, PDFObject fontObj, PDFFontDescriptor descriptor)
      throws IOException {
    super(baseFont, fontObj, descriptor);

    String fontName = descriptor.getFontName();

    // check if it's one of the 14 base fonts
    for (int i = 0; i < baseFonts.length; i++) {
      if (fontName.equalsIgnoreCase(baseFonts[i])) {
        parseFont(fontName);
        return;
      }
    }

    // check if it's a mapped font
    for (int i = 0; i < mappedFonts.length; i += 2) {
      if (fontName.equalsIgnoreCase(mappedFonts[i])) {
        parseFont(mappedFonts[i + 1]);
        return;
      }
    }

    int flags = descriptor.getFlags();
    int style = ((flags & PDFFontDescriptor.FORCEBOLD) != 0) ? Font.BOLD : Font.PLAIN;

    if (fontName.indexOf("Bold") > 0) {
      style |= Font.BOLD;
    }
    if ((descriptor.getItalicAngle() != 0)
        || ((flags & (PDFFontDescriptor.SCRIPT | PDFFontDescriptor.ITALIC)) != 0)) {
      style |= Font.ITALIC;
    }
    String name = null;

    if ((flags & PDFFontDescriptor.FIXED_PITCH) != 0) { // fixed width
      if (((style & Font.BOLD) > 0) && ((style & Font.ITALIC) > 0)) {
        name = "Courier-BoldOblique";
      } else if ((style & Font.BOLD) > 0) {
        name = "Courier-Bold";
      } else if ((style & Font.ITALIC) > 0) {
        name = "Courier-Oblique";
      } else {
        name = "Courier";
      }
    } else if ((flags & PDFFontDescriptor.SERIF) != 0) { // serif font
      if (((style & Font.BOLD) > 0) && ((style & Font.ITALIC) > 0)) {
        name = "Times-BoldItalic";
      } else if ((style & Font.BOLD) > 0) {
        name = "Times-Bold";
      } else if ((style & Font.ITALIC) > 0) {
        name = "Times-Italic";
      } else {
        name = "Times-Roman";
      }
    } else {
      if (((style & Font.BOLD) > 0) && ((style & Font.ITALIC) > 0)) {
        name = "Helvetica-BoldOblique";
      } else if ((style & Font.BOLD) > 0) {
        name = "Helvetica-Bold";
      } else if ((style & Font.ITALIC) > 0) {
        name = "Helvetica-Oblique";
      } else {
        name = "Helvetica";
      }
    }

    parseFont(name);
  }