📄 htmlwriter.java
字号:
/** * Writes a Metatag in the header. * * @param meta the element that has to be written * @throws IOException */ protected void writeHeader(Meta meta) throws IOException { addTabs(2); writeStart(HtmlTags.META); switch(meta.type()) { case Element.HEADER: write(HtmlTags.NAME, ((Header) meta).name()); break; case Element.SUBJECT: write(HtmlTags.NAME, HtmlTags.SUBJECT); break; case Element.KEYWORDS: write(HtmlTags.NAME, HtmlTags.KEYWORDS); break; case Element.AUTHOR: write(HtmlTags.NAME, HtmlTags.AUTHOR); break; } write(HtmlTags.CONTENT, HtmlEncoder.encode(meta.content())); writeEnd(); } /** * Writes a link in the header. * * @param header the element that has to be written * @throws IOException */ protected void writeLink(Header header) throws IOException { addTabs(2); writeStart(MarkupTags.LINK); write(MarkupTags.REL, header.name()); write(MarkupTags.TYPE, MarkupTags.CSS); write(HtmlTags.REFERENCE, header.content()); writeEnd(); } /** * Writes a JavaScript section or, if the markup attribute HtmlTags.URL is set, a JavaScript reference in the header. * * @param header the element that has to be written * @throws IOException */ protected void writeJavaScript(Header header) throws IOException { addTabs(2); writeStart(HtmlTags.SCRIPT); write(HtmlTags.LANGUAGE, HtmlTags.JAVASCRIPT); if (header.getMarkupAttribute(HtmlTags.URL) != null) { /* JavaScript reference example: * * <script language="JavaScript" src="/myPath/MyFunctions.js"/> */ write(HtmlTags.URL, header.getMarkupAttribute(HtmlTags.URL)); os.write(GT); writeEnd(HtmlTags.SCRIPT); } else { /* JavaScript coding convention: * * <script language="JavaScript" type="text/javascript"> * <!-- * // ... JavaScript methods ... * //--> * </script> */ write(MarkupTags.TYPE, MarkupTags.JAVASCRIPT); os.write(GT); addTabs(2); write(new String(BEGINCOMMENT) + "\n"); write(header.content()); addTabs(2); write("//" + new String(ENDCOMMENT)); addTabs(2); writeEnd(HtmlTags.SCRIPT); } } /** * 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 { addTabs(2); 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; } /** * Checks if a given font is the same as the font that was last used. * * @param font the font of an object * @return true if the font differs */ public boolean isOtherFont(Font font) { try { Font cFont = (Font) currentfont.peek(); if (cFont.compareTo(font) == 0) return false; return true; } catch(EmptyStackException ese) { if (standardfont.compareTo(font) == 0) return false; return true; } } /** * 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 imagepath 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 */ public void setHeader(HeaderFooter header) { this.header = header; } /** * Changes the footer of this document. * * @param footer the new footer */ 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 */ public boolean add(String string) throws DocumentException{ if (pause) { return false; } try { write(string); return true; } catch(IOException ioe) { throw new ExceptionConverter(ioe); } } /** * Writes the HTML representation of an element. * * @param element the element * @param indent the indentation */ protected void write(Element element, int indent) throws IOException { Properties styleAttributes = null; switch(element.type()) { case Element.CHUNK: { Chunk chunk = (Chunk) element; // if the chunk contains an image, return the image representation Image image = chunk.getImage(); if (image != null) { write(image, indent); return; } if (chunk.isEmpty()) return; HashMap attributes = chunk.getAttributes(); if (attributes != null && attributes.get(Chunk.NEWPAGE) != null) { return; } // This doesn't seem to work: //if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) { // float p = (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() * 100f) / chunk.font().size(); // styleAttributes = new Properties(); // styleAttributes.setProperty(MarkupTags.CSS_VERTICALALIGN, "" + p + "%"); //} boolean tag = isOtherFont(chunk.font()) || hasMarkupAttributes(chunk) || styleAttributes != null; if (tag) { // start span tag addTabs(indent); writeStart(MarkupTags.SPAN); if (isOtherFont(chunk.font())) { write(chunk.font(), styleAttributes); } if (hasMarkupAttributes(chunk)) { writeMarkupAttributes((MarkupAttributes)chunk); } os.write(GT); } if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) { // start sup or sub tag if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) { writeStart(HtmlTags.SUP); } else { writeStart(HtmlTags.SUB); } os.write(GT); } // contents write(HtmlEncoder.encode(chunk.content())); if (attributes != null && attributes.get(Chunk.SUBSUPSCRIPT) != null) { // end sup or sub tag os.write(LT); os.write(FORWARD); if (((Float)attributes.get(Chunk.SUBSUPSCRIPT)).floatValue() > 0) { write(HtmlTags.SUP); } else { write(HtmlTags.SUB); } os.write(GT); } if (tag) { // end tag writeEnd(MarkupTags.SPAN); } return; } case Element.PHRASE: { Phrase phrase = (Phrase) element; styleAttributes = new Properties(); if (phrase.leadingDefined()) styleAttributes.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(phrase.leading()) + "pt"); // start tag addTabs(indent); writeStart(MarkupTags.SPAN); if (hasMarkupAttributes(phrase)) { writeMarkupAttributes((MarkupAttributes)phrase); } write(phrase.font(), styleAttributes); os.write(GT); currentfont.push(phrase.font()); // contents for (Iterator i = phrase.iterator(); i.hasNext(); ) { write((Element) i.next(), indent + 1); } // end tag addTabs(indent); writeEnd(MarkupTags.SPAN); currentfont.pop(); return; } case Element.ANCHOR: { Anchor anchor = (Anchor) element; styleAttributes = new Properties(); if (anchor.leadingDefined()) styleAttributes.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(anchor.leading()) + "pt"); // start tag addTabs(indent); writeStart(HtmlTags.ANCHOR); if (anchor.name() != null) { write(HtmlTags.NAME, anchor.name()); } if (anchor.reference() != null) { write(HtmlTags.REFERENCE, anchor.reference()); } if (hasMarkupAttributes(anchor)) { writeMarkupAttributes((MarkupAttributes)anchor); } write(anchor.font(), styleAttributes); os.write(GT); currentfont.push(anchor.font()); // contents for (Iterator i = anchor.iterator(); i.hasNext(); ) { write((Element) i.next(), indent + 1); } // end tag addTabs(indent); writeEnd(HtmlTags.ANCHOR); currentfont.pop(); return; } case Element.PARAGRAPH: { Paragraph paragraph = (Paragraph) element; styleAttributes = new Properties(); if (paragraph.leadingDefined()) styleAttributes.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(paragraph.leading()) + "pt"); // start tag addTabs(indent); writeStart(MarkupTags.DIV); if (hasMarkupAttributes(paragraph)) { writeMarkupAttributes((MarkupAttributes)paragraph); } String alignment = HtmlEncoder.getAlignment(paragraph.alignment()); if (!"".equals(alignment)) { write(HtmlTags.ALIGN, alignment); } write(paragraph.font(), styleAttributes); os.write(GT); currentfont.push(paragraph.font()); // contents for (Iterator i = paragraph.iterator(); i.hasNext(); ) { write((Element) i.next(), indent + 1); } // end tag addTabs(indent); writeEnd(MarkupTags.DIV); currentfont.pop(); return; } case Element.SECTION: case Element.CHAPTER:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -