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

📄 cjkfont.java

📁 一个java操作pdf文件的开发包,很好用的.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: CJKFont.java,v 1.31 2002/11/19 08:33:35 blowagie Exp $ * $Name:  $ * * Copyright 2000, 2001, 2002 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 com.lowagie.text.DocumentException;import java.io.*;import java.util.HashMap;import java.util.Properties;import java.util.Hashtable;import java.util.StringTokenizer;import java.util.Enumeration;/** * Creates a CJK font compatible with the fonts in the Adobe Asian font Pack. * * @author  Paulo Soares (psoares@consiste.pt) */class CJKFont extends BaseFont {    /** The encoding used in the PDF document for CJK fonts     */    static final String CJK_ENCODING = "UnicodeBigUnmarked";    private static final int FIRST = 0;    private static final int BRACKET = 1;    private static final int SERIAL = 2;    private static final int V1Y = 880;            static Properties cjkFonts = new Properties();    static Properties cjkEncodings = new Properties();    static Hashtable allCMaps = new Hashtable();    static Hashtable allFonts = new Hashtable();        static {        try {            InputStream is = getResourceStream("cjkfonts.properties");            cjkFonts.load(is);            is.close();            is = getResourceStream("cjkencodings.properties");            cjkEncodings.load(is);            is.close();        }        catch (Exception e) {            cjkFonts = new Properties();            cjkEncodings = new Properties();        }    }        /** The font name */    private String fontName;    /** The style modifier */    private String style = "";    /** The CMap name associated with this font */    private String CMap;        private boolean cidDirect = false;        private char[] translationMap;    private IntHashtable vMetrics;    private IntHashtable hMetrics;    private HashMap fontDesc;    private boolean vertical = false;        /** Creates a CJK font.     * @param fontName the name of the font     * @param enc the encoding of the font     * @param emb always <CODE>false</CODE>. CJK font and not embedded     * @throws DocumentException on error     * @throws IOException on error     */    CJKFont(String fontName, String enc, boolean emb) throws DocumentException, IOException {        fontType = FONT_TYPE_CJK;        String nameBase = getBaseName(fontName);        if (!isCJKFont(nameBase, enc))            throw new DocumentException("Font '" + fontName + "' with '" + enc + "' encoding is not a CJK font.");        if (nameBase.length() < fontName.length()) {            style = fontName.substring(nameBase.length());            fontName = nameBase;        }        this.fontName = fontName;        encoding = CJK_ENCODING;        vertical = enc.endsWith("V");        CMap = enc;        if (enc.startsWith("Identity-")) {            cidDirect = true;            String s = cjkFonts.getProperty(fontName);            s = s.substring(0, s.indexOf('_'));            char c[] = (char[])allCMaps.get(s);            if (c == null) {                c = readCMap(s);                if (c == null)                    throw new DocumentException("The cmap " + s + " does not exist as a resource.");                c[0xff00] = '\n';                allCMaps.put(s, c);            }            translationMap = c;        }        else {            char c[] = (char[])allCMaps.get(enc);            if (c == null) {                String s = cjkEncodings.getProperty(enc);                if (s == null)                    throw new DocumentException("The resource cjkencodings.properties does not contain the encoding " + enc);                StringTokenizer tk = new StringTokenizer(s);                String nt = tk.nextToken();                c = (char[])allCMaps.get(nt);                if (c == null) {                    c = readCMap(nt);                    allCMaps.put(nt, c);                }                if (tk.hasMoreTokens()) {                    String nt2 = tk.nextToken();                    char m2[] = readCMap(nt2);                    for (int k = 0; k < 0x10000; ++k) {                        if (m2[k] == 0)                            m2[k] = c[k];                    }                    allCMaps.put(enc, m2);                    c = m2;                }            }            translationMap = c;        }        fontDesc = (HashMap)allFonts.get(fontName);        if (fontDesc == null) {            fontDesc = readFontProperties(fontName);            allFonts.put(fontName, fontDesc);        }        hMetrics = (IntHashtable)fontDesc.get("W");        vMetrics = (IntHashtable)fontDesc.get("W2");    }        /** Checks if its a valid CJK font.     * @param fontName the font name     * @param enc the encoding     * @return <CODE>true</CODE> if it is CJK font     */    public static boolean isCJKFont(String fontName, String enc) {        String encodings = cjkFonts.getProperty(fontName);        return (encodings != null && (enc.equals("Identity-H") || enc.equals("Identity-V") || encodings.indexOf("_" + enc + "_") >= 0));    }            public int getWidth(String text) {        int total = 0;        for (int k = 0; k < text.length(); ++k) {            int c = text.charAt(k);            if (!cidDirect)                c = translationMap[c];            int v;            if (vertical)                v = vMetrics.get(c);            else                v = hMetrics.get(c);            if (v > 0)                total += v;            else                total += 1000;        }        return total;    }        protected int getRawWidth(int c, String name) {        return 0;    }    public int getKerning(char char1, char char2) {        return 0;    }    private PdfDictionary getFontDescriptor() {        PdfDictionary dic = new PdfDictionary(new PdfName("FontDescriptor"));        dic.put(new PdfName("Ascent"), new PdfLiteral((String)fontDesc.get("Ascent")));        dic.put(new PdfName("CapHeight"), new PdfLiteral((String)fontDesc.get("CapHeight")));        dic.put(new PdfName("Descent"), new PdfLiteral((String)fontDesc.get("Descent")));        dic.put(new PdfName("Flags"), new PdfLiteral((String)fontDesc.get("Flags")));        dic.put(new PdfName("FontBBox"), new PdfLiteral((String)fontDesc.get("FontBBox")));        dic.put(new PdfName("FontName"), new PdfName(fontName + style));        dic.put(new PdfName("ItalicAngle"), new PdfLiteral((String)fontDesc.get("ItalicAngle")));        dic.put(new PdfName("StemV"), new PdfLiteral((String)fontDesc.get("StemV")));        PdfDictionary pdic = new PdfDictionary();        pdic.put(PdfName.PANOSE, new PdfString((String)fontDesc.get("Panose"), null));        dic.put(new PdfName("Style"), pdic);        return dic;    }        private PdfDictionary getCIDFont(PdfIndirectReference fontDescriptor, IntHashtable cjkTag) {        PdfDictionary dic = new PdfDictionary(PdfName.FONT);        dic.put(PdfName.SUBTYPE, new PdfName("CIDFontType0"));        dic.put(new PdfName("BaseFont"), new PdfName(fontName + style));        dic.put(new PdfName("FontDescriptor"), fontDescriptor);        int keys[] = cjkTag.toOrderedKeys();        String w = convertToHCIDMetrics(keys, hMetrics);        if (w != null)            dic.put(new PdfName("W"), new PdfLiteral(w));        if (vertical) {            w = convertToVCIDMetrics(keys, vMetrics, hMetrics);;            if (w != null)                dic.put(new PdfName("W2"), new PdfLiteral(w));        }        PdfDictionary cdic = new PdfDictionary();        cdic.put(PdfName.REGISTRY, new PdfString((String)fontDesc.get("Registry"), null));        cdic.put(PdfName.ORDERING, new PdfString((String)fontDesc.get("Ordering"), null));        cdic.put(PdfName.SUPPLEMENT, new PdfLiteral((String)fontDesc.get("Supplement")));        dic.put(new PdfName("CIDSystemInfo"), cdic);        return dic;    }        private PdfDictionary getFontBaseType(PdfIndirectReference CIDFont) {        PdfDictionary dic = new PdfDictionary(PdfName.FONT);        dic.put(PdfName.SUBTYPE, new PdfName("Type0"));        String name = fontName;        if (style.length() > 0)            name += "-" + style.substring(1);        name += "-" + CMap;        dic.put(new PdfName("BaseFont"), new PdfName(name));        dic.put(new PdfName("Encoding"), new PdfName(CMap));        dic.put(new PdfName("DescendantFonts"), new PdfArray(CIDFont));        return dic;    }        void writeFont(PdfWriter writer, PdfIndirectReference ref, Object params[]) throws DocumentException, IOException {        IntHashtable cjkTag = (IntHashtable)params[0];        PdfIndirectReference ind_font = null;        PdfObject pobj = null;        PdfIndirectObject obj = null;        pobj = getFontDescriptor();        if (pobj != null){            obj = writer.addToBody(pobj);            ind_font = obj.getIndirectReference();        }        pobj = getCIDFont(ind_font, cjkTag);        if (pobj != null){            obj = writer.addToBody(pobj);            ind_font = obj.getIndirectReference();        }        pobj = getFontBaseType(ind_font);        writer.addToBody(pobj, ref);    }        private float getDescNumber(String name) {

⌨️ 快捷键说明

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