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

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml;

namespace Pullenti.Unitext.Internal.Uni
{
    static class UnitextHelper
    {
        public static Pullenti.Unitext.UnitextDocument Create(string fileName, byte[] fileContent, Pullenti.Unitext.CreateDocumentParam pars)
        {
            if (fileContent == null) 
            {
                if (fileName == null) 
                    return new Pullenti.Unitext.UnitextDocument() { SourceFormat = Pullenti.Unitext.FileFormat.Unknown, ErrorMessage = string.Format("Not filename, not content...", fileName) };
                fileName = Path.GetFullPath(fileName);
                if (!File.Exists(fileName)) 
                    return new Pullenti.Unitext.UnitextDocument() { SourceFormat = Pullenti.Unitext.FileFormat.Unknown, ErrorMessage = string.Format("File '{0}' not exists", fileName) };
            }
            Pullenti.Unitext.FileFormat frm = Pullenti.Util.FileFormatsHelper.AnalizeFileFormat(fileName, fileContent);
            Pullenti.Unitext.UnitextDocument doc = null;
            try 
            {
                if (Pullenti.Unitext.UnitextService.Extension != null) 
                    doc = Pullenti.Unitext.UnitextService.Extension.CreateDocument(frm, fileName, fileContent, pars);
                if (doc == null) 
                    doc = _create(frm, fileName, fileContent, pars);
                if (doc == null) 
                    doc = new Pullenti.Unitext.UnitextDocument() { SourceFormat = frm, ErrorMessage = (frm == Pullenti.Unitext.FileFormat.Unknown ? "Format unknown" : "Can't create document") };
            }
            catch(Exception ex) 
            {
                doc = new Pullenti.Unitext.UnitextDocument() { SourceFormat = frm, ErrorMessage = ex.ToString() };
            }
            if (doc.SourceFileName == null) 
                doc.SourceFileName = fileName;
            List<Pullenti.Unitext.UnitextItem> all = new List<Pullenti.Unitext.UnitextItem>();
            doc.GetAllItems(all, 0);
            List<Pullenti.Unitext.UnitextHyperlink> inHyps = new List<Pullenti.Unitext.UnitextHyperlink>();
            Dictionary<string, Pullenti.Unitext.UnitextMisc> ancs = new Dictionary<string, Pullenti.Unitext.UnitextMisc>();
            foreach (Pullenti.Unitext.UnitextItem it in all) 
            {
                Pullenti.Unitext.UnitextMisc mi = it as Pullenti.Unitext.UnitextMisc;
                if (mi != null && mi.Typ == Pullenti.Unitext.UnitextMiscType.Anchor) 
                {
                    if (!ancs.ContainsKey(mi.Id)) 
                        ancs.Add(mi.Id, mi);
                    mi.Tag = null;
                }
                Pullenti.Unitext.UnitextHyperlink hyp = it as Pullenti.Unitext.UnitextHyperlink;
                if (hyp != null) 
                {
                    if (hyp.ToString().Contains("9.4")) 
                    {
                    }
                }
                if (hyp != null && hyp.Href != null && hyp.IsInternal) 
                {
                    inHyps.Add(hyp);
                    if (hyp.Content == null) 
                    {
                        Pullenti.Unitext.UnitextContainer cnt = hyp.Parent as Pullenti.Unitext.UnitextContainer;
                        if (cnt != null) 
                        {
                            int i = cnt.GetChildIndexOf(hyp);
                            if (i >= 0 && ((i + 1) < cnt.Children.Count)) 
                            {
                                int i0 = i;
                                i++;
                                Pullenti.Unitext.UnitextItem vv = cnt.Children[i];
                                while ((vv is Pullenti.Unitext.UnitextNewline) && ((i + 1) < cnt.Children.Count)) 
                                {
                                    i++;
                                    vv = cnt.Children[i];
                                }
                                if (!(vv is Pullenti.Unitext.UnitextNewline)) 
                                {
                                    hyp.Content = vv;
                                    vv.Parent = hyp;
                                    cnt.Children[i] = hyp;
                                    cnt.Children.RemoveAt(i0);
                                }
                            }
                        }
                    }
                }
                Pullenti.Unitext.UnitextImage img = it as Pullenti.Unitext.UnitextImage;
                if (img != null) 
                {
                    if (img.Content == null && img.Id != null) 
                    {
                        if (img.Id.StartsWith("data:image")) 
                        {
                            int ii = img.Id.IndexOf("base64,");
                            if (ii > 0) 
                            {
                                try 
                                {
                                    img.Content = Convert.FromBase64String(img.Id.Substring(ii + 7));
                                    img.Id = null;
                                }
                                catch(Exception ex) 
                                {
                                }
                            }
                        }
                        else if (img.Id.StartsWith("http") || img.Id.StartsWith("www")) 
                        {
                            img.HtmlSrcUri = img.Id;
                            continue;
                        }
                    }
                    if (img.Content != null && img.Content.Length > 10) 
                    {
                        if (img.Content[0] == 0x1F && img.Content[1] == 0x8B) 
                        {
                            try 
                            {
                                img.Content = Pullenti.Util.ArchiveHelper.DecompressGZip(img.Content);
                            }
                            catch(Exception ex287) 
                            {
                            }
                        }
                    }
                    if (img.Content == null && img.Id != null && (img.Id.Length < 30)) 
                        continue;
                    if (pars != null && pars.MaxImageSize > 0 && img.Content != null) 
                    {
                        if (img.Content.Length > pars.MaxImageSize) 
                            img.Content = null;
                    }
                }
            }
            int refid = 1;
            foreach (Pullenti.Unitext.UnitextHyperlink h in inHyps) 
            {
                Pullenti.Unitext.UnitextMisc anc;
                if (!ancs.TryGetValue(h.Href, out anc)) 
                    continue;
                Pullenti.Unitext.UnitextContainer cnt = anc.Parent as Pullenti.Unitext.UnitextContainer;
                Pullenti.Unitext.UnitextMisc anc0 = anc;
                if (cnt != null) 
                {
                    int i = cnt.GetChildIndexOf(anc);
                    Pullenti.Unitext.UnitextItem ait = null;
                    if (i >= 0) 
                    {
                        for (int j = i - 1; j >= 0; j--) 
                        {
                            Pullenti.Unitext.UnitextItem it = cnt.Children[j];
                            if (((it is Pullenti.Unitext.UnitextNewline) || (it is Pullenti.Unitext.UnitextPagebreak) || (it is Pullenti.Unitext.UnitextTable)) || (it is Pullenti.Unitext.UnitextList)) 
                                break;
                            if (it is Pullenti.Unitext.UnitextComment) 
                                continue;
                            Pullenti.Unitext.UnitextMisc um = it as Pullenti.Unitext.UnitextMisc;
                            if (um != null) 
                            {
                                if (um.Typ == Pullenti.Unitext.UnitextMiscType.HorizontalLine) 
                                    break;
                                continue;
                            }
                            ait = it;
                        }
                        if (ait == null) 
                        {
                            for (int j = i + 1; j < cnt.Children.Count; j++) 
                            {
                                Pullenti.Unitext.UnitextItem it = cnt.Children[j];
                                if (((it is Pullenti.Unitext.UnitextNewline) || (it is Pullenti.Unitext.UnitextMisc) || (it is Pullenti.Unitext.UnitextPagebreak)) || (it is Pullenti.Unitext.UnitextComment)) 
                                    continue;
                                ait = it;
                                break;
                            }
                        }
                        if (ait != null) 
                        {
                            if (ait.Id == null) 
                            {
                                if (refid == 101) 
                                {
                                }
                                ait.Id = string.Format("{0}_ref{1}", ait.InnerTag, refid);
                                refid++;
                            }
                            h.Href = ait.Id;
                            anc0 = null;
                        }
                    }
                    if (i > 0 && ait == null) 
                    {
                        for (int j = i - 1; j >= 0; j--) 
                        {
                            if ((cnt.Children[j] is Pullenti.Unitext.UnitextMisc) && (cnt.Children[j] as Pullenti.Unitext.UnitextMisc).Typ == Pullenti.Unitext.UnitextMiscType.Anchor) 
                                anc0 = cnt.Children[j] as Pullenti.Unitext.UnitextMisc;
                            else 
                                break;
                        }
                    }
                }
                if (anc0 != null) 
                {
                    if (anc0 != anc) 
                        h.Href = anc0.Id;
                    anc0.Tag = h;
                }
            }
            foreach (Pullenti.Unitext.UnitextMisc a in ancs.Values) 
            {
                if (a.Tag == null) 
                {
                    Pullenti.Unitext.UnitextContainer cnt = a.Parent as Pullenti.Unitext.UnitextContainer;
                    if (cnt == null) 
                        continue;
                    int i = cnt.GetChildIndexOf(a);
                    if (i >= 0) 
                        cnt.Children.RemoveAt(i);
                }
            }
            return doc;
        }
        public static byte[] LoadDataFromFile(string fileName, int attampts = 0)
        {
            FileStream fStr = null;
            byte[] buf = null;
            try 
            {
                Exception ex = null;
                for (int i = 0; i <= attampts; i++) 
                {
                    try 
                    {
                        fStr = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                        break;
                    }
                    catch(Exception e) 
                    {
                        ex = e;
                    }
                    if (i == 0 && !File.Exists(fileName)) 
                        break;
                    Thread.Sleep(500);
                }
                if (fStr == null) 
                    throw ex;
                if (fStr.Length == 0) 
                    return null;
                buf = new byte[(int)fStr.Length];
                fStr.Read(buf, 0, (int)fStr.Length);
            }
            finally
            {
                if (fStr != null) 
                    fStr.Dispose();
            }
            return buf;
        }
        static Pullenti.Unitext.UnitextDocument _create(Pullenti.Unitext.FileFormat frm, string fileName, byte[] fileContent, Pullenti.Unitext.CreateDocumentParam pars)
        {
            Pullenti.Unitext.FileFormatClass frmcl = Pullenti.Util.FileFormatsHelper.GetFormatClass(frm);
            if (frmcl == Pullenti.Unitext.FileFormatClass.Archive) 
            {
                Pullenti.Unitext.UnitextDocument res = new Pullenti.Unitext.UnitextDocument() { SourceFileName = fileName, SourceFormat = frm };
                if (pars.IgnoreInnerDocuments) 
                    return res;
                Dictionary<string, int> files = null;
                try 
                {
                    files = Pullenti.Util.ArchiveHelper.GetFileNamesFromArchive(fileName, fileContent);
                }
                catch(Exception ex) 
                {
                    res.ErrorMessage = ex.Message;
                    return res;
                }
                if (files == null) 
                {
                    res.ErrorMessage = string.Format("Archive format {0} not supported", frm.ToString());
                    return res;
                }
                if (files.Count == 0) 
                    return res;
                foreach (KeyValuePair<string, int> f in files) 
                {
                    string ext = Path.GetExtension(f.Key);
                    if (ext == ".exe" || ext == ".dll") 
                        continue;
                    try 
                    {
                        byte[] cnt = Pullenti.Util.ArchiveHelper.GetFileFromArchive(fileName, fileContent, f.Key);
                        if (cnt == null || cnt.Length == 0) 
                            continue;
                        Pullenti.Unitext.UnitextDocument d = Pullenti.Unitext.UnitextService.CreateDocument(f.Key, cnt, pars);
                        res.InnerDocuments.Add(d);
                    }
                    catch(Exception ex) 
                    {
                        Pullenti.Unitext.UnitextDocument d = new Pullenti.Unitext.UnitextDocument() { SourceFileName = f.Key, SourceFormat = Pullenti.Util.FileFormatsHelper.AnalizeFormat(ext, null) };
                        d.ErrorMessage = ex.Message;
                        res.InnerDocuments.Add(d);
                    }
                }
                return res;
            }
            if (fileContent == null) 
            {
                FileInfo fi = new FileInfo(fileName);
                if (fi.Length < 10000000) 
                    fileContent = LoadDataFromFile(fileName, 0);
            }
            if (frm == Pullenti.Unitext.FileFormat.Rtf) 
            {
                if (fileContent != null) 
                {
                    using (MemoryStream mem = new MemoryStream(fileContent)) 
                    {
                        return Pullenti.Unitext.Internal.Rtf.RtfHelper.CreateUniDoc(mem, pars);
                    }
                }
                else 
                {
                    using (FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 
                    {
                        return Pullenti.Unitext.Internal.Rtf.RtfHelper.CreateUniDoc(f, pars);
                    }
                }
            }
            if ((frm == Pullenti.Unitext.FileFormat.Docx || frm == Pullenti.Unitext.FileFormat.Xlsx || frm == Pullenti.Unitext.FileFormat.Pptx) || frm == Pullenti.Unitext.FileFormat.Odt || frm == Pullenti.Unitext.FileFormat.Docxml) 
            {
                using (Pullenti.Unitext.Internal.Word.DocxToText d2t = new Pullenti.Unitext.Internal.Word.DocxToText(fileName, fileContent, frm == Pullenti.Unitext.FileFormat.Docxml, (pars != null ? pars.SetFormulaAsImage : false))) 
                {
                    d2t.MaxTableRows = pars.MaxTableRows;
                    Pullenti.Unitext.UnitextDocument doc = d2t.CreateUniDoc(pars.OnlyForPureText, frm, pars);
                    if (doc != null) 
                    {
                        doc.Optimize(false, pars);
                        StyleHelper.ProcessDoc(doc);
                    }
                    return doc;
                }
            }
            if (frm == Pullenti.Unitext.FileFormat.Xlsxml) 
            {
                Pullenti.Unitext.Internal.Misc.MyXmlReader xm = null;
                if (fileContent != null) 
                {
                    xm = Pullenti.Unitext.Internal.Misc.MyXmlReader.Create(fileContent);
                    return Pullenti.Unitext.Internal.Misc.ExcelHelper.CreateDocForXml(xm, pars.MaxTableRows);
                }
                else 
                {
                    xm = Pullenti.Unitext.Internal.Misc.MyXmlReader.Create(LoadDataFromFile(fileName, 0));
                    return Pullenti.Unitext.Internal.Misc.ExcelHelper.CreateDocForXml(xm, pars.MaxTableRows);
                }
            }
            if (frm == Pullenti.Unitext.FileFormat.Doc) 
            {
                Pullenti.Unitext.UnitextDocument doc = null;
                if (fileContent != null) 
                    doc = Pullenti.Unitext.Internal.Word.MSOfficeHelper._uniFromWord97Arr(fileContent);
                else 
                    doc = Pullenti.Unitext.Internal.Word.MSOfficeHelper._uniFromWord97File(fileName);
                if (doc == null) 
                    return null;
                if (doc.Attrs.ContainsKey("word") && doc.Attrs["word"] == "6") 
                {
                    if (pars.IgnoreWord6) 
                    {
                        doc = new Pullenti.Unitext.UnitextDocument() { SourceFormat = Pullenti.Unitext.FileFormat.Doc };
                        doc.ErrorMessage = "Word6 not supported";
                        return doc;
                    }
                }
                doc.Optimize(false, pars);
                return doc;
            }
            if (frm == Pullenti.Unitext.FileFormat.Pdf) 
                return Pullenti.Unitext.Internal.Pdf.PdfHelper.CreateUni(fileName, fileContent, pars);
            if (frm == Pullenti.Unitext.FileFormat.Html) 
            {
                Pullenti.Unitext.Internal.Html.HtmlNode nod = Pullenti.Unitext.Internal.Html.HtmlHelper.CreateNode(fileName, fileContent, null);
                if (nod == null) 
                    return null;
                string dirName = null;
                if (!pars.OnlyForPureText && fileName != null && File.Exists(fileName)) 
                {
                    string dir = Path.GetDirectoryName(Path.GetFullPath(fileName));
                    if (Directory.Exists(dir)) 
                        dirName = dir;
                }
                Pullenti.Unitext.UnitextDocument doc = Pullenti.Unitext.Internal.Html.HtmlHelper.Create(nod, dirName, null, pars);
                return doc;
            }
            if (frm == Pullenti.Unitext.FileFormat.Xml) 
            {
                Pullenti.Unitext.UnitextDocument doc = new Pullenti.Unitext.UnitextDocument() { SourceFormat = Pullenti.Unitext.FileFormat.Xml };
                string txt = Pullenti.Unitext.Internal.Html.HtmlHelper.GetXmlText(fileName, fileContent);
                if (txt != null) 
                    doc.Content = new Pullenti.Unitext.UnitextPlaintext() { Typ = Pullenti.Unitext.UnitextPlaintextType.Xml, Text = txt };
                else 
                    doc.ErrorMessage = "Размер XML слишком большой";
                return doc;
            }
            if (frm == Pullenti.Unitext.FileFormat.Mht || frm == Pullenti.Unitext.FileFormat.Eml) 
            {
                FileStream fs = null;
                if (fileContent == null) 
                    fileContent = LoadDataFromFile(fileName, 0);
                try 
                {
                    Pullenti.Unitext.UnitextDocument doc = Pullenti.Unitext.Internal.Html.MhtHelper._createDoc(fileContent, pars, frm == Pullenti.Unitext.FileFormat.Mht);
                    if (doc != null) 
                    {
                        doc.Optimize(false, pars);
                        doc.SourceFileName = fileName;
                        doc.SourceFormat = frm;
                    }
                    else 
                        doc = new Pullenti.Unitext.UnitextDocument() { SourceFormat = frm, ErrorMessage = "Не удалось сформировать письмо" };
                    return doc;
                }
                finally
                {
                    if (fs != null) 
                        fs.Dispose();
                }
            }
            if (frm == Pullenti.Unitext.FileFormat.Fb2) 
                return Pullenti.Unitext.Internal.Misc.Fb2Helper.CreateDoc(fileName, fileContent, pars);
            if (frm == Pullenti.Unitext.FileFormat.Fb3) 
                return Pullenti.Unitext.Internal.Misc.Fb2Helper.CreateDocZip(fileName, fileContent, pars);
            if (frm == Pullenti.Unitext.FileFormat.Epub) 
                return Pullenti.Unitext.Internal.Misc.EpubHelper.CreateDoc(fileName, fileContent, pars);
            if (frm == Pullenti.Unitext.FileFormat.Txt || frm == Pullenti.Unitext.FileFormat.Csv) 
            {
                string txt = null;
                if (fileContent != null) 
                    txt = Pullenti.Util.TextHelper.ReadStringFromBytes(fileContent, false, false);
                else 
                    txt = Pullenti.Util.TextHelper.ReadStringFromFile(fileName, false, false);
                if (string.IsNullOrEmpty(txt)) 
                    return null;
                int len = txt.Length;
                if (frm == Pullenti.Unitext.FileFormat.Csv) 
                {
                    Pullenti.Unitext.UnitextDocument csv = Pullenti.Unitext.Internal.Misc.CsvHelper.Create(txt);
                    if (csv != null) 
                        return csv;
                }
                UnitextGen gen = new UnitextGen();
                gen.AppendText(txt, true);
                Pullenti.Unitext.UnitextDocument doc = new Pullenti.Unitext.UnitextDocument() { SourceFormat = Pullenti.Unitext.FileFormat.Txt };
                doc.Content = gen.Finish(true, null);
                if (doc.Content == null) 
                    return null;
                return doc;
            }
            if (frm == Pullenti.Unitext.FileFormat.Tif) 
            {
                if (fileContent == null) 
                    fileContent = LoadDataFromFile(fileName, 0);
                Pullenti.Unitext.UnitextDocument doc = Pullenti.Unitext.Internal.Misc.TiffHelper.CreateDoc(fileContent);
                doc.RefreshContentByPages();
                return doc;
            }
            if (frmcl == Pullenti.Unitext.FileFormatClass.Image) 
            {
                Pullenti.Unitext.UnitextDocument doc = new Pullenti.Unitext.UnitextDocument() { SourceFormat = frm, SourceFileName = fileName };
                Pullenti.Unitext.UnilayPage page = new Pullenti.Unitext.UnilayPage();
                page.ImageContent = fileContent ?? UnitextHelper.LoadDataFromFile(fileName, 0);
                doc.Pages.Add(page);
                doc.RefreshContentByPages();
                return doc;
            }
            Pullenti.Unitext.UnitextDocument err = new Pullenti.Unitext.UnitextDocument() { SourceFormat = frm, ErrorMessage = "Unsupported format" };
            if (frm != Pullenti.Unitext.FileFormat.Unknown) 
                err.ErrorMessage = string.Format("{0} {1}", err.ErrorMessage, frm.ToString());
            return err;
        }
        public static Pullenti.Unitext.UnitextItem CreateItem(XmlNode xml, Pullenti.Unitext.UnitextItem par)
        {
            Pullenti.Unitext.UnitextItem res = null;
            switch (xml.LocalName)
            {
                case "container":
                    res = new Pullenti.Unitext.UnitextContainer();
                    break;
                case "text":
                    res = new Pullenti.Unitext.UnitextPlaintext();
                    break;
                case "newline":
                    res = new Pullenti.Unitext.UnitextNewline();
                    break;
                case "pagebreak":
                    res = new Pullenti.Unitext.UnitextPagebreak();
                    break;
                case "table":
                    res = new Pullenti.Unitext.UnitextTable();
                    break;
                case "cell":
                    res = new Pullenti.Unitext.UnitextTablecell();
                    break;
                case "hyperlink":
                    res = new Pullenti.Unitext.UnitextHyperlink();
                    break;
                case "footnote":
                    res = new Pullenti.Unitext.UnitextFootnote();
                    break;
                case "list":
                    res = new Pullenti.Unitext.UnitextList();
                    break;
                case "listitem":
                    res = new Pullenti.Unitext.UnitextListitem();
                    break;
                case "image":
                    res = new Pullenti.Unitext.UnitextImage();
                    break;
                case "comment":
                    res = new Pullenti.Unitext.UnitextComment();
                    break;
                case "docblock":
                    res = new Pullenti.Unitext.UnitextDocblock();
                    break;
                case "misc":
                    res = new Pullenti.Unitext.UnitextMisc();
                    break;
                }
                if (res != null) 
                {
                    res.Parent = par;
                    res.FromXml(xml);
                }
                return res;
        }
        public static Pullenti.Unitext.UnitextDocument CreateDocFromText(string text)
        {
            Pullenti.Unitext.UnitextDocument doc = new Pullenti.Unitext.UnitextDocument() { SourcePlainText = text ?? "" };
            if (string.IsNullOrEmpty(text)) 
                return doc;
            Pullenti.Unitext.UnitextItem cur = null;
            Pullenti.Unitext.UnitextContainer cnt = new Pullenti.Unitext.UnitextContainer() { EndChar = text.Length - 1 };
            doc.Content = cnt;
            cnt.Parent = doc;
            doc.EndChar = text.Length - 1;
            for (int i = 0; i < text.Length; i++) 
            {
                char ch = text[i];
                if (ch == '\r' || ch == '\n') 
                {
                    if (!(cur is Pullenti.Unitext.UnitextNewline)) 
                    {
                        cur = new Pullenti.Unitext.UnitextNewline() { Parent = cnt };
                        cnt.Children.Add(cur);
                        cur.BeginChar = i;
                    }
                    cur.EndChar = i;
                    if (ch == '\r') 
                        (cur as Pullenti.Unitext.UnitextNewline).Count++;
                    else if (ch == '\n') 
                    {
                        if (i == 0 || text[i - 1] != '\r') 
                            (cur as Pullenti.Unitext.UnitextNewline).Count++;
                    }
                }
                else if (ch == 0xC) 
                {
                    cur = new Pullenti.Unitext.UnitextPagebreak() { Parent = cnt };
                    cnt.Children.Add(cur);
                    cur.BeginChar = i;
                    cur.EndChar = i;
                }
                else 
                {
                    if (!(cur is Pullenti.Unitext.UnitextPlaintext)) 
                    {
                        cur = new Pullenti.Unitext.UnitextPlaintext() { Parent = cnt };
                        cnt.Children.Add(cur);
                        cur.BeginChar = i;
                    }
                    cur.EndChar = i;
                }
            }
            foreach (Pullenti.Unitext.UnitextItem ch in cnt.Children) 
            {
                if ((ch is Pullenti.Unitext.UnitextPlaintext) && ch.BeginChar <= ch.EndChar) 
                    (ch as Pullenti.Unitext.UnitextPlaintext).Text = text.Substring(ch.BeginChar, (ch.EndChar - ch.BeginChar) + 1);
            }
            doc.GenerateIds();
            return doc;
        }
        static Pullenti.Unitext.GetPlaintextParam m_StdParams = new Pullenti.Unitext.GetPlaintextParam() { SetPositions = false };
        public static string GetPlaintext(Pullenti.Unitext.UnitextItem it, Pullenti.Unitext.GetPlaintextParam pars = null)
        {
            if (it == null) 
                return null;
            if (pars == null) 
                pars = m_StdParams;
            StringBuilder res = new StringBuilder();
            it.GetPlaintext(res, pars);
            if (res.Length == 0) 
                return null;
            return res.ToString();
        }
        internal static string[] m_Romans = new string[] {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX"};
    }
}