Пример #1
0
        private Task <object> CreateTask()
        {
            CancelToken = new CancellationTokenSource();
            var t = new Task <object>(() =>
            {
                try
                {
                    //System.Console.WriteLine("STEP 1");

                    switch (task)
                    {
                    case TASK_SPLIT:
                        curPos   = 0;
                        BufText  = "";
                        bufLen   = 0;
                        notFound = 0;
                        break;

                    case TASK_ANNOTATE:
                    case TASK_ANNOTATE_BACK:
                        curPos   = 0;
                        BufText  = "";
                        bufLen   = 0;
                        curLine  = new List <object>();
                        hMargin  = TestView != null ? TestView.hMargin : 0;
                        curWidth = hMargin;
                        notFound = 0;
                        break;
                    }

                    tempEndPos     = endPos;
                    tempStartPos   = startPos;
                    mHopelessBreak = false;
                    //System.Console.WriteLine("STEP 2");

                    while ((task == TASK_ANNOTATE || task == TASK_SPLIT) && (curPos < bufLen || curPos == bufLen && tempEndPos < textLen) && (tempLines.Count < visibleLines * 2 || (!Formatting && tempEndPos - endPos < 500)) || task == TASK_ANNOTATE_BACK && (curPos < bufLen || curPos == bufLen && tempStartPos > 0))
                    {
                        //System.Console.WriteLine("STEP 3");

                        if ((task == TASK_ANNOTATE || task == TASK_SPLIT) && bufLen - curPos < 18 && tempEndPos < textLen)
                        {
                            if (notFound > 0)
                            {
                                curLine  = AddNotFound(notFound, curLine);
                                notFound = 0;
                            }
                            BufText = NextBuffer;
                            bufLen  = BufText.Length;
                        }
                        else if (task == TASK_ANNOTATE_BACK && curPos == bufLen)
                        {
                            if (notFound > 0)
                            {
                                curLine  = AddNotFound(notFound, curLine);
                                notFound = 0;
                            }
                            if (curLine.Count > 0)
                            {
                                tempLines.Add(curLine);
                                curLine = new List <object>();
                            }
                            tempBackLines.InsertRange(0, tempLines);
                            tempLines.Clear();
                            if (tempBackLines.Count < visibleLines * 2)
                            {
                                BufText = PrevBuffer;
                                bufLen  = BufText.Length;
                            }
                            else
                            {
                                break;
                            }
                        }
                        //System.Console.WriteLine("STEP 4");

                        if (BufText[curPos] < '\u25CB' || BufText[curPos] > '\u9FA5')
                        {
                            if (CheckCancelled())
                            {
                                //return (-1);
                                return(null);
                            }

                            notFound++;

                            if (BufText[curPos] == ' ' && notFound > 1)
                            {
                                curPos++;
                                curLine  = AddNotFound(notFound, curLine);
                                notFound = 0;

                                if (curFilePos != -1)
                                {
                                    curFilePos++;
                                }

                                continue;
                            }

                            if (notFound > perLine * visibleLines * 2 && task == TASK_ANNOTATE)
                            {
                                notFound--;
                                break;
                            }

                            if (curFilePos != -1)
                            {
                                curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length;
                            }
                            curPos++;
                            continue;
                        }
                        //System.Console.WriteLine("STEP 5");

                        if (notFound > 0)
                        {
                            curLine  = AddNotFound(notFound, curLine);
                            notFound = 0;
                        }

                        int last = -1;
                        //System.Console.WriteLine("STEP 6");

                        int i = 3;
                        for (; i >= 0; i--)
                        {
                            int j = 1;
                            for (; j <= i && curPos + j < bufLen; j++)
                            {
                                if (BufText[curPos + j] < '\u25CB' || BufText[curPos + j] > '\u9FA5')
                                {
                                    break;
                                }
                            }
                            //System.Console.WriteLine("STEP 7");

                            if (j == i + 1)
                            {
                                if (i == 3)
                                {
                                    last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), true);
                                }
                                else
                                {
                                    if (last >= 0)
                                    {
                                        last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), 0, last - 1, false);
                                    }
                                    else
                                    {
                                        last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), false);
                                    }
                                }
                                //System.Console.WriteLine("STEP 8");

                                if (last >= 0)
                                {
                                    if (i == 3)
                                    {     //the found entry may be longer than 4 (3 + 1)
                                        if (Dict.GetLength(last) > bufLen - curPos)
                                        { //the found may be longer than the ending
                                            continue;
                                        }
                                        string word = BufText.SubstringSpecial(curPos, curPos + Dict.GetLength(last));
                                        if (Dict.Equals(last, word))
                                        {
                                            curLine = AddWord(last, curLine);

                                            Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks);
                                            if (bookmark != null)
                                            {
                                                bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1);
                                                FoundBookmarks.Add(bookmark);
                                            }

                                            if (CheckCancelled())
                                            {
                                                return(null);
                                                //return (-1);
                                            }

                                            if (curFilePos != -1)
                                            {
                                                curFilePos += word.Length * 3;
                                            }
                                            curPos += word.Length;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        curLine = AddWord(last, curLine);

                                        Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks);
                                        if (bookmark != null)
                                        {
                                            bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1);
                                            FoundBookmarks.Add(bookmark);
                                        }

                                        if (CheckCancelled())
                                        {
                                            //return (-1);
                                            return(null);
                                        }

                                        if (curFilePos != -1)
                                        {
                                            curFilePos += (i + 1) * 3;
                                        }
                                        curPos += i + 1;

                                        break;
                                    }
                                }
                            }
                        }
                        //System.Console.WriteLine("STEP 9");

                        if (i < 0)
                        {
                            notFound++;
                            if (curFilePos != -1)
                            {
                                curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length;
                            }
                            curPos++;
                        }
                    }
                    //System.Console.WriteLine("STEP 10");

                    if (notFound > 0)
                    {
                        curLine  = AddNotFound(notFound, curLine);
                        notFound = 0;
                    }
                    //System.Console.WriteLine("STEP 11");

                    if (curLine.Count > 0)
                    {
                        if (task == TASK_ANNOTATE_BACK || tempEndPos == textLen && curPos == bufLen || tempLines.Count == 0)
                        { //back annotation or end of text or 1-line text
                            tempLines.Add(curLine);
                            curLine = new List <object>();
                        }
                        else
                        {
                            curPos -= (int)LineView.GetLineSize(curLine, false);
                        }
                    }
                    //System.Console.WriteLine("STEP 12");

                    if (task == TASK_ANNOTATE || task == TASK_SPLIT)
                    {
                        if (annoMode == AnnotationActivity.ANNOTATE_FILE)
                        {
                            tempEndPos -= Encoding.UTF8.GetBytes(BufText.Substring(curPos)).Length;
                        }
                        else
                        {
                            tempEndPos -= bufLen - curPos;
                        }
                    }

                    if (task == TASK_ANNOTATE_BACK)
                    {
                        tempBackLines.InsertRange(0, tempLines);
                        tempLines = tempBackLines;
                    }

                    return(task);
                }
                catch (Exception e)
                {
                    status = Status.Finished;
                    System.Console.WriteLine("Annotation CreateTask ERROR => " + e.Message);

                    Log.Equals("ChineseReader", "Annotation error");
                }

                return(task);
            }, CancelToken.Token, TaskCreationOptions.None);

            t.ContinueWith(antecendent =>
            {
                status = Status.Finished;

                System.Console.WriteLine("C# Post Execute");

                if (CheckCancelled())
                {
                    return;
                }

                inter.OnCompleted(task, splitLineIndex, PastedText, tempLines, curPos, tempStartPos, tempEndPos, curPos < bufLen || (annoMode == AnnotationActivity.ANNOTATE_FILE && tempEndPos + Encoding.UTF8.GetBytes(BufText.SubstringSpecial(0, curPos)).Length < textLen), FoundBookmarks);
            }, CancelToken.Token, TaskContinuationOptions.None, Scheduler);

            return(t);
        }
        public static List <object> BreakWord(string theText)
        {
            int           textLen = theText.Length, curPos = 0, last;
            List <object> words = new List <object>();

            while (curPos < textLen)
            {
                int i = Math.Min(textLen - curPos - 1, 3);

                if (curPos == 0)
                {
                    i = Math.Min(textLen - curPos - 2, 3);
                }

                last = -1;
                for (; i >= 0; i--)
                {
                    if (i == 3 && curPos > 0)
                    {
                        last = Dict.BinarySearch(theText.SubstringSpecial(curPos, curPos + i + 1), true);
                    }
                    else
                    {
                        if (last >= 0)
                        {
                            last = Dict.BinarySearch(theText.SubstringSpecial(curPos, curPos + i + 1), 0, last - 1, false);
                        }
                        else
                        {
                            last = Dict.BinarySearch(theText.SubstringSpecial(curPos, curPos + i + 1), false);
                        }
                    }

                    if (last >= 0)
                    {
                        if (i == 3)
                        {     //the found entry may be longer than 4 (3 + 1)
                            if (Dict.GetLength(last) > textLen - curPos)
                            { //the found may be longer than the ending
                                continue;
                            }
                            string word = theText.SubstringSpecial(curPos, curPos + Dict.GetLength(last));
                            if (Dict.Equals(last, word))
                            {
                                words.Add(last);
                                curPos += word.Length;
                                break;
                            }
                        }
                        else
                        {
                            words.Add(last);
                            curPos += i + 1;
                            break;
                        }
                    }
                }

                if (i == -1 && last < 0)
                {
                    words.Add(theText.SubstringSpecial(curPos, curPos + 1));
                    curPos++;
                }
            }

            return(words);
        }
Пример #3
0
        protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] native_parms)
        {
            try
            {
                switch (task)
                {
                case TASK_SPLIT:
                    curPos   = 0;
                    BufText  = "";
                    bufLen   = 0;
                    notFound = 0;
                    break;

                case TASK_ANNOTATE:
                case TASK_ANNOTATE_BACK:
                    curPos   = 0;
                    BufText  = "";
                    bufLen   = 0;
                    curLine  = new List <object>();
                    hMargin  = TestView != null ? TestView.hMargin : 0;
                    curWidth = hMargin;
                    notFound = 0;
                    break;
                }

                tempEndPos     = endPos;
                tempStartPos   = startPos;
                mHopelessBreak = false;

                while ((task == TASK_ANNOTATE || task == TASK_SPLIT) && (curPos < bufLen || curPos == bufLen && tempEndPos < textLen) && (tempLines.Count < visibleLines * 2 || (!Formatting && tempEndPos - endPos < 500)) || task == TASK_ANNOTATE_BACK && (curPos < bufLen || curPos == bufLen && tempStartPos > 0))
                {
                    if ((task == TASK_ANNOTATE || task == TASK_SPLIT) && bufLen - curPos < 18 && tempEndPos < textLen)
                    {
                        if (notFound > 0)
                        {
                            curLine  = AddNotFound(notFound, curLine);
                            notFound = 0;
                        }
                        BufText = NextBuffer;
                        bufLen  = BufText.Length;
                    }
                    else if (task == TASK_ANNOTATE_BACK && curPos == bufLen)
                    {
                        if (notFound > 0)
                        {
                            curLine  = AddNotFound(notFound, curLine);
                            notFound = 0;
                        }
                        if (curLine.Count > 0)
                        {
                            tempLines.Add(curLine);
                            curLine = new List <object>();
                        }
                        tempBackLines.InsertRange(0, tempLines);
                        tempLines.Clear();
                        if (tempBackLines.Count < visibleLines * 2)
                        {
                            BufText = PrevBuffer;
                            bufLen  = BufText.Length;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (BufText[curPos] < '\u25CB' || BufText[curPos] > '\u9FA5')
                    {
                        if (CheckCancelled())
                        {
                            //return (-1);
                            return(null);
                        }

                        notFound++;

                        if (BufText[curPos] == ' ' && notFound > 1)
                        {
                            curPos++;
                            curLine  = AddNotFound(notFound, curLine);
                            notFound = 0;

                            if (curFilePos != -1)
                            {
                                curFilePos++;
                            }

                            continue;
                        }

                        if (notFound > perLine * visibleLines * 2 && task == TASK_ANNOTATE)
                        {
                            notFound--;
                            break;
                        }

                        if (curFilePos != -1)
                        {
                            curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length;
                        }
                        curPos++;
                        continue;
                    }

                    if (notFound > 0)
                    {
                        curLine  = AddNotFound(notFound, curLine);
                        notFound = 0;
                    }

                    int last = -1;

                    int i = 3;
                    for (; i >= 0; i--)
                    {
                        int j = 1;
                        for (; j <= i && curPos + j < bufLen; j++)
                        {
                            if (BufText[curPos + j] < '\u25CB' || BufText[curPos + j] > '\u9FA5')
                            {
                                break;
                            }
                        }

                        if (j == i + 1)
                        {
                            if (i == 3)
                            {
                                last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), true);
                            }
                            else
                            {
                                if (last >= 0)
                                {
                                    last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), 0, last - 1, false);
                                }
                                else
                                {
                                    last = Dict.BinarySearch(BufText.SubstringSpecial(curPos, curPos + i + 1), false);
                                }
                            }

                            if (last >= 0)
                            {
                                if (i == 3)
                                {     //the found entry may be longer than 4 (3 + 1)
                                    if (Dict.GetLength(last) > bufLen - curPos)
                                    { //the found may be longer than the ending
                                        continue;
                                    }
                                    string word = BufText.SubstringSpecial(curPos, curPos + Dict.GetLength(last));
                                    if (Dict.Equals(last, word))
                                    {
                                        curLine = AddWord(last, curLine);

                                        Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks);
                                        if (bookmark != null)
                                        {
                                            bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1);
                                            FoundBookmarks.Add(bookmark);
                                        }

                                        if (CheckCancelled())
                                        {
                                            return(null);
                                            //return (-1);
                                        }

                                        if (curFilePos != -1)
                                        {
                                            curFilePos += word.Length * 3;
                                        }
                                        curPos += word.Length;
                                        break;
                                    }
                                }
                                else
                                {
                                    curLine = AddWord(last, curLine);

                                    Bookmark bookmark = Bookmark.Search(curFilePos, Bookmarks);
                                    if (bookmark != null)
                                    {
                                        bookmark.SetAnnotatedPosition(tempLines.Count, curLine.Count - 1);
                                        FoundBookmarks.Add(bookmark);
                                    }

                                    if (CheckCancelled())
                                    {
                                        //return (-1);
                                        return(null);
                                    }

                                    if (curFilePos != -1)
                                    {
                                        curFilePos += (i + 1) * 3;
                                    }
                                    curPos += i + 1;

                                    break;
                                }
                            }
                        }
                    }

                    if (i < 0)
                    {
                        notFound++;
                        if (curFilePos != -1)
                        {
                            curFilePos += Encoding.UTF8.GetBytes(BufText.SubstringSpecial(curPos, curPos + 1)).Length;
                        }
                        curPos++;
                    }
                }

                if (notFound > 0)
                {
                    curLine  = AddNotFound(notFound, curLine);
                    notFound = 0;
                }

                if (curLine.Count > 0)
                {
                    if (task == TASK_ANNOTATE_BACK || tempEndPos == textLen && curPos == bufLen || tempLines.Count == 0)
                    { //back annotation or end of text or 1-line text
                        tempLines.Add(curLine);
                        curLine = new List <object>();
                    }
                    else
                    {
                        curPos -= (int)LineView.GetLineSize(curLine, false);
                    }
                }

                if (task == TASK_ANNOTATE || task == TASK_SPLIT)
                {
                    if (annoMode == AnnotationActivity.ANNOTATE_FILE)
                    {
                        tempEndPos -= Encoding.UTF8.GetBytes(BufText.Substring(curPos)).Length;
                    }
                    else
                    {
                        tempEndPos -= bufLen - curPos;
                    }
                }

                if (task == TASK_ANNOTATE_BACK)
                {
                    tempBackLines.InsertRange(0, tempLines);
                    tempLines = tempBackLines;
                }

                return(task);
            }
            catch (Java.Lang.Exception e)
            {
                status = Status.Finished;
                System.Console.WriteLine("Annotation DoInBackground ERROR => " + e.Message);
                Log.Equals("ChineseReader", "Annotation error");
            }

            return(task);
        }