/// <summary>
        /// 將 BrailleCellList 串列轉成對應的點字字型 ASCII 字串,以便顯示於螢幕上。
        /// 範例:
        /// string s = brFontConvert.ToString(brWord.CandidatePhoneticCellLists);
        /// </summary>
        /// <param name="cellList"></param>
        /// <returns></returns>
        public static string ToString(BrailleCellList cellList)
        {
            if (m_FontTable.Count < 1)
            {
                throw new Exception("尚未載入字型對應表!");
            }

            StringBuilder sb = new StringBuilder();

            foreach (BrailleCell brCell in cellList.Items)
            {
                sb.Append(ToChar(brCell.ToString()));
            }
            return(sb.ToString());
        }
示例#2
0
        // 當注音符號有變動
        private void cboPhCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_IsUpdatingUI || m_BrWord == null)
            {
                return;
            }
            if (cboPhCode.SelectedIndex < 0)
            {
                return;
            }

            m_BrWord.PhoneticCode = cboPhCode.Text;
            BrailleCellList cellList = m_ChtWordCvt.CreatePhoneticCellList(m_BrWord.PhoneticCode);

            m_BrWord.CellList.Assign(cellList);
            txtBraille.Text = BrailleFontConverter.ToString(cellList);
        }
        /// <summary>
        /// 修正一串連續中文字的注音字根。
        /// 這裡是利用新注音的智慧型詞彙判斷功能,以修正特定破音字的注音字根。
        /// </summary>
        /// <param name="brWordList">點字串列。</param>
        /// <param name="startIdx">起始索引。</param>
        /// <param name="endIdx">終止索引。</param>
        private void FixPhoneticCodes(List <BrailleWord> brWordList, int startIdx, int endIdx)
        {
            if (startIdx < 0 || endIdx < 0)
            {
                return;
            }
            if ((endIdx - startIdx + 1) < 2)    // 連續的中文字數若未達兩個字以上,就不處理
            {
                return;
            }

            // 連續的中文字元個數大於 1,使用新注音取得中文片語的注音字根,以修正破音字的字根。


            // 取出連續中文字。
            StringBuilder sb = new StringBuilder();

            for (int i = startIdx; i <= endIdx; i++)
            {
                sb.Append(brWordList[i].Text);
            }

            // 取得所有中文字的注音字根。
            string[]    allPhCodes = ZhuyinConverter.GetZhuyinWithPhraseTable(sb.ToString());
            string      phCode;
            BrailleWord brWord;

            for (int wordIdx = 0; wordIdx < allPhCodes.Length; wordIdx++)
            {
                phCode = allPhCodes[wordIdx];
                brWord = brWordList[startIdx + wordIdx];
                if (Zhuyin.IsEqual(brWord.PhoneticCode, phCode))    // 如果跟原有的注音字根相同,就略過
                {
                    continue;
                }

                // 將注音字根轉換成點字碼
                BrailleCellList cellList = CreatePhoneticCellList(phCode);
                if (cellList != null)
                {
                    brWord.CellList.Assign(cellList);
                    brWord.PhoneticCode = phCode;
                }
            }
        }
        /// <summary>
        /// 將一個中文字的注音組字字根碼轉換成點字碼串列,並加入指定的 BrailleWord 物件。
        /// 注意:若轉換成功,會同時加入注音碼和點字碼。
        /// </summary>
        /// <param name="phcode">一個中文字的注音組字字根碼</param>
        /// <param name="brWord">欲加入點字的 BrailleWord 物件</param>
        /// <returns>成功傳回 true,失敗傳回 false。</returns>
        public BrailleCellList CreatePhoneticCellList(string phcode)
        {
            if (StrHelper.IsEmpty(phcode) || phcode.Length < 4)
            {
                return(null);   // 不是中文字
            }
            // 先取出注音符號各部份。
            // TODO: 改成不用空白就能判斷結合韻,否則片語無法使用。
            string firstPhCode  = phcode.Substring(0, 1);       // 第一個注音符號
            string secondPhCode = phcode.Substring(1, 1);       // 第二個注音符號
            string thirdPhCode  = phcode.Substring(2, 1);       // 第三個注音符號
            string joinedPhCode = phcode.Substring(1, 2);       // 第二、三結合韻
            string tonePhCode   = phcode.Substring(3, 1);       // 音調

            // 取出注音符號各部份的點字碼。
            string firstBrCode  = _brailleTable.FindPhonetic(firstPhCode);
            string secondBrCode = _brailleTable.FindPhonetic(secondPhCode);
            string thirdBrCode  = _brailleTable.FindPhonetic(thirdPhCode);
            string toneBrCode   = _brailleTable.FindTone(tonePhCode);

            if (firstBrCode == null && secondBrCode == null && thirdBrCode == null)
            {
                return(null);
            }

            BrailleCellList cellList = new BrailleCellList();

            // 處理特殊的單音字。
            if (StrHelper.IsEmpty(secondPhCode) && StrHelper.IsEmpty(thirdPhCode))
            {
                string monoBrCode = _brailleTable.FindMono(firstPhCode);
                if (String.IsNullOrEmpty(monoBrCode))
                {
                    throw new Exception("無效的注音符號: " + phcode);
                }
                cellList.Add(monoBrCode);

                // 特殊單音字要附加 'ㄦ'
                string erBrCode = _brailleTable.FindPhonetic("ㄦ");
                if (String.IsNullOrEmpty(erBrCode))
                {
                    throw new Exception("點字對照表中無此符號: ㄦ");
                }
                cellList.Add(erBrCode);

                // 再加上聲調
                cellList.Add(toneBrCode);

                return(cellList);
            }

            // 處理結合韻。
            string joinedBrCode = _brailleTable.FindJoined(joinedPhCode);

            if (!String.IsNullOrEmpty(joinedBrCode)) // 是結合韻?
            {
                cellList.Add(firstBrCode);           // 加入第一個注音符號
                cellList.Add(joinedBrCode);          // 加入結合韻
                cellList.Add(toneBrCode);            // 加入聲調
                return(cellList);
            }

            // 不是特殊單音字,也不是結合韻:其他注音符號拼法。例如:"ㄋㄧ ˇ"。
            cellList.Add(firstBrCode);  // 加入第一個注音符號
            cellList.Add(secondBrCode); // 加入第二個注音符號
            cellList.Add(thirdBrCode);  // 加入第三個注音符號
            cellList.Add(toneBrCode);   // 加入聲調
            return(cellList);
        }
        /// <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);
        }