/*
 * 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;

/**
 * Фрагмент плоского текста.
 * 
 */
public class UnitextPlaintext extends UnitextItem {

    /**
     * Текст (без переходов на новую строку и спецсимволов, за исключением табуляции)
     * 
     */
    public String text;

    /**
     * Тип текста
     */
    public UnitextPlaintextType typ = UnitextPlaintextType.SIMPLE;

    /**
     * Если есть нижележаший тексто-графический слой, то здесь ссылки для прямоугольники, для 
     * каждого символа Text идёт ссылка на элемент, которому символ принадлежит. 
     * То есть если Layout != null, то layout.Length = Text.Length.
     */
    public UnilayRectangle[] layout;

    @Override
    public UnitextItem clone() {
        UnitextPlaintext res = new UnitextPlaintext();
        res._cloneFrom(this);
        res.text = text;
        res.typ = typ;
        res.layout = layout;
        return res;
    }

    @Override
    public boolean isWhitespaces() {
        if (text == null) 
            return true;
        for (char ch : text.toCharArray()) {
            if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                return false;
        }
        return true;
    }


    @Override
    public String getInnerTag() {
        return "txt";
    }


    public void mergeWith(UnitextPlaintext pt) {
        if (pt.text == null || text == null) 
            return;
        if (pt.layout != null || layout != null) {
            UnilayRectangle[] lay = new UnilayRectangle[pt.text.length() + text.length()];
            if (layout != null) {
                for (int i = 0; i < layout.length; i++) {
                    if (i < lay.length) 
                        lay[i] = layout[i];
                }
            }
            if (pt.layout != null) {
                for (int i = 0; i < pt.layout.length; i++) {
                    if ((text.length() + i) < lay.length) 
                        lay[text.length() + i] = pt.layout[i];
                }
            }
            layout = lay;
        }
        if (isWhitespaces() && !pt.isWhitespaces()) 
            m_StyledFrag = pt.m_StyledFrag;
        else if (!isWhitespaces() && pt.isWhitespaces()) {
        }
        else if (m_StyledFrag == null) 
            m_StyledFrag = pt.m_StyledFrag;
        else if (pt.m_StyledFrag != null && com.pullenti.unisharp.Utils.stringsNe(m_StyledFrag.toString(), pt.m_StyledFrag.toString())) {
            if (m_StyledFrag.parent != null && pt.m_StyledFrag.parent == m_StyledFrag.parent) {
                m_StyledFrag.parent.children.remove(pt.m_StyledFrag);
                pt.m_StyledFrag.parent = m_StyledFrag;
                m_StyledFrag.children.add(pt.m_StyledFrag);
                pt.m_StyledFrag.dispInline = text.length();
                pt.m_StyledFrag.lenInline = pt.text.length();
            }
        }
        text += pt.text;
        if (pt.id != null && id == null) 
            id = pt.id;
        if (pt.endChar > endChar) 
            endChar = pt.endChar;
    }

    public UnitextPlaintext removeStart(int len) {
        if (text.length() <= len) 
            return null;
        UnitextPlaintext b = new UnitextPlaintext();
        b.parent = parent;
        b.beginChar = beginChar;
        b.endChar = (b.beginChar + len) - 1;
        b.text = text.substring(0, 0 + (b.endChar - b.beginChar) + 1);
        b.typ = typ;
        if (id != null) 
            b.id = (id + "_" + b.beginChar + "_" + b.endChar);
        else 
            b.id = ("txtS_" + b.beginChar + "_" + b.endChar);
        if (layout != null) {
            if (layout.length <= len) 
                layout = null;
            else {
                b.layout = new UnilayRectangle[len];
                for (int i = 0; (i < len) && (i < layout.length); i++) {
                    b.layout[i] = layout[i];
                }
                UnilayRectangle[] lay = new UnilayRectangle[layout.length - len];
                for (int i = 0; i < lay.length; i++) {
                    lay[i] = layout[len + i];
                }
                layout = lay;
            }
        }
        int le = (b.endChar - b.beginChar) + 1;
        text = text.substring(le);
        beginChar += le;
        return b;
    }

    public UnitextPlaintext removeEnd(int len) {
        UnitextPlaintext e = new UnitextPlaintext();
        e.parent = parent;
        e.beginChar = (endChar + 1) - len;
        e.endChar = (e.beginChar + len) - 1;
        int dd = (((endChar - beginChar) + 1)) - (((e.endChar - e.beginChar) + 1));
        if (dd >= text.length()) 
            return null;
        e.text = text.substring(dd);
        e.typ = typ;
        if (id != null) 
            e.id = (id + "_" + e.beginChar + "_" + e.endChar);
        else 
            e.id = ("txtE_" + e.beginChar + "_" + e.endChar);
        if (layout != null) {
            e.layout = new UnilayRectangle[len];
            for (int i = 0; i < len; i++) {
                int j = (layout.length - len) + i;
                if (j >= 0 && (j < layout.length)) 
                    e.layout[i] = layout[j];
            }
            if ((layout.length - len) > 0) {
                UnilayRectangle[] lay = new UnilayRectangle[layout.length - len];
                for (int i = 0; i < lay.length; i++) {
                    if (i < layout.length) 
                        lay[i] = layout[i];
                }
                layout = lay;
            }
            else 
                layout = null;
        }
        endChar -= len;
        text = text.substring(0, 0 + (endChar - beginChar) + 1);
        return e;
    }

    @Override
    public String toString() {
        String ttt = com.pullenti.unisharp.Utils.trimStart(((text != null ? text : "")));
        if (ttt.length() > 100) 
            ttt = ttt.substring(0, 0 + 100) + "...";
        if (typ == UnitextPlaintextType.SIMPLE) 
            return ("'" + ttt + "'");
        else if (typ == UnitextPlaintextType.GENERATED) 
            return ("[" + ttt + "]");
        else 
            return ("<" + ttt + ">");
    }

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) {
            beginChar = res.length();
            if (beginChar == 793) {
            }
        }
        if (pars != null && !pars.keepIgnored && ((ignorable || pars.ignoreLevel > 0))) {
            for (int i = 0; i < text.length(); i++) {
                res.append(' ');
            }
        }
        else if (pars != null && pars.supTemplate != null && typ == UnitextPlaintextType.SUP) {
            String txt = (((text != null ? text : ""))).trim();
            if (!com.pullenti.unisharp.Utils.isNullOrEmpty(txt)) 
                res.append(pars.supTemplate.replace("%1", txt));
        }
        else if (pars != null && pars.subTemplate != null && typ == UnitextPlaintextType.SUB) {
            String txt = (((text != null ? text : ""))).trim();
            if (!com.pullenti.unisharp.Utils.isNullOrEmpty(txt)) 
                res.append(pars.subTemplate.replace("%1", txt));
        }
        else if (text != null && typ != UnitextPlaintextType.XML) {
            String txt = text;
            if (pars != null && pars.pageBreak != null) {
                if (pars.pageBreak.length() == 1 && txt.indexOf(pars.pageBreak.charAt(0)) >= 0) 
                    txt = txt.replace(pars.pageBreak.charAt(0), ' ');
            }
            res.append(txt);
        }
        if (pars != null && pars.setPositions) 
            endChar = res.length() - 1;
    }

    private UnitextStyle _findStyle(int _id) {
        for (UnitextItem it = parent; it != null; it = it.parent) {
            UnitextDocument doc = (UnitextDocument)com.pullenti.unisharp.Utils.cast(it, UnitextDocument.class);
            if (doc != null) {
                if (_id < doc.styles.size()) 
                    return doc.styles.get(_id);
            }
        }
        return null;
    }

    @Override
    public void getHtml(StringBuilder res, GetHtmlParam par) {
        if (!par.callBefore(this, res)) 
            return;
        boolean outSpan = false;
        if (par != null && id != null && par.styles.containsKey(id)) {
            res.append("<span style=\"").append(par.styles.get(id)).append("\" id=\"").append(id).append("\"");
            outSpan = true;
        }
        else if (id != null) {
            res.append("<span id=\"").append(id).append("\"");
            outSpan = true;
        }
        if (par != null && par.outBeginEndChars && beginChar <= endChar) {
            if (!outSpan) 
                res.append("<span");
            outSpan = true;
            res.append(" bc=\"").append(beginChar).append("\" ec=\"").append(endChar).append("\"");
        }
        if (outSpan) 
            res.append('>');
        if (typ == UnitextPlaintextType.SUB) 
            res.append("<SUB>");
        else if (typ == UnitextPlaintextType.SUP) 
            res.append("<SUP>");
        boolean textOuted = false;
        if ((par != null && par.outStyles && this.getStyledFragment(-1) != null) && endChar >= beginChar) {
            UnitextStyledFragment[] map = new UnitextStyledFragment[(endChar + 1) - beginChar];
            boolean hasInline = false;
            for (int i = beginChar; i <= endChar; i++) {
                UnitextStyledFragment ff = this.getStyledFragment(i);
                if (ff == null || (ff.styleId < 0) || ff.getStyle() == null) 
                    continue;
                if (ff.typ == UnitextStyledFragmentType.INLINE) {
                }
                else if (ff.beginChar >= beginChar && ff.endChar <= endChar) {
                    if (parent != null && parent.getStyledFragment(i) == ff) 
                        continue;
                }
                else 
                    continue;
                hasInline = true;
                map[i - beginChar] = ff;
            }
            if (hasInline) {
                textOuted = true;
                UnitextStyledFragment cur = null;
                for (int i = 0; (i < map.length) && (i < text.length()); i++) {
                    if (map[i] == null) {
                        if (cur != null) {
                            cur = null;
                            res.append("</span>");
                        }
                    }
                    else if (cur != map[i]) {
                        if (cur != null) {
                            cur = null;
                            res.append("</span>");
                        }
                        cur = map[i];
                        res.append("<span style=\"");
                        cur.getStyle().getHtml(res);
                        res.append("\">");
                    }
                    com.pullenti.util.MiscHelper.correctHtmlChar(res, text.charAt(i));
                }
                if (cur != null) {
                    cur = null;
                    res.append("</span>");
                }
            }
        }
        if (!textOuted) 
            com.pullenti.util.MiscHelper.correctHtmlValue(res, text, false, false, false);
        if (typ == UnitextPlaintextType.SUB) 
            res.append("</SUB>");
        else if (typ == UnitextPlaintextType.SUP) 
            res.append("</SUP>");
        if (outSpan) 
            res.append("</span>");
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("text");
        this._writeXmlAttrs(xml);
        if (typ != UnitextPlaintextType.SIMPLE) 
            xml.wr.writeAttribute("type", typ.toString().toLowerCase());
        if (layout != null && layout.length > 0) {
            StringBuilder tmp = new StringBuilder();
            for (int i = 0; i < layout.length; i++) {
                if (layout[i] == null || layout[i].page == null) 
                    continue;
                int j;
                for (j = i + 1; j < layout.length; j++) {
                    if (layout[i] != layout[j]) 
                        break;
                }
                UnilayPage page = layout[i].page;
                int n = page.rects.indexOf(layout[i]);
                if (n < 0) 
                    continue;
                if (tmp.length() > 0) 
                    tmp.append(";");
                tmp.append(page.number).append(" ").append(n).append(" ").append((j - i));
                i = j - 1;
            }
            if (tmp.length() > 0) 
                xml.wr.writeAttribute("layout", tmp.toString());
        }
        String txt = com.pullenti.util.MiscHelper.correctXmlValue(text);
        int sp = 0;
        for (char ch : txt.toCharArray()) {
            if (ch != ' ') 
                break;
            else 
                sp++;
        }
        if (sp > 0 && sp == txt.length()) 
            xml.wr.writeAttribute("spaces", ((Integer)sp).toString());
        try {
            xml.wr.writeCharacters(com.pullenti.util.MiscHelper.correctXmlValue(txt));
        } catch (Exception ex) {
        }
        xml.wr.writeEndElement();
    }

    @Override
    public void fromXml(org.w3c.dom.Node xml) throws NumberFormatException {
        super.fromXml(xml);
        int sp = 0;
        if (xml.getAttributes() != null) {
            for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xml.getAttributes())).arr) {
                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "type")) {
                    try {
                        typ = (UnitextPlaintextType)UnitextPlaintextType.of(a.getNodeValue());
                    } catch (Exception ex622) {
                    }
                }
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "spaces")) {
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapsp623 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    com.pullenti.unisharp.Utils.parseInteger(a.getNodeValue(), 0, null, wrapsp623);
                    sp = (wrapsp623.value != null ? wrapsp623.value : 0);
                }
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "layout")) {
                    UnitextDocument doc = null;
                    for (UnitextItem it = parent; it != null; it = it.parent) {
                        if (it instanceof UnitextDocument) {
                            doc = (UnitextDocument)com.pullenti.unisharp.Utils.cast(it, UnitextDocument.class);
                            break;
                        }
                    }
                    if (doc == null || doc.pages.size() == 0) 
                        continue;
                    java.util.ArrayList<UnilayRectangle> lays = new java.util.ArrayList<UnilayRectangle>();
                    for (String xx : com.pullenti.unisharp.Utils.split(a.getNodeValue(), String.valueOf(';'), false)) {
                        String[] nn = com.pullenti.unisharp.Utils.split(xx, String.valueOf(' '), false);
                        if (nn.length != 3) 
                            continue;
                        int pp;
                        int ll;
                        int cou;
                        com.pullenti.unisharp.Outargwrapper<Integer> wrappp628 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                        boolean inoutres629 = com.pullenti.unisharp.Utils.parseInteger(nn[0], 0, null, wrappp628);
                        pp = (wrappp628.value != null ? wrappp628.value : 0);
                        if (!inoutres629) 
                            continue;
                        com.pullenti.unisharp.Outargwrapper<Integer> wrapll626 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                        boolean inoutres627 = com.pullenti.unisharp.Utils.parseInteger(nn[1], 0, null, wrapll626);
                        ll = (wrapll626.value != null ? wrapll626.value : 0);
                        if (!inoutres627) 
                            continue;
                        com.pullenti.unisharp.Outargwrapper<Integer> wrapcou624 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                        boolean inoutres625 = com.pullenti.unisharp.Utils.parseInteger(nn[2], 0, null, wrapcou624);
                        cou = (wrapcou624.value != null ? wrapcou624.value : 0);
                        if (!inoutres625) 
                            continue;
                        if ((pp < 1) || pp > doc.pages.size()) 
                            continue;
                        UnilayPage page = doc.pages.get(pp - 1);
                        if ((ll < 0) || ll >= page.rects.size()) 
                            continue;
                        for (; cou > 0; cou--) {
                            lays.add(page.rects.get(ll));
                        }
                    }
                    if (lays.size() > 0) 
                        layout = lays.toArray(new UnilayRectangle[lays.size()]);
                }
            }
        }
        text = xml.getTextContent();
        if (sp == 1) 
            text = " ";
        else if (sp == 2) 
            text = "  ";
        else if (sp > 2) {
            StringBuilder tmp = new StringBuilder();
            for (int i = 0; i < sp; i++) {
                tmp.append(' ');
            }
            text = tmp.toString();
        }
    }

    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp _typ, Object data) {
        if (_typ == com.pullenti.unitext.internal.uni.LocCorrTyp.NPSP2SP) 
            text = com.pullenti.unitext.internal.uni.UnitextCorrHelper.corrNbsp(text);
        else if (_typ == com.pullenti.unitext.internal.uni.LocCorrTyp.TRIMEND) {
            text = com.pullenti.unisharp.Utils.trimEnd(text);
            if (endChar > 0) 
                endChar = (beginChar + text.length()) - 1;
        }
    }

    @Override
    public void setDefaultTextPos(com.pullenti.unisharp.Outargwrapper<Integer> cp, StringBuilder res) {
        beginChar = cp.value;
        cp.value += text.length();
        endChar = cp.value - 1;
        if (res != null) 
            res.append(text);
    }

    public static UnitextPlaintext _new57(String _arg1) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.text = _arg1;
        return res;
    }

    public static UnitextPlaintext _new58(String _arg1, UnitextPlaintextType _arg2) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.text = _arg1;
        res.typ = _arg2;
        return res;
    }

    public static UnitextPlaintext _new89(String _arg1, Object _arg2) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.text = _arg1;
        res.tag = _arg2;
        return res;
    }

    public static UnitextPlaintext _new353(String _arg1, String _arg2) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.text = _arg1;
        res.pageSectionId = _arg2;
        return res;
    }

    public static UnitextPlaintext _new371(String _arg1, UnitextPlaintextType _arg2, String _arg3, UnitextStyledFragment _arg4) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.text = _arg1;
        res.typ = _arg2;
        res.pageSectionId = _arg3;
        res.m_StyledFrag = _arg4;
        return res;
    }

    public static UnitextPlaintext _new373(String _arg1, UnitextPlaintextType _arg2, UnitextStyledFragment _arg3) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.text = _arg1;
        res.typ = _arg2;
        res.m_StyledFrag = _arg3;
        return res;
    }

    public static UnitextPlaintext _new391(UnitextPlaintextType _arg1, String _arg2) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.typ = _arg1;
        res.text = _arg2;
        return res;
    }

    public static UnitextPlaintext _new400(UnitextItem _arg1) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.parent = _arg1;
        return res;
    }

    public static UnitextPlaintext _new613(String _arg1, int _arg2, int _arg3) {
        UnitextPlaintext res = new UnitextPlaintext();
        res.text = _arg1;
        res.beginChar = _arg2;
        res.endChar = _arg3;
        return res;
    }
    public UnitextPlaintext() {
        super();
    }
}
