Exemplo n.º 1
0
        private FontFormat GetFontFormatForTag(string tag)
        {
            var foundFontFormat = DefaultFontFormat;

            if (string.IsNullOrWhiteSpace(tag))
            {
                return(DefaultFontFormat);
            }

            foundFontFormat = TaggedFontFormats.FirstOrDefault(s => s.Tag == tag.Substring(1, tag.Length - 2)) as TaggedFontFormat;
            //TryFindResource(tag.Substring(1, tag.Length - 2)) as TaggedFontFormat;

            return(foundFontFormat == null ? DefaultFontFormat : foundFontFormat);
        }
Exemplo n.º 2
0
        private IEnumerable <Word> GetWords(string displayText)
        {
            if (string.IsNullOrEmpty(displayText))
            {
                yield break;
            }
            var previousTokenType = TokenType.Unknown;
            var currentTokenType  = TokenType.Unknown;

            var separators = new string[] { " " };
            var tagBegins  = TaggedFontFormats.Select(f => string.Format("<{0}>", f.Tag)).ToArray();  //resourcedTags.Select(i => string.Format("<{0}>", i)).ToArray();
            var tagEnds    = TaggedFontFormats.Select(f => string.Format("</{0}>", f.Tag)).ToArray(); //resourcedTags.Select(i => string.Format("</{0}>", i)).ToArray();

            var currentIndex        = 0;
            var currentWord         = new StringBuilder();
            var currentSeparator    = new StringBuilder();
            var currentTagBegin     = new StringBuilder();
            var currentTagEnd       = new StringBuilder();
            var currentNewLineFound = false;
            var wordFound           = true;

            #region GetNewWord Inline Method

            Func <Word> getNewWord = () =>
            {
                var resultWord = new Word
                {
                    Text         = currentWord.ToString(),
                    Separator    = currentSeparator.ToString(),
                    TagBegin     = currentTagBegin.ToString(),
                    TagEnd       = currentTagEnd.ToString(),
                    NewLineFound = currentNewLineFound,
                };
                currentWord.Clear();
                currentSeparator.Clear();
                currentTagBegin.Clear();
                currentTagEnd.Clear();
                currentNewLineFound = false;
                previousTokenType   = TokenType.Word;
                return(resultWord);
            };

            #endregion GetNewWord Inline Method

            while (currentIndex < displayText.Length)
            {
                wordFound = true;

                if (currentIndex + Environment.NewLine.Length <= displayText.Length && displayText.Substring(currentIndex, Environment.NewLine.Length) == Environment.NewLine)
                {
                    currentNewLineFound = true;
                    yield return(getNewWord());

                    currentIndex += Environment.NewLine.Length;
                    continue;
                    //previousTokenType = currentTokenType;
                    //currentTokenType = TokenType.NewLine;
                    //wordFound = false;
                }

                foreach (var tagBegin in tagBegins)
                {
                    if (currentIndex + tagBegin.Length <= displayText.Length && displayText.Substring(currentIndex, tagBegin.Length) == tagBegin)
                    {
                        yield return(getNewWord());

                        currentTagBegin.Append(tagBegin);
                        currentIndex     += tagBegin.Length;
                        previousTokenType = currentTokenType;
                        currentTokenType  = TokenType.TagBegin;
                        wordFound         = false;
                    }
                }

                foreach (var tagEnd in tagEnds)
                {
                    if (currentIndex + tagEnd.Length <= displayText.Length && displayText.Substring(currentIndex, tagEnd.Length) == tagEnd)
                    {
                        currentTagEnd.Append(tagEnd);
                        currentIndex     += tagEnd.Length;
                        previousTokenType = currentTokenType;
                        currentTokenType  = TokenType.TagEnd;
                        wordFound         = false;
                    }
                }

                foreach (var separator in separators)
                {
                    if (currentIndex + separator.Length <= displayText.Length && displayText.Substring(currentIndex, separator.Length) == separator)
                    {
                        currentSeparator.Append(separator);
                        currentIndex     += separator.Length;
                        previousTokenType = currentTokenType;
                        currentTokenType  = TokenType.Separator;
                        wordFound         = false;
                    }
                }

                if (wordFound)
                {
                    previousTokenType = currentTokenType;
                    currentTokenType  = TokenType.Word;
                }

                if ((currentTokenType == TokenType.Word) && (previousTokenType == TokenType.Separator || previousTokenType == TokenType.TagEnd))
                {
                    yield return(getNewWord());
                }
                else if (currentTokenType == TokenType.Word)
                {
                    currentWord.Append(displayText[currentIndex]);
                    currentIndex++;
                }
            }

            if (currentWord.Length > 0)
            {
                yield return(getNewWord());
            }
        }