⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basefont.java

📁 java itext java itext java itext
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: BaseFont.java,v 1.14 2001/12/10 13:53:21 blowagie Exp $
 * $Name:  $
 *
 * Copyright 2000, 2001 by Paulo Soares.
 *
 * This library is free software; you can redistribute it and/or modify it
 * 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.
 *
 * You should have received a copy of the GNU Library General Public License along
 * with this library; if not, write to the Free Foundation, Inc., 59 Temple Place,
 * Suite 330, Boston, MA 02111-1307 USA.
 *
 * 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/
 *
 * ir-arch Bruno Lowagie,
 * Adolf Baeyensstraat 121
 * 9040 Sint-Amandsberg
 * BELGIUM
 * tel. +32 (0)9 228.10.97
 * bruno@lowagie.com
 *
 */

package com.lowagie.text.pdf;
import java.io.*;
import com.lowagie.text.DocumentException;
import java.util.HashMap;

/**
 * Base class for the several font types supported
 *
 * @author Paulo Soares (psoares@consiste.pt)
 */

public abstract class BaseFont {

    /** The maximum height above the baseline reached by glyphs in this
     * font, excluding the height of glyphs for accented characters.
     */    
    public final static int ASCENT = 1;    
    /** The y coordinate of the top of flat capital letters, measured from
     * the baseline.
     */    
    public final static int CAPHEIGHT = 2;
    /** The maximum depth below the baseline reached by glyphs in this
     * font. The value is a negative number.
     */    
    public final static int DESCENT = 3;
    /** The angle, expressed in degrees counterclockwise from the vertical,
     * of the dominant vertical strokes of the font. The value is
     * negative for fonts that slope to the right, as almost all italic fonts do.
     */    
    public final static int ITALICANGLE = 4;
    /** The lower left x glyph coordinate.
     */    
    public final static int BBOXLLX = 5;
    /** The lower left y glyph coordinate.
     */    
    public final static int BBOXLLY = 6;
    /** The upper right x glyph coordinate.
     */    
    public final static int BBOXURX = 7;
    /** The upper right y glyph coordinate.
     */    
    public final static int BBOXURY = 8;
    
    /** The font is Type 1.
     */    
    public final static int FONT_TYPE_T1 = 0;
    /** The font is True Type with a standard encoding.
     */    
    public final static int FONT_TYPE_TT = 1;
    /** The font is CJK.
     */    
    public final static int FONT_TYPE_CJK = 2;
    /** The font is True Type with a Unicode encoding.
     */    
    public final static int FONT_TYPE_TTUNI = 3;
    /** The Unicode encoding with horizontal writing.
     */    
    public static final String IDENTITY_H = "Identity-H";
    /** The Unicode encoding with vertical writing.
     */    
    public static final String IDENTITY_V = "Identity-V";
    
    /** A possible encoding. */    
    public static final String CP1252 = "Cp1252";
    
    /** A possible encoding. */    
    public static final String WINANSI = "Cp1252";
    
    /** A possible encoding. */    
    public static final String MACROMAN = "MacRoman";
    
    
/** if the font has to be embedded */
    public final static boolean EMBEDDED = true;
    
/** if the font doesn't have to be embedded */
    public final static boolean NOT_EMBEDDED = false;

    /** The font type.
     */    
    int fontType;
/** a not defined character in a custom PDF encoding */
    public final static String notdef = ".notdef";
    
/** table of characters widths for this encoding */
    protected int widths[] = new int[256];
    
/** encoding names */
    protected String differences[] = new String[256];
/** same as differences but with the unicode codes */
    protected char unicodeDifferences[] = new char[256];
    
/** encoding used with this font */
    protected String encoding;
    
/** true if the font is to be embedded in the PDF */
    protected boolean embedded;
    
/**
 * true if the font must use it's built in encoding. In that case the
 * <CODE>encoding</CODE> is only used to map a char to the position inside
 * the font, not to the expected char name.
 */
    protected boolean fontSpecific = true;
    
/** cache for the fonts already used. */
    protected static HashMap fontCache = new HashMap();
    
/** list of the 14 built in fonts. */
    protected static final HashMap BuiltinFonts14 = new HashMap();
    
    /** The subset prefix to be added to the font name when the font is embedded.
     */    
    protected static char subsetPrefix[] = {'A', 'B', 'C', 'D', 'E', 'E', '+'};
    
    static
    {
        BuiltinFonts14.put("Courier", PdfName.COURIER);
        BuiltinFonts14.put("Courier-Bold", PdfName.COURIER_BOLD);
        BuiltinFonts14.put("Courier-BoldOblique", PdfName.COURIER_BOLDOBLIQUE);
        BuiltinFonts14.put("Courier-Oblique", PdfName.COURIER_OBLIQUE);
        BuiltinFonts14.put("Helvetica", PdfName.HELVETICA);
        BuiltinFonts14.put("Helvetica-Bold", PdfName.HELVETICA_BOLD);
        BuiltinFonts14.put("Helvetica-BoldOblique", PdfName.HELVETICA_BOLDOBLIQUE);
        BuiltinFonts14.put("Helvetica-Oblique", PdfName.HELVETICA_OBLIQUE);
        BuiltinFonts14.put("Symbol", PdfName.SYMBOL);
        BuiltinFonts14.put("Times-Roman", PdfName.TIMES_ROMAN);
        BuiltinFonts14.put("Times-Bold", PdfName.TIMES_BOLD);
        BuiltinFonts14.put("Times-BoldItalic", PdfName.TIMES_BOLDITALIC);
        BuiltinFonts14.put("Times-Italic", PdfName.TIMES_ITALIC);
        BuiltinFonts14.put("ZapfDingbats", PdfName.ZAPFDINGBATS);
    }
    
    /** Generates the PDF stream with the Type1 and Truetype fonts returning
     * a PdfStream.
     */    
    class StreamFont extends PdfStream {
        
        /** Generates the PDF stream with the Type1 and Truetype fonts returning
         * a PdfStream.
         * @param contents the content of the stream
         * @param lengths an array of int that describes the several lengths of each part of the font
         * @throws DocumentException error in the stream compression
         */
        public StreamFont(byte contents[], int lengths[]) throws DocumentException
        {
            try {
                bytes = contents;
                dictionary.put(PdfName.LENGTH, new PdfNumber(bytes.length));
                for (int k = 0; k < lengths.length; ++k) {
                    dictionary.put(new PdfName("Length" + (k + 1)), new PdfNumber(lengths[k]));
                }
                flateCompress();
            }
            catch (Exception e) {
                throw new DocumentException(e.getMessage());
            }
        }
    }
    
/**
 *Creates new BaseFont
 */
    protected BaseFont() {
    }
    
    /** Creates a new font. This font can be one of the 14 built in types,
     * a Type1 font referred by an AFM file, a TrueType font (simple or collection) or a CJK font from the
     * Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier
     * appended to the name. These modifiers are: Bold, Italic and BoldItalic. An
     * example would be "STSong-Light,Bold". Note that this modifiers do not work if
     * the font is embedded. Fonts in TrueType collections are addressed by index such as "msgothic.ttc,1".
     * This would get the second font (indexes start at 0), in this case "MS PGothic".
     * <P>
     * The fonts are cached and if they already exist they are extracted from the cache,
     * not parsed again.
     * @param name the name of the font or it's location on file
     * @param encoding the encoding to be applied to this font
     * @param embedded true if the font is to be embedded in the PDF
     * @return returns a new font. This font may come from the cache
     * @throws DocumentException the font is invalid
     * @throws IOException the font file could not be read
     */
    public static BaseFont createFont(String name, String encoding, boolean embedded) throws DocumentException, IOException
    {
        String nameBase = getBaseName(name);
        encoding = normalizeEncoding(encoding);
        boolean isBuiltinFonts14 = BuiltinFonts14.containsKey(name);
        boolean isCJKFont = isBuiltinFonts14 ? false : CJKFont.isCJKFont(nameBase, encoding);
        if (isBuiltinFonts14 || isCJKFont)
            embedded = false;
        else if (encoding.equals(IDENTITY_H) || encoding.equals(IDENTITY_V))
            embedded = true;
        BaseFont fontFound = null;
        BaseFont fontBuilt = null;
        String key = name + "\n" + encoding + "\n" + embedded;
        synchronized (fontCache) {
            fontFound = (BaseFont)fontCache.get(key);
        }
        if (fontFound != null)
            return fontFound;
        if (isBuiltinFonts14 || name.toLowerCase().endsWith(".afm")) {
            fontBuilt = new Type1Font(name, encoding, embedded);
        }
        else if (nameBase.toLowerCase().endsWith(".ttf") || nameBase.toLowerCase().indexOf(".ttc,") > 0) {
            if (encoding.equals(IDENTITY_H) || encoding.equals(IDENTITY_V))
                fontBuilt = new TrueTypeFontUnicode(name, encoding, embedded);
            else
                fontBuilt = new TrueTypeFont(name, encoding, embedded);
        }

⌨️ 快捷键说明

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