/// <summary>
        /// 插入一個空方。
        /// </summary>
        /// <param name="grid"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="count">插入幾個空方。</param>
        private void InsertBlankCell(SourceGrid.Grid grid, int row, int col, int count)
        {
            // 防錯:如果不是有效的儲存格位置就直接返回。
            if (!CheckCellPosition(row, col))
            {
                return;
            }

            int         wordIdx = GetBrailleWordIndex(row, col);
            int         lineIdx = GetBrailleLineIndex(row);
            BrailleLine brLine  = m_BrDoc.Lines[lineIdx];

            while (count > 0)
            {
                brLine.Words.Insert(wordIdx, BrailleWord.NewBlank());
                count--;
            }
            IsDirty = true;

            // Update UI.
            ReformatRow(row);
            SourceGrid.Position pos = new SourceGrid.Position(row, col + 1);
            grid.Selection.Focus(pos, true);    // 修正選取的儲存格範圍。
        }
 /// <summary>
 /// 將一個 BraillWord 物件轉換可對應的點字字型 ASCII 字串,以便顯示於螢幕上。
 /// Note: 如果你需要轉換破音字的其他注音字根的點字,請呼叫另一個 ToString 版本:
 /// public string ToString(BrailleCellList cellList)
 /// </summary>
 /// <param name="brCell"></param>
 /// <returns></returns>
 public static string ToString(BrailleWord brWord)
 {
     return(BrailleFontConverter.ToString(brWord.CellList));
 }
        public void ConvertTest()
        {
            string msg = "ChineseWordConverter.Convert 測試失敗: ";

            var target = new ChineseWordConverter(
                new ZhuyinReverseConverter(new ZhuyinReverseConversionProvider()));

            ContextTagManager context = new ContextTagManager();

            // 測試結合韻。
            string             text      = "我";
            Stack <char>       charStack = new Stack <char>(text);
            List <BrailleWord> expected  = new List <BrailleWord>();
            BrailleWord        brWord    = new BrailleWord(text, " ㄨㄛˇ", "1208");

            expected.Add(brWord);
            List <BrailleWord> actual = target.Convert(charStack, context);

            CollectionAssert.AreEqual(expected, actual, msg + text);
            charStack.Clear();

            // 測試單音字:要在音調記號前加一個空方。
            text      = "智";
            charStack = new Stack <char>(text);
            brWord    = new BrailleWord(text, "ㄓ  ˋ", "013110");
            expected.Clear();
            expected.Add(brWord);
            actual = target.Convert(charStack, context);
            CollectionAssert.AreEqual(expected, actual, msg + text);
            charStack.Clear();

            // 測試無特殊規則的注音。
            text      = "你";
            charStack = new Stack <char>(text);
            brWord    = new BrailleWord(text, "ㄋㄧ ˇ", "1D2108");
            expected.Clear();
            expected.Add(brWord);
            actual = target.Convert(charStack, context);
            CollectionAssert.AreEqual(expected, actual, msg + text);
            charStack.Clear();

            // 測試標點符號。
            text      = ":";
            charStack = new Stack <char>(text);
            brWord    = new BrailleWord(text, "   ˉ", "1212");
            expected.Clear();
            expected.Add(brWord);
            actual = target.Convert(charStack, context);
            CollectionAssert.AreEqual(expected, actual, msg + text);
            charStack.Clear();

            // 測試全形空白
            text      = " ";
            charStack = new Stack <char>(text);
            brWord    = new BrailleWord(text, "   ˉ", "00");
            expected.Clear();
            expected.Add(brWord);
            actual = target.Convert(charStack, context);
            CollectionAssert.AreEqual(expected, actual, msg + text);
            charStack.Clear();

            // 測試簡體字。
            text      = "实";
            charStack = new Stack <char>(text);
            brWord    = new BrailleWord(text, "ㄕ  ˊ", "0A3102");
            expected.Clear();
            expected.Add(brWord);
            actual = target.Convert(charStack, context);
            CollectionAssert.AreEqual(expected, actual, msg + text);
            charStack.Clear();

            // 測試無法轉換的字元:/
            text      = "/";
            charStack = new Stack <char>(text);
            expected  = null;
            actual    = target.Convert(charStack, context);
            CollectionAssert.AreEqual(expected, actual, msg + text);
            charStack.Clear();
        }
        /// <summary>
        /// 把一個字元轉換成點字。
        /// </summary>
        /// <param name="text">一個中文字或標點符號。</param>
        /// <returns>若指定的字串是中文字且轉換成功,則傳回轉換之後的點字物件,否則傳回 null。</returns>
        private BrailleWord InternalConvert(string text)
        {
            BrailleWord brWord = new BrailleWord
            {
                Text = text
            };

            string brCode;

            if (text.Length == 1)
            {
                char ch = text[0];

                // 如果輸入的明眼字是注音符號,就直接傳回注音的點字碼。
                if (Zhuyin.IsBopomofo(ch))
                {
                    // 注意: 不要指定 brWord.PhoneticCode,因為注音符號本身只是個中文符號,
                    //       它並不是中文字,沒有合法的注音組字字根,因此不可指定注音碼。
                    brCode = _brailleTable.FindPhonetic(text);
                    brWord.AddCell(brCode);
                    return(brWord);
                }
                // 如果輸入的明眼字是注音符號的音調記號,就直接傳回對應的點字碼。
                if (Zhuyin.IsTone(ch))
                {
                    // 注意: 不要指定 brWord.PhoneticCode,因為音調記號本身只是個中文符號,
                    //       它並不是中文字,沒有合法的注音組字字根,因此不可指定注音碼。
                    brCode = _brailleTable.FindTone(text);
                    brWord.AddCell(brCode);

                    return(brWord);
                }
            }

            // 嘗試取得該字的注音字根,若可成功取得,則將注音字根轉換成點字碼,並傳回 BrailleWord 物件。
            string phcode = null;

            if (text.IsCJK())  // 若是漢字
            {
                /* 2010-01-03: 不取得所有的注音字根,只取得一組預設的字根,且判斷是否為多音字。等到編輯時使用者要更換注音,才取出所有字根。
                 *  // 取得破音字的所有組字字根,每一組字根長度固定為 4 個字元,不足者以全型空白填補。
                 *  string[] phCodes = ZhuyinQueryHelper.GetZhuyinSymbols(text, true);
                 *  if (phCodes.Length > 0)
                 *  {
                 *      brWord.SetPhoneticCodes(phCodes);
                 *      phcode = phCodes[0];    // 指定第一組字根為預設的字根。
                 *  }
                 */

                // 取得注音字根
                string[] zhuyinCodes = ZhuyinConverter.GetZhuyin(text);

                //if (zhuyinCodes == null || zhuyinCodes.Length == 0)
                //{
                //    // 若 IFELanguage 無法轉換,就用內建的注音字根查詢功能。
                //    zhuyinCodes = ZhuyinQueryHelper.GetZhuyinSymbols(text, true);   // 此方法會傳回一個中文字的所有注音字根。
                //}

                if (zhuyinCodes.Length >= 1)
                {
                    phcode = zhuyinCodes[0];
                }

                if (!String.IsNullOrEmpty(phcode))
                {
                    // 設定多音字旗號屬性.
                    brWord.IsPolyphonic = ZhuyinQueryHelper.IsPolyphonic(text);

                    // TODO: 以下「將注音字根轉換成點字碼」的處理應可省略,因為 FixPhoneticCodes 會重新修正所有中文字的點字碼。

                    // 將注音字根轉換成點字碼
                    BrailleCellList cellList = CreatePhoneticCellList(phcode);
                    if (cellList != null)
                    {
                        brWord.CellList.Assign(cellList);
                        brWord.PhoneticCode = phcode;
                        return(brWord);
                    }
                }
            }

            // 不是中文字,或者無法取得注音字根.

            // 處理標點符號
            string puncBrCode = _brailleTable.FindPunctuation(text);

            if (!String.IsNullOrEmpty(puncBrCode))
            {
                brWord.AddCell(puncBrCode);
                return(brWord);
            }

            // 其它符號
            brCode = _brailleTable.Find(text);
            if (!String.IsNullOrEmpty(brCode))
            {
                brWord.AddCell(brCode);
                return(brWord);
            }

            brWord.Clear();
            brWord = null;
            return(null);
        }
예제 #5
0
        /// <summary>
        /// 把英數字轉換成點字。
        /// </summary>
        /// <param name="text">一個英數字或英文標點符號。</param>
        /// <returns>若指定的字串是中文字且轉換成功,則傳回轉換之後的點字物件,否則傳回 null。</returns>
        private BrailleWord InternalConvert(string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                return(null);
            }

            BrailleWord brWord = new BrailleWord();

            brWord.Text = text;

            string brCode = null;

            // 如果是刪節號
            if (text == "...")
            {
                brCode = m_Table.Find(text);
                brWord.AddCell(brCode);
                return(brWord);
            }

            // 處理英文字母和數字。
            if (text.Length == 1)
            {
                char ch = text[0];
                if (CharHelper.IsAsciiLetter(ch))
                {
                    brCode = m_Table.FindLetter(text);
                    if (!String.IsNullOrEmpty(brCode))
                    {
                        brWord.AddCell(brCode);
                        return(brWord);
                        // 註:大寫記號和連續大寫記號在完成一行之後才處理。
                    }
                    throw new Exception("找不到對應的點字: " + text);
                }
                if (CharHelper.IsAsciiDigit(ch))
                {
                    brCode = m_Table.FindDigit(text, false);                            // 一般數字取下位點。
                    if (!String.IsNullOrEmpty(brCode))
                    {
                        brWord.AddCell(brCode);
                        return(brWord);
                    }
                    throw new Exception("找不到對應的點字: " + text);
                }
            }

            // 處理編號。
            if (text == "#")
            {
                // # 沒有對應的點字碼,只是用它來代表編號數字的開始,
                // 以便後續處理編號用(將編號數字轉成上位點)。
                return(brWord);
            }

            brCode = m_Table.Find(text);
            if (!String.IsNullOrEmpty(brCode))
            {
                brWord.AddCell(brCode);
                return(brWord);
            }

            brWord = null;
            return(null);
        }