private static ITsString GetSpaceAdjustedPunctString(ILgWritingSystemFactory wsFactory, ITsStrFactory tsStrFactory, item item, ITsString wordString, char space, bool followsWord) { if(item.Value.Length > 0) { var index = 0; ITsString tempValue = AdjustPunctStringForCharacter(wsFactory, tsStrFactory, item, wordString, item.Value[index], index, space, followsWord); if(item.Value.Length > 1) { index = item.Value.Length - 1; tempValue = AdjustPunctStringForCharacter(wsFactory, tsStrFactory, item, tempValue, item.Value[index], index, space, followsWord); } return tempValue; } return wordString; }
private static ITsString AdjustPunctStringForCharacter( ILgWritingSystemFactory wsFactory, ITsStrFactory tsStrFactory, item item, ITsString wordString, char punctChar, int index, char space, bool followsWord) { bool spaceBefore = false; bool spaceAfter = false; bool spaceHere = false; char quote = '"'; var charType = Icu.GetCharType(punctChar); switch (charType) { case Icu.UCharCategory.U_END_PUNCTUATION: case Icu.UCharCategory.U_FINAL_PUNCTUATION: spaceAfter = true; break; case Icu.UCharCategory.U_START_PUNCTUATION: case Icu.UCharCategory.U_INITIAL_PUNCTUATION: spaceBefore = true; break; case Icu.UCharCategory.U_OTHER_PUNCTUATION: //handle special characters if(wordString.Text.LastIndexOfAny(new[] {',','.',';',':','?','!',quote}) == wordString.Length - 1) //treat as ending characters { spaceAfter = punctChar != '"' || wordString.Length > 1; //quote characters are extra special, if we find them on their own //it is near impossible to know what to do, but it's usually nothing. } if (punctChar == '\xA1' || punctChar == '\xBF') { spaceHere = true; } if (punctChar == quote && wordString.Length == 1) { spaceBefore = followsWord; //if we find a lonely quotation mark after a word, we'll put a space before it. } break; } var wordBuilder = wordString.GetBldr(); if(spaceBefore) //put a space to the left of the punct { ILgWritingSystem wsEngine; if (TryGetWsEngine(wsFactory, item.lang, out wsEngine)) { wordBuilder.ReplaceTsString(0, 0, tsStrFactory.MakeString("" + space, wsEngine.Handle)); } wordString = wordBuilder.GetString(); } if (spaceHere && followsWord && !LastCharIsSpaceOrQuote(wordString, index, space, quote)) { ILgWritingSystem wsEngine; if (TryGetWsEngine(wsFactory, item.lang, out wsEngine)) { wordBuilder.ReplaceTsString(index, index, tsStrFactory.MakeString("" + space, wsEngine.Handle)); } wordString = wordBuilder.GetString(); } if(spaceAfter) //put a space to the right of the punct { ILgWritingSystem wsEngine; if (TryGetWsEngine(wsFactory, item.lang, out wsEngine)) { wordBuilder.ReplaceTsString(wordBuilder.Length, wordBuilder.Length, tsStrFactory.MakeString("" + space, wsEngine.Handle)); } wordString = wordBuilder.GetString(); } //otherwise punct doesn't deserve a space. return wordString; }
private static void NormalizeItems(item[] items) { if (items == null) return; foreach (var item in items) { if (item.Value == null) continue; item.Value = item.Value.Normalize(NormalizationForm.FormD); } }