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

📄 basefont.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    
    /**
     * 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
     */
    abstract int getRawWidth(int c, String name);
    
    /**
     * Gets the kerning between two Unicode chars.
     * @param char1 the first char
     * @param char2 the second char
     * @return the kerning to be applied in normalized 1000 units
     */
    public abstract int getKerning(char char1, char char2);

    /**
     * Sets the kerning between two Unicode chars.
     * @param char1 the first char
     * @param char2 the second char
     * @param kern the kerning to apply in normalized 1000 units
     * @return <code>true</code> if the kerning was applied, <code>false</code> otherwise
     */
    public abstract boolean setKerning(char char1, char char2, int kern);
    
    /**
     * 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 (fastWinansi) {
            if (char1 < 128 || (char1 >= 160 && char1 <= 255))
                return widths[char1];
            return widths[PdfEncodings.winansi.get(char1)];
        }
        return getWidth(new String(new char[]{char1}));
    }
    
    /**
     * Gets the width of a <CODE>String</CODE> in normalized 1000 units.
     * @param text the <CODE>String</CODE> to get the witdth of
     * @return the width in normalized 1000 units
     */
    public int getWidth(String text) {
        int total = 0;
        if (fastWinansi) {
            int len = text.length();
            for (int k = 0; k < len; ++k) {
                char char1 = text.charAt(k);
                if (char1 < 128 || (char1 >= 160 && char1 <= 255))
                    total += widths[char1];
                else
                    total += widths[PdfEncodings.winansi.get(char1)];
            }
            return total;
        }
        else {
            byte mbytes[] = convertToBytes(text);
            for (int k = 0; k < mbytes.length; ++k)
                total += widths[0xff & mbytes[k]];
        }
        return total;
    }
    
/**
 * Gets the descent of a <CODE>String</CODE> in normalized 1000 units. The descent will always be
 * less than or equal to zero even if all the characters have an higher descent.
 * @param text the <CODE>String</CODE> to get the descent of
 * @return the dexcent in normalized 1000 units
 */
    public int getDescent(String text) {
        int min = 0;
        char chars[] = text.toCharArray();
        for (int k = 0; k < chars.length; ++k) {
            int bbox[] = getCharBBox(chars[k]);
            if (bbox != null && bbox[1] < min)
                min = bbox[1];
        }
        return min;
    }
    
/**
 * Gets the ascent of a <CODE>String</CODE> in normalized 1000 units. The ascent will always be
 * greater than or equal to zero even if all the characters have a lower ascent.
 * @param text the <CODE>String</CODE> to get the ascent of
 * @return the ascent in normalized 1000 units
 */
    public int getAscent(String text) {
        int max = 0;
        char chars[] = text.toCharArray();
        for (int k = 0; k < chars.length; ++k) {
            int bbox[] = getCharBBox(chars[k]);
            if (bbox != null && bbox[3] > max)
                max = bbox[3];
        }
        return max;
    }

/**
 * Gets the descent of a <CODE>String</CODE> in points. The descent will always be
 * less than or equal to zero even if all the characters have an higher descent.
 * @param text the <CODE>String</CODE> to get the descent of
 * @param fontSize the size of the font
 * @return the dexcent in points
 */
    public float getDescentPoint(String text, float fontSize)
    {
        return (float)getDescent(text) * 0.001f * fontSize;
    }
    
/**
 * Gets the ascent of a <CODE>String</CODE> in points. The ascent will always be
 * greater than or equal to zero even if all the characters have a lower ascent.
 * @param text the <CODE>String</CODE> to get the ascent of
 * @param fontSize the size of the font
 * @return the ascent in points
 */
    public float getAscentPoint(String text, float fontSize)
    {
        return (float)getAscent(text) * 0.001f * fontSize;
    }
// ia>    
    
    /**
     * Gets the width of a <CODE>String</CODE> in points taking kerning
     * into account.
     * @param text the <CODE>String</CODE> to get the witdth of
     * @param fontSize the font size
     * @return the width in points
     */
    public float getWidthPointKerned(String text, float fontSize) {
        float size = (float)getWidth(text) * 0.001f * fontSize;
        if (!hasKernPairs())
            return size;
        int len = text.length() - 1;
        int kern = 0;
        char c[] = text.toCharArray();
        for (int k = 0; k < len; ++k) {
            kern += getKerning(c[k], c[k + 1]);
        }
        return size + kern * 0.001f * fontSize;
    }
    
    /**
     * Gets the width of a <CODE>String</CODE> in points.
     * @param text the <CODE>String</CODE> to get the witdth of
     * @param fontSize the font size
     * @return the width in points
     */
    public float getWidthPoint(String text, float fontSize) {
        return (float)getWidth(text) * 0.001f * fontSize;
    }
    
    /**
     * Gets the width of a <CODE>char</CODE> in points.
     * @param char1 the <CODE>char</CODE> to get the witdth of
     * @param fontSize the font size
     * @return the width in points
     */
    public float getWidthPoint(char char1, float fontSize) {
        return getWidth(char1) * 0.001f * fontSize;
    }
    
    /**
     * Converts a <CODE>String</CODE> to a </CODE>byte</CODE> array according
     * to the font's encoding.
     * @param text the <CODE>String</CODE> to be converted
     * @return an array of <CODE>byte</CODE> representing the conversion according to the font's encoding
     */
    byte[] convertToBytes(String text) {
        if (directTextToByte)
            return PdfEncodings.convertToBytes(text, null);
        if (specialMap != null) {
            byte[] b = new byte[text.length()];
            int ptr = 0;
            int length = text.length();
            for (int k = 0; k < length; ++k) {
                char c = text.charAt(k);
                if (specialMap.containsKey((int)c))
                    b[ptr++] = (byte)specialMap.get((int)c);
            }
            if (ptr < length) {
                byte[] b2 = new byte[ptr];
                System.arraycopy(b, 0, b2, 0, ptr);
                return b2;
            }
            else
                return b;
        }
        return PdfEncodings.convertToBytes(text, encoding);
    }
    
    /** 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
     */
    abstract void writeFont(PdfWriter writer, PdfIndirectReference ref, Object params[]) throws DocumentException, IOException;
    
    /** Gets the encoding used to convert <CODE>String</CODE> into <CODE>byte[]</CODE>.
     * @return the encoding name
     */
    public String getEncoding() {
        return encoding;
    }
    
    /** Gets the font parameter identified by <CODE>key</CODE>. Valid values
     * for <CODE>key</CODE> are <CODE>ASCENT</CODE>, <CODE>AWT_ASCENT</CODE>, <CODE>CAPHEIGHT</CODE>, 
     * <CODE>DESCENT</CODE>, <CODE>AWT_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 abstract float getFontDescriptor(int key, float fontSize);
    
    /** Gets the font type. The font types can be: FONT_TYPE_T1,
     * FONT_TYPE_TT, FONT_TYPE_CJK and FONT_TYPE_TTUNI.
     * @return the font type
     */
    public int getFontType() {
        return fontType;
    }
    
    /** Gets the embedded flag.
     * @return <CODE>true</CODE> if the font is embedded.
     */
    public boolean isEmbedded() {
        return embedded;
    }
    
    /** Gets the symbolic flag of the font.
     * @return <CODE>true</CODE> if the font is symbolic
     */
    public boolean isFontSpecific() {
        return fontSpecific;
    }
    
    /** Creates a unique subset prefix to be added to the font name when the font is embedded and subset.
     * @return the subset prefix
     */
    public static String createSubsetPrefix() {
        String s = "";
        for (int k = 0; k < 6; ++k)
            s += (char)(Math.random() * 26 + 'A');
        return s + "+";
    }
    
    /** Gets the Unicode character corresponding to the byte output to the pdf stream.
     * @param index the byte index
     * @return the Unicode character
     */
    char getUnicodeDifferences(int index) {
        return unicodeDifferences[index];
    }
    
    /** Gets the postscript font name.
     * @return the postscript font name
     */
    public abstract String getPostscriptFontName();
    
    /**
     * Sets the font name that will appear in the pdf font dictionary.
     * Use with care as it can easily make a font unreadable if not embedded.
     * @param name the new font name
     */    
    public abstract void setPostscriptFontName(String name);
    
    /** 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 abstract String[][] getFullFontName();
    
    /** 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}.
     * @param name the name of the font
     * @param encoding the encoding of the font
     * @param ttfAfm the true type font or the afm in a byte array
     * @throws DocumentException on error
     * @throws IOException on error
     * @return the full name of the font
     */    
    public static String[][] getFullFontName(String name, String encoding, byte ttfAfm[]) throws DocumentException, IOException {
        String nameBase = getBaseName(name);
        BaseFont fontBuilt = null;
        if (nameBase.toLowerCase().endsWith(".ttf") || nameBase.toLowerCase().endsWith(".otf") || nameBase.toLowerCase().indexOf(".ttc,") > 0)
            fontBuilt = new TrueTypeFont(name, CP1252, false, ttfAfm, true);
        else
            fontBuilt = createFont(name, encoding, false, false, ttfAfm, null);
        return fontBuilt.getFullFontName();
    }
    
    /** Gets all the names from the font. Only the required tables are read.
     * @param name the name of the font
     * @param encoding the encoding of the font
     * @param ttfAfm the true type font or the afm in a byte array
     * @throws DocumentException on error
     * @throws IOException on error
     * @return an array of Object[] built with {getPostscriptFontName(), getFamilyFontName(), getFullFontName()}
     */    
    public static Object[] getAllFontNames(String name, String encoding, byte ttfAfm[]) throws DocumentException, IOException {
        String nameBase = getBaseName(name);
        BaseFont fontBuilt = null;
        if (nameBase.toLowerCase().endsWith(".ttf") || nameBase.toLowerCase().endsWith(".otf") || nameBase.toLowerCase().indexOf(".ttc,") > 0)
            fontBuilt = new TrueTypeFont(name, CP1252, false, ttfAfm, true);
        else
            fontBuilt = createFont(name, encoding, false, false, ttfAfm, null);
        return new Object[]{fontBuilt.getPostscriptFontName(), fontBuilt.getFamilyFontName(), fontBuilt.getFullFontName()};
    }
    
    /** 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 {"", "", "",

⌨️ 快捷键说明

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