📄 chunk.java
字号:
/* * $Id: Chunk.java 3427 2008-05-24 18:32:31Z xlv $ * * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above. If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text;import java.awt.Color;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import com.lowagie.text.pdf.HyphenationEvent;import com.lowagie.text.pdf.PdfAction;import com.lowagie.text.pdf.PdfAnnotation;import com.lowagie.text.pdf.PdfContentByte;import com.lowagie.text.pdf.draw.DrawInterface;/** * This is the smallest significant part of text that can be added to a * document. * <P> * Most elements can be divided in one or more <CODE>Chunk</CODE>s. A chunk * is a <CODE>String</CODE> with a certain <CODE>Font</CODE>. All other * layout parameters should be defined in the object to which this chunk of text * is added. * <P> * Example: <BLOCKQUOTE> * * <PRE> * * <STRONG>Chunk chunk = new Chunk("Hello world", * FontFactory.getFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 0, * 0))); </STRONG> document.add(chunk); * * </PRE> * * </BLOCKQUOTE> */public class Chunk implements Element { // public static membervariables /** The character stand in for an image or a separator. */ public static final String OBJECT_REPLACEMENT_CHARACTER = "\ufffc"; /** This is a Chunk containing a newline. */ public static final Chunk NEWLINE = new Chunk("\n"); /** This is a Chunk containing a newpage. */ public static final Chunk NEXTPAGE = new Chunk(""); static { NEXTPAGE.setNewPage(); } // member variables /** This is the content of this chunk of text. */ protected StringBuffer content = null; /** This is the <CODE>Font</CODE> of this chunk of text. */ protected Font font = null; /** Contains some of the attributes for this Chunk. */ protected HashMap attributes = null; // constructors /** * Empty constructor. */ public Chunk() { this.content = new StringBuffer(); this.font = new Font(); } /** * A <CODE>Chunk</CODE> copy constructor. * @param ck the <CODE>Chunk</CODE> to be copied */ public Chunk(Chunk ck) { if (ck.content != null) { content = new StringBuffer(ck.content.toString()); } if (ck.font != null) { font = new Font(ck.font); } if (ck.attributes != null) { attributes = new HashMap(ck.attributes); } } /** * Constructs a chunk of text with a certain content and a certain <CODE> * Font</CODE>. * * @param content * the content * @param font * the font */ public Chunk(String content, Font font) { this.content = new StringBuffer(content); this.font = font; } /** * Constructs a chunk of text with a certain content, without specifying a * <CODE>Font</CODE>. * * @param content * the content */ public Chunk(String content) { this(content, new Font()); } /** * Constructs a chunk of text with a char and a certain <CODE>Font</CODE>. * * @param c * the content * @param font * the font */ public Chunk(char c, Font font) { this.content = new StringBuffer(); this.content.append(c); this.font = font; } /** * Constructs a chunk of text with a char, without specifying a <CODE>Font * </CODE>. * * @param c * the content */ public Chunk(char c) { this(c, new Font()); } /** * Constructs a chunk containing an <CODE>Image</CODE>. * * @param image * the image * @param offsetX * the image offset in the x direction * @param offsetY * the image offset in the y direction */ public Chunk(Image image, float offsetX, float offsetY) { this(OBJECT_REPLACEMENT_CHARACTER, new Font()); Image copyImage = Image.getInstance(image); copyImage.setAbsolutePosition(Float.NaN, Float.NaN); setAttribute(IMAGE, new Object[] { copyImage, new Float(offsetX), new Float(offsetY), Boolean.FALSE }); } /** * Key for drawInterface of the Separator. * @since 2.1.2 */ public static final String SEPARATOR = "SEPARATOR"; /** * Creates a separator Chunk. * Note that separator chunks can't be used in combination with tab chunks! * @param separator the drawInterface to use to draw the separator. * @since 2.1.2 */ public Chunk(DrawInterface separator) { this(separator, false); } /** * Creates a separator Chunk. * Note that separator chunks can't be used in combination with tab chunks! * @param separator the drawInterface to use to draw the separator. * @param vertical true if this is a vertical separator * @since 2.1.2 */ public Chunk(DrawInterface separator, boolean vertical) { this(OBJECT_REPLACEMENT_CHARACTER, new Font()); setAttribute(SEPARATOR, new Object[] {separator, Boolean.valueOf(vertical)}); } /** * Key for drawInterface of the tab. * @since 2.1.2 */ public static final String TAB = "TAB"; /** * Creates a tab Chunk. * Note that separator chunks can't be used in combination with tab chunks! * @param separator the drawInterface to use to draw the tab. * @param tabPosition an X coordinate that will be used as start position for the next Chunk. * @since 2.1.2 */ public Chunk(DrawInterface separator, float tabPosition) { this(separator, tabPosition, false); } /** * Creates a tab Chunk. * Note that separator chunks can't be used in combination with tab chunks! * @param separator the drawInterface to use to draw the tab. * @param tabPosition an X coordinate that will be used as start position for the next Chunk. * @param newline if true, a newline will be added if the tabPosition has already been reached. * @since 2.1.2 */ public Chunk(DrawInterface separator, float tabPosition, boolean newline) { this(OBJECT_REPLACEMENT_CHARACTER, new Font()); if (tabPosition < 0) { throw new IllegalArgumentException("A tab position may not be lower than 0; yours is " + tabPosition); } setAttribute(TAB, new Object[] {separator, new Float(tabPosition), Boolean.valueOf(newline), new Float(0)}); } /** * Constructs a chunk containing an <CODE>Image</CODE>. * * @param image * the image * @param offsetX * the image offset in the x direction * @param offsetY * the image offset in the y direction * @param changeLeading * true if the leading has to be adapted to the image */ public Chunk(Image image, float offsetX, float offsetY, boolean changeLeading) { this(OBJECT_REPLACEMENT_CHARACTER, new Font()); setAttribute(IMAGE, new Object[] { image, new Float(offsetX), new Float(offsetY), Boolean.valueOf(changeLeading) }); } // implementation of the Element-methods /** * Processes the element by adding it (or the different parts) to an <CODE> * ElementListener</CODE>. * * @param listener * an <CODE>ElementListener</CODE> * @return <CODE>true</CODE> if the element was processed successfully */ public boolean process(ElementListener listener) { try { return listener.add(this); } catch (DocumentException de) { return false; } } /** * Gets the type of the text element. * * @return a type */ public int type() { return Element.CHUNK; } /** * Gets all the chunks in this element. * * @return an <CODE>ArrayList</CODE> */ public ArrayList getChunks() { ArrayList tmp = new ArrayList(); tmp.add(this); return tmp; } // methods that change the member variables /** * appends some text to this <CODE>Chunk</CODE>. * * @param string * <CODE>String</CODE> * @return a <CODE>StringBuffer</CODE> */ public StringBuffer append(String string) { return content.append(string); } /** * Sets the font of this <CODE>Chunk</CODE>. * * @param font * a <CODE>Font</CODE> */ public void setFont(Font font) { this.font = font; } // methods to retrieve information /** * Gets the font of this <CODE>Chunk</CODE>. * * @return a <CODE>Font</CODE> */ public Font getFont() { return font; } /** * Returns the content of this <CODE>Chunk</CODE>. * * @return a <CODE>String</CODE> */ public String getContent() { return content.toString(); } /** * Returns the content of this <CODE>Chunk</CODE>. * * @return a <CODE>String</CODE> */ public String toString() { return getContent(); } /** * Checks is this <CODE>Chunk</CODE> is empty. * * @return <CODE>false</CODE> if the Chunk contains other characters than * space. */ public boolean isEmpty() { return (content.toString().trim().length() == 0) && (content.toString().indexOf("\n") == -1) && (attributes == null); } /** * Gets the width of the Chunk in points. * * @return a width in points */ public float getWidthPoint() { if (getImage() != null) { return getImage().getScaledWidth(); } return font.getCalculatedBaseFont(true).getWidthPoint(getContent(), font.getCalculatedSize()) * getHorizontalScaling(); } // attributes /** * Checks the attributes of this <CODE>Chunk</CODE>. * * @return false if there aren't any. */ public boolean hasAttributes() { return attributes != null; } /** * Gets the attributes for this <CODE>Chunk</CODE>. * <P> * It may be null. * * @return the attributes for this <CODE>Chunk</CODE> */ public HashMap getAttributes() { return attributes; } /** * Sets the attributes all at once. * @param attributes the attributes of a Chunk */ public void setAttributes(HashMap attributes) { this.attributes = attributes; } /** * Sets an arbitrary attribute. * * @param name * the key for the attribute * @param obj
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -