📄 chunk.java
字号:
/*
* $Id: Chunk.java 2748 2007-05-12 15:11:48Z blowagie $
* $Name$
*
* 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 java.util.Hashtable;
import java.util.Set;
import com.lowagie.text.pdf.HyphenationEvent;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
/**
* 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. */
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 });
}
/**
* 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), new Boolean(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 an arbitrary attribute.
*
* @param name
* the key for the attribute
* @param obj
* the value of the attribute
* @return this <CODE>Chunk</CODE>
*/
private Chunk setAttribute(String name, Object obj) {
if (attributes == null)
attributes = new HashMap();
attributes.put(name, obj);
return this;
}
// the attributes are ordered as they appear in the book 'iText in Action'
/** Key for text horizontal scaling. */
public static final String HSCALE = "HSCALE";
/**
* Sets the text horizontal scaling. A value of 1 is normal and a value of
* 0.5f shrinks the text to half it's width.
*
* @param scale
* the horizontal scaling factor
* @return this <CODE>Chunk</CODE>
*/
public Chunk setHorizontalScaling(float scale) {
return setAttribute(HSCALE, new Float(scale));
}
/**
* Gets the horizontal scaling.
*
* @return a percentage in float
*/
public float getHorizontalScaling() {
if (attributes == null)
return 1f;
Float f = (Float) attributes.get(HSCALE);
if (f == null)
return 1f;
return f.floatValue();
}
/** Key for underline. */
public static final String UNDERLINE = "UNDERLINE";
/**
* Sets an horizontal line that can be an underline or a strikethrough.
* Actually, the line can be anywhere vertically and has always the <CODE>
* Chunk</CODE> width. Multiple call to this method will produce multiple
* lines.
*
* @param thickness
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -