Exemplo n.º 1
0
        /// <summary>
        /// 根據前後鄰近的字元判斷中間是否需要加一個空方。
        /// </summary>
        /// <param name="lastWord">前一個字。</param>
        /// <param name="currWord">目前的字。</param>
        /// <returns></returns>
        private static bool NeedSpace(BrailleWord lastWord, BrailleWord currWord)
        {
            if (lastWord == null || currWord == null)
            {
                throw new ArgumentException("傳入 NeedSpace() 的參數為 null!");
            }

            if (lastWord.IsEngPhonetic && currWord.IsEngPhonetic)
            {
                return(false);
            }

            if (String.IsNullOrEmpty(lastWord.Text) || String.IsNullOrEmpty(currWord.Text))
            {
                return(false);
            }

            if (ContextTag.StartsWithContextTag(lastWord.Text))                 // 如果前一個字是情境標籤,就不加空方
            {
                return(false);
            }
            if (ContextTag.StartsWithContextTag(currWord.Text))                 // 如果目前的字是情境標籤,就不加空方
            {
                return(false);
            }

            char lastChar = lastWord.Text[0];
            char currChar = currWord.Text[0];

            // 若前一個字元已經是空白,就不用處理。
            if (Char.IsWhiteSpace(lastChar))
            {
                return(false);
            }

            if (lastChar == '★')  // 「點譯者註」的後面不加空方。
            {
                return(false);
            }

            if (lastChar == '□' || currChar == '□')  // 「滿點」符號的左右均不加空方。
            {
                return(false);
            }

            if (CharHelper.IsAscii(currChar) && !CharHelper.IsAscii(lastChar))
            {
                // 目前的字元是 ASCII,但前一個字元不是(視為中文或其他雙位元組字元),需插入
                // 一個空白,除了一些例外,如:全型+號前後若接數字,即視為數學式子,+號前後都不空方。

                if (Char.IsDigit(currChar))
                {
                    switch (lastChar)
                    {
                    case '+':
                    case '-':
                    case '×':       // 全型乘號 (VS2005 編輯器無法正確顯示)
                    case '÷':       // 全型除號 (VS2005 編輯器無法正確顯示)
                    case '(':
                    case '【':       // // 用粗中刮弧把數字包起來時,代表題號,不用加空方.
                        return(false);
                    }
                }
                return(true);
            }

            if (!CharHelper.IsAscii(currChar) && CharHelper.IsAscii(lastChar))
            {
                // 目前的字元不是 ASCII,但前一個字元是,需插入一個空白,
                // 除了一些例外,例如:12℃ 的溫度符號前面不加空方。

                if (Char.IsDigit(lastChar))
                {
                    switch (currChar)
                    {
                    case '∘':      // 溫度符號
                    case '℃':
                    case '+':
                    case '-':
                    case '×':       // 全型乘號 (VS2005 編輯器無法正確顯示)
                    case '÷':       // 全型除號 (VS2005 編輯器無法正確顯示)
                    case ')':
                    case '】':       // 用粗中刮弧把數字包起來時,代表題號,不用加空方.
                        return(false);
                    }
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        // search and return the color code...
        // create own tokenizer for fastest performance...
        // This was written to search for color for the highest performance...
        public static string SearchColor(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            List <char> _buffer = new List <char>();

            int len = input.Length;

            for (int i = 0; i < len; i++)
            {
                var currentchar = input[i];
                if (currentchar == '#')
                {
                    if ((i + 6) < len)
                    {
                        if (CharHelper.isAsciiHexDigit(input[i + 6]) && CharHelper.isAsciiHexDigit(input[i + 5]) && CharHelper.isAsciiHexDigit(input[i + 4]) && CharHelper.isAsciiHexDigit(input[i + 3]) && CharHelper.isAsciiHexDigit(input[i + 2]) && CharHelper.isAsciiHexDigit(input[i + 1]))
                        {
                            return(input.Substring(i, 7));
                        }
                    }
                    else if ((i + 3) < len)
                    {
                        if (CharHelper.isAsciiHexDigit(input[i + 3]) && CharHelper.isAsciiHexDigit(input[i + 2]) && CharHelper.isAsciiHexDigit(input[i + 1]))
                        {
                            return(input.Substring(i, 4));
                        }
                    }
                }
                else if (CharHelper.IsAscii(currentchar))
                {
                    _buffer.Add(currentchar);
                }
                else
                {
                    string output = new string(_buffer.ToArray());
                    _buffer.Clear();

                    if (!string.IsNullOrWhiteSpace(output))
                    {
                        string lower = output.ToLower().Trim();
                        if (lower == "rgb" || lower == "rgba" || lower == "hsl" || lower == "hsla")
                        {
                            // find the next )
                            int nextbracket = -1;
                            for (int j = i; j < len; j++)
                            {
                                if (input[j] == ')')
                                {
                                    nextbracket = j;
                                    break;
                                }
                            }
                            if (nextbracket > -1)
                            {
                                string nextpart = input.Substring(i, nextbracket - i + 1);
                                return(output + nextpart);
                            }
                        }
                        else if (ColorNameSet.Contains(output))
                        {
                            return(output);
                        }
                    }
                }
            }

            return(null);
        }