// ---------------------------------------------------------------------- public virtual IRtfHtmlStyle TextToHtml(IRtfVisualText visualText) { if (visualText == null) { throw new ArgumentNullException("visualText"); } RtfHtmlStyle htmlStyle = new RtfHtmlStyle(); IRtfTextFormat textFormat = visualText.Format; // background color var color = textFormat.BackgroundColor; if (color.Red != 255 || color.Green != 255 || color.Blue != 255) { htmlStyle.BackgroundColor = ToHtmlColor(color); } // foreground color color = textFormat.ForegroundColor; if (color.Red != 0 || color.Green != 0 || color.Blue != 0) { htmlStyle.ForegroundColor = ToHtmlColor(color); } // font htmlStyle.FontFamily = textFormat.Font.Name; if (textFormat.FontSize > 0) { htmlStyle.FontSize = (textFormat.FontSize / 2) + "pt"; } return(htmlStyle); } // TextToHtml
} // DoEndDocument // ---------------------------------------------------------------------- private void EndParagraph(IRtfInterpreterContext context) { RtfTextAlignment finalParagraphAlignment = context.GetSafeCurrentTextFormat().Alignment; foreach (IRtfVisual alignedVisual in pendingParagraphContent) { switch (alignedVisual.Kind) { case RtfVisualKind.Image: RtfVisualImage image = (RtfVisualImage)alignedVisual; // ReSharper disable RedundantCheckBeforeAssignment if (image.Alignment != finalParagraphAlignment) // ReSharper restore RedundantCheckBeforeAssignment { image.Alignment = finalParagraphAlignment; } break; case RtfVisualKind.Text: RtfVisualText text = (RtfVisualText)alignedVisual; if (text.Format.Alignment != finalParagraphAlignment) { IRtfTextFormat correctedFormat = ((RtfTextFormat)text.Format).DeriveWithAlignment(finalParagraphAlignment); IRtfTextFormat correctedUniqueFormat = context.GetUniqueTextFormatInstance(correctedFormat); text.Format = correctedUniqueFormat; } break; } } pendingParagraphContent.Clear(); } // EndParagraph
// ---------------------------------------------------------------------- public void Add( IRtfTextFormat item ) { if ( item == null ) { throw new ArgumentNullException( "item" ); } InnerList.Add( item ); }
} // CopyTo // ---------------------------------------------------------------------- public void Add(IRtfTextFormat item) { if (item == null) { throw new ArgumentNullException("item"); } InnerList.Add(item); } // Add
} // EndParagraph private void FlushPendingText() { if (_pendingTextFormat != null) { AppendAlignedVisual(new RtfVisualText(_pendingText.ToString(), _pendingTextFormat)); _pendingTextFormat = null; _pendingText.Remove(0, _pendingText.Length); } } // FlushPendingText
public RtfVisualText(string text, IRtfTextFormat format) : base(RtfVisualKind.Text) { if (text == null) { throw new ArgumentNullException(nameof(text)); } if (format == null) { throw new ArgumentNullException(nameof(format)); } Text = text; _format = format; } // RtfVisualText
// ---------------------------------------------------------------------- public RtfVisualText(string text, IRtfTextFormat format) : base(RtfVisualKind.Text) { if (text == null) { throw new ArgumentNullException("text"); } if (format == null) { throw new ArgumentNullException("format"); } this.text = text; this.format = format; } // RtfVisualText
// ---------------------------------------------------------------------- public RtfVisualText( string text, IRtfTextFormat format ) : base(RtfVisualKind.Text) { if ( text == null ) { throw new ArgumentNullException( "text" ); } if ( format == null ) { throw new ArgumentNullException( "format" ); } this.text = text; this.format = format; }
} // Contains // ---------------------------------------------------------------------- public int IndexOf(IRtfTextFormat format) { if (format != null) { // PERFORMANCE: most probably we should maintain a hashmap for fast searching ... int count = Count; for (int i = 0; i < count; i++) { if (format.Equals(InnerList[i])) { return(i); } } } return(-1); } // IndexOf
// ---------------------------------------------------------------------- public int IndexOf( IRtfTextFormat format ) { if ( format != null ) { // PERFORMANCE: most probably we should maintain a hashmap for fast searching ... int count = Count; for ( int i = 0; i < count; i++ ) { if ( format.Equals( InnerList[ i ] ) ) { return i; } } } return -1; }
} // DoBeginDocument // ---------------------------------------------------------------------- protected override void DoInsertText(IRtfInterpreterContext context, string text) { if (combineTextWithSameFormat) { IRtfTextFormat newFormat = context.GetSafeCurrentTextFormat(); if (!newFormat.Equals(pendingTextFormat)) { FlushPendingText(); } pendingTextFormat = newFormat; pendingText.Append(text); } else { AppendAlignedVisual(new RtfVisualText(text, context.GetSafeCurrentTextFormat())); } } // DoInsertText
} // DoBeginDocument // ---------------------------------------------------------------------- protected override void DoInsertText(IRtfInterpreterContext context, string text) { if (this.combineTextWithSameFormat) { IRtfTextFormat newFormat = context.GetSafeCurrentTextFormat(); if (!newFormat.Equals(this.pendingTextFormat)) { FlushPendingText(); } this.pendingTextFormat = newFormat; this.pendingText.Append(text); } else { this.visualContent.Add(new RtfVisualText(text, context.GetSafeCurrentTextFormat())); } } // DoInsertText
// ---------------------------------------------------------------------- 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; }
} // RtfTextFormat // ---------------------------------------------------------------------- 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; } // RtfTextFormat
} // RtfTextFormat public RtfTextFormat(IRtfTextFormat copy) { if (copy == null) { throw new ArgumentNullException(nameof(copy)); } Font = copy.Font; // enough because immutable FontSize = copy.FontSize; SuperScript = copy.SuperScript; IsBold = copy.IsBold; IsItalic = copy.IsItalic; IsUnderline = copy.IsUnderline; IsStrikeThrough = copy.IsStrikeThrough; IsHidden = copy.IsHidden; BackgroundColor = copy.BackgroundColor; // enough because immutable ForegroundColor = copy.ForegroundColor; // enough because immutable Alignment = copy.Alignment; } // RtfTextFormat
} // GetSafeCurrentTextFormat // ---------------------------------------------------------------------- public IRtfTextFormat GetUniqueTextFormatInstance(IRtfTextFormat templateFormat) { if (templateFormat == null) { throw new ArgumentNullException("templateFormat"); } IRtfTextFormat uniqueInstance; int existingEquivalentPos = uniqueTextFormats.IndexOf(templateFormat); if (existingEquivalentPos >= 0) { // we already know an equivalent format -> reference that one for future use uniqueInstance = uniqueTextFormats[existingEquivalentPos]; } else { // this is a yet unknown format -> add it to the known formats and use it uniqueTextFormats.Add(templateFormat); uniqueInstance = templateFormat; } return(uniqueInstance); } // GetUniqueTextFormatInstance
// ---------------------------------------------------------------------- public virtual IRtfHtmlStyle TextToHtml(IRtfVisualText visualText) { if (visualText == null) { throw new ArgumentNullException("visualText"); } RtfHtmlStyle htmlStyle = new RtfHtmlStyle(); IRtfTextFormat textFormat = visualText.Format; // background color Color backgroundColor = textFormat.BackgroundColor.AsDrawingColor; if (backgroundColor.R != 0 || backgroundColor.G != 0 || backgroundColor.B != 0) { htmlStyle.BackgroundColor = ColorTranslator.ToHtml(backgroundColor); } // foreground color Color foregroundColor = textFormat.ForegroundColor.AsDrawingColor; if (foregroundColor.R != 0 || foregroundColor.G != 0 || foregroundColor.B != 0) { htmlStyle.ForegroundColor = ColorTranslator.ToHtml(foregroundColor); } // font htmlStyle.FontFamily = textFormat.Font.Name; if (textFormat.FontSize > 0) { htmlStyle.FontSize = (textFormat.FontSize / 2) + "pt"; } return(htmlStyle); } // TextToHtml
// ---------------------------------------------------------------------- public IRtfTextFormat GetUniqueTextFormatInstance( IRtfTextFormat templateFormat ) { if ( templateFormat == null ) { throw new ArgumentNullException( "templateFormat" ); } IRtfTextFormat uniqueInstance; int existingEquivalentPos = uniqueTextFormats.IndexOf( templateFormat ); if ( existingEquivalentPos >= 0 ) { // we already know an equivalent format -> reference that one for future use uniqueInstance = uniqueTextFormats[ existingEquivalentPos ]; } else { // this is a yet unknown format -> add it to the known formats and use it uniqueTextFormats.Add( templateFormat ); uniqueInstance = templateFormat; } return uniqueInstance; }
// ---------------------------------------------------------------------- private void FlushPendingText() { if ( pendingTextFormat != null ) { AppendAlignedVisual( new RtfVisualText( pendingText.ToString(), pendingTextFormat ) ); pendingTextFormat = null; pendingText.Remove( 0, pendingText.Length ); } }
// ---------------------------------------------------------------------- protected override void DoInsertText( IRtfInterpreterContext context, string text ) { if ( combineTextWithSameFormat ) { IRtfTextFormat newFormat = context.GetSafeCurrentTextFormat(); if ( !newFormat.Equals( pendingTextFormat ) ) { FlushPendingText(); } pendingTextFormat = newFormat; pendingText.Append( text ); } else { AppendAlignedVisual( new RtfVisualText( text, context.GetSafeCurrentTextFormat() ) ); } }
// ---------------------------------------------------------------------- public RtfDocument( int rtfVersion, IRtfFont defaultFont, IRtfFontCollection fontTable, IRtfColorCollection colorTable, string generator, IRtfTextFormatCollection uniqueTextFormats, IRtfDocumentInfo documentInfo, IRtfDocumentPropertyCollection userProperties, IRtfVisualCollection visualContent ) { if ( rtfVersion != RtfSpec.RtfVersion1 ) { throw new RtfUnsupportedStructureException( Strings.UnsupportedRtfVersion( rtfVersion ) ); } if ( defaultFont == null ) { throw new ArgumentNullException( "defaultFont" ); } if ( fontTable == null ) { throw new ArgumentNullException( "fontTable" ); } if ( colorTable == null ) { throw new ArgumentNullException( "colorTable" ); } if ( uniqueTextFormats == null ) { throw new ArgumentNullException( "uniqueTextFormats" ); } if ( documentInfo == null ) { throw new ArgumentNullException( "documentInfo" ); } if ( userProperties == null ) { throw new ArgumentNullException( "userProperties" ); } if ( visualContent == null ) { throw new ArgumentNullException( "visualContent" ); } this.rtfVersion = rtfVersion; this.defaultFont = defaultFont; defaultTextFormat = new RtfTextFormat( defaultFont, RtfSpec.DefaultFontSize ); this.fontTable = fontTable; this.colorTable = colorTable; this.generator = generator; this.uniqueTextFormats = uniqueTextFormats; this.documentInfo = documentInfo; this.userProperties = userProperties; this.visualContent = visualContent; }
} // FormatHtmlText #endregion // HtmlFormat #region RtfVisuals // ---------------------------------------------------------------------- protected override void DoVisitText(IRtfVisualText visualText) { if (!EnterVisual(visualText)) { return; } // suppress hidden text if (visualText.Format.IsHidden && settings.IsShowHiddenText == false) { return; } IRtfTextFormat textFormat = visualText.Format; switch (textFormat.Alignment) { case RtfTextAlignment.Left: //Writer.AddStyleAttribute( HtmlTextWriterStyle.TextAlign, "left" ); break; case RtfTextAlignment.Center: Writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center"); break; case RtfTextAlignment.Right: Writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "right"); break; case RtfTextAlignment.Justify: Writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "justify"); break; } if (!IsInListItem) { BeginParagraph(); } // format tags if (textFormat.IsBold) { RenderBTag(); } if (textFormat.IsItalic) { RenderITag(); } if (textFormat.IsUnderline) { RenderUTag(); } if (textFormat.IsStrikeThrough) { RenderSTag(); } // span with style IRtfHtmlStyle htmlStyle = GetHtmlStyle(visualText); if (!htmlStyle.IsEmpty) { if (!string.IsNullOrEmpty(htmlStyle.ForegroundColor)) { Writer.AddStyleAttribute(HtmlTextWriterStyle.Color, htmlStyle.ForegroundColor); } if (!string.IsNullOrEmpty(htmlStyle.BackgroundColor)) { Writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, htmlStyle.BackgroundColor); } if (!string.IsNullOrEmpty(htmlStyle.FontFamily)) { Writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, htmlStyle.FontFamily); } if (!string.IsNullOrEmpty(htmlStyle.FontSize)) { Writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, htmlStyle.FontSize); } RenderSpanTag(); } // visual hyperlink bool isHyperlink = false; if (settings.ConvertVisualHyperlinks) { string href = ConvertVisualHyperlink(visualText.Text); if (!string.IsNullOrEmpty(href)) { isHyperlink = true; Writer.AddAttribute(HtmlTextWriterAttribute.Href, href); RenderATag(); } } // subscript and superscript if (textFormat.SuperScript < 0) { RenderSubTag(); } else if (textFormat.SuperScript > 0) { RenderSupTag(); } string htmlText = FormatHtmlText(visualText.Text); Writer.Write(htmlText); // subscript and superscript if (textFormat.SuperScript < 0) { RenderEndTag(); // sub } else if (textFormat.SuperScript > 0) { RenderEndTag(); // sup } // visual hyperlink if (isHyperlink) { RenderEndTag(); // a } // span with style if (!htmlStyle.IsEmpty) { RenderEndTag(); } // format tags if (textFormat.IsStrikeThrough) { RenderEndTag(); // s } if (textFormat.IsUnderline) { RenderEndTag(); // u } if (textFormat.IsItalic) { RenderEndTag(); // i } if (textFormat.IsBold) { RenderEndTag(); // b } LeaveVisual(visualText); } // DoVisitText
// ---------------------------------------------------------------------- public RtfVisualSpecialChar(RtfVisualSpecialCharKind charKind, IRtfTextFormat format) : base(RtfVisualKind.Special) { this.charKind = charKind; this.format = format; } // RtfVisualSpecialChar
} // this[ int ] // ---------------------------------------------------------------------- public bool Contains(IRtfTextFormat format) { return(IndexOf(format) >= 0); } // Contains
// ---------------------------------------------------------------------- public bool Contains( IRtfTextFormat format ) { return IndexOf( format ) >= 0; }
} // GetHtmlStyle #endregion // HtmlFormat #region RtfVisuals // ---------------------------------------------------------------------- protected override void DoVisitText(IRtfVisualText visualText) { if (!EnterVisual(visualText)) { return; } // suppress hidden text if (visualText.Format.IsHidden && _settings.IsShowHiddenText == false) { return; } IRtfTextFormat textFormat = visualText.Format; if (!IsInListItem && BeginParagraph()) { switch (textFormat.Alignment) { case RtfTextAlignment.Left: //Writer.AddStyleAttribute( HtmlTextWriterStyle.TextAlign, "left" ); break; case RtfTextAlignment.Center: Writer.WriteAttributeString("style", "text-align:center"); break; case RtfTextAlignment.Right: Writer.WriteAttributeString("style", "text-align:right"); break; case RtfTextAlignment.Justify: Writer.WriteAttributeString("style", "text-align:justify"); break; } } // format tags if (textFormat.IsBold) { RenderBTag(); } if (textFormat.IsItalic) { RenderITag(); } if (textFormat.IsUnderline) { RenderUTag(); } if (textFormat.IsStrikeThrough) { RenderSTag(); } // span with style IRtfHtmlStyle htmlStyle = GetHtmlStyle(visualText); if (!htmlStyle.IsEmpty) { RenderSpanTag(); var styles = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(htmlStyle.ForegroundColor)) { styles["color"] = htmlStyle.ForegroundColor; } if (!string.IsNullOrEmpty(htmlStyle.BackgroundColor)) { styles["background-color"] = htmlStyle.BackgroundColor; } if (!string.IsNullOrEmpty(htmlStyle.FontFamily)) { styles["font-family"] = htmlStyle.FontFamily; } if (!string.IsNullOrEmpty(htmlStyle.FontSize)) { styles["font-size"] = htmlStyle.FontSize; } if (styles.Count > 0) { _writer.WriteStartAttribute("style"); var first = true; foreach (var kvp in styles) { if (!first) { _writer.WriteString(";"); } _writer.WriteString(kvp.Key); _writer.WriteString(":"); _writer.WriteString(kvp.Value); first = false; } _writer.WriteEndAttribute(); } } // visual hyperlink bool isHyperlink = false; if (_settings.ConvertVisualHyperlinks) { string href = ConvertVisualHyperlink(visualText.Text); if (!string.IsNullOrEmpty(href)) { isHyperlink = true; RenderATag(); Writer.WriteAttributeString("href", href); } } // subscript and superscript if (textFormat.SuperScript < 0) { RenderSubTag(); } else if (textFormat.SuperScript > 0) { RenderSupTag(); } Writer.WriteString(visualText.Text); // subscript and superscript if (textFormat.SuperScript < 0) { RenderEndTag(); // sub } else if (textFormat.SuperScript > 0) { RenderEndTag(); // sup } // visual hyperlink if (isHyperlink) { RenderEndTag(); // a } // span with style if (!htmlStyle.IsEmpty) { RenderEndTag(); } // format tags if (textFormat.IsStrikeThrough) { RenderEndTag(); // s } if (textFormat.IsUnderline) { RenderEndTag(); // u } if (textFormat.IsItalic) { RenderEndTag(); // i } if (textFormat.IsBold) { RenderEndTag(); // b } LeaveVisual(visualText); } // DoVisitText
// ---------------------------------------------------------------------- public void CopyTo( IRtfTextFormat[] array, int index ) { InnerList.CopyTo( array, index ); }