Exemple #1
0
        /// <summary>
        /// Ends a Header tag.
        /// </summary>
        /// <returns>void</returns>
        /// <param name="text">SpannableStringBuilder</param>
        static void endHeader(SpannableStringBuilder text)
        {
            int len = text.Length();
            var obj = getLast(text, Java.Lang.Class.FromType(typeof(Header)));

            int where = text.GetSpanStart(obj);

            text.RemoveSpan(obj);

            // Back off not to change only the text, not the blank line.
            while (len > where && text.CharAt(len - 1) == '\n')
            {
                len--;
            }

            if (where != len)
            {
                Header h = (Header)obj;

                text.SetSpan(new RelativeSizeSpan(HEADER_SIZES [h.mLevel]),
                             where, len, SpanTypes.ExclusiveExclusive);
                text.SetSpan(new StyleSpan(TypefaceStyle.Bold),
                             where, len, SpanTypes.ExclusiveExclusive);
            }
        }
Exemple #2
0
        /// <summary>
        /// Handles the P tag.
        /// </summary>
        /// <returns>void</returns>
        /// <param name="text">Text within the P Tag</param>
        static void handleP(SpannableStringBuilder text)
        {
            int len = text.Length();

            if (len >= 1 && text.CharAt(len - 1) == '\n')
            {
                if (len >= 2 && text.CharAt(len - 2) == '\n')
                {
                    return;
                }

                text.Append("\n");
                return;
            }

            if (len != 0)
            {
                text.Append("\n\n");
            }
        }
Exemple #3
0
        private static void TrimTrailingWhitespace(SpannableStringBuilder source)
        {
            if (source == null && source.Length() <= 0)
            {
                return;
            }

            int i = source.Length();

            // loop back to the first non-whitespace character
            while (--i >= 0 && Character.IsWhitespace(source.CharAt(i)))
            {
            }

            source.Delete(i, source.Length() - 1);
        }
        public virtual void show(View anchor, List <object> line, int wordNum, int showX, bool redraw)
        {
            try
            {
                this.showX = showX;

                if (redraw)
                {
                    this.dismiss();
                }

                if (line != null)
                {
                    this.line                 = line;
                    this.wordIndex            = wordNum;
                    this.entry                = (int)line[wordNum];
                    splitButton.Visibility    = ViewStates.Visible;
                    bookmarkButton.Visibility = ViewStates.Visible;
                }
                else
                {
                    this.entry                = wordNum;
                    splitButton.Visibility    = ViewStates.Gone;
                    bookmarkButton.Visibility = ViewStates.Gone;
                }

                string sTrad = Dict.GetCh(entry, "traditional");
                string sSimp = Dict.GetCh(entry, "simplified");

                SpannableStringBuilder text = new SpannableStringBuilder();

                if (sSimp.Length > 1)
                {
                    List <object> subWords = BreakWord(sSimp);
                    foreach (object word in subWords)
                    {
                        if (word is int)
                        { //was found
                            string ch = Dict.GetCh((int)word, "simplified");
                            text.Append(ch).Append(new Java.Lang.String(" "));
                            WordSpan clickable = new WordSpan(this);
                            clickable.link = ch;
                            text.SetSpan(clickable, text.Length() - ch.Length - 1, text.Length() - 1, SpanTypes.User);
                        }
                        else
                        {
                            text.Append((string)word).Append(new Java.Lang.String(" "));
                        }
                    }
                }
                else
                {
                    text.Append(sSimp).Append(new Java.Lang.String(" "));
                    text.SetSpan(new AbsoluteSizeSpan(24, true), text.Length() - sSimp.Length - 1, text.Length() - 1, SpanTypes.User);
                    splitButton.Visibility = ViewStates.Gone;
                }

                if (!sTrad.Equals(sSimp))
                {
                    text.Append(sTrad);
                }

                mChars.SetText(text, TextView.BufferType.Spannable);

                text.Clear();
                string pinyin = Dict.PinyinToTones(Dict.GetPinyin(entry));
                text.Append("[ ").Append(new Java.Lang.String(pinyin)).Append(new Java.Lang.String(" ]  "));

                appendInlineButtons(text, pinyin);

                text.Append("\n");

                string[] parts = Regex.Split(Regex.Replace(Dict.GetEnglish(entry), @"/", "\n• "), @"\\$");

                int i = 0;
                foreach (string s in parts)
                {
                    if (i++ % 2 == 1)
                    {
                        pinyin = Dict.PinyinToTones(s);
                        text.Append("\n\n[ ").Append(new Java.Lang.String(pinyin)).Append(new Java.Lang.String(" ]  "));
                        appendInlineButtons(text, pinyin);
                        text.Append("\n");
                    }
                    else
                    {
                        text.Append("• ");

                        int beforeAppended = text.Length();

                        int bracketIndex, bracketEnd = 0;
                        while ((bracketIndex = s.IndexOf("[", bracketEnd, StringComparison.Ordinal)) > -1)
                        {
                            text.Append(s, bracketEnd, bracketIndex);
                            bracketEnd = s.IndexOf("]", bracketIndex);
                            text.Append(Dict.PinyinToTones(s.SubstringSpecial(bracketIndex, bracketEnd)));
                        }

                        text.Append(s, bracketEnd, s.Length);

                        int afterAppended = text.Length();

                        for (int m = beforeAppended; m < afterAppended; m++)
                        {
                            if (text.CharAt(m) >= '\u25CB' && text.CharAt(m) <= '\u9FA5')
                            {
                                int n = m + 1;
                                while (n < text.Length() && text.CharAt(n) >= '\u25CB' && text.CharAt(n) <= '\u9FA5')
                                {
                                    n++;
                                }

                                WordSpan clickable = new WordSpan(this);
                                clickable.link = text.SubSequence(m, n - m).ToString();
                                text.SetSpan(clickable, m, n, SpanTypes.User);
                            }
                        }
                    }
                }

                mContent.SetText(text, TextView.BufferType.Spannable);

                LinearLayout bookmarkTitleLayout = (LinearLayout)mRootView.FindViewById(Resource.Id.bookmark);
                if (mContext.annoMode != AnnotationActivity.ANNOTATE_FILE)
                {
                    bookmarkButton.Visibility      = ViewStates.Gone;
                    bookmarkTitleLayout.Visibility = ViewStates.Gone;
                }
                else
                {
                    int  lineNum      = mContext.lines.IndexOf(line);
                    long bookmarkPos  = mContext.GetPosition(mContext.lines, lineNum + 1, wordIndex, true);
                    int  bookmarksPos = Bookmark.SearchClosest(bookmarkPos, mContext.mBookmarks);
                    bool isBookmarked = mContext.mBookmarks.Count > bookmarksPos && mContext.mBookmarks[bookmarksPos].mPosition == bookmarkPos;

                    ToggleBookmark(bookmarkButton, isBookmarked);

                    if (isBookmarked)
                    {
                        bookmarkTitleLayout.Visibility = ViewStates.Visible;
                        ((TextView)mRootView.FindViewById(Resource.Id.bookmarkTitle)).Text = mContext.mBookmarks[bookmarksPos].mTitle;
                    }
                    else
                    {
                        bookmarkTitleLayout.Visibility = ViewStates.Gone;
                    }
                }

                ToggleStar(null, AnnotationActivity.sharedPrefs.GetString("stars", "").StartsWith(Dict.GetCh(entry, "simplified") + "\n") || AnnotationActivity.sharedPrefs.GetString("stars", "").Contains("\n" + Dict.GetCh(entry, "simplified") + "\n"));

                this.parent = anchor;

                float xPos = 0, yPos = 0, arrowPos;

                int[] location = new int[2];

                anchor.GetLocationOnScreen(location);

                Rect anchorRect = new Rect(location[0], location[1], location[0] + anchor.Width, location[1] + anchor.Height);

                if (redraw)
                {
                    mWindow.ShowAtLocation(anchor, GravityFlags.NoGravity, 0, 0);
                }
                mBubble.Measure(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);

                float rootHeight = mBubble.MeasuredHeight;
                float rootWidth  = mBubble.MeasuredWidth;

                xPos = showX - (rootWidth / 2);

                if ((xPos + rootWidth) > screenX)
                {
                    xPos = screenX - rootWidth;
                }
                else if (xPos < 0)
                {
                    xPos = 0;
                }

                arrowPos = showX - xPos;

                float dyTop    = anchorRect.Top - 60 * scale;
                float dyBottom = screenY - anchorRect.Bottom - 20 * scale;

                bool onTop = dyBottom <rootHeight && dyTop> dyBottom;

                if (onTop)
                {
                    if (rootHeight + 20 * scale > dyTop)
                    {
                        yPos       = 60 * scale;
                        rootHeight = anchorRect.Top - yPos - 20 * scale;
                    }
                    else
                    {
                        yPos = anchorRect.Top - rootHeight - 20 * scale;
                    }
                }
                else
                {
                    yPos = anchorRect.Bottom;

                    if (rootHeight > dyBottom)
                    {
                        rootHeight = dyBottom;
                    }
                }

                showArrow((onTop ? Resource.Id.arrow_down : Resource.Id.arrow_up), arrowPos, rootWidth);

                mWindow.Update((int)Math.Round(xPos), (int)Math.Round(yPos), (int)Math.Round(rootWidth), (int)Math.Round(rootHeight + 21 * scale));
                mScroll.FullScroll(FocusSearchDirection.Up);
            }
            catch (Exception e)
            {
                Toast.MakeText(mContext, "Error: " + e.Message, ToastLength.Long).Show();
            }
        }