Ejemplo n.º 1
0
        public static bool Text_CalcSize_prefix(string text, ref Vector2 __result)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(false);
            }

            int      font_index = (int)Text.Font;
            GUIStyle style      = Text.CurFontStyle;

            CacheData cache = s_cache.getData(text, font_index);

            if (cache.line_count == 0 || cache.width == 0)
            {
                WordWrap_Unity.setupFont(style, font_index);
                cache.width      = WordWrap.calcSize(style.richText, text, out int line_count);
                cache.line_count = line_count;
                cache.str        = text;
            }

            __result.x = cache.width + style.padding.horizontal;
            __result.y = calcHeight(style, font_index, cache.line_count);

            return(false);
        }
Ejemplo n.º 2
0
 private static void setupKerning()
 {
     for (int i = 0; i < 3; i++)
     {
         GUIStyle style = Text.fontStyles[i];
         WordWrap_Unity.setupKerning(i, style);
     }
 }
Ejemplo n.º 3
0
        private static readonly StringBuilder s_buf = new StringBuilder(256);        // about

        public static int calcSize(bool tag, string str, out int line_count)
        {
            int width     = 0;
            int max_width = 0;
            int line      = 1;
            int len       = str.Length;
            int i         = 0;
            int prev_c    = 0;

            for (; i < len; i++)
            {
                int c = str[i];
                if (c == '<' && tag)
                {
                    if (i + 1 >= len)
                    {
                        goto end;
                    }
                    i = str.IndexOf('>', i + 1);
                    if (i < 0)
                    {
                        goto end;
                    }
                    prev_c = 0;
                    continue;
                }

                c      = WordWrap_Unity.takeCharAndWidth(str, ref i, out int w, prev_c);
                prev_c = c;

                if (c == '\n')
                {
                    if (width > max_width)
                    {
                        max_width = width;
                    }
                    width = 0;
                    line++;
                }

                if (c == '\t')
                {
                    width = (width / w + 1) * w;
                }
                else
                {
                    width += w;
                }
            }

end:
            line_count = line;
            return(width > max_width ? width : max_width);
        }
Ejemplo n.º 4
0
        public static bool Text_CalcHeight_prefix(string text, float width, ref float __result)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(false);
            }

            int      font_index = (int)Text.Font;
            GUIStyle style      = Text.CurFontStyle;
            int      line_count;
            string   str = text;

            CacheData cache = s_cache.getData(str, font_index);

            if (style.wordWrap)
            {
                if (cache.width != 0 && cache.width <= (int)width)
                {
                    line_count = cache.line_count;
                    goto end;
                }

                if (
                    (cache.wrap_width == (int)width) ||
                    (cache.wrap_width != 0 && cache.wrap_str == null && cache.wrap_width <= (int)width)
                    )
                {
                    line_count = cache.wrap_line_count;
                    goto end;
                }

                WordWrap_Unity.setupFont(style, font_index);
                string new_label = WordWrap.modify(str, (int)width, out line_count);
                cache.wrap_width      = (int)width;
                cache.wrap_line_count = line_count;
                cache.wrap_str        = new_label;

                line_count = cache.wrap_line_count;
            }
            else
            {
                if (cache.width == 0)
                {
                    s_calcSize(style, font_index, str, cache);
                }

                line_count = cache.line_count;
            }
end:
            __result = calcHeight(style, font_index, line_count);

            return(false);
        }
Ejemplo n.º 5
0
        public static bool Text_CalcHeight_prefix(string text, float width, ref float __result)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(false);
            }

            int      font_index = (int)Text.Font;
            GUIStyle style      = Text.CurFontStyle;
            int      line_count;

            {
                CacheData cache = s_cache.getData(text, font_index);

                if (style.wordWrap)
                {
                    if (cache.wrap_line_count == 0 || cache.wrap_width != (int)width)
                    {
                        WordWrap_Unity.setupFont(style, font_index);
                        cache.wrap_str   = WordWrap.wrap(style.richText, text, (int)width, out cache.wrap_line_count);
                        cache.wrap_width = (int)width;
                    }
                    line_count = cache.wrap_line_count;
                }
                else
                {
                    if (cache.line_count == 0)
                    {
                        cache.line_count = lineCount(text);
                        cache.str        = text;
                    }
                    line_count = cache.line_count;
                }
            }

            __result = calcHeight(style, font_index, line_count);

            return(false);
        }
Ejemplo n.º 6
0
        public static bool Widgets_Label_prefix(Rect rect, string label)
        {
            if (Event.current.type != EventType.Repaint)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(label))
            {
                return(false);
            }

            float scale = Prefs.UIScale;

            if (scale > 1f)
            {
                float num = scale / 2f;
                if (Math.Abs(num - Mathf.Floor(num)) > Single.Epsilon)
                {
                    rect.xMin = Widgets.AdjustCoordToUIScalingFloor(rect.xMin);
                    rect.yMin = Widgets.AdjustCoordToUIScalingFloor(rect.yMin);
                    rect.xMax = Widgets.AdjustCoordToUIScalingCeil(rect.xMax + 1E-05f);                    // + 0.00001f
                    rect.yMax = Widgets.AdjustCoordToUIScalingCeil(rect.yMax + 1E-05f);
                }
            }

            int      font_index = (int)Text.Font;
            GUIStyle style      = Text.CurFontStyle;
            int      line_count;

            {
                CacheData cache = s_cache.getData(label, font_index);
                if (style.wordWrap)
                {
                    if (cache.wrap_line_count == 0 || cache.wrap_width != (int)rect.width)
                    {
                        WordWrap_Unity.setupFont(style, font_index);
                        cache.wrap_str   = WordWrap.wrap(style.richText, label, (int)rect.width, out cache.wrap_line_count);
                        cache.wrap_width = (int)rect.width;
                    }
                    label      = cache.wrap_str;
                    line_count = cache.wrap_line_count;
                }
                else
                {
                    if (cache.line_count == 0)
                    {
                        cache.line_count = lineCount(label);
                        cache.str        = label;
                    }
                    label      = cache.str;
                    line_count = cache.line_count;
                }
            }


            int text_height = calcHeight(style, font_index, line_count);

            TextAnchor alignment = style.alignment;
            float      offset_y;

            switch (alignment)
            {
            case TextAnchor.MiddleLeft:
                offset_y        = rect.height / 2 - text_height / 2;
                style.alignment = TextAnchor.UpperLeft;
                break;

            case TextAnchor.MiddleCenter:
                offset_y        = rect.height / 2 - text_height / 2;
                style.alignment = TextAnchor.UpperCenter;
                break;

            case TextAnchor.MiddleRight:
                offset_y        = rect.height / 2 - text_height / 2;
                style.alignment = TextAnchor.UpperRight;
                break;

            case TextAnchor.LowerLeft:
                offset_y        = rect.height - text_height;
                style.alignment = TextAnchor.UpperLeft;
                break;

            case TextAnchor.LowerCenter:
                offset_y        = rect.height - text_height;
                style.alignment = TextAnchor.UpperCenter;
                break;

            case TextAnchor.LowerRight:
                offset_y        = rect.height - text_height;
                style.alignment = TextAnchor.UpperRight;
                break;

            default:
                offset_y = 0;
                break;
            }
            rect.y += offset_y;

            bool ww = style.wordWrap;

            style.wordWrap = false;
            GUI.Label(rect, label, style);
            style.wordWrap  = ww;
            style.alignment = alignment;

            return(false);
        }
Ejemplo n.º 7
0
            public bool nextChop(string str, int width_max)
            {
                int len = str.Length;
                int c;

                int start = next_index;
                int i     = start;
                int width = 0;
                int prv_c = 0;
                int w;

retry:
                for (; i < len; i++)
                {
                    c = WordWrap_Unity.takeCharAndWidth(str, ref i, out w);

                    if (c == '\n')
                    {
                        goto new_line;
                    }

                    if (c == '\t')
                    {
                        int x = (width / w + 1) * w;
                        w = x - width;
                    }
                    else
                    {
                        w -= kerning.offset(prv_c, c);
                    }
                    if (width + w > width_max)
                    {
                        goto find_chop_point;
                    }
                    width += w;
                }
                return(false);

new_line:
                m_new_line_count++;
                start = i + 1;
                width = 0;
                i++;
                goto retry;

find_chop_point:
                CharType cur_type;

                if (i == start)                // too narrow
                {
                    i++;
                    goto do_chop;
                }

                cur_type = charType(c);
                switch (cur_type)
                {
                case CharType.OpenBracket: goto do_chop;

                case CharType.Other: goto open_bracket;

                case CharType.JaKanji: goto ja_kanji;

                case CharType.JaHiraKata: goto ja_hira_kata;

                case CharType.ForceInclude: goto force_include;
                }

                {
                    int tmp_i = i;
                    for (; i > start; i--)
                    {
                        c = str[i - 1];
                        if (isDivider(c))
                        {
                            goto open_bracket;
                        }
                    }
                    i = tmp_i;
                }
                // too long
                goto do_chop;

force_include:
                if (width + w / 3 <= width_max)
                {
                    i++;
                }
                goto do_chop;

ja_hira_kata:
                if (i - 4 > start)
                {
                    if (c == 0x30fc)                   // PROLONGED SOUND MARK
                    {
                        goto ja_prolonged_sound_0;
                    }
                    int f = ja_hira_kata(str[i]);
                    if ((f & JA_HK_SMALL_TU_BIT) != 0)
                    {
                        goto ja_hk_small_tu;
                    }
                    if ((f & JA_HK_SMALL_BIT) != 0)
                    {
                        goto ja_hk_small_0;
                    }

                    // big.
                    if ((f & JA_KATAKANA_BIT) != 0)
                    {
                        f = ja_hira_kata(str[i - 1]);
                        if (f == (JA_KATAKANA_BIT | JA_HK_BIG_BIT))
                        {
                            f = ja_hira_kata(str[i - 2]);
                            if ((f & JA_KATAKANA_BIT) == 0)
                            {
                                i = i - 1;
                            }
                        }
                    }
                    else
                    {
                        // hiragana
                        bool kanji = ja_kanji(str[i - 1]);
                        if (kanji)
                        {
                            kanji = ja_kanji(str[i - 2]);
                            if (!kanji)
                            {
                                i = i - 1;
                            }
                        }
                        else
                        {
                            f = ja_hiragana(str[i - 1]);
                            if ((f & JA_HK_BIG_BIT) != 0)
                            {
                                kanji = ja_kanji(str[i - 2]);
                                if (!kanji)
                                {
                                    f = ja_hiragana(str[i - 2]);
                                    if (f == 0)
                                    {
                                        i = i - 1;
                                    }
                                }
                            }
                        }
                    }

                    goto open_bracket;
                }
                goto do_chop;

ja_hk_small_tu:
                {
                    int c1 = str[i - 1];
                    if (c1 == 0x30fc)                   // PROLONGED SOUND MARK
                    {
                        goto ja_prolonged_sound_1;
                    }
                    int f = ja_hira_kata(c1);
                    if (f == 0)
                    {
                        if (!ja_kanji(c1))
                        {
                            goto open_bracket;
                        }
                        int c2 = str[i - 2];
                        if (!ja_kanji(c2))
                        {
                            i = i - 1;
                            goto open_bracket;
                        }
                        int c3 = str[i - 3];
                        if (!ja_kanji(c3))
                        {
                            i = i - 2;
                        }
                        goto open_bracket;
                    }
                    if ((f & JA_HK_SMALL_TU_BIT) != 0)
                    {
                        goto open_bracket;
                    }
                    if ((f & JA_HK_SMALL_BIT) != 0)
                    {
                        goto ja_hk_small_1;
                    }
                    i = i - 1;                    // BIG
                }
                goto open_bracket;

ja_prolonged_sound_0:
                {
                    int f = ja_hira_kata(str[i - 1]);
                    if (f == 0)
                    {
                        goto open_bracket;
                    }
                    if ((f & JA_HK_SMALL_TU_BIT) != 0)
                    {
                        goto open_bracket;
                    }
                    if ((f & JA_HK_SMALL_BIT) != 0)
                    {
                        goto ja_hk_small_1;
                    }
                    i = i - 1;                    // BIG
                }
                goto open_bracket;

ja_prolonged_sound_1:
                {
                    int f = ja_hira_kata(str[i - 2]);
                    if (f == 0)
                    {
                        goto open_bracket;
                    }
                    if ((f & JA_HK_SMALL_TU_BIT) != 0)
                    {
                        goto open_bracket;
                    }
                    if ((f & JA_HK_SMALL_BIT) != 0)
                    {
                        goto ja_hk_small_2;
                    }
                    i = i - 2;                    // BIG
                }
                goto open_bracket;

ja_hk_small_0:
                {
                    int f = ja_hira_kata(str[i - 1]);
                    if ((f & JA_HK_BIG_BIT) != 0)
                    {
                        i = i - 1;
                    }
                }
                goto open_bracket;

ja_hk_small_1:
                {
                    int f = ja_hira_kata(str[i - 2]);
                    if ((f & JA_HK_BIG_BIT) != 0)
                    {
                        i = i - 2;
                    }
                }
                goto open_bracket;

ja_hk_small_2:
                {
                    int f = ja_hira_kata(str[i - 3]);
                    if ((f & JA_HK_BIG_BIT) != 0)
                    {
                        i = i - 3;
                    }
                }
                goto open_bracket;

ja_kanji:
                if (i - 4 > start)
                {
                    bool kanji = ja_kanji(str[i - 1]);
                    if (!kanji)
                    {
                        goto open_bracket;
                    }
                    kanji = ja_kanji(str[i - 2]);
                    if (kanji)
                    {
                        goto do_chop;
                    }

                    i = i - 1;
                    goto open_bracket;
                }
                goto do_chop;

open_bracket:
                if (i - 4 > start)
                {
                    bool bracket = isOopenBracket(str[i - 1]);
                    if (!bracket)
                    {
                        goto do_chop;
                    }
                    i = i - 1;
                    goto do_chop;
                }
                goto do_chop;

do_chop:
                if (i < len)
                {
                    if (skipNewline(str, ref i))
                    {
                        goto new_line;
                    }
                }

                m_chop_end = i;
                if (i < len && isSpace(str[i]))
                {
                    i++;
                    for (; i < len; i++)
                    {
                        c = str[i];

                        if (!isSpace(c))
                        {
                            if (skipNewline(str, ref i))
                            {
                                i++;
                            }
                            break;
                        }
                    }
                }
                m_next_index = i;

                return(true);
            }
Ejemplo n.º 8
0
 public void init()
 {
     m_next_index     = 0;
     m_new_line_count = 0;
     kerning          = WordWrap_Unity.getKerning();
 }
Ejemplo n.º 9
0
            public bool nextChop()
            {
                m_chop0 = m_next_chop0;

                int i              = m_next_index;
                int width          = m_next_width;
                int prev_char_type = m_next_prev_char_type;
                int prev_c         = m_next_prev_c;

                int chop_index = -1;
                int chop_width = -1;
                int w, c;
                int char_type;

                for (; i < m_len; i++)
                {
                    c = m_str[i];

                    if (c == '<' && m_tag)
                    {
                        i++;
                        if (i >= m_len)
                        {
                            break;
                        }
                        i = m_str.IndexOf('>', i);
                        if (i < 0)
                        {
                            m_chop1 = m_len;
                            return(false);
                        }
                        prev_c = 0;
                        continue;
                    }

                    c      = WordWrap_Unity.takeCharAndWidth(m_str, ref i, out w, prev_c);
                    prev_c = c;

                    if (c == '\n')
                    {
                        chop_index = -1;
                        width      = 0;
                        m_line_count++;
                        continue;
                    }

                    if (c == '\t')
                    {
                        int x = (width / w + 1) * w;
                        w = x - width;
                    }

                    width += w;

                    char_type = charType(c);
                    if (char_type != CT_HARD)
                    {
                        chop_index = i;
                        chop_width = width;
                    }
                    if (width > m_max_width)
                    {
                        goto find_chop_point;
                    }
                }

                m_chop1 = i;
                return(false);

find_chop_point:
                if (m_chop0 == i)
                {
                    m_chop1               = i + 1;
                    m_next_index          = i + 1;
                    m_next_chop0          = i + 1;
                    m_next_width          = 0;
                    m_next_prev_char_type = 0;
                    m_next_prev_c         = 0;
                    goto end;
                }

                switch (char_type)
                {
                case CT_SPACE:
                    m_chop1 = i;
                    i++;
                    goto skip_space;

                case CT_HARD:
                    if (chop_index > 0)
                    {
                        m_chop1               = chop_index + 1;
                        m_next_index          = i + 1;
                        m_next_chop0          = chop_index + 1;
                        m_next_width          = width - chop_width;
                        m_next_prev_char_type = char_type;
                        m_next_prev_c         = c;
                    }
                    else
                    {
                        m_chop1               = i;
                        m_next_index          = i + 1;
                        m_next_chop0          = i;
                        m_next_width          = w;
                        m_next_prev_char_type = 0;
                        m_next_prev_c         = 0;
                    }
                    break;

                case CT_SOFT:
                    if (m_chop0 + 1 < i)
                    {
                        if (isOpen(m_str[i - 1]) || (isDoNotSplit(c) && !isDoNotSplit(m_str[i - 1])))
                        {
                            goto chop_back;
                        }
                    }
                    if (isFullWidthClose(c))
                    {
                        int w2 = w / 2;
                        if ((w2 > 0) && (m_max_width - (width - w) >= w2))
                        {
                            i++;
                            m_chop1 = i;
                            goto skip_space;
                        }
                    }
                    if (isClose(c))
                    {
                        if (m_chop0 + 1 < i)
                        {
                            if ((charType(m_str[i - 1]) == CT_SOFT) && !isClose(m_str[i - 1]) && !isDoNotSplit(m_str[i - 1]))
                            {
                                goto chop_back;
                            }
                        }
                    }
                    m_chop1               = i;
                    m_next_index          = i + 1;
                    m_next_chop0          = i;
                    m_next_width          = w;
                    m_next_prev_char_type = 0;
                    m_next_prev_c         = 0;
                    break;
                }
                goto end;

chop_back:
                m_chop1               = i - 1;
                m_next_index          = i - 1;
                m_next_chop0          = i - 1;
                m_next_width          = 0;
                m_next_prev_char_type = 0;
                m_next_prev_c         = 0;
                goto end;

skip_space:
                for (; i < m_len; i++)
                {
                    c = m_str[i];
                    if (c == '\r')
                    {
                        i++;
                        if (i < m_len && m_str[i] == '\n')
                        {
                            i++;
                        }
                        break;
                    }
                    if (c == '\n')
                    {
                        i++;
                        break;
                    }
                    if (!isSpace(c))
                    {
                        break;
                    }
                }

                m_next_index          = i;
                m_next_chop0          = i;
                m_next_width          = 0;
                m_next_prev_char_type = 0;
                m_next_prev_c         = 0;

end:
                //m_line_count++;
                return(true);
            }
Ejemplo n.º 10
0
        public static bool Widgets_Label_prefix(Rect rect, string label)
        {
            if (Event.current.type != EventType.Repaint)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(label))
            {
                return(false);
            }

            int      font_index = (int)Text.Font;
            GUIStyle style      = Text.CurFontStyle;

            string str   = label;
            int    width = (int)rect.width;

            int line_count;

            CacheData cache = s_cache.getData(str, font_index);

            if (style.wordWrap)
            {
                if (cache.width != 0 && cache.width <= width)
                {
                    line_count = cache.line_count;
                    goto end;
                }

                if (
                    (cache.wrap_width == width) ||
                    (cache.wrap_width != 0 && cache.wrap_str == null && cache.wrap_width <= width)
                    )
                {
                    line_count = cache.wrap_line_count;
                    if (cache.wrap_str != null)
                    {
                        str = cache.wrap_str;
                    }
                    goto end;
                }

                WordWrap_Unity.setupFont(style, font_index);
                string new_label = WordWrap.modify(str, width, out line_count);

                cache.wrap_width      = (int)width;
                cache.wrap_line_count = line_count;
                cache.wrap_str        = new_label;

                line_count = cache.wrap_line_count;
                if (cache.wrap_str != null)
                {
                    str = cache.wrap_str;
                }
            }
            else
            {
                if (cache.width == 0)
                {
                    s_calcSize(style, font_index, str, cache);
                }
                line_count = cache.line_count;
            }
end:
            int text_height = calcHeight(style, font_index, line_count);

            TextAnchor alignment = style.alignment;
            float      offset_y;

            switch (alignment)
            {
            case TextAnchor.MiddleLeft:
                offset_y        = rect.height / 2 - text_height / 2;
                style.alignment = TextAnchor.UpperLeft;
                break;

            case TextAnchor.MiddleCenter:
                offset_y        = rect.height / 2 - text_height / 2;
                style.alignment = TextAnchor.UpperCenter;
                break;

            case TextAnchor.MiddleRight:
                offset_y        = rect.height / 2 - text_height / 2;
                style.alignment = TextAnchor.UpperRight;
                break;

            case TextAnchor.LowerLeft:
                offset_y        = rect.height - text_height;
                style.alignment = TextAnchor.UpperLeft;
                break;

            case TextAnchor.LowerCenter:
                offset_y        = rect.height - text_height;
                style.alignment = TextAnchor.UpperCenter;
                break;

            case TextAnchor.LowerRight:
                offset_y        = rect.height - text_height;
                style.alignment = TextAnchor.UpperRight;
                break;

            default:
                offset_y = 0;
                break;
            }
            rect.y += offset_y;

            bool ww = style.wordWrap;

            style.wordWrap = false;
            GUI.Label(rect, str, style);
            style.wordWrap  = ww;
            style.alignment = alignment;

            return(false);
        }