public static TokenBase Load(BinaryReader reader, int id)
        {
            var type = (TokenType)reader.ReadByte();

            switch (type)
            {
            case TokenType.TagOpenToken:
                return(TagOpenToken.Load(reader, id));

            case TokenType.TagCloseToken:
                return(TagCloseToken.Load(reader, id));

            case TokenType.TextToken:
                return(TextToken.Load(reader, id));

            case TokenType.WhitespaceToken:
                return(new WhitespaceToken(id));

            case TokenType.NewPageToken:
                return(new NewPageToken(id));

            case TokenType.PictureToken:
                return(PictureToken.Load(reader, id));

            default:
                return(null);
            }
        }
        private void CreateLine(TextToken token)
        {
            CreateEmptyLine(token);

            string text = token.Text;
            _block.UpdateHeight(GetTextHeight(text));
            _textWidth = GetTextWidth(text, false, _lastOpenTag.TextProperties);
            _block.AddText(
                text, 
                _lastOpenTag.TextProperties, 
                _fontSize, 
                GetTextSize(text, _lastOpenTag.TextProperties), 
                token.Part, 
                token.ID);
        }
        private void AppendToLine(TextToken token, double textWidth)
        {
            string text = token.Text;
            if (string.IsNullOrEmpty(text))
                return;

            _block = _block ?? new TextTokenBlock
                                   {
                                       TextAlign = _lastOpenTag.TextProperties.TextAlign,
                                       MarginLeft = _marginLeft,
                                       MarginRight = _marginRight,
                                       FirstTokenID = _firstTokenID,
                                       TextIndent = _textIndent
                                   };
            
            _block.LastTokenID = token.ID;
            _block.UpdateHeight(GetTextHeight(text));
            if (_separator)
            {
                TextVisualProperties properties = _lastOpenTag.TextProperties.Clone();
                var inlineItem = _block.Inlines.OfType<TextElement>().LastOrDefault();
                if (inlineItem != null && string.IsNullOrEmpty(inlineItem.LinkID))
                    properties.LinkID = string.Empty;
                _block.AddText(" ", properties, _fontSize, GetTextSize(" ", properties));
            }
            _block.AddText(text, _lastOpenTag.TextProperties, _fontSize, GetTextSize(text, _lastOpenTag.TextProperties), token.Part, token.ID);
            _textWidth += textWidth;
        }
 private bool AppendTextToken(TextToken textToken, string lastText, int stopTokenID, string stopText, ref bool firstText)
 {
     bool flag = false;
     if (textToken.ID == stopTokenID && !string.IsNullOrEmpty(stopText))
     {
         textToken = new TextToken(stopTokenID, stopText + "-") {Part = stopText};
         flag = true;
     }
     if (firstText && !string.IsNullOrEmpty(lastText) && textToken.Text.StartsWith(lastText))
     {
         string text = textToken.Text.Remove(0, lastText.Length);
         if (text.StartsWith("-"))
             text = text.Remove(0, 1);
         textToken = new TextToken(textToken.ID, text);
     }
     AppendToLine(textToken);
     firstText = false;
     return flag;
 }
        private void AppendToLine(TextToken token)
        {
            if (_separator && (_block == null || _block.Inlines.All(t => !(t is TextElement))))
                _separator = false;
            double width = GetTextWidth(token.Text, _separator, _lastOpenTag.TextProperties);
            if (CanAddText(width))
            {
                AppendToLine(token, width);
            }
            else
            {
                string[] partsOfWord = _hypher.GetPartsOfWord(token.Text, _hyphenation);
                if (partsOfWord.Length > 1)
                {
                    double textWidth = 0.0;
                    string str = string.Empty;
                    foreach (string part in partsOfWord)
                    {
                        string partWithSeparator = part;
                        if (!partWithSeparator.EndsWith("-"))
                            partWithSeparator = partWithSeparator + "-";

                        if (CanAddText(textWidth + GetTextWidth(partWithSeparator, _separator, _lastOpenTag.TextProperties)))
                        {
                            str = str + part;
                            textWidth += GetTextWidth(part, _separator, _lastOpenTag.TextProperties);
                        }
                        else
                            break;
                    }

                    if (!string.IsNullOrEmpty(str))
                    {
                        string partWithSeparator = str;
                        if (!partWithSeparator.EndsWith("-"))
                            partWithSeparator = partWithSeparator + "-";
                        AppendToLine(new TextToken(token.ID, partWithSeparator)
                                         {
                                             Part = str
                                         }, textWidth);

                        string rightPart = token.Text.Remove(0, str.Length);
                        if (rightPart.StartsWith("-"))
                            rightPart = rightPart.Remove(0, 1);
                        token = new TextToken(token.ID, rightPart);
                        CreateEmptyLine(token);
                        AppendToLine(token);
                        return;
                    }
                }
                CreateLine(token);
            }
            _separator = false;
        }
Exemple #6
0
        private bool CheckLastWord(BookTokenIterator bookTokenIterator, string query, out TextToken result)
        {
            result = null;
            while (bookTokenIterator.MoveNext())
            {
                var textToken = bookTokenIterator.Current as TextToken;
                if (textToken == null)
                    continue;

                if (textToken.Text.StartsWith(query, StringComparison.InvariantCultureIgnoreCase))
                {
                    result = textToken;
                    return true;
                }
                return false;
            }
            return false;
        }