| Constructor: |
PdfChunk(String string,
PdfChunk other) {
// constructors
thisChunk[0] = this;
value = string;
this.font = other.font;
this.attributes = other.attributes;
this.noStroke = other.noStroke;
this.baseFont = other.baseFont;
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();
changeLeading = ((Boolean)obj[3]).booleanValue();
}
encoding = font.getFont().getEncoding();
splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER);
if (splitCharacter == null)
splitCharacter = DefaultSplitCharacter.DEFAULT;
}
Constructs a PdfChunk-object. Parameters:
string - the content of the PdfChunk-object
other - Chunk with the same style you want for the new Chunk
|
PdfChunk(Chunk chunk,
PdfAction action) {
thisChunk[0] = this;
value = chunk.getContent();
Font f = chunk.getFont();
float size = f.getSize();
if (size == Font.UNDEFINED)
size = 12;
baseFont = f.getBaseFont();
int style = f.getStyle();
if (style == Font.UNDEFINED) {
style = Font.NORMAL;
}
if (baseFont == null) {
// translation of the font-family to a PDF font-family
baseFont = f.getCalculatedBaseFont(false);
}
else {
// bold simulation
if ((style & Font.BOLD) != 0)
attributes.put(Chunk.TEXTRENDERMODE, new Object[]{new Integer(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float(size / 30f), null});
// italic simulation
if ((style & Font.ITALIC) != 0)
attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE});
}
font = new PdfFont(baseFont, size);
// other style possibilities
HashMap attr = chunk.getAttributes();
if (attr != null) {
for (Iterator i = attr.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
Object name = entry.getKey();
if (keysAttributes.containsKey(name)) {
attributes.put(name, entry.getValue());
}
else if (keysNoStroke.containsKey(name)) {
noStroke.put(name, entry.getValue());
}
}
if ("".equals(attr.get(Chunk.GENERICTAG))) {
attributes.put(Chunk.GENERICTAG, chunk.getContent());
}
}
if (f.isUnderlined()) {
Object obj[] = {null, new float[]{0, 1f / 15, 0, -1f / 3, 0}};
Object unders[][] = Utilities.addToArray((Object[][])attributes.get(Chunk.UNDERLINE), obj);
attributes.put(Chunk.UNDERLINE, unders);
}
if (f.isStrikethru()) {
Object obj[] = {null, new float[]{0, 1f / 15, 0, 1f / 3, 0}};
Object unders[][] = Utilities.addToArray((Object[][])attributes.get(Chunk.UNDERLINE), obj);
attributes.put(Chunk.UNDERLINE, unders);
}
if (action != null)
attributes.put(Chunk.ACTION, action);
// the color can't be stored in a PdfFont
noStroke.put(Chunk.COLOR, f.getColor());
noStroke.put(Chunk.ENCODING, font.getFont().getEncoding());
Object obj[] = (Object[])attributes.get(Chunk.IMAGE);
if (obj == null) {
image = null;
}
else {
attributes.remove(Chunk.HSCALE); // images are scaled in other ways
image = (Image)obj[0];
offsetX = ((Float)obj[1]).floatValue();
offsetY = ((Float)obj[2]).floatValue();
changeLeading = ((Boolean)obj[3]).booleanValue();
}
font.setImage(image);
Float hs = (Float)attributes.get(Chunk.HSCALE);
if (hs != null)
font.setHorizontalScaling(hs.floatValue());
encoding = font.getFont().getEncoding();
splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER);
if (splitCharacter == null)
splitCharacter = DefaultSplitCharacter.DEFAULT;
}
Constructs a PdfChunk-object. Parameters:
chunk - the original Chunk-object
action - the PdfAction if the Chunk comes from an Anchor
|
| Method from com.lowagie.text.pdf.PdfChunk Detail: |
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)});
}
}
Correction for the tab position based on the left starting position. |
public boolean changeLeading() {
return changeLeading;
}
|
Color color() {
return (Color)noStroke.get(Chunk.COLOR);
}
Returns the color of this Chunk. |
PdfFont font() {
return font;
}
Returns the font of this Chunk. |
Object getAttribute(String name) {
if (attributes.containsKey(name))
return attributes.get(name);
return noStroke.get(name);
}
Gets an attribute. The search is made in attributes
and noStroke. |
float getCharWidth(int c) {
if (noPrint(c))
return 0;
return font.width(c);
}
|
String getEncoding() {
return encoding;
}
Gets the encoding of this string. |
Image getImage() {
return image;
}
Gets the image in the PdfChunk. |
float getImageOffsetX() {
return offsetX;
}
Gets the image offset in the x direction |
float getImageOffsetY() {
return offsetY;
}
Gets the image offset in the y direction |
public float getTextRise() {
Float f = (Float) getAttribute(Chunk.SUBSUPSCRIPT);
if (f != null) {
return f.floatValue();
}
return 0.0f;
}
Gets the text displacement relative to the baseline. |
public int getUnicodeEquivalent(int c) {
return baseFont.getUnicodeEquivalent(c);
}
Gets the Unicode equivalent to a CID.
The (inexistent) CID is translated as '\n'.
It has only meaning with CJK fonts with Identity encoding. |
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 width of the PdfChunk taking into account the
extra character and word spacing. |
protected int getWord(String text,
int start) {
int len = text.length();
while (start < len) {
if (!Character.isLetter(text.charAt(start)))
break;
++start;
}
return start;
}
|
boolean isAttribute(String name) {
if (attributes.containsKey(name))
return true;
return noStroke.containsKey(name);
}
Checks if the attribute exists. |
boolean isExtSplitCharacter(int start,
int current,
int end,
char[] cc,
PdfChunk[] ck) {
return splitCharacter.isSplitCharacter(start, current, end, cc, ck);
}
|
boolean isHorizontalSeparator() {
if (isAttribute(Chunk.SEPARATOR)) {
Object[] o = (Object[])getAttribute(Chunk.SEPARATOR);
return !((Boolean)o[1]).booleanValue();
}
return false;
}
Checks if this PdfChunk is a horizontal Separator Chunk. |
boolean isImage() {
return image != null;
}
Checks if there is an image in the PdfChunk. |
public boolean isNewlineSplit() {
return newlineSplit;
}
Checks if the PdfChunk split was caused by a newline. |
boolean isSeparator() {
return isAttribute(Chunk.SEPARATOR);
}
Checks if this PdfChunk is a Separator Chunk. |
boolean isSpecialEncoding() {
return encoding.equals(CJKFont.CJK_ENCODING) || encoding.equals(BaseFont.IDENTITY_H);
}
Tells you if this string is in Chinese, Japanese, Korean or Identity-H. |
boolean isStroked() {
return (!attributes.isEmpty());
}
Checks if this PdfChunk needs some special metrics handling. |
boolean isTab() {
return isAttribute(Chunk.TAB);
}
Checks if this PdfChunk is a tab Chunk. |
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;
}
|
public static boolean noPrint(int c) {
return ((c >= 0x200b && c < = 0x200f) || (c >= 0x202a && c < = 0x202e));
}
|
void setImageOffsetX(float offsetX) {
this.offsetX = offsetX;
}
Sets the image offset in the x direction |
void setImageOffsetY(float offsetY) {
this.offsetY = offsetY;
}
Sets the image offset in the y direction |
void setValue(String value) {
this.value = value;
}
|
PdfChunk split(float width) {
newlineSplit = false;
if (image != null) {
if (image.getScaledWidth() > width) {
PdfChunk pc = new PdfChunk(Chunk.OBJECT_REPLACEMENT_CHARACTER, this);
value = "";
attributes = new HashMap();
image = null;
font = PdfFont.getDefaultFont();
return pc;
}
else
return null;
}
HyphenationEvent hyphenationEvent = (HyphenationEvent)noStroke.get(Chunk.HYPHENATION);
int currentPosition = 0;
int splitPosition = -1;
float currentWidth = 0;
// loop over all the characters of a string
// or until the totalWidth is reached
int lastSpace = -1;
float lastSpaceWidth = 0;
int length = value.length();
char valueArray[] = value.toCharArray();
char character = 0;
BaseFont ft = font.getFont();
boolean surrogate = false;
if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ") != ' ") {
while (currentPosition < length) {
// the width of every character is added to the currentWidth
char cidChar = valueArray[currentPosition];
character = (char)ft.getUnicodeEquivalent(cidChar);
// if a newLine or carriageReturn is encountered
if (character == '\n") {
newlineSplit = true;
String returnValue = value.substring(currentPosition + 1);
value = value.substring(0, currentPosition);
if (value.length() < 1) {
value = "\u0001";
}
PdfChunk pc = new PdfChunk(returnValue, this);
return pc;
}
currentWidth += font.width(cidChar);
if (character == ' ") {
lastSpace = currentPosition + 1;
lastSpaceWidth = currentWidth;
}
if (currentWidth > width)
break;
// if a split-character is encountered, the splitPosition is altered
if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, thisChunk))
splitPosition = currentPosition + 1;
currentPosition++;
}
}
else {
while (currentPosition < length) {
// the width of every character is added to the currentWidth
character = valueArray[currentPosition];
// if a newLine or carriageReturn is encountered
if (character == '\r" || character == '\n") {
newlineSplit = true;
int inc = 1;
if (character == '\r" && currentPosition + 1 < length && valueArray[currentPosition + 1] == '\n")
inc = 2;
String returnValue = value.substring(currentPosition + inc);
value = value.substring(0, currentPosition);
if (value.length() < 1) {
value = " ";
}
PdfChunk pc = new PdfChunk(returnValue, this);
return pc;
}
surrogate = Utilities.isSurrogatePair(valueArray, currentPosition);
if (surrogate)
currentWidth += font.width(Utilities.convertToUtf32(valueArray[currentPosition], valueArray[currentPosition + 1]));
else
currentWidth += font.width(character);
if (character == ' ") {
lastSpace = currentPosition + 1;
lastSpaceWidth = currentWidth;
}
if (surrogate)
currentPosition++;
if (currentWidth > width)
break;
// if a split-character is encountered, the splitPosition is altered
if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, null))
splitPosition = currentPosition + 1;
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
if (splitPosition < 0) {
String returnValue = value;
value = "";
PdfChunk pc = new PdfChunk(returnValue, this);
return pc;
}
if (lastSpace > splitPosition && splitCharacter.isSplitCharacter(0, 0, 1, singleSpace, null))
splitPosition = lastSpace;
if (hyphenationEvent != null && lastSpace >= 0 && lastSpace < currentPosition) {
int wordIdx = getWord(value, lastSpace);
if (wordIdx > lastSpace) {
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;
}
|
public String toString() {
return value;
}
|
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;
}
Removes all the ' ' and '-'-characters on the right of a String.
|
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;
}
|
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;
}
|
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;
}
|
float width() {
return font.width(value);
}
Returns the width of this PdfChunk. |