?? type1font.java
?? iText可以制作中文PDF文件的JAVA源程序最新版下載
?? JAVA
?? 第 1 頁 / 共 2 頁
字號:
??
/* * $Id: Type1Font.java,v 1.34 2002/11/19 08:33:39 blowagie Exp $ * $Name: $ * * Copyright 2001, 2002 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 com.lowagie.text.ExceptionConverter;import com.lowagie.text.DocumentException;import java.util.HashMap;import java.util.ArrayList;import java.util.StringTokenizer;import java.io.*;/** Reads a Type1 font * * @author Paulo Soares (psoares@consiste.pt) */class Type1Font extends BaseFont{ /** 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 * element of this array contains a <CODE>Object[3]</CODE> with an * Integer, Integer and String. This is the code, width and name. */ private ArrayList CharMetrics = new ArrayList();/** 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 pfbTypes[] = {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 { is = getResourceStream(afmFile + ".afm"); 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 throw new DocumentException(afmFile + " is not an AFM font file."); try { EncodingScheme = EncodingScheme.trim(); if (EncodingScheme.equals("AdobeStandardEncoding") || EncodingScheme.equals("StandardEncoding")) { fontSpecific = false; } " ".getBytes(enc); // check if the encoding exists createEncoding(); } catch (Exception e) { throw new DocumentException(e.getMessage()); } } /** Gets the width from the font according to the <CODE>name</CODE> or, * if the <CODE>name</CODE> is null, meaning it is a symbolic font, * the char <CODE>c</CODE>. * @param c the char if the font is symbolic * @param name the glyph name * @return the width of the char */ protected int getRawWidth(int c, String name) { try { if (name == null) { // font specific for (int k = 0; k < CharMetrics.size(); ++k) { Object metrics[] = (Object[])CharMetrics.get(k); if (((Integer)(metrics[0])).intValue() == c) return ((Integer)(metrics[1])).intValue(); } } else { if (name.equals(".notdef")) return 0; for (int k = 0; k < CharMetrics.size(); ++k) { Object metrics[] = (Object[])CharMetrics.get(k); if (name.equals(metrics[2])) return ((Integer)(metrics[1])).intValue(); } } } catch (Exception e) { throw new ExceptionConverter(e); } return 0; } /** Gets the kerning between two Unicode characters. The characters * are converted to names and this names are used to find the kerning * pairs in the <CODE>HashMap</CODE> <CODE>KernPairs</CODE>. * @param char1 the first char * @param char2 the second char * @return the kerning to be applied */ public int getKerning(char char1, char char2) { String first = GlyphList.unicodeToName((int)char1); if (first == null) return 0; String second = GlyphList.unicodeToName((int)char2); if (second == null) return 0; Object obj[] = (Object[])KernPairs.get(first); if (obj == null) return 0; for (int k = 0; k < obj.length; k += 2) { if (second.equals(obj[k])) return ((Integer)obj[k + 1]).intValue(); } return 0; } /** Reads the font metrics * @param rf the AFM file * @throws DocumentException the AFM file is invalid * @throws IOException the AFM file could not be read */ public void process(RandomAccessFileOrArray rf) throws DocumentException, IOException { String line; boolean isMetrics = false; while ((line = rf.readLine()) != null) { StringTokenizer tok = new StringTokenizer(line); if (!tok.hasMoreTokens()) continue; String ident = tok.nextToken(); if (ident.equals("FontName")) FontName = tok.nextToken("\u00ff").substring(1); else if (ident.equals("FullName")) FullName = tok.nextToken("\u00ff").substring(1); else if (ident.equals("FamilyName")) FamilyName = tok.nextToken("\u00ff").substring(1); else if (ident.equals("Weight")) Weight = tok.nextToken("\u00ff").substring(1); else if (ident.equals("ItalicAngle")) ItalicAngle = Float.valueOf(tok.nextToken()).floatValue(); else if (ident.equals("IsFixedPitch")) IsFixedPitch = tok.nextToken().equals("true"); else if (ident.equals("CharacterSet")) CharacterSet = tok.nextToken("\u00ff").substring(1); else if (ident.equals("FontBBox")) { llx = (int)Float.valueOf(tok.nextToken()).floatValue(); lly = (int)Float.valueOf(tok.nextToken()).floatValue(); urx = (int)Float.valueOf(tok.nextToken()).floatValue(); ury = (int)Float.valueOf(tok.nextToken()).floatValue(); } else if (ident.equals("UnderlinePosition")) UnderlinePosition = (int)Float.valueOf(tok.nextToken()).floatValue(); else if (ident.equals("UnderlineThickness")) UnderlineThickness = (int)Float.valueOf(tok.nextToken()).floatValue(); else if (ident.equals("EncodingScheme")) EncodingScheme = tok.nextToken("\u00ff").substring(1); else if (ident.equals("CapHeight")) CapHeight = (int)Float.valueOf(tok.nextToken()).floatValue();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -