📄 basefont.java
字号:
/*
* $Id: BaseFont.java 2734 2007-04-30 15:35:47Z psoares33 $
* $Name$
*
* Copyright 2000-2006 by 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.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import com.lowagie.text.DocumentException;
/**
* Base class for the several font types supported
*
* @author Paulo Soares (psoares@consiste.pt)
*/
public abstract class BaseFont {
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER = "Courier";
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER_BOLD = "Courier-Bold";
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER_OBLIQUE = "Courier-Oblique";
/** This is a possible value of a base 14 type 1 font */
public static final String COURIER_BOLDOBLIQUE = "Courier-BoldOblique";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA = "Helvetica";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA_BOLD = "Helvetica-Bold";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA_OBLIQUE = "Helvetica-Oblique";
/** This is a possible value of a base 14 type 1 font */
public static final String HELVETICA_BOLDOBLIQUE = "Helvetica-BoldOblique";
/** This is a possible value of a base 14 type 1 font */
public static final String SYMBOL = "Symbol";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_ROMAN = "Times-Roman";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_BOLD = "Times-Bold";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_ITALIC = "Times-Italic";
/** This is a possible value of a base 14 type 1 font */
public static final String TIMES_BOLDITALIC = "Times-BoldItalic";
/** This is a possible value of a base 14 type 1 font */
public static final String ZAPFDINGBATS = "ZapfDingbats";
/** The maximum height above the baseline reached by glyphs in this
* font, excluding the height of glyphs for accented characters.
*/
public static final int ASCENT = 1;
/** The y coordinate of the top of flat capital letters, measured from
* the baseline.
*/
public static final int CAPHEIGHT = 2;
/** The maximum depth below the baseline reached by glyphs in this
* font. The value is a negative number.
*/
public static final 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 static final int ITALICANGLE = 4;
/** The lower left x glyph coordinate.
*/
public static final int BBOXLLX = 5;
/** The lower left y glyph coordinate.
*/
public static final int BBOXLLY = 6;
/** The upper right x glyph coordinate.
*/
public static final int BBOXURX = 7;
/** The upper right y glyph coordinate.
*/
public static final int BBOXURY = 8;
/** java.awt.Font property */
public static final int AWT_ASCENT = 9;
/** java.awt.Font property */
public static final int AWT_DESCENT = 10;
/** java.awt.Font property */
public static final int AWT_LEADING = 11;
/** java.awt.Font property */
public static final int AWT_MAXADVANCE = 12;
/** The font is Type 1.
*/
public static final int FONT_TYPE_T1 = 0;
/** The font is True Type with a standard encoding.
*/
public static final int FONT_TYPE_TT = 1;
/** The font is CJK.
*/
public static final int FONT_TYPE_CJK = 2;
/** The font is True Type with a Unicode encoding.
*/
public static final int FONT_TYPE_TTUNI = 3;
/** A font already inside the document.
*/
public static final int FONT_TYPE_DOCUMENT = 4;
/** A Type3 font.
*/
public static final int FONT_TYPE_T3 = 5;
/** 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 CP1250 = "Cp1250";
/** A possible encoding. */
public static final String CP1252 = "Cp1252";
/** A possible encoding. */
public static final String CP1257 = "Cp1257";
/** A possible encoding. */
public static final String WINANSI = "Cp1252";
/** A possible encoding. */
public static final String MACROMAN = "MacRoman";
public static final int[] CHAR_RANGE_LATIN = {0, 0x17f, 0x2000, 0x206f, 0x20a0, 0x20cf, 0xfb00, 0xfb06};
public static final int[] CHAR_RANGE_ARABIC = {0, 0x7f, 0x0600, 0x067f, 0x20a0, 0x20cf, 0xfb50, 0xfbff, 0xfe70, 0xfeff};
public static final int[] CHAR_RANGE_HEBREW = {0, 0x7f, 0x0590, 0x05ff, 0x20a0, 0x20cf, 0xfb1d, 0xfb4f};
public static final int[] CHAR_RANGE_CYRILLIC = {0, 0x7f, 0x0400, 0x052f, 0x2000, 0x206f, 0x20a0, 0x20cf};
/** if the font has to be embedded */
public static final boolean EMBEDDED = true;
/** if the font doesn't have to be embedded */
public static final boolean NOT_EMBEDDED = false;
/** if the font has to be cached */
public static final boolean CACHED = true;
/** if the font doesn't have to be cached */
public static final boolean NOT_CACHED = false;
/** The path to the font resources. */
public static final String RESOURCE_PATH = "com/lowagie/text/pdf/fonts/";
/** The fake CID code that represents a newline. */
public static final char CID_NEWLINE = '\u7fff';
protected ArrayList subsetRanges;
/** The font type.
*/
int fontType;
/** a not defined character in a custom PDF encoding */
public static final 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];
protected int charBBoxes[][] = new int[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();
/** Forces the output of the width array. Only matters for the 14
* built-in fonts.
*/
protected boolean forceWidthsOutput = false;
/** Converts <CODE>char</CODE> directly to <CODE>byte</CODE>
* by casting.
*/
protected boolean directTextToByte = false;
/** Indicates if all the glyphs and widths for that particular
* encoding should be included in the document.
*/
protected boolean subset = true;
protected boolean fastWinansi = false;
/**
* Custom encodings use this map to key the Unicode character
* to the single byte code.
*/
protected IntHashtable specialMap;
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.
*/
static 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;
put(PdfName.LENGTH, new PdfNumber(bytes.length));
for (int k = 0; k < lengths.length; ++k) {
put(new PdfName("Length" + (k + 1)), new PdfNumber(lengths[k]));
}
flateCompress();
}
catch (Exception e) {
throw new DocumentException(e);
}
}
/**
* Generates the PDF stream for a font.
* @param contents the content of a stream
* @param subType the subtype of the font.
* @throws DocumentException
*/
public StreamFont(byte contents[], String subType) throws DocumentException {
try {
bytes = contents;
put(PdfName.LENGTH, new PdfNumber(bytes.length));
if (subType != null)
put(PdfName.SUBTYPE, new PdfName(subType));
flateCompress();
}
catch (Exception e) {
throw new DocumentException(e);
}
}
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -