📄 pdfchunk.java
字号:
String pre = hyphenationEvent.getHyphenatedWordPre(value.substring(lastSpace, wordIdx), font.getFont(), font.size(), width - lastSpaceWidth); String post = hyphenationEvent.getHyphenatedWordPost(); if (pre.length() > 0) { String returnValue = post + value.substring(wordIdx); value = trim(value.substring(0, lastSpace) + pre); PdfChunk pc = new PdfChunk(returnValue, this); return pc; } } } String returnValue = value.substring(splitPosition); value = trim(value.substring(0, splitPosition)); PdfChunk pc = new PdfChunk(returnValue, this); 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(); char character; while (currentPosition < length) { // the width of every character is added to the currentWidth character = value.charAt(currentPosition); currentWidth += font.width(character); if (currentWidth > width) break; 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; } 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 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(); }/** * Checks if a character can be used to split a <CODE>PdfString</CODE>. * <P> * for the moment every character less than or equal to SPACE and the character '-' are 'splitCharacters'. * * @param start start position in the array * @param current current position in the array * @param end end position in the array * @param cc the character array that has to be checked * @param ck chunk array * @return <CODE>true</CODE> if the character can be used to split a string, <CODE>false</CODE> otherwise */ public boolean isSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) { char c; if (ck == null) c = cc[current]; else c = ck[Math.min(current, ck.length - 1)].getUnicodeEquivalent(cc[current]); if (c <= ' ' || c == '-' || c == '\u2010') { return true; } if (c < 0x2002) return false; return ((c >= 0x2002 && c <= 0x200b) || (c >= 0x2e80 && c < 0xd7a0) || (c >= 0xf900 && c < 0xfb00) || (c >= 0xfe30 && c < 0xfe50) || (c >= 0xff61 && c < 0xffa0)); } 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(char c) { if (noPrint(c)) return 0; return font.width(c); } public static boolean noPrint(char c) { return ((c >= 0x200b && c <= 0x200f) || (c >= 0x202a && c <= 0x202e)); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -