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

public class UnitextGen {

    private StringBuilder m_Text = new StringBuilder();

    private com.pullenti.unitext.UnitextContainer m_Res = new com.pullenti.unitext.UnitextContainer();

    private com.pullenti.unitext.UnitextStyledFragment m_CurStyle;

    private com.pullenti.unitext.UnitextStyledFragment m_NumStyle;

    private String m_CurSection = null;

    private boolean m_NewRes = false;

    public void clearAll() {
        m_Text.setLength(0);
        m_Stack.clear();
        m_CurStyle = null;
        if (m_NewRes) 
            m_Res = new com.pullenti.unitext.UnitextContainer();
        else 
            m_Res.children.clear();
    }

    public void setStyle(com.pullenti.unitext.UnitextStyledFragment st) {
        if (st == null) 
            return;
        if (m_CurStyle == null) {
            m_CurStyle = st;
            m_Res.m_StyledFrag = st;
        }
        else if (m_CurStyle != st) {
            this.flushText();
            m_CurStyle = st;
        }
    }

    public void setNumStyle(com.pullenti.unitext.UnitextStyledFragment st) {
        m_NumStyle = st;
    }

    public boolean textGenRegime = false;

    public com.pullenti.unitext.UnitextItem finish(boolean isContent, com.pullenti.unitext.CreateDocumentParam pars) {
        this.flushText();
        com.pullenti.unitext.UnitextItem res = m_Res;
        if (m_CurStyle != null) {
            if (m_Res.children.size() == 0) 
                return null;
        }
        else 
            res = m_Res.optimize(isContent, pars);
        m_NewRes = res == m_Res;
        return res;
    }

    private java.util.ArrayList<com.pullenti.unitext.UnitextItem> m_Stack = new java.util.ArrayList<com.pullenti.unitext.UnitextItem>();

    public char lastNotSpaceChar = (char)0;

    public char lastChar = (char)0;

    public String getLastText() {
        return m_Text.toString();
    }


    private void flushText() {
        if (m_Text.length() > 0) {
            com.pullenti.unitext.UnitextPlaintext t = com.pullenti.unitext.UnitextPlaintext._new57(m_Text.toString());
            t.m_StyledFrag = m_CurStyle;
            t.pageSectionId = m_CurSection;
            if (textGenRegime) {
                t.typ = com.pullenti.unitext.UnitextPlaintextType.GENERATED;
                if (m_NumStyle != null) 
                    t.m_StyledFrag = m_NumStyle;
            }
            m_Text.setLength(0);
            this.append(t, null, -1, false);
        }
    }

    public void appendNewline(boolean ifNotPrevNewline, boolean isSoft) {
        if (ifNotPrevNewline) {
            if (m_Res.children.size() > 0 && m_Text.length() == 0) {
                if (m_Res.children.get(m_Res.children.size() - 1) instanceof com.pullenti.unitext.UnitextNewline) 
                    return;
            }
        }
        this.append(com.pullenti.unitext.UnitextNewline._new369(m_CurSection, (isSoft ? com.pullenti.unitext.WhitespaceType.SOFT : com.pullenti.unitext.WhitespaceType.ORIGINAL)), null, -1, false);
        lastChar = '\r';
    }

    public void appendPagebreak() {
        this.append(com.pullenti.unitext.UnitextPagebreak._new370(m_CurSection), null, -1, false);
        lastChar = '\f';
    }

    public void appendPagesection(com.pullenti.unitext.UnitextPagesection s) {
        if (s != null && com.pullenti.unisharp.Utils.stringsEq(s.id, m_CurSection)) 
            return;
        if (m_Res.children.size() > 0) {
            if (m_Res.children.size() == 1 && (m_Res.children.get(0) instanceof com.pullenti.unitext.UnitextNewline)) {
            }
            else if (!(m_Res.children.get(m_Res.children.size() - 1) instanceof com.pullenti.unitext.UnitextPagebreak)) 
                this.appendPagebreak();
            if (s != null) {
                for (com.pullenti.unitext.UnitextItem ch : m_Res.children) {
                    if (ch.pageSectionId == null) 
                        ch.pageSectionId = s.id;
                }
            }
        }
        if (s != null) 
            m_CurSection = s.id;
    }

    public void append(com.pullenti.unitext.UnitextItem blk, IUnitextGenNumStyle numStyl, int numLevel, boolean doNumStyleAsText) {
        this.flushText();
        if (blk != null && m_CurStyle != null && blk.m_StyledFrag == null) 
            blk.m_StyledFrag = m_CurStyle;
        if (blk != null && blk.pageSectionId == null) 
            blk.pageSectionId = m_CurSection;
        if (numStyl != null && numStyl.getTxt() != null) {
            com.pullenti.unitext.UnitextList list0 = this.appendListItem(blk, numStyl.getTxt(), null, numStyl.getLvl(), numStyl);
            return;
        }
        if (numStyl == null || (numLevel < 0)) {
            if (blk != null) {
                if ((blk instanceof com.pullenti.unitext.UnitextFootnote) && m_Res.children.size() > 0) {
                    com.pullenti.unitext.UnitextPlaintext txt = (com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(m_Res.children.get(m_Res.children.size() - 1), com.pullenti.unitext.UnitextPlaintext.class);
                    if (txt != null && txt.typ == com.pullenti.unitext.UnitextPlaintextType.SUP) {
                        if (com.pullenti.unisharp.Utils.stringsEq(txt.text, ((com.pullenti.unitext.UnitextFootnote)com.pullenti.unisharp.Utils.cast(blk, com.pullenti.unitext.UnitextFootnote.class)).customMark)) 
                            m_Res.children.remove(m_Res.children.size() - 1);
                    }
                }
                if (numStyl != null) 
                    this.appendNewline(true, false);
                m_Res.children.add(blk);
                if (numStyl != null) 
                    this.appendNewline(false, false);
            }
            return;
        }
        String num = numStyl.process(numLevel, false);
        if (num == null) {
            if (blk != null) 
                m_Res.children.add(blk);
            return;
        }
        if (doNumStyleAsText) {
            this.appendNewline(true, false);
            m_Res.children.add(com.pullenti.unitext.UnitextPlaintext._new371(num + " ", com.pullenti.unitext.UnitextPlaintextType.GENERATED, m_CurSection, m_NumStyle));
            if (blk != null) 
                m_Res.children.add(blk);
            this.appendNewline(false, false);
            return;
        }
        UniTextGenNumLevel numlev = numStyl.getLevel(numLevel);
        com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(blk, com.pullenti.unitext.UnitextContainer.class);
        if (cnt != null) {
            for (int i = 0; i < cnt.children.size(); i++) {
                if (cnt.children.get(i) instanceof com.pullenti.unitext.UnitextMisc) 
                    continue;
                if (cnt.children.get(i) instanceof com.pullenti.unitext.UnitextPagebreak) {
                    this.appendPagebreak();
                    cnt.children.remove(i);
                }
                else if (cnt.children.get(i) instanceof com.pullenti.unitext.UnitextNewline) {
                    this.appendNewline(false, false);
                    cnt.children.remove(i);
                }
                break;
            }
        }
        com.pullenti.unitext.UnitextList list = this.appendListItem(blk, num, numStyl.getId(), numLevel, numStyl);
        if (list != null && numlev.type == UniTextGenNumType.BULLET) 
            list.unorderPrefix = (numlev.format != null ? numlev.format : "");
    }

    public com.pullenti.unitext.UnitextList appendListItem(com.pullenti.unitext.UnitextItem content, String pref, String numStyleId, int numLevel, IUnitextGenNumStyle numStyl) {
        this.flushText();
        com.pullenti.unitext.UnitextList list = null;
        for (int i = m_Res.children.size() - 1; i >= 0; i--) {
            if ((m_Res.children.get(i) instanceof com.pullenti.unitext.UnitextNewline) || (m_Res.children.get(i) instanceof com.pullenti.unitext.UnitextComment) || (m_Res.children.get(i) instanceof com.pullenti.unitext.UnitextMisc)) {
            }
            else {
                list = (com.pullenti.unitext.UnitextList)com.pullenti.unisharp.Utils.cast(m_Res.children.get(i), com.pullenti.unitext.UnitextList.class);
                if ((list != null && numStyleId != null && com.pullenti.unisharp.Utils.stringsNe((String)com.pullenti.unisharp.Utils.cast(list.tag, String.class), numStyleId)) && list.items.get(list.items.size() - 1).sublist != null) {
                    com.pullenti.unitext.UnitextList sub = list.items.get(list.items.size() - 1).sublist;
                    if (com.pullenti.unisharp.Utils.stringsEq((String)com.pullenti.unisharp.Utils.cast(sub.tag, String.class), numStyleId)) 
                        list = sub;
                    else if ((numStyl != null && numLevel == 0 && numStyl.getLevel(0) != null) && numStyl.getLevel(0).type == UniTextGenNumType.BULLET) 
                        list = sub;
                }
                break;
            }
        }
        if (list != null && ((com.pullenti.unisharp.Utils.stringsEq((String)com.pullenti.unisharp.Utils.cast(list.tag, String.class), numStyleId) || numStyleId == null))) {
            if (numStyleId == null && numStyl == null) {
                numStyleId = (String)com.pullenti.unisharp.Utils.cast(list.tag, String.class);
                numLevel = -1;
            }
            while (true) {
                if (list.level == numLevel) {
                    com.pullenti.unitext.UnitextListitem it0 = com.pullenti.unitext.UnitextListitem._new372(content);
                    if (m_CurStyle != null) 
                        it0.m_StyledFrag = m_CurStyle;
                    if (pref != null) 
                        it0.prefix = com.pullenti.unitext.UnitextPlaintext._new373(pref, com.pullenti.unitext.UnitextPlaintextType.GENERATED, m_NumStyle);
                    list.items.add(it0);
                    return list;
                }
                if ((numLevel < 0) || (list.level < numLevel)) {
                    if (list.items.size() == 0) 
                        break;
                    com.pullenti.unitext.UnitextListitem last = list.items.get(list.items.size() - 1);
                    if (last.sublist != null) {
                        list = last.sublist;
                        continue;
                    }
                    if (numLevel < 0) {
                        numLevel = list.level;
                        continue;
                    }
                    if (list.level < (numLevel - 1)) 
                        break;
                    last.sublist = com.pullenti.unitext.UnitextList._new374(numLevel, numStyleId);
                    if (m_CurStyle != null) 
                        last.sublist.m_StyledFrag = m_CurStyle;
                    list = last.sublist;
                    continue;
                }
                break;
            }
        }
        if (numLevel < 0) 
            numLevel = 0;
        com.pullenti.unitext.UnitextList list0 = null;
        if ((list != null && numStyleId != null && com.pullenti.unisharp.Utils.stringsNe((String)com.pullenti.unisharp.Utils.cast(list.tag, String.class), numStyleId)) && numLevel == 0) {
            com.pullenti.unitext.UnitextListitem li00 = list.items.get(list.items.size() - 1);
            if (li00.content == null || com.pullenti.unisharp.Utils.isNullOrEmpty(li00.content.getPlaintextString(null))) {
            }
            else {
                if (numStyl != null && li00.sublist == null && pref == null) 
                    pref = numStyl.process(numLevel, true);
                list0 = list;
            }
        }
        list = com.pullenti.unitext.UnitextList._new375(numLevel, numStyleId, m_CurSection);
        if (m_CurStyle != null) 
            list.m_StyledFrag = m_CurStyle;
        if (numStyl != null && numStyl.isBullet()) 
            list.unorderPrefix = numStyl.getTxt();
        if (list0 != null) {
            if (list0.items.get(list0.items.size() - 1).sublist == null) 
                list0.items.get(list0.items.size() - 1).sublist = list;
            else 
                list = list0.items.get(list0.items.size() - 1).sublist;
        }
        else {
            m_Res.children.add(list);
            m_Res.children.add(new com.pullenti.unitext.UnitextNewline());
        }
        com.pullenti.unitext.UnitextListitem it = com.pullenti.unitext.UnitextListitem._new372(content);
        if (m_CurStyle != null) 
            it.m_StyledFrag = m_CurStyle;
        if (pref != null && list.unorderPrefix == null) 
            it.prefix = com.pullenti.unitext.UnitextPlaintext._new373(pref, com.pullenti.unitext.UnitextPlaintextType.GENERATED, m_NumStyle);
        list.items.add(it);
        return list;
    }

    public void appendText(String txt, boolean checkTable) {
        int inTableLev = 0;
        for (int i = 0; i < txt.length(); i++) {
            char ch = txt.charAt(i);
            lastChar = ch;
            if (((int)ch) == 0xD || ((int)ch) == 0xA) {
                if (((int)ch) == 0xD && ((i + 1) < txt.length()) && ((int)txt.charAt(i + 1)) == 0xA) 
                    i++;
                this.appendNewline(false, false);
                continue;
            }
            if (ch == '\f' && inTableLev == 0) {
                this.appendPagebreak();
                continue;
            }
            if (((int)ch) == 0x1E && checkTable) 
                inTableLev++;
            if (((int)ch) == 0x1F && checkTable) 
                inTableLev--;
            if (((int)ch) == 0xAD) 
                continue;
            if ((((int)ch) != 9 && ((int)ch) != 7 && ch != '\f') && (((int)ch) < 0x1E)) 
                ch = ' ';
            m_Text.append(ch);
            if (!com.pullenti.unisharp.Utils.isWhitespace(ch)) 
                lastNotSpaceChar = ch;
        }
    }

    public static String convertToPt(String val, String detTyp) {
        int vv = convertToMM(val, detTyp);
        if (vv == 0) 
            return val;
        else 
            return (((Integer)vv).toString() + "pt");
    }

    public static int convertToMM(String val, String detTyp) {
        if (val == null) 
            return 0;
        float f = 0.0F;
        int i;
        String typ = null;
        for (i = 0; i < val.length(); i++) {
            if (Character.isLetter(val.charAt(i)) || val.charAt(i) == ';') {
                typ = val.substring(i).toLowerCase();
                val = val.substring(0, 0 + i);
                break;
            }
        }
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(typ)) 
            typ = detTyp;
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(typ)) 
            return (int)f;
        com.pullenti.unisharp.Outargwrapper<Float> wrapf378 = new com.pullenti.unisharp.Outargwrapper<Float>();
        boolean inoutres379 = com.pullenti.util.MiscHelper.tryParseFloat(val, wrapf378);
        f = (wrapf378.value != null ? wrapf378.value : 0);
        if (inoutres379) {
        }
        else 
            return 0;
        if (typ.startsWith("pt")) {
        }
        else if (typ.startsWith("pc")) 
            f = (f * (72.0F)) / (6.0F);
        else if (typ.startsWith("cm")) 
            f = (f * (72.0F)) / 2.54F;
        else if (typ.startsWith("mm")) 
            f = (f * (72.0F)) / 25.4F;
        else if (typ.startsWith("px")) 
            f = f * 0.75F;
        else if (typ.startsWith("in")) 
            f = f * 72F;
        else 
            return 0;
        return (int)f;
    }
    public UnitextGen() {
    }
}
