private void Indent(BrailleLine brLine, int indents) { for (int i = 0; i < indents; i++) { brLine.Insert(0, BrailleWord.NewBlank()); } }
/// <summary> /// 在指定的位置左邊附加一個空方,若該位置已經有空方,則不做任何處理。 /// </summary> /// <param name="brLine"></param> /// <param name="index"></param> /// <returns>這次調整一共增加或刪除了幾個 word。</returns> private static int PrefixBlankCell(BrailleLine brLine, int index) { int wordOffset = 0; if (index > 0 && !BrailleWord.IsBlank(brLine[index - 1])) { brLine.Words.Insert(index, BrailleWord.NewBlank()); wordOffset = 1; } return(wordOffset); }
/// <summary> /// 在指定的位置右邊附加一個空方,若該位置已經有空方,則不做任何處理。 /// </summary> /// <param name="brLine"></param> /// <param name="index"></param> /// <returns>這次調整一共增加或刪除了幾個 word。</returns> private static int PostfixBlankCell(BrailleLine brLine, int index) { int wordOffset = 0; index++; if (index < brLine.WordCount) // 如果已經到結尾,就不加空方。 { if (!BrailleWord.IsBlank(brLine[index])) { brLine.Words.Insert(index, BrailleWord.NewBlank()); wordOffset = 1; } } return(wordOffset); }
/// <summary> /// 根據中文點字的冒號規則修正傳入的點字行。 /// 規則:冒號之後若是 "我" 字,必須加一空方。 /// </summary> /// <param name="brLine"></param> /// <param name="index"></param> /// <returns>這次調整一共增加或刪除了幾個 word。</returns> private static int ApplyColonRule(BrailleLine brLine, int index) { int wordOffset = 0; index++; if (index < brLine.WordCount) { // 若下一個點字是我,則加一空方。 if (brLine[index].Text == "我") { brLine.Words.Insert(index, BrailleWord.NewBlank()); wordOffset = 1; // 跳過空方 } } return(wordOffset); }
/// <summary> /// 補加必要的空白:在英數字母和中文字之間補上空白。 /// </summary> /// <param name="brLine"></param> public static void AddSpaces(BrailleLine brLine) { int wordIdx = 0; BrailleWord brWord; BrailleWord lastBrWord; int wordOffset; while (wordIdx < brLine.WordCount) { brWord = brLine[wordIdx]; if (brWord.Text.Length < 1) { wordIdx++; continue; } if (Char.IsWhiteSpace(brWord.Text[0])) { wordIdx++; continue; } if (wordIdx == 0) { wordIdx++; continue; } wordOffset = AddBlankForSpecialCharacters(brLine, wordIdx); if (wordOffset > 0) { wordIdx += wordOffset; continue; } // 取前一個字元。 lastBrWord = brLine[wordIdx - 1]; if (NeedSpace(lastBrWord, brWord)) { brLine.Words.Insert(wordIdx, BrailleWord.NewBlank()); wordIdx++; } wordIdx++; } }
/// <summary> /// 套用問號、驚嘆號的點字規則。 /// 規則:後面要加一空方,若後面跟著下引號('」'),則不加空方。 /// </summary> /// <param name="brLine"></param> /// <param name="index"></param> /// <returns></returns> private static int ApplyQuestionRule(BrailleLine brLine, int index) { index++; if (index >= brLine.WordCount) // 如果已經到結尾或超過,就不加空方。 { return(0); } int wordOffset = 0; BrailleWord brWord = brLine[index]; if (!BrailleWord.IsBlank(brWord)) // 若原本已有空方,就不再多加。 { if (!brWord.Text.Equals("」")) { brLine.Words.Insert(index, BrailleWord.NewBlank()); wordOffset = 1; } } return(wordOffset); }
/// <summary> /// 套用句號規則: /// 1.在同一點字行中,句號之後,須空一方,再點寫下文; /// 如在行末不夠點寫句號時,須將原句最末一字與句號移至次一行連書, /// 然後空方接寫下文;如句號恰在一行之最末一方,下文換行點寫時,次行之首無須空方。 /// 2.句號可與後引號(包括後單或雙引號)、刪節號、後括弧 /// (包括後圓括弧、後方括弧、後大括弧)、後夾註號連書,但不得單獨書於一行之首)。 /// </summary> /// <param name="brLine"></param> /// <param name="index"></param> /// <returns></returns> private static int ApplyPeriodRule(BrailleLine brLine, int index) { index++; if (index >= brLine.WordCount) // 如果已經到結尾或超過,就不加空方。 { return(0); } int wordOffset = 0; BrailleWord brWord = brLine[index]; if (!BrailleWord.IsBlank(brWord)) // 若原本已有空方,就不再多加。 { // 句號可與標點符號連書而無須加空方。 if (!BrailleWord.IsChinesePunctuation(brWord)) { brLine.Words.Insert(index, BrailleWord.NewBlank()); wordOffset = 1; } } return(wordOffset); }
private bool m_IsEngPhonetic; // 是否為英語音標(用來判斷不要加空方). //private bool m_QuotationResolved; // 是否已經識別出左右引號(英文的單引號和雙引號都是同一個符號,但點字不同) static BrailleWord() { m_Blank = BrailleWord.NewBlank(); }
/// <summary> /// 把一行明眼字串轉換成點字串列。 /// 此時不考慮一行幾方和斷行的問題,只進行單純的轉換。 /// 斷行由其他函式負責處理,因為有些點字規則必須在斷行時才能處理。 /// </summary> /// <param name="lineNumber">字串的行號。此參數只是用來當轉換失敗時,傳給轉換失敗事件處理常式的資訊。</param> /// <param name="line">輸入的明眼字串。</param> /// <param name="isTitle">輸出參數,是否為標題。</param> /// <returns>點字串列。若則傳回 null,表示該列不需要轉成點字。</returns> public BrailleLine ConvertLine(int lineNumber, string line) { if (line == null) { return(null); } BrailleLine brLine = new BrailleLine(); string orgLine = line; // 保存原始的字串。 // 把換行符號之後的字串去掉 int i = line.IndexOfAny(new char[] { '\r', '\n' }); if (i >= 0) { line = line.Substring(0, i); } // 若去掉換行字元之後變成空字串,則傳回只包含一個空方的列。 if (String.IsNullOrEmpty(line)) { brLine.Words.Add(BrailleWord.NewBlank()); return(brLine); } // 預先處理特殊標籤的字元替換。 line = PreprocessTagsForLine(line); if (line == null) { return(null); } // 如果是原書頁碼,先檢查格式是否正確。 try { GetOrgPageNumber(line); } catch (Exception ex) { m_ErrorMsg.Append(String.Format("第 {0} 列 : ", lineNumber)); m_ErrorMsg.Append(ex.Message); m_ErrorMsg.Append("\r\n"); return(null); } line = StrHelper.Reverse(line); Stack <char> charStack = new Stack <char>(line); char ch; List <BrailleWord> brWordList; StringBuilder text = new StringBuilder(); ConvertionFailedEventArgs cvtFailedArgs = new ConvertionFailedEventArgs(); TextConvertedEventArgs textCvtArgs = new TextConvertedEventArgs(); while (charStack.Count > 0) { brWordList = ConvertWord(charStack); if (brWordList != null && brWordList.Count > 0) { // 成功轉換成點字,有 n 個字元會從串流中取出 brLine.Words.AddRange(brWordList); text.Length = 0; foreach (BrailleWord brWord in brWordList) { text.Append(brWord.Text); } textCvtArgs.SetArgValues(lineNumber, text.ToString()); OnTextConverted(textCvtArgs); } else { // 無法判斷和處理的字元應該會留存在串流中,將之取出。 ch = charStack.Pop(); int charIndex = line.Length - charStack.Count; // 引發事件。 cvtFailedArgs.SetArgs(lineNumber, charIndex, orgLine, ch); OnConvertionFailed(cvtFailedArgs); if (cvtFailedArgs.Stop) { break; } } // 如果進入分數情境,就把整個分數處理完。 if (m_ContextTagManager.IsActive(ContextTagNames.Fraction)) { try { brWordList = ConvertFraction(lineNumber, charStack); if (brWordList != null && brWordList.Count > 0) { // 成功轉換成點字,有 n 個字元會從串流中取出 brLine.Words.AddRange(brWordList); } } catch (Exception ex) { m_ErrorMsg.Append(String.Format("第 {0} 列 : ", lineNumber)); m_ErrorMsg.Append(ex.Message); m_ErrorMsg.Append("\r\n"); } } } ChineseBrailleRule.ApplyPunctuationRules(brLine); // 套用中文標點符號規則。 // 不刪除多餘空白,因為原本輸入時可能就希望縮排。 //ChineseBrailleRule.ShrinkSpaces(brLine); // 把連續全形空白刪到只剩一個。 // 將編號的數字修正成上位點。 if (m_EnglishConverter != null) { EnglishBrailleRule.FixNumbers(brLine, m_EnglishConverter.BrailleTable as EnglishBrailleTable); } EnglishBrailleRule.ApplyCapitalRule(brLine); // 套用大寫規則。 EnglishBrailleRule.ApplyDigitRule(brLine); // 套用數字規則。 EnglishBrailleRule.AddSpaces(brLine); // 補加必要的空白。 ChineseBrailleRule.ApplyBracketRule(brLine); // 套用括弧規則。 // 不刪除多於空白,因為原本輸入時可能就希望縮排。 //EnglishBrailleRule.ShrinkSpaces(brLine); // 把連續空白刪到只剩一個。 return(brLine); }
/// <summary> /// 將一行點字串列斷成多行。 /// </summary> /// <param name="brLine">來源點字串列。</param> /// <param name="cellsPerLine">每行最大方數。</param> /// <param name="context">情境物件。</param> /// <returns>斷行之後的多行串列。若為 null 表示無需斷行(指定的點字串列未超過每行最大方數)。</returns> public List <BrailleLine> BreakLine(BrailleLine brLine, int cellsPerLine, ContextTagManager context) { if (context != null && context.IndentCount > 0) // 若目前位於縮排區塊中 { // 每列最大方數要扣掉縮排數量,並於事後補縮排的空方。 // NOTE: 必須在斷行之後才補縮排的空方! cellsPerLine -= context.IndentCount; } // 若指定的點字串列未超過每行最大方數,則無須斷行,傳回 null。 if (brLine.CellCount <= cellsPerLine) { // 補縮排的空方。 if (context != null && context.IndentCount > 0) // 若目前位於縮排區塊中 { this.Indent(brLine, context.IndentCount); } return(null); } List <BrailleLine> lines = new List <BrailleLine>(); BrailleLine newLine = null; int wordIndex = 0; int breakIndex = 0; bool needHyphen = false; bool isBroken = false; // 是否已經斷行了? int indents = 0; // 第一次斷行時,不會有系統自斷加上的縮排,因此初始為 0。 int maxCells = cellsPerLine;; // 計算折行之後的縮排格數。 indents = BrailleProcessor.CalcNewLineIndents(brLine); while (wordIndex < brLine.WordCount) { breakIndex = BrailleProcessor.CalcBreakPoint(brLine, maxCells, out needHyphen); newLine = brLine.Copy(wordIndex, breakIndex); // 複製到新行。 if (needHyphen) // 是否要附加連字號? { newLine.Words.Add(new BrailleWord("-", BrailleCellCode.Hyphen)); } newLine.TrimEnd(); // 去尾空白。 // 如果是折下來的新行,就自動補上需要縮排的格數。 if (isBroken) { for (int i = 0; i < indents; i++) { newLine.Insert(0, BrailleWord.NewBlank()); } } brLine.RemoveRange(0, breakIndex); // 從原始串列中刪除掉已經複製到新行的點字。 wordIndex = 0; lines.Add(newLine); // 防錯:檢驗每個斷行後的 line 的方數是否超過每列最大方數。 // 若超過,即表示之前的斷行處理有問題,須立即停止執行,否則錯誤會 // 直到在雙視編輯的 Grid 顯示時才出現 index out of range,不易抓錯! System.Diagnostics.Debug.Assert(newLine.CellCount <= cellsPerLine, "斷行錯誤! 超過每列最大方數!"); // 被折行之後的第一個字需要再根據規則調整。 EnglishBrailleRule.ApplyCapitalRule(brLine); // 套用大寫規則。 EnglishBrailleRule.ApplyDigitRule(brLine); // 套用數字規則。 isBroken = true; // 已經至少折了一行 maxCells = cellsPerLine - indents; // 下一行開始就要自動縮排,共縮 indents 格。 } // 補縮排的空方。 if (context != null && context.IndentCount > 0) // 若目前位於縮排區塊中 { indents = context.IndentCount; foreach (BrailleLine aLine in lines) { this.Indent(aLine, indents); } } return(lines); }