📄 pdfchunk.java
字号:
if (character == '\r' && currentPosition + 1 < length && value.charAt(currentPosition) == '\n')
inc = 2;
String returnValue = value.substring(currentPosition + inc);
value = value.substring(0, currentPosition);
if (value.length() < 1) {
value = " ";
}
setContent(value);
PdfChunk pc = new PdfChunk(returnValue, font, attributes, noStroke);
return pc;
}
currentWidth += font.width(character);
if (currentWidth > width)
break;
// if a split-character is encountered, the splitPosition is altered
if (splitCharacter.isSplitCharacter(character))
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 = "";
setContent(value);
PdfChunk pc = new PdfChunk(returnValue, font, attributes, noStroke);
return pc;
}
String returnValue = value.substring(splitPosition);
value = trim(value.substring(0, splitPosition));
setContent(value);
PdfChunk pc = new PdfChunk(returnValue, font, attributes, noStroke);
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.scaledWidth() > width) {
PdfChunk pc = new PdfChunk("", font, attributes, noStroke);
value = "";
attributes.remove(Chunk.IMAGE);
image = null;
font = new PdfFont(PdfFont.HELVETICA, 12);
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);
setContent(value);
PdfChunk pc = new PdfChunk(returnValue, font, attributes, noStroke);
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);
setContent(value);
PdfChunk pc = new PdfChunk(returnValue, font, attributes, noStroke);
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() {
if (image != null)
return image.scaledWidth();
return font.getFont().getWidthPoint(value, font.size());
}
/**
* 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 charracter 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.scaledWidth() + charSpacing;
}
int numberOfSpaces = 0;
int idx = -1;
while ((idx = value.indexOf(' ', idx + 1)) >= 0)
++numberOfSpaces;
return font.getFont().getWidthPoint(value, font.size()) + value.length() * charSpacing + numberOfSpaces * wordSpacing;
}
/**
* Trims the last space.
* @return the width of the space trimmed, otherwise 0
*/
public float trimLastSpace()
{
if (value.length() > 1 && value.endsWith(" ")) {
value = value.substring(0, value.length() - 1);
setContent(value);
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.size() > 0);
}
/**
* 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;
}
/**
* Gets the image offset in the x direction
* @return the image offset in the x direction
*/
float getImageOffsetX()
{
return offsetX;
}
/**
* Gets the image offset in the y direction
* @return Gets the image offset in the y direction
*/
float getImageOffsetY()
{
return offsetY;
}
/**
* sets the value.
*/
void setValue(String value)
{
this.value = value;
setContent(value);
}
/**
* 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 c the character that has to be checked
* @return <CODE>true</CODE> if the character can be used to split a string, <CODE>false</CODE> otherwise
*/
public boolean isSplitCharacter(char c)
{
if (c <= ' ' || c == '-') {
return true;
}
if (c < 0x2e80)
return false;
return ((c >= 0x2e80 && c < 0xd7a0)
|| (c >= 0xf900 && c < 0xfb00)
|| (c >= 0xfe30 && c < 0xfe50)
|| (c >= 0xff61 && c < 0xffa0));
}
/**
* 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>
*/
static String trim(String string) {
while (string.endsWith(" ") || string.endsWith("\t")) {
string = string.substring(0, string.length() - 1);
}
return string;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -