truetypefontunicode.java

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

JAVA
497
字号
                if (metric[1] == 1000)                    continue;                int m = metric[0];                if (m == lastNumber + 1) {                    buf.append(' ').append(metric[1]);                }                else {                    if (!firstTime) {                        buf.append(']');                    }                    firstTime = false;                    buf.append(m).append('[').append(metric[1]);                }                lastNumber = m;            }            if (buf.length() > 1) {                buf.append("]]");                dic.put(PdfName.W, new PdfLiteral(buf.toString()));            }        }        return dic;    }        /** Generates the font dictionary.     * @param descendant the descendant dictionary     * @param subsetPrefix the subset prefix     * @param toUnicode the ToUnicode stream     * @return the stream     */        private PdfDictionary getFontBaseType(PdfIndirectReference descendant, String subsetPrefix, PdfIndirectReference toUnicode) {        PdfDictionary dic = new PdfDictionary(PdfName.FONT);        dic.put(PdfName.SUBTYPE, PdfName.TYPE0);        // The PDF Reference manual advises to add -encoding to CID font names		if (cff)		  dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName+"-"+encoding));		  //dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName));		else		  dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName));		  //dic.put(PdfName.BASEFONT, new PdfName(fontName));        dic.put(PdfName.ENCODING, new PdfName(encoding));        dic.put(PdfName.DESCENDANTFONTS, new PdfArray(descendant));        if (toUnicode != null)            dic.put(PdfName.TOUNICODE, toUnicode);          return dic;    }    /** The method used to sort the metrics array.     * @param o1 the first element     * @param o2 the second element     * @return the comparison     */        public int compare(Object o1, Object o2) {        int m1 = ((int[])o1)[0];        int m2 = ((int[])o2)[0];        if (m1 < m2)            return -1;        if (m1 == m2)            return 0;        return 1;    }        private static final byte[] rotbits = {(byte)0x80,(byte)0x40,(byte)0x20,(byte)0x10,(byte)0x08,(byte)0x04,(byte)0x02,(byte)0x01};        /** 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 {        HashMap longTag = (HashMap)params[0];        addRangeUni(longTag, true, subset);        Object metrics[] = longTag.values().toArray();        Arrays.sort(metrics, this);        PdfIndirectReference ind_font = null;        PdfObject pobj = null;        PdfIndirectObject obj = null;        PdfIndirectReference cidset = null;        if (writer.getPDFXConformance() == PdfWriter.PDFA1A || writer.getPDFXConformance() == PdfWriter.PDFA1B) {            PdfStream stream;            if (metrics.length == 0) {                stream = new PdfStream(new byte[]{(byte)0x80});            }            else {                int top = ((int[])metrics[metrics.length - 1])[0];                byte[] bt = new byte[top / 8 + 1];                for (int k = 0; k < metrics.length; ++k) {                    int v = ((int[])metrics[k])[0];                    bt[v / 8] |= rotbits[v % 8];                }                stream = new PdfStream(bt);                stream.flateCompress();            }            cidset = writer.addToBody(stream).getIndirectReference();        }        // sivan: cff        if (cff) {			RandomAccessFileOrArray rf2 = new RandomAccessFileOrArray(rf);			byte b[] = new byte[cffLength];			try {				rf2.reOpen();				rf2.seek(cffOffset);				rf2.readFully(b);			} finally {				try {					rf2.close();				} catch (Exception e) {					// empty on purpose				}			}            if (subset || subsetRanges != null) {                CFFFontSubset cff = new CFFFontSubset(new RandomAccessFileOrArray(b),longTag);                b = cff.Process( (cff.getNames())[0] );            }			pobj = new StreamFont(b, "CIDFontType0C");			obj = writer.addToBody(pobj);			ind_font = obj.getIndirectReference();        } else {            byte[] b;            if (subset || directoryOffset != 0) {                TrueTypeFontSubSet sb = new TrueTypeFontSubSet(fileName, new RandomAccessFileOrArray(rf), longTag, directoryOffset, false, false);                b = sb.process();            }            else {                b = getFullFont();            }            int lengths[] = new int[]{b.length};            pobj = new StreamFont(b, lengths);            obj = writer.addToBody(pobj);            ind_font = obj.getIndirectReference();        }        String subsetPrefix = "";        if (subset)            subsetPrefix = createSubsetPrefix();        PdfDictionary dic = getFontDescriptor(ind_font, subsetPrefix, cidset);        obj = writer.addToBody(dic);        ind_font = obj.getIndirectReference();        pobj = getCIDFontType2(ind_font, subsetPrefix, metrics);        obj = writer.addToBody(pobj);        ind_font = obj.getIndirectReference();        pobj = getToUnicode(metrics);        PdfIndirectReference toUnicodeRef = null;                if (pobj != null) {            obj = writer.addToBody(pobj);            toUnicodeRef = obj.getIndirectReference();        }        pobj = getFontBaseType(ind_font, subsetPrefix, toUnicodeRef);        writer.addToBody(pobj, ref);    }    /** A forbidden operation. Will throw a null pointer exception.     * @param text the text     * @return always <CODE>null</CODE>     */        byte[] convertToBytes(String text) {        return null;    }    byte[] convertToBytes(char char1) {        return null;    }    /**     * Checks if a character exists in this font.     * @param c the character to check     * @return <CODE>true</CODE> if the character has a glyph,     * <CODE>false</CODE> otherwise     */    public boolean charExists(char c) {        HashMap map = null;        if (fontSpecific)            map = cmap10;        else            map = cmap31;        if (map == null)            return false;        if (fontSpecific) {            if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000)                return map.get(new Integer(c & 0xff)) != null;            else                return false;        }        else            return map.get(new Integer(c)) != null;    }        /**     * Sets the character advance.     * @param c the character     * @param advance the character advance normalized to 1000 units     * @return <CODE>true</CODE> if the advance was set,     * <CODE>false</CODE> otherwise     */    public boolean setCharAdvance(char c, int advance) {        HashMap map = null;        if (fontSpecific)            map = cmap10;        else            map = cmap31;        if (map == null)            return false;        int m[] = null;        if (fontSpecific) {            if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000)                m = (int[])map.get(new Integer(c & 0xff));            else                return false;        }        else            m = (int[])map.get(new Integer(c));        if (m == null)            return false;        else            m[1] = advance;        return true;    }        public int[] getCharBBox(char c) {        if (bboxes == null)            return null;        HashMap map = null;        if (fontSpecific)            map = cmap10;        else            map = cmap31;        if (map == null)            return null;        int m[] = null;        if (fontSpecific) {            if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000)                m = (int[])map.get(new Integer(c & 0xff));            else                return null;        }        else            m = (int[])map.get(new Integer(c));        if (m == null)            return null;        return bboxes[m[0]];    }}

⌨️ 快捷键说明

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