📄 rtfdocument.java
字号:
/**
* Gets the RtfMapper object of this RtfDocument
*
* @return The RtfMapper
*/
public RtfMapper getMapper() {
return this.mapper;
}
/**
* Generates a random integer that is unique with respect to the document.
*
* @return A random int
*/
public int getRandomInt() {
Integer newInt = null;
do {
newInt = new Integer((int) (Math.random() * Integer.MAX_VALUE));
} while(this.previousRandomInts.contains(newInt));
this.previousRandomInts.add(newInt);
return newInt.intValue();
}
/**
* Gets the RtfDocumentHeader of this RtfDocument
*
* @return The RtfDocumentHeader of this RtfDocument
*/
public RtfDocumentHeader getDocumentHeader() {
return this.documentHeader;
}
/**
* Replaces special characters with their unicode values
* @param str The original <code>String</code>
* @param useHex indicated if the hexadecimal value has to be used
* @param softLineBreaks whether to use soft line breaks instead of default hard ones.
*
* @deprecated replaced by {@link #filterSpecialChar(OutputStream, String, boolean, boolean)}
* @return The converted String
*/
public String filterSpecialChar(String str, boolean useHex, boolean softLineBreaks) {
if(str == null) {
return "";
}
int length = str.length();
int z = 'z';
StringBuffer ret = new StringBuffer(length);
for (int i = 0; i < length; i++) {
char ch = str.charAt(i);
if (ch == '\\') {
ret.append("\\\\");
} else if (ch == '\n') {
if(softLineBreaks) {
ret.append("\\line ");
} else {
ret.append("\\par ");
}
} else if (ch == '\t') {
ret.append("\\tab ");
} else if ((ch) > z && this.documentSettings.isAlwaysUseUnicode()) {
if(useHex) {
ret.append("\\\'").append(Long.toHexString(ch));
} else {
ret.append("\\u").append((long) ch).append('?');
}
} else {
ret.append(ch);
}
}
String s = ret.toString();
if(s.indexOf("$newpage$") >= 0) {
String before = s.substring(0, s.indexOf("$newpage$"));
String after = s.substring(s.indexOf("$newpage$") + 9);
ret = new StringBuffer(before);
ret.append("\\page\\par ");
ret.append(after);
return ret.toString();
}
return s;
}
/**
* Writes the given string to the given {@link OutputStream} encoding the string characters.
*
* @param out destination OutputStream
* @param str string to write
* @param useHex if <code>true</code> hex encoding characters is preferred to unicode encoding if possible
* @param softLineBreaks if <code>true</code> return characters are written as soft line breaks
*
* @throws IOException
*/
public void filterSpecialChar(final OutputStream out, final String str, final boolean useHex, final boolean softLineBreaks) throws IOException
{
if(out == null) {
throw(new NullPointerException("null OutpuStream"));
}
final boolean alwaysUseUniCode = this.documentSettings.isAlwaysUseUnicode();
if(str == null) {
return;
}
final int len = str.length();
if(len == 0) {
return;
}
for(int k = 0; k < len; k++) {
final char c = str.charAt(k);
if(c < 0x20) {
//allow return and tab only
if(c == '\n') {
out.write(softLineBreaks ? FSC_LINE : FSC_PAR);
} else
if(c == '\t') {
out.write(FSC_TAB);
} else {
out.write('?');
}
} else
if((c == '\\') || (c == '{') || (c == '}')) {
//escape
out.write(FSC_BACKSLASH);
out.write(c);
} else
if((c == '$') && (len-k >= FSC_NEWPAGE.length) && subMatch(str, k, FSC_NEWPAGE)) {
//"$newpage$" -> "\\page\\par "
out.write(FSC_PAGE_PAR);
k += FSC_NEWPAGE.length-1;
} else {
if((c > 0xff) || ((c > 'z') && alwaysUseUniCode)) {
if(useHex && (c <= 0xff)) {
//encode as 2 char hex string
out.write(FSC_HEX_PREFIX);
out.write(RtfImage.byte2charLUT, c*2, 2);
} else {
//encode as decimal, signed short value
out.write(FSC_UNI_PREFIX);
String s = Short.toString((short)c);
for(int x = 0; x < s.length(); x++) {
out.write(s.charAt(x));
}
out.write('?');
}
} else {
out.write(c);
}
}
}
}
/**
* Returns <code>true</code> if <tt>m.length</tt> characters in <tt>str</tt>, starting at offset <tt>soff</tt>
* match the bytes in the given array <tt>m</tt>.
*
* @param str the string to search for a match
* @param soff the starting offset in str
* @param m the array to match
* @return <code>true</code> if there is match
*/
private static boolean subMatch(final String str, int soff, final byte[] m)
{
for(int k = 0; k < m.length; k++) {
if(str.charAt(soff++) != m[k]) {
return(false);
}
}
return(true);
}
/**
* Whether to automagically generate table of contents entries when
* adding Chapters or Sections.
*
* @param autogenerate Whether to automatically generate TOC entries
*/
public void setAutogenerateTOCEntries(boolean autogenerate) {
this.autogenerateTOCEntries = autogenerate;
}
/**
* Get whether to autmatically generate table of contents entries
*
* @return Wheter to automatically generate TOC entries
*/
public boolean getAutogenerateTOCEntries() {
return this.autogenerateTOCEntries;
}
/**
* Gets the RtfDocumentSettings that specify how the rtf document is generated.
*
* @return The current RtfDocumentSettings.
*/
public RtfDocumentSettings getDocumentSettings() {
return this.documentSettings;
}
/**
* Gets the last RtfBasicElement that was directly added to the RtfDocument.
*
* @return The last RtfBasicElement that was directly added to the RtfDocument.
*/
public RtfBasicElement getLastElementWritten() {
return this.lastElementWritten;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -