protected void writeRtfContent()

in fop-core/src/main/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfTextrun.java [396:510]


    protected void writeRtfContent() throws IOException {
        /**
         *TODO: The textrun's children are iterated twice:
         * 1. To determine the last RtfParagraphBreak
         * 2. To write the children
         * Maybe this can be done more efficient.
         */

        boolean bHasTableCellParent
            = this.getParentOfClass(RtfTableCell.class) != null;
        RtfAttributes attrBlockLevel = new RtfAttributes();

        //determine, if this RtfTextrun is the last child of its parent
        boolean bLast = false;
        for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
            if (it.next() == this) {
                bLast = !it.hasNext();
                break;
            }
        }

        //get last RtfParagraphBreak, which is not followed by any visible child
        RtfParagraphBreak lastParagraphBreak = null;
        if (bLast) {
            RtfElement aBefore = null;
            for (Object o : getChildren()) {
                final RtfElement e = (RtfElement) o;
                if (e instanceof RtfParagraphBreak) {
                    //If the element before was a paragraph break or a bookmark
                    //they will be hidden and are therefore not considered as visible
                    if (!(aBefore instanceof RtfParagraphBreak)
                            && !(aBefore instanceof RtfBookmark)) {
                        lastParagraphBreak = (RtfParagraphBreak) e;
                    }
                } else {
                    if (!(e instanceof RtfOpenGroupMark)
                            && !(e instanceof RtfCloseGroupMark)
                            && e.isEmpty()) {
                        lastParagraphBreak = null;
                    }
                }
                aBefore = e;
            }
        }

        //may contain for example \intbl
        writeAttributes(attrib, null);

        if (rtfListItem != null) {
            rtfListItem.getRtfListStyle().writeParagraphPrefix(this);
        }

        //write all children
        boolean bPrevPar = false;
        boolean bBookmark = false;
        boolean bFirst = true;
        for (Object o : getChildren()) {
            final RtfElement e = (RtfElement) o;
            final boolean bRtfParagraphBreak = (e instanceof RtfParagraphBreak);

            if (bHasTableCellParent) {
                attrBlockLevel.set(e.getRtfAttributes());
            }


            /**
             * -Write RtfParagraphBreak only, if the previous visible child
             * was't also a RtfParagraphBreak.
             * -Write RtfParagraphBreak only, if it is not the first visible
             * child.
             * -If the RtfTextrun is the last child of its parent, write a
             * RtfParagraphBreak only, if it is not the last child.
             * -If the RtfParagraphBreak can not be hidden (e.g. a table cell requires it)
             * it is also written
             */
            boolean bHide = false;
            bHide = bRtfParagraphBreak;
            bHide = bHide
                    && (bPrevPar
                    || bFirst
                    || (bSuppressLastPar && bLast && lastParagraphBreak != null
                    && e == lastParagraphBreak)
                    || bBookmark)
                    && ((RtfParagraphBreak) e).canHide();

            if (!bHide) {
                newLine();
                e.writeRtf();

                if (rtfListItem != null && e instanceof RtfParagraphBreak) {
                    rtfListItem.getRtfListStyle().writeParagraphPrefix(this);
                }
            }

            if (e instanceof RtfParagraphBreak) {
                bPrevPar = true;
            } else if (e instanceof RtfBookmark) {
                bBookmark = true;
            } else if (e instanceof RtfCloseGroupMark) {
                //do nothing
            } else if (e instanceof RtfOpenGroupMark) {
                //do nothing
            } else {
                bPrevPar = bPrevPar && e.isEmpty();
                bFirst = bFirst && e.isEmpty();
                bBookmark = false;
            }
        } //for (Iterator it = ...)

        //
        if (bHasTableCellParent) {
            writeAttributes(attrBlockLevel, null);
        }

    }