예제 #1
0
        /**
         * Writes the HTML representation of a section.
         *
         * @param   section     the section to write
         * @param   indent      the indentation
         */

        protected void writeSection(Section section, int indent)
        {
            if (section.Title != null)
            {
                int depth = section.depth() - 1;
                if (depth > 5)
                {
                    depth = 5;
                }
                Properties styleAttributes = new Properties();
                if (section.Title.LeadingDefined)
                {
                    styleAttributes.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(section.Title.leading()) + "pt");
                }
                // start tag
                addTabs(indent);
                writeStart(HtmlTags.H[depth]);
                write(section.Title.Font, styleAttributes);
                String alignment = HtmlEncoder.getAlignment(section.Title.alignment());
                if (!"".equals(alignment))
                {
                    write(HtmlTags.ALIGN, alignment);
                }
                if (hasMarkupAttributes(section.Title))
                {
                    writeMarkupAttributes((MarkupAttributes)section.Title);
                }
                os.write(GT);
                currentfont.push(section.Title.Font);
                // contents
                for (Iterator i = section.Title.iterator(); i.hasNext();)
                {
                    write((Element)i.next(), indent + 1);
                }
                // end tag
                addTabs(indent);
                writeEnd(HtmlTags.H[depth]);
                currentfont.pop();
            }
            for (Iterator i = section.iterator(); i.hasNext();)
            {
                write((Element)i.next(), indent);
            }
        }
예제 #2
0
        /**
         * Writes the HTML representation of an element.
         *
         * @param   element     the element
         * @param   indent      the indentation
         */

        protected void write(IElement element, int indent)
        {
            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.Image;
                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 + "%");
                //}
                bool 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: {
                // part of the start tag + contents
                writeSection((Section)element, indent);
                return;
            }

            case Element.LIST: {
                List list = (List)element;
                // start tag
                addTabs(indent);
                if (list.isNumbered())
                {
                    writeStart(HtmlTags.ORDEREDLIST);
                }
                else
                {
                    writeStart(HtmlTags.UNORDEREDLIST);
                }
                if (hasMarkupAttributes(list))
                {
                    writeMarkupAttributes((MarkupAttributes)list);
                }
                os.write(GT);
                // contents
                for (Iterator i = list.getItems().iterator(); i.hasNext();)
                {
                    write((Element)i.next(), indent + 1);
                }
                // end tag
                addTabs(indent);
                if (list.isNumbered())
                {
                    writeEnd(HtmlTags.ORDEREDLIST);
                }
                else
                {
                    writeEnd(HtmlTags.UNORDEREDLIST);
                }
                return;
            }

            case Element.LISTITEM: {
                ListItem listItem = (ListItem)element;
                styleAttributes = new Properties();
                if (listItem.LeadingDefined)
                {
                    styleAttributes.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(listItem.leading()) + "pt");
                }

                // start tag
                addTabs(indent);
                writeStart(HtmlTags.LISTITEM);
                if (hasMarkupAttributes(listItem))
                {
                    writeMarkupAttributes((MarkupAttributes)listItem);
                }
                write(listItem.Font, styleAttributes);
                os.write(GT);
                currentfont.push(listItem.Font);
                // contents
                for (Iterator i = listItem.iterator(); i.hasNext();)
                {
                    write((Element)i.next(), indent + 1);
                }
                // end tag
                addTabs(indent);
                writeEnd(HtmlTags.LISTITEM);
                currentfont.pop();
                return;
            }

            case Element.CELL: {
                Cell cell = (Cell)element;

                // start tag
                addTabs(indent);
                if (cell.Header)
                {
                    writeStart(HtmlTags.HEADERCELL);
                }
                else
                {
                    writeStart(HtmlTags.CELL);
                }
                if (hasMarkupAttributes(cell))
                {
                    writeMarkupAttributes((MarkupAttributes)cell);
                }
                if (cell.borderWidth() != Rectangle.UNDEFINED)
                {
                    write(HtmlTags.BORDERWIDTH, String.valueOf(cell.borderWidth()));
                }
                if (cell.BorderColor != null)
                {
                    write(HtmlTags.BORDERCOLOR, HtmlEncoder.encode(cell.BorderColor));
                }
                if (cell.BackgroundColor != null)
                {
                    write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(cell.BackgroundColor));
                }
                String alignment = HtmlEncoder.getAlignment(cell.HorizontalAlignment);
                if (!"".equals(alignment))
                {
                    write(HtmlTags.HORIZONTALALIGN, alignment);
                }
                alignment = HtmlEncoder.getAlignment(cell.VerticalAlignment);
                if (!"".equals(alignment))
                {
                    write(HtmlTags.VERTICALALIGN, alignment);
                }
                if (cell.CellWidth != null)
                {
                    write(HtmlTags.WIDTH, cell.CellWidth);
                }
                if (cell.Colspan != 1)
                {
                    write(HtmlTags.COLSPAN, String.valueOf(cell.Colspan));
                }
                if (cell.Rowspan != 1)
                {
                    write(HtmlTags.ROWSPAN, String.valueOf(cell.Rowspan));
                }
                if (cell.NoWrap)
                {
                    write(HtmlTags.NOWRAP, String.valueOf(true));
                }
                os.write(GT);
                // contents
                if (cell.isEmpty())
                {
                    write(NBSP);
                }
                else
                {
                    for (Iterator i = cell.getElements(); i.hasNext();)
                    {
                        write((Element)i.next(), indent + 1);
                    }
                }
                // end tag
                addTabs(indent);
                if (cell.Header)
                {
                    writeEnd(HtmlTags.HEADERCELL);
                }
                else
                {
                    writeEnd(HtmlTags.CELL);
                }
                return;
            }

            case Element.ROW: {
                Row row = (Row)element;

                // start tag
                addTabs(indent);
                writeStart(HtmlTags.ROW);
                if (hasMarkupAttributes(row))
                {
                    writeMarkupAttributes((MarkupAttributes)row);
                }
                os.write(GT);
                // contents
                Element cell;
                for (int i = 0; i < row.columns(); i++)
                {
                    if ((cell = (Element)row.getCell(i)) != null)
                    {
                        write(cell, indent + 1);
                    }
                }
                // end tag
                addTabs(indent);
                writeEnd(HtmlTags.ROW);
                return;
            }

            case Element.TABLE: {
                Table table = (Table)element;
                table.complete();
                // start tag
                addTabs(indent);
                writeStart(HtmlTags.TABLE);
                if (hasMarkupAttributes(table))
                {
                    writeMarkupAttributes((MarkupAttributes)table);
                }
                os.write(SPACE);
                write(HtmlTags.WIDTH);
                os.write(EQUALS);
                os.write(QUOTE);
                if (!"".equals(table.absWidth()))
                {
                    write(table.absWidth());
                }
                else
                {
                    write(String.valueOf(table.widthPercentage()));
                    write("%");
                }
                os.write(QUOTE);
                String alignment = HtmlEncoder.getAlignment(table.alignment());
                if (!"".equals(alignment))
                {
                    write(HtmlTags.ALIGN, alignment);
                }
                write(HtmlTags.CELLPADDING, String.valueOf(table.cellpadding()));
                write(HtmlTags.CELLSPACING, String.valueOf(table.cellspacing()));
                if (table.borderWidth() != Rectangle.UNDEFINED)
                {
                    write(HtmlTags.BORDERWIDTH, String.valueOf(table.borderWidth()));
                }
                if (table.BorderColor != null)
                {
                    write(HtmlTags.BORDERCOLOR, HtmlEncoder.encode(table.BorderColor));
                }
                if (table.BackgroundColor != null)
                {
                    write(HtmlTags.BACKGROUNDCOLOR, HtmlEncoder.encode(table.BackgroundColor));
                }
                os.write(GT);
                // contents
                Row row;
                for (Iterator iterator = table.iterator(); iterator.hasNext();)
                {
                    row = (Row)iterator.next();
                    write(row, indent + 1);
                }
                // end tag
                addTabs(indent);
                writeEnd(HtmlTags.TABLE);
                return;
            }

            case Element.ANNOTATION: {
                Annotation annotation = (Annotation)element;
                writeComment(annotation.Title + ": " + annotation.Content);
                if (hasMarkupAttributes(annotation))
                {
                    os.write(BEGINCOMMENT);
                    writeMarkupAttributes((MarkupAttributes)annotation);
                    os.write(ENDCOMMENT);
                }
                return;
            }

            case Element.GIF:
            case Element.JPEG:
            case Element.PNG: {
                Image image = (Image)element;
                if (image.url() == null)
                {
                    return;
                }

                // start tag
                addTabs(indent);
                writeStart(HtmlTags.IMAGE);
                String path = image.url().toString();
                if (imagepath != null)
                {
                    if (path.indexOf("/") > 0)
                    {
                        path = imagepath + path.substring(path.lastIndexOf("/") + 1);
                    }
                    else
                    {
                        path = imagepath + path;
                    }
                }
                write(HtmlTags.URL, path);
                if ((image.alignment() & Image.LEFT) > 0)
                {
                    write(HtmlTags.ALIGN, HtmlTags.ALIGN_LEFT);
                }
                else if ((image.alignment() & Image.RIGHT) > 0)
                {
                    write(HtmlTags.ALIGN, HtmlTags.ALIGN_RIGHT);
                }
                else if ((image.alignment() & Image.MIDDLE) > 0)
                {
                    write(HtmlTags.ALIGN, HtmlTags.ALIGN_MIDDLE);
                }
                if (image.alt() != null)
                {
                    write(HtmlTags.ALT, image.alt());
                }
                write(HtmlTags.PLAINWIDTH, String.valueOf(image.scaledWidth()));
                write(HtmlTags.PLAINHEIGHT, String.valueOf(image.scaledHeight()));
                if (hasMarkupAttributes(image))
                {
                    writeMarkupAttributes((MarkupAttributes)image);
                }
                writeEnd();
                return;
            }

            default:
                return;
            }
        }