Esempio n. 1
0
        }         // DoVisitBreak

        // ----------------------------------------------------------------------
        private void WriteColor(string name, IRtfColor color)
        {
            WriteStartElement(name);
            WriteElementString("red", color.Red.ToString(CultureInfo.InvariantCulture));
            WriteElementString("green", color.Green.ToString(CultureInfo.InvariantCulture));
            WriteElementString("blue", color.Blue.ToString(CultureInfo.InvariantCulture));
            WriteEndElement();
        }         // WriteColor
        } // CopyTo

        public void Add(IRtfColor item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }
            InnerList.Add(item);
        } // Add
 // ----------------------------------------------------------------------
 public void Add( IRtfColor item )
 {
     if ( item == null )
     {
         throw new ArgumentNullException( "item" );
     }
     InnerList.Add( item );
 }
        }         // ForegroundColor

        // ----------------------------------------------------------------------
        public RtfTextFormat DeriveWithForegroundColor(IRtfColor derivedForegroundColor)
        {
            if (derivedForegroundColor == null)
            {
                throw new ArgumentNullException("derivedForegroundColor");
            }
            if (foregroundColor.Equals(derivedForegroundColor))
            {
                return(this);
            }

            RtfTextFormat copy = new RtfTextFormat(this);

            copy.foregroundColor = derivedForegroundColor;
            return(copy);
        }         // DeriveWithForegroundColor
Esempio n. 5
0
        } // DeriveWithHidden

        public RtfTextFormat DeriveWithBackgroundColor(IRtfColor derivedBackgroundColor)
        {
            if (derivedBackgroundColor == null)
            {
                throw new ArgumentNullException(nameof(derivedBackgroundColor));
            }
            if (BackgroundColor.Equals(derivedBackgroundColor))
            {
                return(this);
            }

            var copy = new RtfTextFormat(this);

            copy.BackgroundColor = derivedBackgroundColor;
            return(copy);
        } // DeriveWithBackgroundColor
 // ----------------------------------------------------------------------
 public RtfTextFormat( IRtfTextFormat copy )
 {
     if ( copy == null )
     {
         throw new ArgumentNullException( "copy" );
     }
     font = copy.Font; // enough because immutable
     fontSize = copy.FontSize;
     superScript = copy.SuperScript;
     bold = copy.IsBold;
     italic = copy.IsItalic;
     underline = copy.IsUnderline;
     strikeThrough = copy.IsStrikeThrough;
     hidden = copy.IsHidden;
     backgroundColor = copy.BackgroundColor; // enough because immutable
     foregroundColor = copy.ForegroundColor; // enough because immutable
     alignment = copy.Alignment;
 }
Esempio n. 7
0
        }         // VisitChildrenOf

        // ----------------------------------------------------------------------
        void IRtfElementVisitor.VisitTag(IRtfTag tag)
        {
            if (Context.State != RtfInterpreterState.InDocument)
            {
                if (Context.FontTable.Count > 0)
                {
                    // somewhat of a hack to detect state change from header to in-document for
                    // rtf-docs which do neither have a generator group nor encapsulate the
                    // actual document content in a group.
                    if (Context.ColorTable.Count > 0 || RtfSpec.TagViewKind.Equals(tag.Name))
                    {
                        Context.State = RtfInterpreterState.InDocument;
                    }
                }
            }

            switch (Context.State)
            {
            case RtfInterpreterState.Init:
                if (RtfSpec.TagRtf.Equals(tag.Name))
                {
                    Context.State      = RtfInterpreterState.InHeader;
                    Context.RtfVersion = tag.ValueAsNumber;
                }
                else
                {
                    throw new RtfStructureException(Strings.InvalidInitTagState(tag.ToString()));
                }
                break;

            case RtfInterpreterState.InHeader:
                switch (tag.Name)
                {
                case RtfSpec.TagDefaultFont:
                    Context.DefaultFontId = RtfSpec.TagFont + tag.ValueAsNumber;
                    break;
                }
                break;

            case RtfInterpreterState.InDocument:
                switch (tag.Name)
                {
                case RtfSpec.TagPlain:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveNormal();
                    break;

                case RtfSpec.TagParagraphDefaults:
                case RtfSpec.TagSectionDefaults:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithAlignment(RtfTextAlignment.Left);
                    break;

                case RtfSpec.TagBold:
                    bool bold = !tag.HasValue || tag.ValueAsNumber != 0;
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithBold(bold);
                    break;

                case RtfSpec.TagItalic:
                    bool italic = !tag.HasValue || tag.ValueAsNumber != 0;
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithItalic(italic);
                    break;

                case RtfSpec.TagUnderLine:
                    bool underline = !tag.HasValue || tag.ValueAsNumber != 0;
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithUnderline(underline);
                    break;

                case RtfSpec.TagUnderLineNone:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithUnderline(false);
                    break;

                case RtfSpec.TagStrikeThrough:
                    bool strikeThrough = !tag.HasValue || tag.ValueAsNumber != 0;
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithStrikeThrough(strikeThrough);
                    break;

                case RtfSpec.TagHidden:
                    bool hidden = !tag.HasValue || tag.ValueAsNumber != 0;
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithHidden(hidden);
                    break;

                case RtfSpec.TagFont:
                    string fontId = tag.FullName;
                    if (Context.FontTable.ContainsFontWithId(fontId))
                    {
                        Context.WritableCurrentTextFormat =
                            Context.WritableCurrentTextFormat.DeriveWithFont(
                                Context.FontTable[fontId]);
                    }
                    else
                    {
                        if (Settings.IgnoreUnknownFonts && Context.FontTable.Count > 0)
                        {
                            Context.WritableCurrentTextFormat =
                                Context.WritableCurrentTextFormat.DeriveWithFont(Context.FontTable[0]);
                        }
                        else
                        {
                            throw new RtfUndefinedFontException(Strings.UndefinedFont(fontId));
                        }
                    }
                    break;

                case RtfSpec.TagFontSize:
                    int fontSize = tag.ValueAsNumber;
                    if (fontSize >= 0)
                    {
                        Context.WritableCurrentTextFormat =
                            Context.WritableCurrentTextFormat.DeriveWithFontSize(fontSize);
                    }
                    else
                    {
                        throw new RtfInvalidDataException(Strings.InvalidFontSize(fontSize));
                    }
                    break;

                case RtfSpec.TagFontSubscript:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithSuperScript(false);
                    break;

                case RtfSpec.TagFontSuperscript:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithSuperScript(true);
                    break;

                case RtfSpec.TagFontNoSuperSub:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithSuperScript(0);
                    break;

                case RtfSpec.TagFontDown:
                    int moveDown = tag.ValueAsNumber;
                    if (moveDown == 0)
                    {
                        moveDown = 6;                                         // the default value according to rtf spec
                    }
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithSuperScript(-moveDown);
                    break;

                case RtfSpec.TagFontUp:
                    int moveUp = tag.ValueAsNumber;
                    if (moveUp == 0)
                    {
                        moveUp = 6;                                         // the default value according to rtf spec
                    }
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithSuperScript(moveUp);
                    break;

                case RtfSpec.TagAlignLeft:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithAlignment(RtfTextAlignment.Left);
                    break;

                case RtfSpec.TagAlignCenter:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithAlignment(RtfTextAlignment.Center);
                    break;

                case RtfSpec.TagAlignRight:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithAlignment(RtfTextAlignment.Right);
                    break;

                case RtfSpec.TagAlignJustify:
                    Context.WritableCurrentTextFormat =
                        Context.WritableCurrentTextFormat.DeriveWithAlignment(RtfTextAlignment.Justify);
                    break;

                case RtfSpec.TagColorBackground:
                case RtfSpec.TagColorBackgroundWord:
                case RtfSpec.TagColorHighlight:
                case RtfSpec.TagColorForeground:
                    int colorIndex = tag.ValueAsNumber;
                    if (colorIndex >= 0 && colorIndex < Context.ColorTable.Count)
                    {
                        IRtfColor newColor     = Context.ColorTable[colorIndex];
                        bool      isForeground = RtfSpec.TagColorForeground.Equals(tag.Name);
                        Context.WritableCurrentTextFormat = isForeground ?
                                                            Context.WritableCurrentTextFormat.DeriveWithForegroundColor(newColor) :
                                                            Context.WritableCurrentTextFormat.DeriveWithBackgroundColor(newColor);
                    }
                    else
                    {
                        throw new RtfUndefinedColorException(Strings.UndefinedColor(colorIndex));
                    }
                    break;

                case RtfSpec.TagSection:
                    NotifyInsertBreak(RtfVisualBreakKind.Section);
                    break;

                case RtfSpec.TagParagraph:
                    NotifyInsertBreak(RtfVisualBreakKind.Paragraph);
                    break;

                case RtfSpec.TagLine:
                    NotifyInsertBreak(RtfVisualBreakKind.Line);
                    break;

                case RtfSpec.TagPage:
                    NotifyInsertBreak(RtfVisualBreakKind.Page);
                    break;

                case RtfSpec.TagTabulator:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.Tabulator);
                    break;

                case RtfSpec.TagTilde:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.NonBreakingSpace);
                    break;

                case RtfSpec.TagEmDash:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.EmDash);
                    break;

                case RtfSpec.TagEnDash:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.EnDash);
                    break;

                case RtfSpec.TagEmSpace:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.EmSpace);
                    break;

                case RtfSpec.TagEnSpace:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.EnSpace);
                    break;

                case RtfSpec.TagQmSpace:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.QmSpace);
                    break;

                case RtfSpec.TagBulltet:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.Bullet);
                    break;

                case RtfSpec.TagLeftSingleQuote:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.LeftSingleQuote);
                    break;

                case RtfSpec.TagRightSingleQuote:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.RightSingleQuote);
                    break;

                case RtfSpec.TagLeftDoubleQuote:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.LeftDoubleQuote);
                    break;

                case RtfSpec.TagRightDoubleQuote:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.RightDoubleQuote);
                    break;

                case RtfSpec.TagHyphen:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.OptionalHyphen);
                    break;

                case RtfSpec.TagUnderscore:
                    NotifyInsertSpecialChar(RtfVisualSpecialCharKind.NonBreakingHyphen);
                    break;
                }
                break;
            }
        }         // IRtfElementVisitor.VisitTag
 // ----------------------------------------------------------------------
 private void WriteColor( string name, IRtfColor color )
 {
     WriteStartElement( name );
     WriteElementString( "red", color.Red.ToString() );
     WriteElementString( "green", color.Green.ToString() );
     WriteElementString( "blue", color.Blue.ToString() );
     WriteEndElement();
 }
        // ----------------------------------------------------------------------
        public RtfTextFormat DeriveWithForegroundColor( IRtfColor derivedForegroundColor )
        {
            if ( derivedForegroundColor == null )
            {
                throw new ArgumentNullException( "derivedForegroundColor" );
            }
            if ( foregroundColor.Equals( derivedForegroundColor ) )
            {
                return this;
            }

            RtfTextFormat copy = new RtfTextFormat( this );
            copy.foregroundColor = derivedForegroundColor;
            return copy;
        }
Esempio n. 10
0
        } // TextToHtml

        private string ToHtmlColor(IRtfColor color)
        {
            return(string.Format("#{0:x2}{1:x2}{2:x2}", color.Red, color.Green, color.Blue));
        }
Esempio n. 11
0
 // ----------------------------------------------------------------------
 public void CopyTo( IRtfColor[] array, int index )
 {
     InnerList.CopyTo( array, index );
 }