예제 #1
0
파일: Debug.cs 프로젝트: Blecki/EtcScript
 private static string GetOperandString(Operand operand, ListIterator<object> iterator)
 {
     if (operand == Operand.NEXT)
     {
         iterator.Advance();
         return iterator.Next().ToString();
     }
     else if (operand == Operand.STRING)
     {
         iterator.Advance();
         return "STRING[" + iterator.Next().ToString() + "]";
     }
     else if (operand == Operand.NONE)
         return "";
     return operand.ToString();
 }
예제 #2
0
파일: Debug.cs 프로젝트: Blecki/EtcScript
        internal static String GetOpcodeString(ListIterator<Object> iterator)
        {
            if (iterator.AtEnd()) return "";

            var instruction = iterator.Next();
            if (instruction == null) return "null";
            else if (instruction is Instruction)
            {
                var ins = (instruction as Instruction?).Value;
                var r = ins.Opcode.ToString() + "  ";
                r += GetOperandString(ins.FirstOperand, iterator) + "  ";
                r += GetOperandString(ins.SecondOperand, iterator) + "  ";
                r += GetOperandString(ins.ThirdOperand, iterator) + "  ";
                if (!String.IsNullOrEmpty(ins.Annotation)) r += "#" + ins.Annotation;
                return r;
            }
            else return instruction.ToString();
        }
예제 #3
0
        /**
         * Gets the content bytes from a content object, which may be a reference
         * a stream or an array.
         * @param contentObject the object to read bytes from
         * @return the content bytes
         * @throws IOException
         */
        public static byte[] GetContentBytesFromContentObject(PdfObject contentObject)
        {
            byte[] result;
            switch (contentObject.Type)
            {
            case PdfObject.INDIRECT:
                PRIndirectReference refi         = (PRIndirectReference)contentObject;
                PdfObject           directObject = PdfReader.GetPdfObject(refi);
                result = GetContentBytesFromContentObject(directObject);
                break;

            case PdfObject.STREAM:
                PRStream stream = (PRStream)PdfReader.GetPdfObject(contentObject);
                result = PdfReader.GetStreamBytes(stream);
                break;

            case PdfObject.ARRAY:
                // Stitch together all content before calling ProcessContent(), because
                // ProcessContent() resets state.
                MemoryStream             allBytes     = new MemoryStream();
                PdfArray                 contentArray = (PdfArray)contentObject;
                ListIterator <PdfObject> iter         = contentArray.GetListIterator();
                while (iter.HasNext())
                {
                    PdfObject element = iter.Next();
                    byte[]    b;
                    allBytes.Write(b = GetContentBytesFromContentObject(element), 0, b.Length);
                    allBytes.WriteByte((byte)' ');
                }
                result = allBytes.ToArray();
                break;

            default:
                String msg = "Unable to handle Content of type " + contentObject.GetType();
                throw new InvalidOperationException(msg);
            }
            return(result);
        }
예제 #4
0
        /// <exception cref="NBoilerpipe.BoilerpipeProcessingException"></exception>
        public bool Process(TextDocument doc)
        {
            IList <TextBlock> textBlocks = doc.GetTextBlocks();

            if (textBlocks.Count < 2)
            {
                return(false);
            }
            int       maxNumWords  = -1;
            TextBlock largestBlock = null;
            int       level        = -1;
            int       i            = 0;
            int       n            = -1;

            foreach (TextBlock tb in textBlocks)
            {
                if (tb.IsContent())
                {
                    int nw = tb.GetNumWords();
                    if (nw > maxNumWords)
                    {
                        largestBlock = tb;
                        maxNumWords  = nw;
                        n            = i;
                        if (expandToSameLevelText)
                        {
                            level = tb.GetTagLevel();
                        }
                    }
                }
                i++;
            }
            foreach (TextBlock tb_1 in textBlocks)
            {
                if (tb_1 == largestBlock)
                {
                    tb_1.SetIsContent(true);
                }
                else
                {
                    tb_1.SetIsContent(false);
                    tb_1.AddLabel(DefaultLabels.MIGHT_BE_CONTENT);
                }
            }
            if (expandToSameLevelText && n != -1)
            {
                for (ListIterator <TextBlock> it = textBlocks.ListIterator(n); it.HasPrevious();)
                {
                    TextBlock tb_2 = it.Previous();
                    int       tl   = tb_2.GetTagLevel();
                    if (tl < level)
                    {
                        break;
                    }
                    else
                    {
                        if (tl == level)
                        {
                            tb_2.SetIsContent(true);
                        }
                    }
                }
                for (ListIterator <TextBlock> it_1 = textBlocks.ListIterator(n); it_1.HasNext();)
                {
                    TextBlock tb_2 = it_1.Next();
                    int       tl   = tb_2.GetTagLevel();
                    if (tl < level)
                    {
                        break;
                    }
                    else
                    {
                        if (tl == level)
                        {
                            tb_2.SetIsContent(true);
                        }
                    }
                }
            }
            return(true);
        }
예제 #5
0
        internal void Propagate(PdfObject obj, PdfIndirectReference refo, bool restricted)
        {
            if (obj == null)
            {
                return;
            }
            //        if (refo != null)
            //            AddToBody(obj, refo);
            if (obj is PdfIndirectReference)
            {
                return;
            }
            switch (obj.Type)
            {
            case PdfObject.DICTIONARY:
            case PdfObject.STREAM: {
                PdfDictionary dic = (PdfDictionary)obj;
                foreach (PdfName key in dic.Keys)
                {
                    if (restricted && (key.Equals(PdfName.PARENT) || key.Equals(PdfName.KIDS)))
                    {
                        continue;
                    }
                    PdfObject ob = dic.Get(key);
                    if (ob != null && ob.IsIndirect())
                    {
                        PRIndirectReference ind = (PRIndirectReference)ob;
                        if (!SetVisited(ind) && !IsPage(ind))
                        {
                            PdfIndirectReference refi = GetNewReference(ind);
                            Propagate(PdfReader.GetPdfObjectRelease(ind), refi, restricted);
                        }
                    }
                    else
                    {
                        Propagate(ob, null, restricted);
                    }
                }
                break;
            }

            case PdfObject.ARRAY: {
                //PdfArray arr = new PdfArray();
                for (ListIterator <PdfObject> it = ((PdfArray)obj).GetListIterator(); it.HasNext();)
                {
                    PdfObject ob = it.Next();
                    if (ob != null && ob.IsIndirect())
                    {
                        PRIndirectReference ind = (PRIndirectReference)ob;
                        if (!IsVisited(ind) && !IsPage(ind))
                        {
                            PdfIndirectReference refi = GetNewReference(ind);
                            Propagate(PdfReader.GetPdfObjectRelease(ind), refi, restricted);
                        }
                    }
                    else
                    {
                        Propagate(ob, null, restricted);
                    }
                }
                break;
            }

            case PdfObject.INDIRECT: {
                throw new Exception(MessageLocalization.GetComposedMessage("reference.pointing.to.reference"));
            }
            }
        }
        /// <summary>
        /// Generates a list of numbers from a string.
        /// </summary>
        /// <param name="ranges">the comma separated ranges</param>
        /// <param name="maxNumber">the maximum number in the range</param>
        /// <returns>a list with the numbers as  Integer </returns>
        public static ArrayList Expand(string ranges, int maxNumber)
        {
            SequenceList parse = new SequenceList(ranges);
            ArrayList    list  = new ArrayList();
            bool         sair  = false;

            while (!sair)
            {
                sair = parse.GetAttributes();
                if (parse.Low == -1 && parse.High == -1 && !parse.Even && !parse.Odd)
                {
                    continue;
                }
                if (parse.Low < 1)
                {
                    parse.Low = 1;
                }
                if (parse.High < 1 || parse.High > maxNumber)
                {
                    parse.High = maxNumber;
                }
                if (parse.Low > maxNumber)
                {
                    parse.Low = maxNumber;
                }

                //System.out.Println("low="+parse.low+",high="+parse.high+",odd="+parse.odd+",even="+parse.even+",inverse="+parse.inverse);
                int inc = 1;
                if (parse.Inverse)
                {
                    if (parse.Low > parse.High)
                    {
                        int t = parse.Low;
                        parse.Low  = parse.High;
                        parse.High = t;
                    }
                    for (ListIterator it = new ListIterator(list); it.HasNext();)
                    {
                        int n = (int)it.Next();
                        if (parse.Even && (n & 1) == 1)
                        {
                            continue;
                        }
                        if (parse.Odd && (n & 1) == 0)
                        {
                            continue;
                        }
                        if (n >= parse.Low && n <= parse.High)
                        {
                            it.Remove();
                        }
                    }
                }
                else
                {
                    if (parse.Low > parse.High)
                    {
                        inc = -1;
                        if (parse.Odd || parse.Even)
                        {
                            --inc;
                            if (parse.Even)
                            {
                                parse.Low &= ~1;
                            }
                            else
                            {
                                parse.Low -= ((parse.Low & 1) == 1 ? 0 : 1);
                            }
                        }
                        for (int k = parse.Low; k >= parse.High; k += inc)
                        {
                            list.Add(k);
                        }
                    }
                    else
                    {
                        if (parse.Odd || parse.Even)
                        {
                            ++inc;
                            if (parse.Odd)
                            {
                                parse.Low |= 1;
                            }
                            else
                            {
                                parse.Low += ((parse.Low & 1) == 1 ? 1 : 0);
                            }
                        }
                        for (int k = parse.Low; k <= parse.High; k += inc)
                        {
                            list.Add(k);
                        }
                    }
                }
            }
            return(list);
        }
예제 #7
0
        /**
         * Removes the bookmark entries for a number of page ranges. The page ranges
         * consists of a number of pairs with the start/end page range. The page numbers
         * are inclusive.
         * @param list the bookmarks
         * @param pageRange the page ranges, always in pairs.
         */
        public static void EliminatePages(ArrayList list, int[] pageRange)
        {
            if (list == null)
            {
                return;
            }

            for (ListIterator it = new ListIterator(list); it.HasNext();)
            {
                Hashtable map = (Hashtable)it.Next();
                bool      hit = false;
                if ("GoTo".Equals(map["Action"]))
                {
                    String page = (String)map["Page"];
                    if (page != null)
                    {
                        page = page.Trim();
                        int idx = page.IndexOf(' ');
                        int pageNum;
                        if (idx < 0)
                        {
                            pageNum = int.Parse(page);
                        }
                        else
                        {
                            pageNum = int.Parse(page.Substring(0, idx));
                        }
                        int len = pageRange.Length & 0x7ffffffe;
                        for (int k = 0; k < len; k += 2)
                        {
                            if (pageNum >= pageRange[k] && pageNum <= pageRange[k + 1])
                            {
                                hit = true;
                                break;
                            }
                        }
                    }
                }
                ArrayList kids = (ArrayList)map["Kids"];
                if (kids != null)
                {
                    EliminatePages(kids, pageRange);
                    if (kids.Count == 0)
                    {
                        map.Remove("Kids");
                        kids = null;
                    }
                }
                if (hit)
                {
                    if (kids == null)
                    {
                        it.Remove();
                    }
                    else
                    {
                        map.Remove("Action");
                        map.Remove("Page");
                        map.Remove("Named");
                    }
                }
            }
        }
예제 #8
0
        /**
         * Removes the bookmark entries for a number of page ranges. The page ranges
         * consists of a number of pairs with the start/end page range. The page numbers
         * are inclusive.
         * @param list the bookmarks
         * @param pageRange the page ranges, always in pairs.
         */
        public static void EliminatePages(IList <Dictionary <String, Object> > list, int[] pageRange)
        {
            if (list == null)
            {
                return;
            }

            for (ListIterator <Dictionary <String, Object> > it = new ListIterator <Dictionary <string, object> >(list); it.HasNext();)
            {
                Dictionary <String, Object> map = it.Next();
                bool hit = false;
                if (map.ContainsKey("Action") && "GoTo".Equals(map["Action"]))
                {
                    String page = null;
                    if (map.ContainsKey("Page"))
                    {
                        page = (String)map["Page"];
                    }
                    if (page != null)
                    {
                        page = page.Trim();
                        int idx = page.IndexOf(' ');
                        int pageNum;
                        if (idx < 0)
                        {
                            pageNum = int.Parse(page);
                        }
                        else
                        {
                            pageNum = int.Parse(page.Substring(0, idx));
                        }
                        int len = pageRange.Length & 0x7ffffffe;
                        for (int k = 0; k < len; k += 2)
                        {
                            if (pageNum >= pageRange[k] && pageNum <= pageRange[k + 1])
                            {
                                hit = true;
                                break;
                            }
                        }
                    }
                }
                IList <Dictionary <String, Object> > kids = null;
                if (map.ContainsKey("Kids"))
                {
                    kids = (IList <Dictionary <String, Object> >)map["Kids"];
                }
                if (kids != null)
                {
                    EliminatePages(kids, pageRange);
                    if (kids.Count == 0)
                    {
                        map.Remove("Kids");
                        kids = null;
                    }
                }
                if (hit)
                {
                    if (kids == null)
                    {
                        it.Remove();
                    }
                    else
                    {
                        map.Remove("Action");
                        map.Remove("Page");
                        map.Remove("Named");
                    }
                }
            }
        }
예제 #9
0
        /// <exception cref="NBoilerpipe.BoilerpipeProcessingException"></exception>
        public bool Process(TextDocument doc)
        {
            IList <TextBlock> textBlocks = doc.GetTextBlocks();

            if (textBlocks.Count < 2)
            {
                return(false);
            }
            bool      changes = false;
            TextBlock prevBlock;
            int       offset;

            if (contentOnly)
            {
                prevBlock = null;
                offset    = 0;
                foreach (TextBlock tb in textBlocks)
                {
                    offset++;
                    if (tb.IsContent())
                    {
                        prevBlock = tb;
                        break;
                    }
                }
                if (prevBlock == null)
                {
                    return(false);
                }
            }
            else
            {
                prevBlock = textBlocks[0];
                offset    = 1;
            }
            for (ListIterator <TextBlock> it = textBlocks.ListIterator <TextBlock>(offset); it.HasNext();)
            {
                TextBlock block = it.Next();
                if (!block.IsContent())
                {
                    prevBlock = block;
                    continue;
                }
                int diffBlocks = block.GetOffsetBlocksStart() - prevBlock.GetOffsetBlocksEnd() - 1;
                if (diffBlocks <= maxBlocksDistance)
                {
                    bool ok = true;
                    if (contentOnly)
                    {
                        if (!prevBlock.IsContent() || !block.IsContent())
                        {
                            ok = false;
                        }
                    }
                    if (ok && sameTagLevelOnly && prevBlock.GetTagLevel() != block.GetTagLevel())
                    {
                        ok = false;
                    }
                    if (ok)
                    {
                        prevBlock.MergeNext(block);
                        it.Remove();
                        changes = true;
                    }
                    else
                    {
                        prevBlock = block;
                    }
                }
                else
                {
                    prevBlock = block;
                }
            }
            return(changes);
        }
예제 #10
0
        /**
         * Write the content of the <code>RtfCell</code>.
         *
         * @param os The <code>Stream</code> to which to write the content of
         * the <code>RtfCell</code> to.
         * @return true if writing the cell content succeeded
         * @throws DocumentException
         */
        public bool WriteCellContent(MemoryStream os)
        {
            try {
                if (mergeType == MERGE_HORIZ_PREV || mergeType == MERGE_BOTH_PREV)
                {
                    return(true);
                }

                if (!emptyCell)
                {
                    Paragraph    container    = null;
                    ListIterator cellIterator = new ListIterator(store.Elements);
                    while (cellIterator.HasNext())
                    {
                        IElement element = (IElement)cellIterator.Next();
                        // should we wrap it in a paragraph
                        if (!(element is Paragraph))
                        {
                            if (container != null)
                            {
                                container.Add(element);
                            }
                            else
                            {
                                container           = new Paragraph();
                                container.Alignment = store.HorizontalAlignment;
                                container.Add(element);
                            }
                        }
                        else
                        {
                            if (container != null)
                            {
                                writer.AddElement(container, os);
                                container = null;
                                container = null;
                            }


                            // if horizontal alignment is undefined overwrite
                            // with that of enclosing cell
                            if (element is Paragraph && ((Paragraph)element).Alignment == Element.ALIGN_UNDEFINED)
                            {
                                ((Paragraph)element).Alignment = store.HorizontalAlignment;
                            }
                            writer.AddElement(element, os);
                            if (element.Type == Element.PARAGRAPH && cellIterator.HasNext())
                            {
                                os.WriteByte(RtfWriter.escape);
                                os.Write(RtfWriter.paragraph, 0, RtfWriter.paragraph.Length);
                            }
                        }
                    }
                    if (container != null)
                    {
                        writer.AddElement(container, os);
                        container = null;
                    }
                }
                else
                {
                    os.WriteByte(RtfWriter.escape);
                    os.Write(RtfWriter.paragraphDefaults, 0, RtfWriter.paragraphDefaults.Length);
                    os.WriteByte(RtfWriter.escape);
                    os.Write(cellInTable, 0, cellInTable.Length);
                }
                os.WriteByte(RtfWriter.escape);
                os.Write(cellEnd, 0, cellEnd.Length);
            } catch (IOException) {
                return(false);
            }
            return(true);
        }