documentfont.java

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

JAVA
661
字号
            urx = bf.getFontDescriptor(BBOXURX, 1000);            ury = bf.getFontDescriptor(BBOXURY, 1000);        }        if (first != null && last != null && newWidths != null) {            int f = first.intValue();            ArrayList ar = newWidths.getArrayList();            for (int k = 0; k < ar.size(); ++k) {                widths[f + k] = ((PdfNumber)ar.get(k)).intValue();            }        }        fillFontDesc((PdfDictionary)PdfReader.getPdfObject(font.get(PdfName.FONTDESCRIPTOR)));    }        private void fillFontDesc(PdfDictionary fontDesc) {        if (fontDesc == null)            return;        PdfNumber v = (PdfNumber)PdfReader.getPdfObject(fontDesc.get(PdfName.ASCENT));        if (v != null)            Ascender = v.floatValue();        v = (PdfNumber)PdfReader.getPdfObject(fontDesc.get(PdfName.CAPHEIGHT));        if (v != null)            CapHeight = v.floatValue();        v = (PdfNumber)PdfReader.getPdfObject(fontDesc.get(PdfName.DESCENT));        if (v != null)            Descender = v.floatValue();        v = (PdfNumber)PdfReader.getPdfObject(fontDesc.get(PdfName.ITALICANGLE));        if (v != null)            ItalicAngle = v.floatValue();        PdfArray bbox = (PdfArray)PdfReader.getPdfObject(fontDesc.get(PdfName.FONTBBOX));        if (bbox != null) {            ArrayList ar = bbox.getArrayList();            llx = ((PdfNumber)ar.get(0)).floatValue();            lly = ((PdfNumber)ar.get(1)).floatValue();            urx = ((PdfNumber)ar.get(2)).floatValue();            ury = ((PdfNumber)ar.get(3)).floatValue();            if (llx > urx) {                float t = llx;                llx = urx;                urx = t;            }            if (lly > ury) {                float t = lly;                lly = ury;                ury = t;            }        }    }        private void fillEncoding(PdfName encoding) {        if (PdfName.MAC_ROMAN_ENCODING.equals(encoding) || PdfName.WIN_ANSI_ENCODING.equals(encoding)) {            byte b[] = new byte[256];            for (int k = 0; k < 256; ++k)                b[k] = (byte)k;            String enc = WINANSI;            if (PdfName.MAC_ROMAN_ENCODING.equals(encoding))                enc = MACROMAN;            String cv = PdfEncodings.convertToString(b, enc);            char arr[] = cv.toCharArray();            for (int k = 0; k < 256; ++k) {                uni2byte.put(arr[k], k);                byte2uni.put(k, arr[k]);            }        }        else {            for (int k = 0; k < 256; ++k) {                uni2byte.put(stdEnc[k], k);                byte2uni.put(k, stdEnc[k]);            }        }    }        /** Gets the family name of the font. If it is a True Type font     * each array element will have {Platform ID, Platform Encoding ID,     * Language ID, font name}. The interpretation of this values can be     * found in the Open Type specification, chapter 2, in the 'name' table.<br>     * For the other fonts the array has a single element with {"", "", "",     * font name}.     * @return the family name of the font     *     */    public String[][] getFamilyFontName() {        return getFullFontName();    }        /** Gets the font parameter identified by <CODE>key</CODE>. Valid values     * for <CODE>key</CODE> are <CODE>ASCENT</CODE>, <CODE>CAPHEIGHT</CODE>, <CODE>DESCENT</CODE>,     * <CODE>ITALICANGLE</CODE>, <CODE>BBOXLLX</CODE>, <CODE>BBOXLLY</CODE>, <CODE>BBOXURX</CODE>     * and <CODE>BBOXURY</CODE>.     * @param key the parameter to be extracted     * @param fontSize the font size in points     * @return the parameter in points     *     */    public float getFontDescriptor(int key, float fontSize) {        if (cjkMirror != null)            return cjkMirror.getFontDescriptor(key, fontSize);        switch (key) {            case AWT_ASCENT:            case ASCENT:                return Ascender * fontSize / 1000;            case CAPHEIGHT:                return CapHeight * fontSize / 1000;            case AWT_DESCENT:            case DESCENT:                return Descender * fontSize / 1000;            case ITALICANGLE:                return ItalicAngle;            case BBOXLLX:                return llx * fontSize / 1000;            case BBOXLLY:                return lly * fontSize / 1000;            case BBOXURX:                return urx * fontSize / 1000;            case BBOXURY:                return ury * fontSize / 1000;            case AWT_LEADING:                return 0;            case AWT_MAXADVANCE:                return (urx - llx) * fontSize / 1000;        }        return 0;    }        /** Gets the full name of the font. If it is a True Type font     * each array element will have {Platform ID, Platform Encoding ID,     * Language ID, font name}. The interpretation of this values can be     * found in the Open Type specification, chapter 2, in the 'name' table.<br>     * For the other fonts the array has a single element with {"", "", "",     * font name}.     * @return the full name of the font     *     */    public String[][] getFullFontName() {        return new String[][]{{"", "", "", fontName}};    }        /** Gets all the entries of the names-table. If it is a True Type font     * each array element will have {Name ID, Platform ID, Platform Encoding ID,     * Language ID, font name}. The interpretation of this values can be     * found in the Open Type specification, chapter 2, in the 'name' table.<br>     * For the other fonts the array has a single element with {"4", "", "", "",     * font name}.     * @return the full name of the font     * @since 2.0.8     */    public String[][] getAllNameEntries() {        return new String[][]{{"4", "", "", "", fontName}};    }    /** Gets the kerning between two Unicode chars.     * @param char1 the first char     * @param char2 the second char     * @return the kerning to be applied     *     */    public int getKerning(char char1, char char2) {        return 0;    }        /** Gets the postscript font name.     * @return the postscript font name     *     */    public String getPostscriptFontName() {        return fontName;    }        /** Gets the width from the font according to the Unicode char <CODE>c</CODE>     * or the <CODE>name</CODE>. If the <CODE>name</CODE> is null it's a symbolic font.     * @param c the unicode char     * @param name the glyph name     * @return the width of the char     *     */    int getRawWidth(int c, String name) {        return 0;    }        /** Checks if the font has any kerning pairs.     * @return <CODE>true</CODE> if the font has any kerning pairs     *     */    public boolean hasKernPairs() {        return false;    }        /** Outputs to the writer the font dictionaries and streams.     * @param writer the writer for this document     * @param ref the font indirect reference     * @param params several parameters that depend on the font type     * @throws IOException on error     * @throws DocumentException error in generating the object     *     */    void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params) throws DocumentException, IOException {    }    /**     * Gets the width of a <CODE>char</CODE> in normalized 1000 units.     * @param char1 the unicode <CODE>char</CODE> to get the width of     * @return the width in normalized 1000 units     */    public int getWidth(char char1) {        if (cjkMirror != null)            return cjkMirror.getWidth(char1);        else if (isType0) {            int[] ws = (int[])metrics.get(new Integer((int)char1));            if (ws != null)                return ws[1];            else                return 0;        }        else            return super.getWidth(char1);    }        public int getWidth(String text) {        if (cjkMirror != null)            return cjkMirror.getWidth(text);        else if (isType0) {            char[] chars = text.toCharArray();            int len = chars.length;            int total = 0;            for (int k = 0; k < len; ++k) {                int[] ws = (int[])metrics.get(new Integer((int)chars[k]));                if (ws != null)                    total += ws[1];            }            return total;        }        else            return super.getWidth(text);    }        byte[] convertToBytes(String text) {        if (cjkMirror != null)            return PdfEncodings.convertToBytes(text, CJKFont.CJK_ENCODING);        else if (isType0) {            char[] chars = text.toCharArray();            int len = chars.length;            byte[] b = new byte[len * 2];            int bptr = 0;            for (int k = 0; k < len; ++k) {                int[] ws = (int[])metrics.get(new Integer((int)chars[k]));                if (ws != null) {                    int g = ws[0];                    b[bptr++] = (byte)(g / 256);                    b[bptr++] = (byte)(g);                }            }            if (bptr == b.length)                return b;            else {                byte[] nb = new byte[bptr];                System.arraycopy(b, 0, nb, 0, bptr);                return nb;            }        }        else {            char cc[] = text.toCharArray();            byte b[] = new byte[cc.length];            int ptr = 0;            for (int k = 0; k < cc.length; ++k) {                if (uni2byte.containsKey(cc[k]))                    b[ptr++] = (byte)uni2byte.get(cc[k]);            }            if (ptr == b.length)                return b;            else {                byte[] b2 = new byte[ptr];                System.arraycopy(b, 0, b2, 0, ptr);                return b2;            }        }    }        byte[] convertToBytes(char char1) {        if (cjkMirror != null)            return PdfEncodings.convertToBytes(char1, CJKFont.CJK_ENCODING);        else if (isType0) {            int[] ws = (int[])metrics.get(new Integer((int)char1));            if (ws != null) {                int g = ws[0];                return new byte[]{(byte)(g / 256), (byte)(g)};            }            else                return new byte[0];        }        else {            if (uni2byte.containsKey(char1))                return new byte[]{(byte)uni2byte.get(char1)};            else                return new byte[0];        }    }        PdfIndirectReference getIndirectReference() {        return refFont;    }        public boolean charExists(char c) {        if (cjkMirror != null)            return cjkMirror.charExists(c);        else if (isType0) {            return metrics.containsKey(new Integer((int)c));        }        else            return super.charExists(c);    }        /**     * Sets the font name that will appear in the pdf font dictionary.     * It does nothing in this case as the font is already in the document.     * @param name the new font name     */        public void setPostscriptFontName(String name) {    }        public boolean setKerning(char char1, char char2, int kern) {        return false;    }        public int[] getCharBBox(char c) {        return null;    }        protected int[] getRawCharBBox(int c, String name) {        return null;    }}

⌨️ 快捷键说明

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