📄 rtfwriter.java
字号:
out.write(alignCenter);
break;
case Element.ALIGN_JUSTIFIED:
out.write(escape);
out.write(alignJustify);
break;
}
out.write(openGroup);
out.write(extendedEscape);
out.write(pictureGroup);
out.write(openGroup);
out.write(escape);
out.write(picture);
out.write(escape);
switch (type) {
case Image.ORIGINAL_JPEG:
out.write(pictureJPEG);
break;
case Image.ORIGINAL_PNG:
out.write(picturePNG);
break;
case Image.ORIGINAL_WMF:
case Image.ORIGINAL_BMP:
out.write(pictureWMF);
break;
}
out.write(escape);
out.write(pictureWidth);
writeInt(out, (int) (image.getPlainWidth() * TWIPSFACTOR));
out.write(escape);
out.write(pictureHeight);
writeInt(out, (int) (image.getPlainHeight() * TWIPSFACTOR));
// For some reason this messes up the intended image size. It makes it too big. Weird
//
// out.write(escape);
// out.write(pictureIntendedWidth);
// writeInt(out, (int) (image.plainWidth() * twipsFactor));
// out.write(escape);
// out.write(pictureIntendedHeight);
// writeInt(out, (int) (image.plainHeight() * twipsFactor));
if (image.getWidth() > 0) {
out.write(escape);
out.write(pictureScaleX);
writeInt(out, (int) (100 / image.getWidth() * image.getPlainWidth()));
}
if (image.getHeight() > 0) {
out.write(escape);
out.write(pictureScaleY);
writeInt(out, (int) (100 / image.getHeight() * image.getPlainHeight()));
}
out.write(delimiter);
InputStream imgIn;
if (type == Image.ORIGINAL_BMP) {
imgIn = new ByteArrayInputStream(MetaDo.wrapBMP(image));
}
else {
if (image.getOriginalData() == null) {
imgIn = image.getUrl().openStream();
} else {
imgIn = new ByteArrayInputStream(image.getOriginalData());
}
if (type == Image.ORIGINAL_WMF) { //remove the placeable header
long skipLength = 22;
while(skipLength > 0) {
skipLength = skipLength - imgIn.skip(skipLength);
}
}
}
int buffer = -1;
int count = 0;
out.write((byte) '\n');
while ((buffer = imgIn.read()) != -1) {
String helperStr = Integer.toHexString(buffer);
if (helperStr.length() < 2) helperStr = "0" + helperStr;
out.write(helperStr.getBytes());
count++;
if (count == 64) {
out.write((byte) '\n');
count = 0;
}
}
imgIn.close();
out.write(closeGroup);
out.write(closeGroup);
out.write((byte) '\n');
}
/**
* Write an <code>Annotation</code>
*
* @param annotationElement The <code>Annotation</code> to be written
* @param out The <code>ByteArrayOutputStream</code> to write to
*
* @throws IOException
*/
private void writeAnnotation(Annotation annotationElement, ByteArrayOutputStream out) throws IOException {
int id = getRandomInt();
out.write(openGroup);
out.write(extendedEscape);
out.write(annotationID);
out.write(delimiter);
writeInt(out, id);
out.write(closeGroup);
out.write(openGroup);
out.write(extendedEscape);
out.write(annotationAuthor);
out.write(delimiter);
out.write(annotationElement.title().getBytes());
out.write(closeGroup);
out.write(openGroup);
out.write(extendedEscape);
out.write(annotation);
out.write(escape);
out.write(paragraphDefaults);
out.write(delimiter);
out.write(annotationElement.content().getBytes());
out.write(closeGroup);
}
/**
* Add a <code>Meta</code> element. It is written to the Inforamtion Group
* and merged with the main <code>ByteArrayOutputStream</code> when the
* Document is closed.
*
* @param metaName The type of <code>Meta</code> element to be added
* @param meta The <code>Meta</code> element to be added
*
* Currently only the Meta Elements Author, Subject, Keywords, Title, Producer and CreationDate are supported.
*
* @throws IOException
*/
private void writeMeta(byte[] metaName, Meta meta) throws IOException {
info.write(openGroup);
try {
info.write(escape);
info.write(metaName);
info.write(delimiter);
if (meta.type() == Meta.CREATIONDATE) {
writeFormatedDateTime(meta.getContent());
} else {
info.write(meta.getContent().getBytes());
}
} finally {
info.write(closeGroup);
}
}
/**
* Writes a date. The date is formated <strong>Year, Month, Day, Hour, Minute, Second</strong>
*
* @param date The date to be written
*
* @throws IOException
*/
private void writeFormatedDateTime(String date) throws IOException {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
ParsePosition pp = new ParsePosition(0);
Date d = sdf.parse(date, pp);
if (d == null) {
d = new Date();
}
cal.setTime(d);
info.write(escape);
info.write(year);
writeInt(info, cal.get(Calendar.YEAR));
info.write(escape);
info.write(month);
writeInt(info, cal.get(Calendar.MONTH));
info.write(escape);
info.write(day);
writeInt(info, cal.get(Calendar.DAY_OF_MONTH));
info.write(escape);
info.write(hour);
writeInt(info, cal.get(Calendar.HOUR_OF_DAY));
info.write(escape);
info.write(minute);
writeInt(info, cal.get(Calendar.MINUTE));
info.write(escape);
info.write(second);
writeInt(info, cal.get(Calendar.SECOND));
}
/**
* Add a new <code>Font</code> to the list of fonts. If the <code>Font</code>
* already exists in the list of fonts, then it is not added again.
*
* @param newFont The <code>Font</code> to be added
*
* @return The index of the <code>Font</code> in the font list
*/
protected int addFont(Font newFont) {
int fn = -1;
for (int i = 0; i < fontList.size(); i++) {
if (newFont.getFamilyname().equals(((Font) fontList.get(i)).getFamilyname())) {
fn = i;
}
}
if (fn == -1) {
fontList.add(newFont);
return fontList.size() - 1;
}
return fn;
}
/**
* Add a new <code>Color</code> to the list of colours. If the <code>Color</code>
* already exists in the list of colours, then it is not added again.
*
* @param newColor The <code>Color</code> to be added
*
* @return The index of the <code>color</code> in the colour list
*/
protected int addColor(Color newColor) {
int cn = 0;
if (newColor == null) {
return cn;
}
cn = colorList.indexOf(newColor);
if (cn == -1) {
colorList.add(newColor);
return colorList.size() - 1;
}
return cn;
}
/**
* Merge all the different <code>ArrayList</code>s and <code>ByteArrayOutputStream</code>s
* to the final <code>ByteArrayOutputStream</code>
*
* @return <code>true</code> if all information was sucessfully written to the <code>ByteArrayOutputStream</code>
*/
private boolean writeDocument() {
try {
writeDocumentIntro();
writeFontList();
os.write((byte) '\n');
writeColorList();
os.write((byte) '\n');
writeList();
os.write((byte) '\n');
writeInfoGroup();
os.write((byte) '\n');
writeDocumentFormat();
os.write((byte) '\n');
ByteArrayOutputStream hf = new ByteArrayOutputStream();
writeSectionDefaults(hf);
hf.writeTo(os);
content.writeTo(os);
os.write(closeGroup);
return true;
} catch (IOException e) {
System.err.println(e.getMessage());
return false;
}
}
/** Write the Rich Text file settings
* @throws IOException
*/
private void writeDocumentIntro() throws IOException {
os.write(openGroup);
os.write(escape);
os.write(docBegin);
os.write(escape);
os.write(ansi);
os.write(escape);
os.write(ansiCodepage);
writeInt(os, 1252);
os.write((byte)'\n');
os.write(escape);
os.write(defaultFont);
writeInt(os, 0);
}
/**
* Write the font list to the final <code>ByteArrayOutputStream</code>
* @throws IOException
*/
private void writeFontList() throws IOException {
Font fnt;
os.write(openGroup);
os.write(escape);
os.write(fontTable);
for (int i = 0; i < fontList.size(); i++) {
fnt = (Font) fontList.get(i);
os.write(openGroup);
os.write(escape);
os.write(fontNumber);
writeInt(os, i);
os.write(escape);
switch (Font.getFamilyIndex(fnt.getFamilyname())) {
case Font.COURIER:
os.write(fontModern);
os.write(escape);
os.write(fontCharset);
writeInt(os, 0);
os.write(delimiter);
os.write(fontCourier);
break;
case Font.HELVETICA:
os.write(fontSwiss);
os.write(escape);
os.write(fontCharset);
writeInt(os, 0);
os.write(delimiter);
os.write(fontArial);
break;
case Font.SYMBOL:
os.write(fontRoman);
os.write(escape);
os.write(fontCharset);
writeInt(os, 2);
os.write(delimiter);
os.write(fontSymbol);
break;
case Font.TIMES_ROMAN:
os.write(fontRoman);
os.write(escape);
os.write(fontCharset);
writeInt(os, 0);
os.write(delimiter);
os.write(fontTimesNewRoman);
break;
case Font.ZAPFDINGBATS:
os.write(fontTech);
os.write(escape);
os.write(fontCharset);
writeInt(os, 0);
os.write(delimiter);
os.write(fontWindings);
break;
default:
os.write(fontRoman);
os.write(escape);
os.write(fontCharset);
writeInt(os, 0);
os.write(delimiter);
os.write(filterSpecialChar(fnt.getFamilyname(), true).getBytes());
}
os.write(commaDelimiter);
os.write(closeGroup);
}
os.write(closeGroup);
}
/**
* Write the colour list to the final <code>ByteArrayOutputStream</code>
* @throws IOException
*/
private void writeColorList() throws IOException {
Color color = null;
os.write(openGroup);
os.write(escape);
os.write(colorTable);
for (int i = 0; i < colorList.size(); i++) {
color = (Color) colorList.get(i);
os.write(escape);
os.write(colorRed);
writeInt(os, color.getRed());
os.write(escape);
os.write(colorGreen);
writeInt(os, color.getGreen());
os.write(escape);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -