📄 pdfchunk.java
字号:
/*
* $Id: PdfChunk.java,v 1.24 2001/12/10 13:53:21 blowagie Exp $
* $Name: $
*
* Copyright 1999, 2000, 2001 by Bruno Lowagie.
*
* This library is free software; you can redistribute it and/or modify it
* 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.
*
* You should have received a copy of the GNU Library General Public License along
* with this library; if not, write to the Free Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*
* 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/
*
* ir-arch Bruno Lowagie,
* Adolf Baeyensstraat 121
* 9040 Sint-Amandsberg
* BELGIUM
* tel. +32 (0)9 228.10.97
* bruno@lowagie.com
*
*/
package com.lowagie.text.pdf;
import java.awt.Color;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.SplitCharacter;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
/**
* A <CODE>PdfChunk</CODE> is the PDF translation of a <CODE>Chunk</CODE>.
* <P>
* A <CODE>PdfChunk</CODE> is a <CODE>PdfString</CODE> in a certain
* <CODE>PdfFont</CODE> and <CODE>Color</CODE>.
*
* @see PdfString
* @see PdfFont
* @see com.lowagie.text.Chunk
* @see com.lowagie.text.Font
*/
class PdfChunk extends PdfString implements SplitCharacter{
/** The allowed attributes in variable <CODE>attributes</CODE>. */
private static final HashMap keysAttributes = new HashMap();
/** The allowed attributes in variable <CODE>noStroke</CODE>. */
private static final HashMap keysNoStroke = new HashMap();
static {
keysAttributes.put(Chunk.LINK, null);
keysAttributes.put(Chunk.STRIKETHRU, null);
keysAttributes.put(Chunk.UNDERLINE, null);
keysAttributes.put(Chunk.REMOTEGOTO, null);
keysAttributes.put(Chunk.LOCALGOTO, null);
keysAttributes.put(Chunk.LOCALDESTINATION, null);
keysAttributes.put(Chunk.GENERICTAG, null);
keysAttributes.put(Chunk.NEWPAGE, null);
keysAttributes.put(Chunk.IMAGE, null);
keysNoStroke.put(Chunk.SUBSUPSCRIPT, null);
keysNoStroke.put(Chunk.SPLITCHARACTER, null);
}
// membervariables
/** The font for this <CODE>PdfChunk</CODE>. */
protected PdfFont font;
/**
* Metric attributes.
* <P>
* This attributes require the mesurement of characters widths when rendering
* such as underline.
*/
protected HashMap attributes = new HashMap();
/**
* Non metric attributes.
* <P>
* This attributes do not require the mesurement of characters widths when rendering
* such as Color.
*/
protected HashMap noStroke = new HashMap();
/** <CODE>true</CODE> if the chunk split was cause by a newline. */
protected boolean newlineSplit;
/** The image in this <CODE>PdfChunk</CODE>, if it has one */
protected Image image;
/** The offset in the x direction for the image */
protected float offsetX;
/** The offset in the y direction for the image */
protected float offsetY;
// constructors
/**
* Constructs a <CODE>PdfChunk</CODE>-object.
*
* @param string the content of the <CODE>PdfChunk</CODE>-object
* @param font the <CODE>PdfFont</CODE>
* @param attributes the metrics attributes
* @param noStroke the non metric attributes
*/
private PdfChunk(String string, PdfFont font, HashMap attributes, HashMap noStroke) {
super(string);
this.font = font;
this.attributes = attributes;
this.noStroke = noStroke;
Object obj[] = (Object[])attributes.get(Chunk.IMAGE);
if (obj == null)
image = null;
else {
image = (Image)obj[0];
offsetX = ((Float)obj[1]).floatValue();
offsetY = ((Float)obj[2]).floatValue();
}
encoding = font.getFont().getEncoding();
}
/**
* Constructs a <CODE>PdfChunk</CODE>-object.
*
* @param chunk the original <CODE>Chunk</CODE>-object
* @param action the <CODE>PdfAction</CODE> if the <CODE>Chunk</CODE> comes from an <CODE>Anchor</CODE>
*/
PdfChunk(Chunk chunk, PdfAction action) {
super(chunk.content());
Font f = chunk.font();
float size = f.size();
if (size == Font.UNDEFINED)
size = 12;
BaseFont bf = f.getBaseFont();
if (bf == null) {
// translation of the font-family to a PDF font-family
int family;
int style = f.style();
if (style == Font.UNDEFINED) {
style = Font.NORMAL;
}
switch(f.family()) {
case Font.COURIER:
switch(style & Font.BOLDITALIC) {
case Font.BOLD:
family = PdfFont.COURIER_BOLD;
break;
case Font.ITALIC:
family = PdfFont.COURIER_OBLIQUE;
break;
case Font.BOLDITALIC:
family = PdfFont.COURIER_BOLDOBLIQUE;
break;
default:
case Font.NORMAL:
family = PdfFont.COURIER;
break;
}
break;
case Font.TIMES_NEW_ROMAN:
switch(style & Font.BOLDITALIC) {
case Font.BOLD:
family = PdfFont.TIMES_BOLD;
break;
case Font.ITALIC:
family = PdfFont.TIMES_ITALIC;
break;
case Font.BOLDITALIC:
family = PdfFont.TIMES_BOLDITALIC;
break;
default:
case Font.NORMAL:
family = PdfFont.TIMES_ROMAN;
break;
}
break;
case Font.SYMBOL:
family = PdfFont.SYMBOL;
break;
case Font.ZAPFDINGBATS:
family = PdfFont.ZAPFDINGBATS;
break;
default:
case Font.HELVETICA:
switch(style & Font.BOLDITALIC) {
case Font.BOLD:
family = PdfFont.HELVETICA_BOLD;
break;
case Font.ITALIC:
family = PdfFont.HELVETICA_OBLIQUE;
break;
case Font.BOLDITALIC:
family = PdfFont.HELVETICA_BOLDOBLIQUE;
break;
default:
case Font.NORMAL:
family = PdfFont.HELVETICA;
break;
}
break;
}
// creation of the PdfFont with the right size
font = new PdfFont(family, size);
}
else
font = new PdfFont(bf, size);
// other style possibilities
HashMap attr = chunk.getAttributes();
if (attr != null) {
for (Iterator i = attr.keySet().iterator(); i.hasNext();) {
Object name = i.next();
if (keysAttributes.containsKey(name)) {
attributes.put(name, attr.get(name));
}
else if (keysNoStroke.containsKey(name)) {
noStroke.put(name, attr.get(name));
}
}
if ("".equals(attr.get(Chunk.GENERICTAG))) {
attributes.put(Chunk.GENERICTAG, chunk.content());
}
}
if (f.isUnderlined())
attributes.put(Chunk.UNDERLINE, null);
if (f.isStrikethru())
attributes.put(Chunk.STRIKETHRU, null);
if (action != null)
attributes.put(Chunk.LINK, action);
// the color can't be stored in a PdfFont
noStroke.put(Chunk.COLOR, f.color());
noStroke.put(Chunk.ENCODING, font.getFont().getEncoding());
Object obj[] = (Object[])attributes.get(Chunk.IMAGE);
if (obj == null)
image = null;
else {
image = (Image)obj[0];
offsetX = ((Float)obj[1]).floatValue();
offsetY = ((Float)obj[2]).floatValue();
}
font.setImage(image);
encoding = font.getFont().getEncoding();
}
// methods
/**
* Splits 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 split(float width) {
newlineSplit = false;
if (image != null) {
if (image.scaledWidth() > width) {
PdfChunk pc = new PdfChunk("*", font, attributes, noStroke);
value = "";
attributes = new HashMap();
image = null;
font = new PdfFont(PdfFont.HELVETICA, 12);
return pc;
}
else
return null;
}
SplitCharacter splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER);
if (splitCharacter == null)
splitCharacter = this;
int currentPosition = 0;
int splitPosition = -1;
float currentWidth = 0;
// loop over all the characters of a string
// or until the totalWidth is reached
int length = value.length();
char character = 0;
while (currentPosition < length) {
// the width of every character is added to the currentWidth
character = value.charAt(currentPosition);
// if a newLine or carriageReturn is encountered
if (character == '\r' || character == '\n') {
newlineSplit = true;
int inc = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -