public static int GetLineSize(List <object> line, bool isFile) { int res = 0; try { int length = line.Count; for (int j = 0; j < length; j++) { object word = line[j]; if (word is string) { if (((string)word).Length == 0) { res += 1; } else { res += isFile ? Encoding.UTF8.GetBytes((string)word).Length : ((string)word).Length; } } else { res += isFile ? Dict.GetLength((int)word) * 3 : Dict.GetLength((int)word); } } } catch (Exception e) { Console.WriteLine("GetLineSize ERROR => " + e.Message); } return(res); }
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); }
private void SplitButton_Click(object sender, EventArgs e) { WordPopup view = sender as WordPopup; view.dismiss(); List <object> subWords = BreakWord(Dict.GetCh(entry)); int lineIndex = mContext.lines.IndexOf(line); mContext.linesAdapter.NotifyDataSetChanged(); object firstWord = subWords[0]; if (mContext.annoMode == AnnotationActivity.ANNOTATE_FILE) { mContext.curPos = 0; mContext.endPos = mContext.GetPosition(mContext.lines, lineIndex + 1, wordIndex, true) + (firstWord is int?Dict.GetLength((int)firstWord) : ((string)firstWord).Length) * 3; } else { mContext.endPos = (int)mContext.GetPosition(mContext.lines, lineIndex + 1, wordIndex, false) + (firstWord is int?Dict.GetLength((int)firstWord) : ((string)firstWord).Length); } List <object> curLine = (List <object>)line; int toRemove = curLine.Count - wordIndex; while (toRemove-- > 0) { curLine.RemoveAt(wordIndex); } curLine.Add(subWords[0]); int curWidth = 5; foreach (object word in curLine) { curWidth += mContext.testView.GetWordWidth((string)word); } mContext.SplitAnnotation(lineIndex, curWidth, curLine); }
public long GetPosition(List <List <object> > list, int lineNum, int wordNum, bool isFile) { long pos = startPos; for (int i = 0; i < lineNum; i++) { List <object> row = list[i]; int len = row.Count; if (i == lineNum - 1 && wordNum > -1) { len = wordNum; } for (int j = 0; j < len; j++) { object word = row[j]; if (word is string) { try { int wordLen; if (isFile) { wordLen = Encoding.UTF8.GetBytes((string)word).Length; } else { wordLen = ((string)word).Length; } if (wordLen == 0) { pos += 1; } else { pos += wordLen; } } catch (Exception e) { Toast.MakeText(this.Application, e.Message, ToastLength.Long).Show(); } } else { if (isFile) { pos += Dict.GetLength((int)word) * 3; } else { pos += Dict.GetLength((int)word); } } } } return(pos); }
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); }
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); }