Example usage for com.lowagie.text.rtf.text RtfTab RtfTab

List of usage examples for com.lowagie.text.rtf.text RtfTab RtfTab

Introduction

In this page you can find the example usage for com.lowagie.text.rtf.text RtfTab RtfTab.

Prototype

public RtfTab(float position, int type) 

Source Link

Document

Constructs a new RtfTab with the given position and type.

Usage

From source file:ca.nines.ise.writer.RTFWriter.java

License:Open Source License

@Override
public void render(DOM dom, Annotation annotation) throws DocumentException, IOException {
    this.preprocess(dom, annotation);

    fontStack = new ArrayDeque<>();
    fontStack.push(FontFactory.getFont("Times New Roman", 12, Color.BLACK));
    Font font;/*from w ww.j  ava  2s.c om*/

    boolean inSP = false; // in speech prefix
    boolean inSD = false; // in stage direction
    boolean inDQ = false; // in a double quote
    boolean inS = false; // in a speech
    boolean inHW = false;
    char part = 'i';

    String mode = "verse";

    doc.open();
    startParagraph();

    for (Node n : dom) {
        switch (n.type()) {
        case ABBR:
            break;
        case CHAR:
            addChunk(n.unicode());
            break;
        case EMPTY:
            switch (n.getName()) {
            case "FNLOC":
                EmptyNode fnloc = (EmptyNode) n;
                Note note = annotation.get(Integer.parseInt(fnloc.getAttribute("ref")) - 1);
                if (!note.hasNoteLevel("1")) {
                    break;
                }
                this.footnote(note);
                break;
            case "TLN":
            case "L":
                if (mode.equals("prose")) {
                    break;
                }
                if (inS) {
                    startParagraph(p2);
                } else {
                    startParagraph(p1);
                }
                EmptyNode en = (EmptyNode) n;
                if (en.hasAttribute("part")) {
                    part = en.getAttribute("part").charAt(0);
                } else {
                    part = 'i';
                }
                break;
            }
            break;
        case END:
            switch (n.getName()) {
            case "FOREIGN":
                fontStack.pop();
                break;
            case "HW":
                inHW = false;
                break;
            case "I":
                fontStack.pop();
                break;
            case "LD":
                startParagraph();
                break;
            case "S":
                inS = false;
                break;
            case "SD":
                fontStack.pop();
                inSD = false;
                break;
            case "SP":
                addChunk(". ");
                inSP = false;
                break;
            }
            break;
        case START:
            switch (n.getName()) {
            case "FOREIGN":
                font = new Font(fontStack.getFirst());
                font.setStyle(Font.ITALIC);
                fontStack.push(font);
                break;
            case "HW":
                inHW = true;
                break;
            case "I":
                font = new Font(fontStack.getFirst());
                font.setStyle(Font.ITALIC);
                fontStack.push(font);
                break;
            case "LD":
                startParagraph(ld);
                break;
            case "MODE":
                mode = ((TagNode) n).getAttribute("t");
                break;
            case "S":
                if (mode.equals("prose")) {
                    startParagraph(prose);
                }
                inS = true;
                break;
            case "SD":
                font = new Font(fontStack.getFirst());
                font.setStyle(Font.ITALIC);
                StartNode start = (StartNode) n;
                if (start.hasAttribute("t") && start.getAttribute("t").contains("exit")) {
                    startParagraph(exit);
                }
                if (start.hasAttribute("t") && start.getAttribute("t").contains("optional")) {
                    font.setColor(Color.GRAY);
                }
                fontStack.push(font);
                inSD = true;
                break;
            case "SP":
                inSP = true;
                break;
            }
            break;
        case TEXT:
            String txt = n.getText();
            txt = txt.replace("--", "\u2014");
            txt = txt.replace("\n", "");

            if (inSP) {
                addChunk(txt.toUpperCase());
                break;
            }

            if (inHW) {
                txt = txt.replaceFirst("[(]", "");
                inHW = false;
            }

            if (inSD) {
                // DOES NOT MATCH AFTER A TAG.
                if ((txt.indexOf('[') >= 0) || (txt.indexOf(']') >= 0)) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < txt.length(); i++) {
                        char c = txt.charAt(i);
                        if (c == '[' || c == ']') {
                            if (sb.length() > 0) {
                                addChunk(sb.toString());
                                sb = new StringBuilder();
                            }
                            addDirect("{\\i0" + String.valueOf(c) + "}");
                        } else {
                            sb.append(c);
                        }
                    }
                    if (sb.length() > 0) {
                        addChunk(sb.toString());
                    }
                    break;
                } else {
                    addChunk(txt);
                    break;
                }
            }

            if (part != 'i' && inS) {
                RtfTab tab = null;
                String tabStr = "";
                if (part == 'm') {
                    tab = new RtfTab(100, RtfTab.TAB_LEFT_ALIGN);
                    tabStr = "\t";
                }
                if (part == 'f') {
                    tab = new RtfTab(200, RtfTab.TAB_LEFT_ALIGN);
                    tabStr = "\t\t";
                }
                if (tab != null) {
                    p.add(tab);
                    addChunk(tabStr);
                }
                part = 'i'; // ensure it's only done once for m or f.
            }

            // fix quotation marks.
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < txt.length(); i++) {
                char c = txt.charAt(i);
                switch (c) {
                case '"':
                    if (inDQ) {
                        // typographer's end quote.
                        sb.append("\u201D");
                        inDQ = false;
                    } else {
                        // typographer's start quote.
                        sb.append("\u201C");
                        inDQ = true;
                    }
                    break;
                case '\'':
                    sb.append("\u2019"); // appostrophe
                    break;
                default:
                    sb.append(c);
                }
            }
            addChunk(sb.toString());
            break;
        }
    }

    startParagraph();

    doc.close();
}