|
|
@ -53,6 +53,7 @@ import java.awt.Color; |
|
|
|
import java.awt.FontMetrics; |
|
|
|
import java.awt.FontMetrics; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Iterator; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
@ -92,6 +93,8 @@ public class PdfChunk { |
|
|
|
|
|
|
|
|
|
|
|
private static final String BREAK_TAG = "<br>"; |
|
|
|
private static final String BREAK_TAG = "<br>"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final static char EMPTY_SYMBOL = ' '; |
|
|
|
|
|
|
|
|
|
|
|
private boolean breakTag = false; |
|
|
|
private boolean breakTag = false; |
|
|
|
|
|
|
|
|
|
|
|
public float getHeight() { |
|
|
|
public float getHeight() { |
|
|
@ -309,7 +312,6 @@ public class PdfChunk { |
|
|
|
if (splitCharacter == null) |
|
|
|
if (splitCharacter == null) |
|
|
|
splitCharacter = DefaultSplitCharacter.DEFAULT; |
|
|
|
splitCharacter = DefaultSplitCharacter.DEFAULT; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// methods
|
|
|
|
// methods
|
|
|
|
|
|
|
|
|
|
|
|
/** Gets the Unicode equivalent to a CID. |
|
|
|
/** Gets the Unicode equivalent to a CID. |
|
|
@ -362,11 +364,11 @@ public class PdfChunk { |
|
|
|
// or until the totalWidth is reached
|
|
|
|
// or until the totalWidth is reached
|
|
|
|
int length = value.length(); |
|
|
|
int length = value.length(); |
|
|
|
char valueArray[] = value.toCharArray(); |
|
|
|
char valueArray[] = value.toCharArray(); |
|
|
|
BreakIterator iterator = BreakIterator.getLineInstance(Locale.getDefault()); |
|
|
|
BreakIterator iterator = new SpaceWithPunctuationBreakIterator(value, BreakIterator.getLineInstance(Locale.getDefault())); |
|
|
|
BreakIterator iterator1 = new SpaceWithPunctuationBreakIterator(value, iterator); |
|
|
|
|
|
|
|
char character = 0; |
|
|
|
char character = 0; |
|
|
|
|
|
|
|
boolean hasEmptySymbolEndOfLine = false; //行末有空格存在 ps:不存在连续空格键
|
|
|
|
while (currentPosition < length) { |
|
|
|
while (currentPosition < length) { |
|
|
|
int next = iterator1.next(); |
|
|
|
int next = iterator.next(); |
|
|
|
if(next < 1){ |
|
|
|
if(next < 1){ |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -386,11 +388,17 @@ public class PdfChunk { |
|
|
|
} |
|
|
|
} |
|
|
|
String substring = value.substring(start, next); |
|
|
|
String substring = value.substring(start, next); |
|
|
|
currentWidth += font.width(substring); |
|
|
|
currentWidth += font.width(substring); |
|
|
|
if (currentWidth + indent.getRight() > width){ |
|
|
|
if (currentWidth + indent.getRight() > width) { |
|
|
|
|
|
|
|
if (dealWithEmptySymbol(substring, currentWidth + indent.getRight(), width)) { |
|
|
|
|
|
|
|
//行末空格(加上该空格大于限制的行宽,减去则小于限制的行宽)、需要去掉该空格,不然下划线、删除线什么的会变长
|
|
|
|
|
|
|
|
//该空格不能留给下一行
|
|
|
|
|
|
|
|
hasEmptySymbolEndOfLine=true; |
|
|
|
|
|
|
|
start = next; |
|
|
|
|
|
|
|
} else { |
|
|
|
currentPosition = start - 1; |
|
|
|
currentPosition = start - 1; |
|
|
|
|
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
start = next; |
|
|
|
start = next; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
@ -401,12 +409,26 @@ public class PdfChunk { |
|
|
|
} |
|
|
|
} |
|
|
|
// otherwise, the string has to be truncated
|
|
|
|
// otherwise, the string has to be truncated
|
|
|
|
String returnValue = value.substring(start); |
|
|
|
String returnValue = value.substring(start); |
|
|
|
value = value.substring(0, start); |
|
|
|
value = value.substring(0, start - (hasEmptySymbolEndOfLine ? 1 : 0)); |
|
|
|
PdfChunk pc = new PdfChunk(returnValue, this); |
|
|
|
PdfChunk pc = new PdfChunk(returnValue, this); |
|
|
|
return pc; |
|
|
|
return pc; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* @param text 文本 |
|
|
|
|
|
|
|
* @param totalWidth 已经处理过的文本和当前文本的宽度和 |
|
|
|
|
|
|
|
* @param lineWidth 行宽 |
|
|
|
|
|
|
|
* @return true : 去掉text末尾的空格后小于行宽 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private boolean dealWithEmptySymbol(String text, float totalWidth, float lineWidth) { |
|
|
|
|
|
|
|
if (null == text || 0 == text.length()) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//HTML解析后不存在连续多个空格键存在的情况,因此只需去除末尾的空格键
|
|
|
|
|
|
|
|
return text.charAt(text.length() - 1) == EMPTY_SYMBOL && totalWidth - getFont().width(EMPTY_SYMBOL) < lineWidth; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Truncates this <CODE>PdfChunk</CODE> if it's too long for the given width. |
|
|
|
* Truncates this <CODE>PdfChunk</CODE> if it's too long for the given width. |
|
|
|
* <P> |
|
|
|
* <P> |
|
|
|
* Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated. |
|
|
|
* Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated. |
|
|
|