pdf.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,058 行 · 第 1/2 页

JAVA
1,058
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.quercus.lib.pdf;import com.caucho.quercus.annotation.NotNull;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.env.BooleanValue;import com.caucho.quercus.env.Env;import com.caucho.quercus.env.TempBufferBytesValue;import com.caucho.quercus.env.Value;import com.caucho.quercus.env.StringValue;import com.caucho.util.L10N;import com.caucho.vfs.Path;import com.caucho.vfs.TempStream;import com.caucho.vfs.TempBuffer;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.logging.Logger;/** * pdf object oriented API facade */public class PDF {  private static final Logger log = Logger.getLogger(PDF.class.getName());  private static final L10N L = new L10N(PDF.class);  private static final double KAPPA = 0.5522847498;  private static final int PAGE_GROUP = 8;  private static HashMap<String,Font> _faceMap = new HashMap<String,Font>();  private HashMap<PDFFont,PDFFont> _fontMap    = new HashMap<PDFFont,PDFFont>();  private HashMap<PDFProcSet,PDFProcSet> _procSetMap    = new HashMap<PDFProcSet,PDFProcSet>();  private TempStream _tempStream;  private WriteStream _os;  private PDFWriter _out;  private ArrayList<PDFPage> _pageGroup = new ArrayList<PDFPage>();  private ArrayList<Integer> _pagesGroupList = new ArrayList<Integer>();  private int _pageCount;  private int _catalogId;  private int _pageParentId;  private PDFPage _page;  private PDFStream _stream;  public PDF(Env env)  {    _out = new PDFWriter(env.getOut());  }  public boolean begin_document(@Optional String fileName,                                @Optional String optList)    throws IOException  {    _tempStream = new TempStream();    _tempStream.openWrite();    _os = new WriteStream(_tempStream);    _out = new PDFWriter(_os);    _out.beginDocument();    _catalogId = _out.allocateId(1);    _pageParentId = _out.allocateId(1);    return true;  }  public boolean begin_page(double width, double height)    throws IOException  {    if (PAGE_GROUP <= _pageGroup.size()) {      _out.writePageGroup(_pageParentId, _pageGroup);      _pageGroup.clear();      _pagesGroupList.add(_pageParentId);      _pageParentId = _out.allocateId(1);    }    _page = new PDFPage(_out, _pageParentId, width, height);    _stream = _page.getStream();    _pageCount++;    _pageGroup.add(_page);    return true;  }  public boolean begin_page_ext(double width, double height, String opt)    throws IOException  {    return begin_page(width, height);  }  public boolean set_info(String key, String value)  {    if ("Author".equals(key)) {      _out.setAuthor(key);      return true;    }    else if ("Title".equals(key)) {      _out.setTitle(key);      return true;    }    else if ("Creator".equals(key)) {      _out.setCreator(key);      return true;    }    else      return false;  }  public boolean set_parameter(String key, String value)  {    return false;  }  public boolean set_value(String key, double value)  {    return false;  }  /**   * Returns the result as a string.   */  public Value get_buffer(Env env)  {    TempStream ts = _tempStream;    _tempStream = null;    if (ts == null)      return BooleanValue.FALSE;    StringValue result = env.createBinaryBuilder();    for (TempBuffer ptr = ts.getHead();	 ptr != null;	 ptr = ptr.getNext()) {      result.append(ptr.getBuffer(), 0, ptr.getLength());    }    ts.destroy();    return result;  }  /**   * Returns the error message.   */  public String get_errmsg()  {    return "";  }  /**   * Returns the error number.   */  public int get_errnum()  {    return 0;  }  /**   * Returns the value for a parameter.   */  public String get_parameter(String name, @Optional double modifier)  {    if ("fontname".equals(name)) {      PDFFont font = _stream.getFont();      if (font != null)        return font.getFontName();      else        return null;    }    else      return null;  }  /**   * Returns the value for a parameter.   */  public double get_value(String name, @Optional double modifier)  {    if ("ascender".equals(name)) {      PDFFont font = _stream.getFont();      if (font != null)        return font.getAscender();      else        return 0;    }    else if ("capheight".equals(name)) {      PDFFont font = _stream.getFont();      if (font != null)        return font.getCapHeight();      else        return 0;    }    else if ("descender".equals(name)) {      PDFFont font = _stream.getFont();      if (font != null)        return font.getDescender();      else        return 0;    }    else if ("fontsize".equals(name)) {      return _stream.getFontSize();    }    else      return 0;  }  public boolean initgraphics(Env env)  {    env.stub("initgraphics");    return false;  }  /**   * Loads a font for later use.   *   * @param name the font name, e.g. Helvetica   * @param encoding the font encoding, e.g. winansi   * @param opt any options   */  public PDFFont load_font(String name, String encoding, String opt)    throws IOException  {    Font face = loadFont(name);    PDFFont font = new PDFFont(face, encoding, opt);    PDFFont oldFont = _fontMap.get(font);    if (oldFont != null)      return oldFont;    font.setId(_out.allocateId(1));    _fontMap.put(font, font);    _out.addPendingObject(font);    return font;  }  private Font loadFont(String name)    throws IOException  {    synchronized (_faceMap) {      Font face = _faceMap.get(name);      if (face == null) {        face = new AfmParser().parse(name);        _faceMap.put(name, face);      }      return face;    }  }  /**   * Sets the dashing   *   * @param b black length   * @param w which length   */  public boolean setdash(double b, double w)  {    _stream.setDash(b, w);    return true;  }  /**   * Sets the dashing   */  public boolean setdashpattern(Env env, @Optional String optlist)  {    env.stub("setdashpattern");    return false;  }  /**   * Sets the flatness   */  public boolean setflat(Env env, double flatness)  {    env.stub("setflat");    return false;  }  /**   * Sets the linecap style   */  public boolean setlinecap(Env env,                            int cap)  {    env.stub("setlinecap");    return false;  }  /**   * Sets the linejoin style   */  public boolean setlinejoin(Env env,                             int linejoin)  {    env.stub("setlinejoin");    return false;  }  /**   * Sets the current font   *   * @param name the font name, e.g. Helvetica   * @param encoding the font encoding, e.g. winansi   * @param opt any options   */  public boolean setfont(@NotNull PDFFont font, double size)    throws IOException  {    if (font == null)      return false;    _stream.setFont(font, size);    _page.addResource(font.getResource());    return true;  }  /**   * Sets the matrix style   */  public boolean setmatrix(Env env,                           double a,                           double b,                           double c,                           double d,                           double e,                           double f)  {    env.stub("setmatrix");    return false;  }  /**   * Sets the miter limit   */  public boolean setmiterlimit(Env env, double v)  {    env.stub("setmiterlimit");    return false;  }  /**   * Sets the shading pattern   */  public boolean shading_pattern(Env env,                                 int shading,                                 @Optional String optlist)  {    env.stub("shading_pattern");    return false;  }  /**   * Define a blend   */  public int shading(Env env,                     String type,                     double x1,                     double y1,                     double x2,                     double y2,                     double c1,                     double c2,                     double c3,                     double c4,                     @Optional String optlist)  {    env.stub("shading");    return 0;  }  /**   * Fill with a shading object.   */  public boolean shfill(Env env,                        int shading)  {    env.stub("shfill");    return false;  }  /**   * Returns the length of a string for a font.   */  public double stringwidth(String string, @NotNull PDFFont font, double size)  {    if (font == null)      return 0;    return size * font.stringWidth(string) / 1000.0;  }  /**   * Sets the text position.   */  public boolean set_text_pos(double x, double y)  {    _stream.setTextPos(x, y);    return true;  }  /**   * Fills   */  public boolean fill()  {    _stream.fill();    return true;  }  /**   * Closes the path   */  public boolean closepath()  {    _stream.closepath();    return true;  }  /**   * Appends the current path to the clipping path.   */  public boolean clip()  {    _stream.clip();    return true;  }  /**   * Closes the path strokes   */  public boolean closepath_stroke()  {    _stream.closepathStroke();    return true;  }  /**   * Closes the path strokes   */  public boolean closepath_fill_stroke()  {    _stream.closepathFillStroke();    return true;  }  /**   * Fills   */  public boolean fill_stroke()  {    _stream.fillStroke();    return true;  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?