pdfchunk.java

来自「源码包含生成 PDF 和 HTML 的类库」· Java 代码 · 共 846 行 · 第 1/2 页

JAVA
846
字号
        return pc;    }    /** * Truncates this <CODE>PdfChunk</CODE> if it's too long for the given width. * <P> * Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated. * * @param		width		a given width * @return		the <CODE>PdfChunk</CODE> that doesn't fit into the width. */        PdfChunk truncate(float width) {        if (image != null) {            if (image.getScaledWidth() > width) {                PdfChunk pc = new PdfChunk("", this);                value = "";                attributes.remove(Chunk.IMAGE);                image = null;                font = PdfFont.getDefaultFont();                return pc;            }            else                return null;        }                int currentPosition = 0;        float currentWidth = 0;                // it's no use trying to split if there isn't even enough place for a space        if (width < font.width()) {            String returnValue = value.substring(1);            value = value.substring(0, 1);            PdfChunk pc = new PdfChunk(returnValue, this);            return pc;        }                // loop over all the characters of a string        // or until the totalWidth is reached        int length = value.length();        boolean surrogate = false;        char character;        while (currentPosition < length) {            // the width of every character is added to the currentWidth            surrogate = Utilities.isSurrogatePair(value, currentPosition);            if (surrogate)                currentWidth += font.width(Utilities.convertToUtf32(value, currentPosition));            else                currentWidth += font.width(value.charAt(currentPosition));            if (currentWidth > width)                break;            if (surrogate)                currentPosition++;            currentPosition++;        }                // if all the characters fit in the total width, null is returned (there is no overflow)        if (currentPosition == length) {            return null;        }                // otherwise, the string has to be truncated        //currentPosition -= 2;        // we have to chop off minimum 1 character from the chunk        if (currentPosition == 0) {            currentPosition = 1;            if (surrogate)                ++currentPosition;        }        String returnValue = value.substring(currentPosition);        value = value.substring(0, currentPosition);        PdfChunk pc = new PdfChunk(returnValue, this);        return pc;    }        // methods to retrieve the membervariables    /** * Returns the font of this <CODE>Chunk</CODE>. * * @return	a <CODE>PdfFont</CODE> */        PdfFont font() {        return font;    }    /** * Returns the color of this <CODE>Chunk</CODE>. * * @return	a <CODE>Color</CODE> */        Color color() {        return (Color)noStroke.get(Chunk.COLOR);    }    /** * Returns the width of this <CODE>PdfChunk</CODE>. * * @return	a width */        float width() {        return font.width(value);    }    /** * Checks if the <CODE>PdfChunk</CODE> split was caused by a newline. * @return <CODE>true</CODE> if the <CODE>PdfChunk</CODE> split was caused by a newline. */        public boolean isNewlineSplit()    {        return newlineSplit;    }    /** * Gets the width of the <CODE>PdfChunk</CODE> taking into account the * extra character and word spacing. * @param charSpacing the extra character spacing * @param wordSpacing the extra word spacing * @return the calculated width */        public float getWidthCorrected(float charSpacing, float wordSpacing)    {        if (image != null) {            return image.getScaledWidth() + charSpacing;        }        int numberOfSpaces = 0;        int idx = -1;        while ((idx = value.indexOf(' ', idx + 1)) >= 0)            ++numberOfSpaces;        return width() + (value.length() * charSpacing + numberOfSpaces * wordSpacing);    }        /**     * Gets the text displacement relative to the baseline.     * @return a displacement in points     */    public float getTextRise() {    	Float f = (Float) getAttribute(Chunk.SUBSUPSCRIPT);    	if (f != null) {    		return f.floatValue();    	}    	return 0.0f;    }    /** * Trims the last space. * @return the width of the space trimmed, otherwise 0 */        public float trimLastSpace()    {        BaseFont ft = font.getFont();        if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {            if (value.length() > 1 && value.endsWith("\u0001")) {                value = value.substring(0, value.length() - 1);                return font.width('\u0001');            }        }        else {            if (value.length() > 1 && value.endsWith(" ")) {                value = value.substring(0, value.length() - 1);                return font.width(' ');            }        }        return 0;    }        public float trimFirstSpace()    {        BaseFont ft = font.getFont();        if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {            if (value.length() > 1 && value.startsWith("\u0001")) {                value = value.substring(1);                return font.width('\u0001');            }        }        else {            if (value.length() > 1 && value.startsWith(" ")) {                value = value.substring(1);                return font.width(' ');            }        }        return 0;    }    /** * Gets an attribute. The search is made in <CODE>attributes</CODE> * and <CODE>noStroke</CODE>. * @param name the attribute key * @return the attribute value or null if not found */        Object getAttribute(String name)    {        if (attributes.containsKey(name))            return attributes.get(name);        return noStroke.get(name);    }    /** *Checks if the attribute exists. * @param name the attribute key * @return <CODE>true</CODE> if the attribute exists */        boolean isAttribute(String name)    {        if (attributes.containsKey(name))            return true;        return noStroke.containsKey(name);    }    /** * Checks if this <CODE>PdfChunk</CODE> needs some special metrics handling. * @return <CODE>true</CODE> if this <CODE>PdfChunk</CODE> needs some special metrics handling. */        boolean isStroked()    {        return (!attributes.isEmpty());    }        /**     * Checks if this <CODE>PdfChunk</CODE> is a Separator Chunk.     * @return	true if this chunk is a separator.     * @since	2.1.2     */    boolean isSeparator() {    	return isAttribute(Chunk.SEPARATOR);    }        /**     * Checks if this <CODE>PdfChunk</CODE> is a horizontal Separator Chunk.     * @return	true if this chunk is a horizontal separator.     * @since	2.1.2     */    boolean isHorizontalSeparator() {    	if (isAttribute(Chunk.SEPARATOR)) {    		Object[] o = (Object[])getAttribute(Chunk.SEPARATOR);    		return !((Boolean)o[1]).booleanValue();    	}    	return false;    }        /**     * Checks if this <CODE>PdfChunk</CODE> is a tab Chunk.     * @return	true if this chunk is a separator.     * @since	2.1.2     */    boolean isTab() {    	return isAttribute(Chunk.TAB);    }        /**     * Correction for the tab position based on the left starting position.     * @param	newValue	the new value for the left X.     * @since	2.1.2     */    void adjustLeft(float newValue) {    	Object[] o = (Object[])attributes.get(Chunk.TAB);    	if (o != null) {    		attributes.put(Chunk.TAB, new Object[]{o[0], o[1], o[2], new Float(newValue)});    	}    }    /** * Checks if there is an image in the <CODE>PdfChunk</CODE>. * @return <CODE>true</CODE> if an image is present */        boolean isImage()    {        return image != null;    }    /** * Gets the image in the <CODE>PdfChunk</CODE>. * @return the image or <CODE>null</CODE> */        Image getImage()    {        return image;    }    /** * Sets the image offset in the x direction * @param  offsetX the image offset in the x direction */        void setImageOffsetX(float offsetX)    {        this.offsetX = offsetX;    }    /** * Gets the image offset in the x direction * @return the image offset in the x direction */        float getImageOffsetX()    {        return offsetX;    }    /** * Sets the image offset in the y direction * @param  offsetY the image offset in the y direction */        void setImageOffsetY(float offsetY)    {        this.offsetY = offsetY;    }    /** * Gets the image offset in the y direction * @return Gets the image offset in the y direction */        float getImageOffsetY()    {        return offsetY;    }    /** * sets the value. * @param value content of the Chunk */        void setValue(String value)    {        this.value = value;    }    /**     * @see java.lang.Object#toString()     */    public String toString() {        return value;    }    /**     * Tells you if this string is in Chinese, Japanese, Korean or Identity-H.     * @return true if the Chunk has a special encoding     */        boolean isSpecialEncoding() {        return encoding.equals(CJKFont.CJK_ENCODING) || encoding.equals(BaseFont.IDENTITY_H);    }        /**     * Gets the encoding of this string.     *     * @return		a <CODE>String</CODE>     */        String getEncoding() {        return encoding;    }    int length() {        return value.length();    }        int lengthUtf32() {        if (!BaseFont.IDENTITY_H.equals(encoding))            return value.length();        int total = 0;        int len = value.length();        for (int k = 0; k < len; ++k) {            if (Utilities.isSurrogateHigh(value.charAt(k)))                ++k;            ++total;        }        return total;    }        boolean isExtSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) {        return splitCharacter.isSplitCharacter(start, current, end, cc, ck);    }    /** * Removes all the <VAR>' '</VAR> and <VAR>'-'</VAR>-characters on the right of a <CODE>String</CODE>. * <P> * @param	string		the <CODE>String<CODE> that has to be trimmed. * @return	the trimmed <CODE>String</CODE> */        String trim(String string) {        BaseFont ft = font.getFont();        if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {            while (string.endsWith("\u0001")) {                string = string.substring(0, string.length() - 1);            }        }        else {            while (string.endsWith(" ") || string.endsWith("\t")) {                string = string.substring(0, string.length() - 1);            }        }        return string;    }    public boolean changeLeading() {        return changeLeading;    }        float getCharWidth(int c) {        if (noPrint(c))            return 0;        return font.width(c);    }        public static boolean noPrint(int c) {        return ((c >= 0x200b && c <= 0x200f) || (c >= 0x202a && c <= 0x202e));    }    }

⌨️ 快捷键说明

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