📄 htmlwriter.java
字号:
* @throws IOException
*/
protected void writeLink(Header header) throws IOException {
addTabs(2);
writeStart(HtmlTags.LINK);
write(HtmlTags.REL, header.name());
write(HtmlTags.TYPE, HtmlTags.CSS);
write(HtmlTags.REFERENCE, header.content());
writeEnd();
}
/**
* Writes some comment.
* <P>
* This method writes some comment.
*
* @param comment the comment that has to be written
* @throws IOException
*/
protected void writeComment(String comment) throws IOException {
os.write(BEGINCOMMENT);
write(comment);
os.write(ENDCOMMENT);
}
// public methods
/**
* Changes the standardfont.
*
* @param standardfont The font
*/
public void setStandardFont(Font standardFont) {
this.standardFont = standardFont;
}
/**
* Sets the basepath for images.
* <P>
* This is especially useful if you add images using a file,
* rather than an URL. In PDF there is no problem, since
* the images are added inline, but in HTML it is sometimes
* necessary to use a relative path or a special path to some
* images directory.
*
* @param the new imagepath
*/
public void setImagepath(String imagepath) {
this.imagepath = imagepath;
}
/**
* Resets the imagepath.
*/
public void resetImagepath() {
imagepath = null;
}
/**
* Changes the header of this document.
*
* @param header the new header
*
* @author David Freels
*/
public void setHeader(HeaderFooter header) {
this.header = header;
}
/**
* Changes the footer of this document.
*
* @param footer the new footer
*
* @author David Freels
*/
public void setFooter(HeaderFooter footer) {
this.footer = footer;
}
/**
* Signals that a <CODE>String</CODE> was added to the <CODE>Document</CODE>.
*
* @return <CODE>true</CODE> if the string was added, <CODE>false</CODE> if not.
* @throws DocumentException when a document isn't open yet, or has been closed
* @author Evelyne De Cordier
*/
public boolean add(String string) throws DocumentException{
if (pause) {
return false;
}
try
{
write(string);
return true;
}
catch(IOException ioe) {
throw new DocumentException(ioe.getMessage());
}
}
/**
* Writes the HTML representation of an element.
*
* @param element the element
* @param indent the indentation
*/
protected void write(Element element, int indent) throws IOException {
switch(element.type()) {
case Element.CHUNK:
{
Chunk chunk = (Chunk) element;
// if the chunk contains an image, return the image representation
try {
Image image = chunk.getImage();
write(image, indent);
return;
}
catch(NullPointerException npe) {
}
if (chunk.isEmpty()) return;
HashMap attributes = chunk.getAttributes();
if (chunk.font().isStandardFont() && attributes == null) {
addTabs(indent);
write(HtmlEncoder.encode(chunk.content()));
os.write(NEWLINE);
return;
}
else {
if (attributes != null && attributes.get(Chunk.NEWPAGE) != null) {
return;
}
addTabs(indent);
writeStart(HtmlTags.CHUNK);
if (! chunk.font().isStandardFont()) {
write(chunk.font());
}
os.write(GT);
writeFontStyleStart(chunk.font().style());
if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) {
if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) {
writeStart(HtmlTags.SUP);
}
else {
writeStart(HtmlTags.SUB);
}
os.write(GT);
}
write(HtmlEncoder.encode(chunk.content()));
if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) {
os.write(LT);
os.write(FORWARD);
if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) {
write(HtmlTags.SUP);
}
else {
write(HtmlTags.SUB);
}
os.write(GT);
}
writeFontStyleEnd(chunk.font().style());
writeEnd(HtmlTags.CHUNK);
}
return;
}
case Element.PHRASE:
{
Phrase phrase = (Phrase) element;
addTabs(indent);
writeStart(HtmlTags.PHRASE);
write(phrase.font());
os.write(GT);
if (phrase.font().style() != Font.UNDEFINED && phrase.font().style() != Font.NORMAL) {
addTabs(indent);
writeFontStyleStart(phrase.font().style());
os.write(NEWLINE);
}
os.write(NEWLINE);
for (Iterator i = phrase.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
if (phrase.font().style() != Font.UNDEFINED && phrase.font().style() != Font.NORMAL) {
addTabs(indent);
writeFontStyleEnd(phrase.font().style());
os.write(NEWLINE);
}
addTabs(indent);
writeEnd(HtmlTags.PHRASE);
return;
}
case Element.ANCHOR:
{
Anchor anchor = (Anchor) element;
if (!anchor.font().isStandardFont()) {
addTabs(indent);
writeStart(HtmlTags.PHRASE);
write(anchor.font());
os.write(GT);
os.write(NEWLINE);
}
if (anchor.font().style() != Font.UNDEFINED && anchor.font().style() != Font.NORMAL) {
addTabs(indent);
writeFontStyleStart(anchor.font().style());
os.write(NEWLINE);
}
addTabs(indent);
writeStart(HtmlTags.ANCHOR);
if (anchor.name() != null) {
write(HtmlTags.NAME, anchor.name());
}
if (anchor.reference() != null) {
write(HtmlTags.REFERENCE, anchor.reference());
}
os.write(GT);
os.write(NEWLINE);
for (Iterator i = anchor.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
addTabs(indent);
writeEnd(HtmlTags.ANCHOR);
if (anchor.font().style() != Font.UNDEFINED && anchor.font().style() != Font.NORMAL) {
addTabs(indent);
writeFontStyleEnd(anchor.font().style());
os.write(NEWLINE);
}
if (!anchor.font().isStandardFont()) {
addTabs(indent);
writeEnd(HtmlTags.PHRASE);;
}
return;
}
case Element.PARAGRAPH:
{
Paragraph paragraph = (Paragraph) element;
addTabs(indent);
writeStart(HtmlTags.PARAGRAPH);
String alignment = HtmlEncoder.getAlignment(paragraph.alignment());
if (!"".equals(alignment)) {
write(HtmlTags.ALIGN, alignment);
}
os.write(GT);
os.write(NEWLINE);
if (!paragraph.font().isStandardFont()) {
addTabs(indent);
writeStart(HtmlTags.PHRASE);
write(paragraph.font());
os.write(GT);
os.write(NEWLINE);
}
if (paragraph.font().style() != Font.UNDEFINED && paragraph.font().style() != Font.NORMAL) {
addTabs(indent);
writeFontStyleStart(paragraph.font().style());
os.write(NEWLINE);
}
for (Iterator i = paragraph.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
if (paragraph.font().style() != Font.UNDEFINED && paragraph.font().style() != Font.NORMAL) {
addTabs(indent);
writeFontStyleEnd(paragraph.font().style());
os.write(NEWLINE);
}
if (!paragraph.font().isStandardFont()) {
addTabs(indent);
writeEnd(HtmlTags.PHRASE);
}
addTabs(indent);
writeEnd(HtmlTags.PARAGRAPH);
return;
}
case Element.SECTION:
case Element.CHAPTER:
{
Section section = (Section) element;
addTabs(indent);
writeStart(HtmlTags.DIV);
writeSection(section, indent);
writeEnd(HtmlTags.DIV);
return;
}
case Element.LIST:
{
List list = (List) element;
addTabs(indent);
if (list.isNumbered()) {
writeStart(HtmlTags.ORDEREDLIST);
}
else {
writeStart(HtmlTags.UNORDEREDLIST);
}
os.write(GT);
os.write(NEWLINE);
for (Iterator i = list.getItems().iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
}
addTabs(indent);
if (list.isNumbered()) {
writeEnd(HtmlTags.ORDEREDLIST);
}
else {
writeEnd(HtmlTags.UNORDEREDLIST);
}
return;
}
case Element.LISTITEM:
{
ListItem listItem = (ListItem) element;
addTabs(indent);
writeStart(HtmlTags.LISTITEM);
os.write(GT);
os.write(NEWLINE);
if (!listItem.font().isStandardFont()) {
addTabs(indent);
writeStart(HtmlTags.PHRASE);
write(listItem.font());
os.write(GT);
os.write(NEWLINE);
}
if (listItem.font().style() != Font.UNDEFINED && listItem.font().style() != Font.NORMAL) {
addTabs(indent);
writeFontStyleStart(listItem.font().style());
os.write(NEWLINE);
}
for (Iterator i = listItem.iterator(); i.hasNext(); ) {
write((Element) i.next(), indent + 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -