Beispiel #1
0
        public CellToken(IEnumerable <IToken> tokens, CellToken previous)
        {
            foreach (var token in tokens)
            {
                if (token is CellWidth width)
                {
                    Width = width.Value;
                }
                else if (token is CellWidthType widthType)
                {
                    WidthUnit = widthType.Value;
                }
                else if (token is RightCellBoundary rightBoundary)
                {
                    RightBoundary = rightBoundary.Value;
                }
                else
                {
                    _styles.Add(token);
                }
            }

            if (WidthUnit == CellWidthUnit.Null)
            {
                WidthUnit = CellWidthUnit.Twip;
                if (previous == null)
                {
                    Width = RightBoundary.ToTwip();
                }
                else
                {
                    Width = (RightBoundary - previous.RightBoundary).ToTwip();
                }
            }
            Index = previous == null ? 0 : previous.Index + 1;
        }
Beispiel #2
0
        public StyleList Merge(IToken token)
        {
            if (token is SectionDefault)
            {
                this.RemoveWhere(t => t.Type == TokenType.SectionFormat || t.Type == TokenType.ParagraphFormat || t.Type == TokenType.RowFormat || t.Type == TokenType.CellFormat);
            }
            else if (token is ParagraphDefault)
            {
                this.RemoveWhere(t => t.Type == TokenType.ParagraphFormat);
            }
            else if (token is RowDefaults)
            {
                this.RemoveWhere(t => t.Type == TokenType.RowFormat || t.Type == TokenType.CellFormat);
            }
            else if (token is CellDefaults)
            {
                this.RemoveWhere(t => t.Type == TokenType.CellFormat);
            }
            else if (token is PlainStyle)
            {
                this.RemoveWhere(t => t.Type == TokenType.CharacterFormat);
            }
            else if (IsUnderline(token))
            {
                this.RemoveWhere(IsUnderline);
                if (!(token is ControlWord <bool> ulBool) || ulBool.Value)
                {
                    Add(token);
                }
            }
            else if (token is ControlWord <bool> boolean && !boolean.Value)
            {
                this.RemoveWhere(t => t is ControlWord <bool> boolStyle && boolStyle.Name == boolean.Name);
            }
            else if (token is SuperSubEnd)
            {
                this.RemoveWhere(t => t is SuperscriptStart || t is SubscriptStart);
            }
            else if (token is ControlWord <BorderPosition> borderPosition)
            {
                Add(new BorderToken(borderPosition));
            }
            else if (token is RightCellBoundary)
            {
                var i = Count - 1;
                while (i >= 0 && this[i].Type == TokenType.CellFormat && !(this[i] is CellToken))
                {
                    i--;
                }
                var previous = i >= 0 && this[i] is CellToken prevCell ? prevCell : null;
                i++;
                var cellToken = new CellToken(this.Skip(i).Concat(new[] { token }), previous);
                while (i < Count)
                {
                    RemoveAt(i);
                }

                Add(cellToken);
            }
            else if (Count > 0 && this[Count - 1] is BorderToken borderToken && borderToken.Add(token))
            {
                // Do nothing
            }