type1font.java

来自「有关对pdf操作的代码」· Java 代码 · 共 826 行 · 第 1/3 页

JAVA
826
字号
/* * $Id: Type1Font.java 3117 2008-01-31 05:53:22Z xlv $ * $Name$ * * Copyright 2001-2006 Paulo Soares * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library 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. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.pdf;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.StringTokenizer;import com.lowagie.text.DocumentException;import com.lowagie.text.pdf.fonts.FontsResourceAnchor;/** Reads a Type1 font * * @author Paulo Soares (psoares@consiste.pt) */class Type1Font extends BaseFont{    private static FontsResourceAnchor resourceAnchor;        /** The PFB file if the input was made with a <CODE>byte</CODE> array.     */        protected byte pfb[];/** The Postscript font name. */    private String FontName;/** The full name of the font. */    private String FullName;/** The family name of the font. */    private String FamilyName;/** The weight of the font: normal, bold, etc. */    private String Weight = "";/** The italic angle of the font, usually 0.0 or negative. */    private float ItalicAngle = 0.0f;/** <CODE>true</CODE> if all the characters have the same *  width. */    private boolean IsFixedPitch = false;/** The character set of the font. */    private String CharacterSet;/** The llx of the FontBox. */    private int llx = -50;/** The lly of the FontBox. */    private int lly = -200;/** The lurx of the FontBox. */    private int urx = 1000;/** The ury of the FontBox. */    private int ury = 900;/** The underline position. */    private int UnderlinePosition = -100;/** The underline thickness. */    private int UnderlineThickness = 50;/** The font's encoding name. This encoding is 'StandardEncoding' or *  'AdobeStandardEncoding' for a font that can be totally encoded *  according to the characters names. For all other names the *  font is treated as symbolic. */    private String EncodingScheme = "FontSpecific";/** A variable. */    private int CapHeight = 700;/** A variable. */    private int XHeight = 480;/** A variable. */    private int Ascender = 800;/** A variable. */    private int Descender = -200;/** A variable. */    private int StdHW;/** A variable. */    private int StdVW = 80;    /** Represents the section CharMetrics in the AFM file. Each *  value of this array contains a <CODE>Object[4]</CODE> with an *  Integer, Integer, String and int[]. This is the code, width, name and char bbox. *  The key is the name of the char and also an Integer with the char number. */    private HashMap CharMetrics = new HashMap();/** Represents the section KernPairs in the AFM file. The key is *  the name of the first character and the value is a <CODE>Object[]</CODE> *  with 2 elements for each kern pair. Position 0 is the name of *  the second character and position 1 is the kerning distance. This is *  repeated for all the pairs. */    private HashMap KernPairs = new HashMap();/** The file in use. */    private String fileName;/** <CODE>true</CODE> if this font is one of the 14 built in fonts. */    private boolean builtinFont = false;/** Types of records in a PFB file. ASCII is 1 and BINARY is 2. *  They have to appear in the PFB file in this sequence. */    private static final int PFB_TYPES[] = {1, 2, 1};        /** Creates a new Type1 font.     * @param ttfAfm the AFM file if the input is made with a <CODE>byte</CODE> array     * @param pfb the PFB file if the input is made with a <CODE>byte</CODE> array     * @param afmFile the name of one of the 14 built-in fonts or the location of an AFM file. The file must end in '.afm'     * @param enc the encoding to be applied to this font     * @param emb true if the font is to be embedded in the PDF     * @throws DocumentException the AFM file is invalid     * @throws IOException the AFM file could not be read     */    Type1Font(String afmFile, String enc, boolean emb, byte ttfAfm[], byte pfb[]) throws DocumentException, IOException    {        if (emb && ttfAfm != null && pfb == null)            throw new DocumentException("Two byte arrays are needed if the Type1 font is embedded.");        if (emb && ttfAfm != null)            this.pfb = pfb;        encoding = enc;        embedded = emb;        fileName = afmFile;        fontType = FONT_TYPE_T1;        RandomAccessFileOrArray rf = null;        InputStream is = null;        if (BuiltinFonts14.containsKey(afmFile)) {            embedded = false;            builtinFont = true;            byte buf[] = new byte[1024];            try {                if (resourceAnchor == null)                    resourceAnchor = new FontsResourceAnchor();                is = getResourceStream(RESOURCE_PATH + afmFile + ".afm", resourceAnchor.getClass().getClassLoader());                if (is == null) {                    String msg = afmFile + " not found as resource. (The *.afm files must exist as resources in the package com.lowagie.text.pdf.fonts)";                    System.err.println(msg);                    throw new DocumentException(msg);                }                ByteArrayOutputStream out = new ByteArrayOutputStream();                while (true) {                    int size = is.read(buf);                    if (size < 0)                        break;                    out.write(buf, 0, size);                }                buf = out.toByteArray();            }            finally {                if (is != null) {                    try {                        is.close();                    }                    catch (Exception e) {                        // empty on purpose                    }                }            }            try {                rf = new RandomAccessFileOrArray(buf);                process(rf);            }            finally {                if (rf != null) {                    try {                        rf.close();                    }                    catch (Exception e) {                        // empty on purpose                    }                }            }        }        else if (afmFile.toLowerCase().endsWith(".afm")) {            try {                if (ttfAfm == null)                    rf = new RandomAccessFileOrArray(afmFile);                else                    rf = new RandomAccessFileOrArray(ttfAfm);                process(rf);            }            finally {                if (rf != null) {                    try {                        rf.close();                    }                    catch (Exception e) {                        // empty on purpose                    }                }            }        }        else if (afmFile.toLowerCase().endsWith(".pfm")) {            try {                ByteArrayOutputStream ba = new ByteArrayOutputStream();                if (ttfAfm == null)                    rf = new RandomAccessFileOrArray(afmFile);                else                    rf = new RandomAccessFileOrArray(ttfAfm);                Pfm2afm.convert(rf, ba);                rf.close();                rf = new RandomAccessFileOrArray(ba.toByteArray());                process(rf);            }            finally {                if (rf != null) {                    try {                        rf.close();                    }                    catch (Exception e) {                        // empty on purpose                    }                }            }        }        else            throw new DocumentException(afmFile + " is not an AFM or PFM font file.");        EncodingScheme = EncodingScheme.trim();        if (EncodingScheme.equals("AdobeStandardEncoding") || EncodingScheme.equals("StandardEncoding")) {

⌨️ 快捷键说明

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