/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.rtf;

/**
 * Поддержка формата RTF
 */
public class RtfHelper {


    private static java.util.HashMap<Integer, Integer> m_LangIdToCodepage;

    private static void _manageEncodings(java.util.ArrayList<RtfItem> items) {
        java.util.HashMap<String, Integer> fontCharsets = new java.util.HashMap<String, Integer>();
        java.util.HashMap<String, com.pullenti.util.EncodingWrapper> fontEncodings = new java.util.HashMap<String, com.pullenti.util.EncodingWrapper>();
        String fontName = null;
        java.util.ArrayList<RtfEncoder> defs = new java.util.ArrayList<RtfEncoder>();
        java.util.ArrayList<RtfEncoder> curs = new java.util.ArrayList<RtfEncoder>();
        boolean isAnsi = false;
        for (int i = 0; i < items.size(); i++) {
            RtfItem it = items.get(i);
            while (curs.size() > 0) {
                if (curs.get(0).level > it.level) 
                    curs.remove(0);
                else 
                    break;
            }
            while (defs.size() > 0) {
                if (defs.get(0).level > it.level) 
                    defs.remove(0);
                else 
                    break;
            }
            if (it.typ == RftItemTyp.TEXT) {
                if (it.text == null && it.codes != null && it.codes.length > 0) {
                    if (it.codes.length > 5) {
                    }
                    RtfEncoder cur = (curs.size() > 0 ? curs.get(0) : null);
                    if (cur != null && cur.level > it.level) 
                        cur = null;
                    RtfEncoder def = (defs.size() > 0 ? defs.get(0) : null);
                    if (def != null && def.level > it.level) 
                        def = null;
                    com.pullenti.util.EncodingWrapper enc = (cur == null ? null : cur.encoding);
                    if (fontName != null && fontEncodings.containsKey(fontName)) 
                        enc = fontEncodings.get(fontName);
                    if (enc == null && def != null) 
                        enc = def.encoding;
                    if (enc == null) 
                        enc = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.CP1252, null, 0);
                    try {
                        String str = enc.getString(it.codes, 0, -1);
                        it.text = str;
                    } catch (Exception ex253) {
                    }
                }
                else if (i > 0 && items.get(i - 1).typ == RftItemTyp.COMMAND && items.get(i - 1).text.charAt(0) == 'f') {
                    if (it.text != null && it.text.endsWith(" Cyr;") && fontName != null) {
                        if (!fontEncodings.containsKey(fontName)) 
                            fontEncodings.put(fontName, new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.CP1251, null, 0));
                    }
                }
                continue;
            }
            if (it.typ != RftItemTyp.COMMAND) 
                continue;
            String cmd = it.text;
            if (cmd == null) 
                continue;
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "plain")) {
                if (curs.size() > 0) 
                    curs.remove(0);
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "ansi")) {
                isAnsi = true;
                continue;
            }
            if (cmd.startsWith("ansicpg")) {
                int cp;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcp255 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres256 = com.pullenti.unisharp.Utils.parseInteger(cmd.substring(7), 0, null, wrapcp255);
                cp = (wrapcp255.value != null ? wrapcp255.value : 0);
                if (!inoutres256) 
                    continue;
                defs.add(0, RtfEncoder._new254(it.level, new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, null, cp)));
                continue;
            }
            if (cmd.startsWith("deflangfe")) {
                int cp;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcp260 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres261 = com.pullenti.unisharp.Utils.parseInteger(cmd.substring(9), 0, null, wrapcp260);
                cp = (wrapcp260.value != null ? wrapcp260.value : 0);
                if (!inoutres261) 
                    continue;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcp258 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres259 = com.pullenti.unisharp.Utils.tryGetValue(m_LangIdToCodepage, cp, wrapcp258);
                cp = (wrapcp258.value != null ? wrapcp258.value : 0);
                if (!inoutres259) 
                    continue;
                defs.add(0, RtfEncoder._new254(it.level, new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, null, cp)));
                continue;
            }
            if (cmd.startsWith("deflang")) {
                int cp;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcp265 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres266 = com.pullenti.unisharp.Utils.parseInteger(cmd.substring(7), 0, null, wrapcp265);
                cp = (wrapcp265.value != null ? wrapcp265.value : 0);
                if (!inoutres266) 
                    continue;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcp263 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres264 = com.pullenti.unisharp.Utils.tryGetValue(m_LangIdToCodepage, cp, wrapcp263);
                cp = (wrapcp263.value != null ? wrapcp263.value : 0);
                if (!inoutres264) 
                    continue;
                defs.add(0, RtfEncoder._new254(it.level, new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, null, cp)));
                continue;
            }
            if (cmd.startsWith("langfe")) 
                continue;
            if (cmd.startsWith("lang")) {
                int cp;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcp270 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres271 = com.pullenti.unisharp.Utils.parseInteger(cmd.substring(4), 0, null, wrapcp270);
                cp = (wrapcp270.value != null ? wrapcp270.value : 0);
                if (!inoutres271) 
                    continue;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcp268 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres269 = com.pullenti.unisharp.Utils.tryGetValue(m_LangIdToCodepage, cp, wrapcp268);
                cp = (wrapcp268.value != null ? wrapcp268.value : 0);
                if (!inoutres269) 
                    continue;
                if ((i > 0 && items.get(i - 1).typ == RftItemTyp.COMMAND && items.get(i - 1).text.startsWith("f")) && curs.size() > 0 && curs.get(0).level == it.level) 
                    continue;
                curs.add(0, RtfEncoder._new254(it.level, new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, null, cp)));
                continue;
            }
            if (cmd.length() >= 2 && cmd.charAt(0) == 'f' && Character.isDigit(cmd.charAt(1))) {
                if (com.pullenti.unisharp.Utils.stringsEq(cmd, "f674")) {
                }
                if (fontCharsets.containsKey(cmd)) {
                    int cs = fontCharsets.get(cmd);
                    com.pullenti.util.EncodingWrapper enc = null;
                    switch (cs) { 
                    case 204:
                        enc = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.CP1251, null, 0);
                        break;
                    case 238:
                        enc = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, null, 1250);
                        break;
                    case 161:
                        enc = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, null, 1253);
                        break;
                    case 162:
                        enc = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, null, 1254);
                        break;
                }
                if (enc != null) 
                    curs.add(0, RtfEncoder._new254(it.level, enc));
                }
                else 
                    fontName = cmd;
                continue;
            }
            if (cmd.startsWith("fcharset") && fontName != null) {
                int cod;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapcod273 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                boolean inoutres274 = com.pullenti.unisharp.Utils.parseInteger(cmd.substring(8), 0, null, wrapcod273);
                cod = (wrapcod273.value != null ? wrapcod273.value : 0);
                if (inoutres274) {
                    if (!fontCharsets.containsKey(fontName)) 
                        fontCharsets.put(fontName, cod);
                }
                continue;
            }
        }
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < (items.size() - 1); i++) {
            RtfItem it = items.get(i);
            RtfItem it1 = items.get(i + 1);
            if ((it1.typ == RftItemTyp.TEXT && it.typ == RftItemTyp.TEXT && it1.text != null) && it.text != null) {
                tmp.setLength(0);
                tmp.append(it.text);
                for (int j = i + 1; j < items.size(); j++) {
                    it1 = items.get(j);
                    if (it1.typ != RftItemTyp.TEXT || it1.text == null) 
                        break;
                    tmp.append(it1.text);
                    com.pullenti.unisharp.Utils.putArrayValue(items, j, null);
                    i = j;
                }
                it.text = tmp.toString();
            }
        }
        int k = 0;
        for (int i = 0; i < items.size(); i++) {
            if (items.get(i) != null) {
                com.pullenti.unisharp.Utils.putArrayValue(items, k, items.get(i));
                k++;
            }
        }
        if (k < items.size()) 
            for(int jjj = k + items.size() - k - 1, mmm = k; jjj >= mmm; jjj--) items.remove(jjj);
    }

    public static class RtfEncoder {
    
        public int level;
    
        public com.pullenti.util.EncodingWrapper encoding;
    
        @Override
        public String toString() {
            return (((Integer)level).toString() + ": " + encoding.toString());
        }
    
        public static RtfEncoder _new254(int _arg1, com.pullenti.util.EncodingWrapper _arg2) {
            RtfEncoder res = new RtfEncoder();
            res.level = _arg1;
            res.encoding = _arg2;
            return res;
        }
        public RtfEncoder() {
        }
    }


    public static com.pullenti.unitext.UnitextDocument createUniDoc(com.pullenti.unisharp.Stream stream, com.pullenti.unitext.CreateDocumentParam pars) throws java.io.IOException {
        com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new47(com.pullenti.unitext.FileFormat.RTF);
        stream.setPosition(0L);
        java.util.ArrayList<RtfItem> items = RtfItem.parseList(stream);
        _manageEncodings(items);
        com.pullenti.unitext.internal.uni.UnitextGen cntGen = new com.pullenti.unitext.internal.uni.UnitextGen();
        com.pullenti.unitext.internal.uni.UnitextGen headGen = null;
        com.pullenti.unitext.internal.uni.UnitextGen footGen = null;
        StringBuilder _html = null;
        int i = 0;
        if (items.size() > 100 && items.get(1).typ == RftItemTyp.COMMAND && com.pullenti.unisharp.Utils.stringsEq(items.get(1).text, "rtf1")) {
            for (int ii = 2; ii < 20; ii++) {
                if (items.get(ii).typ == RftItemTyp.COMMAND && com.pullenti.unisharp.Utils.stringsEq(items.get(ii).text, "fromhtml1")) {
                    _html = new StringBuilder();
                    cntGen = null;
                    for (; (ii < 500) && ((ii + 1) < items.size()); ii++) {
                        if ((items.get(ii).typ == RftItemTyp.COMMAND && com.pullenti.unisharp.Utils.stringsEq(items.get(ii).text, "*") && items.get(ii + 1).typ == RftItemTyp.COMMAND) && items.get(ii + 1).text.startsWith("htmltag")) {
                            i = ii;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        for (; i < items.size(); i++) {
            RtfItem it = items.get(i);
            if (it.typ == RftItemTyp.COMMAND) {
                String cmd = it.text;
                if (cmd == null) 
                    continue;
                if (cmd.startsWith("header") || cmd.startsWith("footer")) {
                    if (!cmd.startsWith("headery") && !cmd.startsWith("footery")) {
                        int ii;
                        for (ii = i + 1; ii < items.size(); ii++) {
                            if (items.get(ii).level < it.level) 
                                break;
                        }
                        if (headGen == null && cmd.startsWith("head")) {
                            headGen = new com.pullenti.unitext.internal.uni.UnitextGen();
                            _manageUni(items, _html, headGen, i + 1, ii, false, 0);
                        }
                        else if (footGen == null && cmd.startsWith("foot")) {
                            footGen = new com.pullenti.unitext.internal.uni.UnitextGen();
                            _manageUni(items, _html, footGen, i + 1, ii, false, 0);
                        }
                        i = ii - 1;
                        continue;
                    }
                }
            }
            if (items.get(i) instanceof RtfTablerow) {
            }
            int j = _manageUni(items, _html, cntGen, i, i + 1, false, 0);
            i = j - 1;
        }
        if (_html == null) {
            doc.content = cntGen.finish(true, null);
            com.pullenti.unitext.UnitextPagesection sect = new com.pullenti.unitext.UnitextPagesection();
            if (headGen != null) {
                com.pullenti.unitext.UnitextItem fi = headGen.finish(true, null);
                if (fi != null) 
                    sect.items.add(com.pullenti.unitext.UnitextPagesectionItem._new48(fi));
            }
            if (footGen != null) {
                com.pullenti.unitext.UnitextItem fi = footGen.finish(true, null);
                if (fi != null) 
                    sect.items.add(com.pullenti.unitext.UnitextPagesectionItem._new277(fi, true));
            }
            if (sect.items.size() > 0) 
                doc.sections.add(sect);
        }
        else {
            String txt = _html.toString();
            com.pullenti.unitext.internal.html.HtmlNode nod = com.pullenti.unitext.internal.html.HtmlParser.parse(_html, false);
            if (nod == null) 
                return null;
            doc = com.pullenti.unitext.internal.html.HtmlHelper.create(nod, null, null, pars);
            doc.sourceFormat = com.pullenti.unitext.FileFormat.RTF;
        }
        doc.optimize(false, null);
        return doc;
    }

    public static class RtfTablerow extends com.pullenti.unitext.internal.rtf.RtfItem {
    
        public java.util.ArrayList<String> props = new java.util.ArrayList<String>();
    
        public boolean isLast() {
            return props.contains("lastrow");
        }
    
    
        @Override
        public String toString() {
            return (((Integer)level).toString() + ": TableRow" + (this.isLast() ? " Last" : ""));
        }
        public RtfTablerow() {
            super();
        }
    }


    public static class RtfListinf extends com.pullenti.unitext.internal.rtf.RtfItem {
    
        public java.util.ArrayList<String> props = new java.util.ArrayList<String>();
    
        @Override
        public String toString() {
            return (((Integer)level).toString() + ": Listitem " + text);
        }
        public RtfListinf() {
            super();
        }
    }


    private static int _manageUni(java.util.ArrayList<RtfItem> items, StringBuilder _html, com.pullenti.unitext.internal.uni.UnitextGen gen, int i0, int i1, boolean inTab, int lev) {
        int i;
        for (i = i0; i < i1; i++) {
            RtfItem it = items.get(i);
            if (it.typ == RftItemTyp.BRACKETOPEN || it.typ == RftItemTyp.BRACKETCLOSE) 
                continue;
            if (it.typ == RftItemTyp.PROXY) {
                gen.append(((RtfItemProxy)com.pullenti.unisharp.Utils.cast(it, RtfItemProxy.class)).uni, null, -1, false);
                continue;
            }
            if (it.typ == RftItemTyp.TEXT) {
                if (it.text != null) {
                    if (_html != null) 
                        com.pullenti.util.MiscHelper.correctHtmlValue(_html, it.text, false, false, false);
                    else 
                        gen.appendText(it.text, false);
                }
                continue;
            }
            if (it instanceof RtfItemImage) {
                RtfItemImage im = (RtfItemImage)com.pullenti.unisharp.Utils.cast(it, RtfItemImage.class);
                com.pullenti.unitext.UnitextImage img = com.pullenti.unitext.UnitextImage._new278(im.codes);
                if (im.width > 0 && im.height > 0) {
                    img.width = (((Integer)im.width).toString() + "pt");
                    img.height = (((Integer)im.height).toString() + "pt");
                }
                gen.append(img, null, -1, false);
                continue;
            }
            if (it.typ != RftItemTyp.COMMAND) 
                continue;
            String cmd = it.text;
            if (cmd == null) 
                continue;
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "annotation")) {
            }
            if ((com.pullenti.unisharp.Utils.stringsEq(cmd, "*") || cmd.startsWith("fonttbl") || com.pullenti.unisharp.Utils.stringsEq(cmd, "stylesheet")) || cmd.startsWith("nonshppict") || com.pullenti.unisharp.Utils.stringsEq(cmd, "deleted")) {
                int htmlOutLev = -1;
                int lastOutInd = -1;
                for (++i; i < items.size(); i++) {
                    if (items.get(i).level < it.level) {
                        i--;
                        break;
                    }
                    else if (items.get(i) instanceof RtfItemImage) {
                        if (com.pullenti.unisharp.Utils.stringsEq(cmd, "*")) {
                            RtfItemImage im = (RtfItemImage)com.pullenti.unisharp.Utils.cast(items.get(i), RtfItemImage.class);
                            com.pullenti.unitext.UnitextImage img = com.pullenti.unitext.UnitextImage._new278(items.get(i).codes);
                            if (im.width > 0 && im.height > 0) {
                                img.width = (((Integer)im.width).toString() + "pt");
                                img.height = (((Integer)im.height).toString() + "pt");
                            }
                            gen.append(img, null, -1, false);
                        }
                    }
                    else if (items.get(i).typ == RftItemTyp.COMMAND) {
                        if (com.pullenti.unisharp.Utils.stringsEq(items.get(i).text, "footnote")) {
                            i--;
                            break;
                        }
                        if (com.pullenti.unisharp.Utils.stringsEq(items.get(i).text, "annotation")) {
                            i--;
                            break;
                        }
                        if (com.pullenti.unisharp.Utils.stringsEq(items.get(i).text, "shptxt")) {
                            i--;
                            break;
                        }
                        if (com.pullenti.unisharp.Utils.stringsEq(items.get(i).text, "fldinst")) {
                            i--;
                            break;
                        }
                        if (_html != null && items.get(i).text.startsWith("htmltag")) 
                            htmlOutLev = items.get(i).level;
                    }
                    else if (items.get(i).typ == RftItemTyp.TEXT && _html != null) {
                        _html.append(items.get(i).text);
                        lastOutInd = i;
                    }
                }
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "fldinst")) {
                java.util.ArrayList<RtfItem> iii = new java.util.ArrayList<RtfItem>();
                int ii;
                for (ii = i + 1; (ii < items.size()) && (ii < i1); ii++) {
                    if (items.get(ii).level < it.level) 
                        break;
                    else {
                        iii.add(items.get(ii));
                        if (items.get(ii).text != null && items.get(ii).text.startsWith("SYMBOL")) {
                            String sss = items.get(ii).text.substring(6).trim();
                            int jj = sss.indexOf(' ');
                            if (jj > 0) 
                                sss = sss.substring(0, 0 + jj);
                            com.pullenti.unisharp.Outargwrapper<Integer> wrapjj280 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                            boolean inoutres281 = com.pullenti.unisharp.Utils.parseInteger(sss, 0, null, wrapjj280);
                            jj = (wrapjj280.value != null ? wrapjj280.value : 0);
                            if (inoutres281) {
                                char chu = (char)0;
                                if ((items.get(ii).text.indexOf("Wingdings") >= 0)) 
                                    chu = com.pullenti.unitext.internal.misc.WingdingsHelper.getUnicode(jj);
                                else 
                                    chu = com.pullenti.unitext.internal.misc.SymbolHelper.getUnicode(jj);
                                if (chu == ((char)0)) 
                                    chu = '?';
                                gen.appendText((String.valueOf(chu)), false);
                            }
                        }
                    }
                }
                i = ii - 1;
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "field")) {
                java.util.ArrayList<RtfItem> iii = new java.util.ArrayList<RtfItem>();
                int ii;
                String ur = null;
                for (ii = i + 1; ii < items.size(); ii++) {
                    if (items.get(ii).level < it.level) 
                        break;
                    else {
                        iii.add(items.get(ii));
                        if (items.get(ii).typ == RftItemTyp.TEXT && items.get(ii).text != null && items.get(ii).text.trim().startsWith("HYPERLINK")) {
                            String fff = items.get(ii).text;
                            int jj0 = fff.indexOf('"');
                            int jj1 = fff.lastIndexOf('"');
                            if (jj0 > 0 && jj1 > jj0) {
                                String hy = fff.substring(jj0 + 1, jj0 + 1 + jj1 - jj0 - 1);
                                ur = hy;
                            }
                            else {
                                fff = fff.substring(9).trim();
                                if (fff.startsWith("http")) 
                                    ur = fff;
                                else {
                                }
                            }
                            if (ur == null) {
                            }
                        }
                    }
                }
                com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                _manageUni(iii, null, gg, 0, iii.size(), false, lev + 1);
                com.pullenti.unitext.UnitextItem cnt = gg.finish(true, null);
                if (cnt != null) {
                    StringBuilder tmp = new StringBuilder();
                    cnt.getPlaintext(tmp, new com.pullenti.unitext.GetPlaintextParam());
                    if (ur == null || tmp.length() > 300) 
                        gen.append(cnt, null, -1, false);
                    else {
                        com.pullenti.unitext.UnitextHyperlink hr = com.pullenti.unitext.UnitextHyperlink._new59(ur);
                        hr.content = cnt;
                        if (gen != null) 
                            gen.append(hr, null, -1, false);
                        else if (_html != null) 
                            cnt.getPlaintext(_html, null);
                    }
                    i = ii - 1;
                }
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "annotation")) {
                java.util.ArrayList<RtfItem> iii = new java.util.ArrayList<RtfItem>();
                int ii;
                for (ii = i; ii < items.size(); ii++) {
                    if (items.get(ii).level < (it.level - 1)) 
                        break;
                    else 
                        iii.add(items.get(ii));
                }
                com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                _manageUni(items, _html, gg, i + 1, ii, false, lev);
                com.pullenti.unitext.UnitextItem cnt = gg.finish(false, null);
                if (cnt != null) {
                    StringBuilder tmp = new StringBuilder();
                    cnt.getPlaintext(tmp, null);
                    if (tmp.length() > 0) 
                        gen.append(com.pullenti.unitext.UnitextComment._new50(tmp.toString()), null, -1, false);
                }
                i = ii - 1;
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "shptxt")) {
                java.util.ArrayList<RtfItem> iii = new java.util.ArrayList<RtfItem>();
                int ii;
                for (ii = i; (ii < items.size()) && (ii < i1); ii++) {
                    if (items.get(ii).level < (it.level - 1)) 
                        break;
                    else 
                        iii.add(items.get(ii));
                }
                com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                _manageUni(items, _html, gg, i + 1, ii, false, lev);
                com.pullenti.unitext.UnitextItem cnt = gg.finish(true, null);
                if (cnt instanceof com.pullenti.unitext.UnitextContainer) 
                    ((com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(cnt, com.pullenti.unitext.UnitextContainer.class)).typ = com.pullenti.unitext.UnitextContainerType.SHAPE;
                else if (cnt != null) {
                    com.pullenti.unitext.UnitextContainer cnt2 = com.pullenti.unitext.UnitextContainer._new94(com.pullenti.unitext.UnitextContainerType.SHAPE);
                    cnt2.children.add(cnt);
                    cnt = cnt2.optimize(false, null);
                }
                if (cnt != null) 
                    gen.append(cnt, null, -1, false);
                i = ii - 1;
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "footnote")) {
                java.util.ArrayList<RtfItem> iii = new java.util.ArrayList<RtfItem>();
                int ii;
                boolean endnote = false;
                for (ii = i; (ii < items.size()) && (ii < i1); ii++) {
                    if (items.get(ii).level <= (it.level - 1)) 
                        break;
                    else {
                        iii.add(items.get(ii));
                        if (items.get(ii).typ == RftItemTyp.COMMAND && com.pullenti.unisharp.Utils.stringsEq(items.get(ii).text, "ftnalt")) 
                            endnote = true;
                    }
                }
                com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                _manageUni(items, _html, gg, i + 1, ii, false, lev);
                com.pullenti.unitext.UnitextItem cnt = gg.finish(false, null);
                if (cnt != null) {
                    com.pullenti.unitext.UnitextFootnote fn = com.pullenti.unitext.UnitextFootnote._new285(cnt, endnote);
                    if (cnt instanceof com.pullenti.unitext.UnitextContainer) {
                        com.pullenti.unitext.UnitextContainer cc = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(cnt, com.pullenti.unitext.UnitextContainer.class);
                        if (cc.children.size() > 1 && (cc.children.get(0) instanceof com.pullenti.unitext.UnitextPlaintext) && ((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(cc.children.get(0), com.pullenti.unitext.UnitextPlaintext.class)).typ == com.pullenti.unitext.UnitextPlaintextType.SUP) {
                            fn.customMark = ((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(cc.children.get(0), com.pullenti.unitext.UnitextPlaintext.class)).text;
                            cc.children.remove(0);
                            fn.content = cc.optimize(true, null);
                        }
                    }
                    gen.append(fn, null, -1, false);
                }
                i = ii - 1;
                continue;
            }
            if (cmd.startsWith("listtext")) {
                String listId = null;
                int listLevel = 0;
                int ii;
                boolean ok = false;
                boolean endOfNum = false;
                StringBuilder num = new StringBuilder();
                java.util.ArrayList<RtfItem> tmp = new java.util.ArrayList<RtfItem>();
                for (ii = i + 1; ii < items.size(); ii++) {
                    RtfItem itt = items.get(ii);
                    tmp.add(itt);
                    if (itt.typ == RftItemTyp.TEXT) {
                        if (listId != null || endOfNum) {
                            ok = true;
                            break;
                        }
                        num.append((itt.text != null ? itt.text : ""));
                        continue;
                    }
                    if (itt.typ == RftItemTyp.BRACKETCLOSE && num.length() > 0) 
                        endOfNum = true;
                    if (itt.typ != RftItemTyp.COMMAND) 
                        continue;
                    if (inTab) {
                        if ((com.pullenti.unisharp.Utils.stringsEq(itt.text, "cell") || com.pullenti.unisharp.Utils.stringsEq(itt.text, "row") || com.pullenti.unisharp.Utils.stringsEq(itt.text, "trowd")) || com.pullenti.unisharp.Utils.stringsEq(itt.text, "intbl") || com.pullenti.unisharp.Utils.stringsEq(itt.text, "nestcell")) 
                            break;
                    }
                    if (itt.text.startsWith("ls") && itt.text.length() > 2 && Character.isDigit(itt.text.charAt(2))) {
                        if (listId != null) 
                            break;
                        listId = itt.text.substring(2);
                        continue;
                    }
                    if (itt.text.startsWith("ilvl") && itt.text.length() > 4 && Character.isDigit(itt.text.charAt(4))) {
                        com.pullenti.unisharp.Outargwrapper<Integer> wraplistLevel286 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                        com.pullenti.unisharp.Utils.parseInteger(itt.text.substring(4), 0, null, wraplistLevel286);
                        listLevel = (wraplistLevel286.value != null ? wraplistLevel286.value : 0);
                        continue;
                    }
                    if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "listtext")) 
                        break;
                }
                if (ok) {
                    int jj;
                    for (jj = ii + 1; jj < items.size(); jj++) {
                        RtfItem itt = items.get(jj);
                        tmp.add(itt);
                        if (itt.typ != RftItemTyp.COMMAND) 
                            continue;
                        if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "par")) {
                            jj++;
                            break;
                        }
                        if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "sect")) 
                            break;
                        if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "listtext") || com.pullenti.unisharp.Utils.stringsEq(itt.text, "trowd")) 
                            break;
                        if (itt.text.startsWith("ls")) 
                            break;
                    }
                    if ((ii > 2 && items.get(ii - 1).typ == RftItemTyp.COMMAND && items.get(ii - 2).typ == RftItemTyp.COMMAND) && com.pullenti.unisharp.Utils.stringsEq(items.get(ii - 2).text, "*")) 
                        ii -= 2;
                    com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                    _manageUni(items, _html, gg, ii, jj, false, lev);
                    gen.appendListItem(gg.finish(true, null), num.toString().trim(), listId, listLevel, null);
                    i = jj - 1;
                    continue;
                }
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "line")) {
                if (_html != null) 
                    _html.append("\r\n");
                else 
                    gen.appendNewline(false, false);
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "page") || com.pullenti.unisharp.Utils.stringsEq(cmd, "sect")) {
                gen.appendPagebreak();
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "par")) {
                if (_html != null) 
                    _html.append("\r\n");
                else 
                    gen.appendNewline(false, false);
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "brdrt") || com.pullenti.unisharp.Utils.stringsEq(cmd, "brdrb")) {
                if (_html != null) 
                    _html.append("\r\n<hr/>");
                else 
                    gen.append(com.pullenti.unitext.UnitextMisc._new287(com.pullenti.unitext.UnitextMiscType.HORIZONTALLINE), null, -1, false);
                continue;
            }
            if (com.pullenti.unisharp.Utils.stringsEq(cmd, "intbl") && !inTab) {
            }
            if (((com.pullenti.unisharp.Utils.stringsEq(cmd, "trowd") && !inTab)) || ((com.pullenti.unisharp.Utils.stringsEq(cmd, "intbl") && !inTab))) {
                if (lev < 5) {
                    if (lev > 10) {
                    }
                    RichTextTable rtfTab = new RichTextTable();
                    RichTextRow rtfRow = new RichTextRow();
                    int ii = _manageTableRow(items, i, rtfRow, lev + 1);
                    if (ii > i) {
                        rtfTab.addChild(rtfRow);
                        i = ii;
                        while (i < items.size()) {
                            if (rtfRow.lastRow) 
                                break;
                            rtfRow = new RichTextRow();
                            ii = _manageTableRow(items, i, rtfRow, lev + 1);
                            if (ii < 0) 
                                break;
                            rtfTab.addChild(rtfRow);
                            if (ii <= i) 
                                break;
                            i = ii;
                        }
                        rtfTab.correct();
                        com.pullenti.unitext.UnitextTable tab = _createTable(rtfTab);
                        if (gen != null) 
                            gen.append(tab, null, -1, false);
                        continue;
                    }
                }
            }
            int sub = -1;
            if (cmd.startsWith("super") || cmd.startsWith("up")) 
                sub = 0;
            else if (cmd.startsWith("sub")) 
                sub = 1;
            if (sub >= 0) {
                int ii;
                boolean ok = false;
                StringBuilder tmp = new StringBuilder();
                for (ii = i + 1; ii < items.size(); ii++) {
                    if (items.get(ii).typ == RftItemTyp.BRACKETCLOSE || items.get(ii).typ == RftItemTyp.BRACKETOPEN) {
                        ok = true;
                        break;
                    }
                    else if (items.get(ii).typ == RftItemTyp.TEXT) 
                        tmp.append((items.get(ii).text != null ? items.get(ii).text : ""));
                    else if (items.get(ii).typ != RftItemTyp.COMMAND) 
                        break;
                    else if (com.pullenti.unisharp.Utils.stringsEq(items.get(ii).text, "pard")) {
                        ok = true;
                        break;
                    }
                    else if (com.pullenti.unisharp.Utils.stringsEq(items.get(ii).text, "footnote")) 
                        break;
                }
                if (ok) {
                    String tt = tmp.toString().trim();
                    if (tt.length() > 0 && (tt.length() < 10)) {
                        gen.append(com.pullenti.unitext.UnitextPlaintext._new58(tt, (sub > 0 ? com.pullenti.unitext.UnitextPlaintextType.SUB : com.pullenti.unitext.UnitextPlaintextType.SUP)), null, -1, false);
                        i = ii - 1;
                    }
                }
                continue;
            }
        }
        return i;
    }

    private static com.pullenti.unitext.UnitextTable _createTable(RichTextTable tbl) {
        com.pullenti.unitext.UnitextTable res = new com.pullenti.unitext.UnitextTable();
        int rn = 0;
        for (RichTextItem rr : tbl.children) {
            int cn = 0;
            for (RichTextItem c : rr.children) {
                for (; ; cn++) {
                    if (res.getCell(rn, cn) == null) 
                        break;
                }
                com.pullenti.unitext.UnitextTablecell cel = res.addCell(rn, (rn + ((RichTextCell)com.pullenti.unisharp.Utils.cast(c, RichTextCell.class)).rowsSpan) - 1, cn, (cn + ((RichTextCell)com.pullenti.unisharp.Utils.cast(c, RichTextCell.class)).colsSpan) - 1, (com.pullenti.unitext.UnitextItem)com.pullenti.unisharp.Utils.cast(c.tag, com.pullenti.unitext.UnitextItem.class), false);
                if (cel != null) 
                    ((RichTextCell)com.pullenti.unisharp.Utils.cast(c, RichTextCell.class)).resColIndex = cel.getColBegin();
            }
            rn++;
        }
        if (res.getColsCount() > 1) {
            int[] wi = new int[res.getColsCount()];
            for (int kk = 0; kk < 3; kk++) {
                for (RichTextItem rr : tbl.children) {
                    RichTextRow r = (RichTextRow)com.pullenti.unisharp.Utils.cast(rr, RichTextRow.class);
                    if (r == null) 
                        continue;
                    if (r.cellsInfo.size() != r.children.size()) 
                        continue;
                    int sum = 0;
                    for (RichTextRow.CellInfo ci : r.cellsInfo) {
                        sum += ci.width;
                    }
                    if (sum <= 0) 
                        continue;
                    for (int i = 0; i < r.children.size(); i++) {
                        RichTextCell c = (RichTextCell)com.pullenti.unisharp.Utils.cast(r.children.get(i), RichTextCell.class);
                        if ((c.resColIndex < 0) || c.resColIndex >= wi.length) 
                            continue;
                        int w = (r.cellsInfo.get(i).width * 100) / sum;
                        int co = 0;
                        for (int j = c.resColIndex; (j < (c.resColIndex + c.colsSpan)) && (j < wi.length); j++) {
                            if (wi[j] > 0) 
                                w -= wi[j];
                            else 
                                co++;
                        }
                        if (co == 1 && w > 0) {
                            for (int j = c.resColIndex; (j < (c.resColIndex + c.colsSpan)) && (j < wi.length); j++) {
                                if (wi[j] == 0) {
                                    wi[j] = w;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            for (int i = 0; i < wi.length; i++) {
                if (wi[i] > 0) 
                    res.setColWidth(i, (((Integer)wi[i]).toString() + "%"));
            }
        }
        return res;
    }

    private static int _manageTableRow(java.util.ArrayList<RtfItem> items, int i, RichTextRow rtfRow, int lev) {
        boolean ok = false;
        for (; i < items.size(); i++) {
            RtfItem it = items.get(i);
            if (it.typ == RftItemTyp.COMMAND && ((com.pullenti.unisharp.Utils.stringsEq(it.text, "trowd") || com.pullenti.unisharp.Utils.stringsEq(it.text, "intbl")))) {
                ok = true;
                break;
            }
            if (it.typ == RftItemTyp.TEXT || it.typ == RftItemTyp.IMAGE) 
                break;
        }
        if (!ok) 
            return -1;
        java.util.ArrayList<Integer> colWidth = new java.util.ArrayList<Integer>();
        for (; i < items.size(); i++) {
            RtfItem it = items.get(i);
            if (it.typ == RftItemTyp.COMMAND && com.pullenti.unisharp.Utils.stringsEq(it.text, "row")) 
                return i + 1;
            int j;
            ok = false;
            for (j = i; j < items.size(); j++) {
                RtfItem itt = items.get(j);
                if (itt.typ != RftItemTyp.COMMAND) 
                    continue;
                if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "cell")) {
                    ok = true;
                    break;
                }
                if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "row")) {
                    j--;
                    break;
                }
                if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "nestcell")) {
                    RichTextRow nRow = new RichTextRow();
                    int jj = _manageNestedTableRow(items, i, nRow, lev + 1);
                    if (jj > 0) {
                        RichTextTable nTab = new RichTextTable();
                        nTab.addChild(nRow);
                        while (true) {
                            nRow = new RichTextRow();
                            int jjj = _manageNestedTableRow(items, jj, nRow, lev + 1);
                            if (jjj < 0) 
                                break;
                            nTab.addChild(nRow);
                            jj = jjj;
                        }
                        nTab.correct();
                        RtfItemProxy ppp = RtfItemProxy._new289(RftItemTyp.PROXY);
                        ppp.uni = _createTable(nTab);
                        for(int jjj = i + 1 + jj - i - 1 - 1, mmm = i + 1; jjj >= mmm; jjj--) items.remove(jjj);
                        com.pullenti.unisharp.Utils.putArrayValue(items, i, ppp);
                        continue;
                    }
                }
                if (rtfRow != null) 
                    rtfRow._addCmd(itt.text);
            }
            if (ok && rtfRow != null) {
                com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                _manageUni(items, null, gg, i, j, true, lev);
                RichTextCell cel = new RichTextCell();
                cel.tag = gg.finish(true, null);
                cel.endOf = true;
                rtfRow.addChild(cel);
            }
            i = j;
        }
        return -1;
    }

    public static class RtfItemProxy extends com.pullenti.unitext.internal.rtf.RtfItem {
    
        public com.pullenti.unitext.UnitextItem uni;
    
        public static RtfItemProxy _new289(com.pullenti.unitext.internal.rtf.RftItemTyp _arg1) {
            RtfItemProxy res = new RtfItemProxy();
            res.typ = _arg1;
            return res;
        }
        public RtfItemProxy() {
            super();
        }
    }


    private static int _manageNestedTableRow(java.util.ArrayList<RtfItem> items, int i, RichTextRow rtfRow, int lev) {
        boolean endOfRow = false;
        for (; i < items.size(); i++) {
            RtfItem it = items.get(i);
            if (it.typ == RftItemTyp.COMMAND && ((com.pullenti.unisharp.Utils.stringsEq(it.text, "row") || com.pullenti.unisharp.Utils.stringsEq(it.text, "cell")))) 
                return -1;
            int j;
            boolean ok = false;
            for (j = i; j < items.size(); j++) {
                RtfItem itt = items.get(j);
                if (itt.typ != RftItemTyp.COMMAND) 
                    continue;
                if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "nestcell")) {
                    ok = true;
                    break;
                }
                if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "row") || com.pullenti.unisharp.Utils.stringsEq(itt.text, "cell")) 
                    return -1;
                if (com.pullenti.unisharp.Utils.stringsEq(itt.text, "nesttableprops") || com.pullenti.unisharp.Utils.stringsEq(itt.text, "nestrow")) {
                    endOfRow = true;
                    break;
                }
            }
            if (ok) {
                com.pullenti.unitext.internal.uni.UnitextGen gg = new com.pullenti.unitext.internal.uni.UnitextGen();
                _manageUni(items, null, gg, i, j, true, lev + 1);
                RichTextCell cel = new RichTextCell();
                cel.tag = gg.finish(true, null);
                cel.endOf = true;
                rtfRow.addChild(cel);
            }
            i = j;
            if (endOfRow) 
                break;
        }
        if (!endOfRow) 
            return -1;
        for (; i < items.size(); i++) {
            RtfItem it = items.get(i);
            if (it.typ != RftItemTyp.COMMAND) 
                continue;
            if (com.pullenti.unisharp.Utils.stringsEq(it.text, "row") || com.pullenti.unisharp.Utils.stringsEq(it.text, "cell")) 
                return -1;
            if (com.pullenti.unisharp.Utils.stringsEq(it.text, "nestrow")) 
                return i + 1;
            rtfRow._addCmd(it.text);
        }
        return -1;
    }
    public RtfHelper() {
    }
    public static RtfHelper _globalInstance;
    
    static {
        try { _globalInstance = new RtfHelper(); } 
        catch(Exception e) { }
        m_LangIdToCodepage = new java.util.HashMap<Integer, Integer>();
        for (int i : new int[] {1052, 1050, 1029, 1038, 1045, 1048, 2074, 1051, 1060}) {
            m_LangIdToCodepage.put(i, 1250);
        }
        for (int i : new int[] {2092, 1059, 1026, 1071, 1087, 1088, 1104, 1049, 3098, 1092, 1058, 2115}) {
            m_LangIdToCodepage.put(i, 1251);
        }
        for (int i : new int[] {1078, 1069, 1027, 1030, 2067, 1043, 3081, 10249, 4105, 9225, 2057, 6153, 8201, 5129, 13321, 7177, 11273, 1033, 12297, 1080, 1035, 2060, 3084, 1036, 5132, 6156, 4108, 1110, 3079, 1031, 5127, 4103, 2055, 1039, 1057, 1040, 2064, 2110, 1086, 1044, 2068, 1046, 2070, 11274, 16394, 13322, 9226, 5130, 7178, 12298, 17418, 4106, 18442, 2058, 19466, 6154, 15370, 10250, 20490, 1034, 14346, 8202, 1089, 2077, 1053}) {
            m_LangIdToCodepage.put(i, 1252);
        }
        m_LangIdToCodepage.put(1032, 1253);
        for (int i : new int[] {1068, 1055, 1091}) {
            m_LangIdToCodepage.put(i, 1254);
        }
        m_LangIdToCodepage.put(1037, 1255);
        for (int i : new int[] {5121, 15361, 3073, 2049, 11265, 13313, 12289, 4097, 6145, 8193, 16385, 1025, 10241, 7169, 14337, 9217, 1065, 1056}) {
            m_LangIdToCodepage.put(i, 1256);
        }
        for (int i : new int[] {1061, 1062, 1063, 1066}) {
            m_LangIdToCodepage.put(i, 1257);
        }
    }
}
