Ejemplo n.º 1
0
        public void RtfStyle_ToString_RightToLeft()
        {
            CheckDisposed();

            // Build up a font table and style table for testing
            RtfStyleInfoTable styleTable = new RtfStyleInfoTable(null, true);

            BuildStyleTable(styleTable);

            RtfStyle style     = (RtfStyle)styleTable["Normal"];
            string   rtfString = style.ToString("Normal", true);

            Assert.AreEqual(@"\s1\rtlpar\qr\f1\fs20\snext1 Normal", rtfString);

            style     = (RtfStyle)styleTable["Paragraph"];
            rtfString = style.ToString("Paragraph", true);
            Assert.AreEqual(@"\s2\rtlpar\qr\fi360\lin180\rin90\sb1440\sa720\f1\fs20\sbasedon1\snext2 Paragraph", rtfString);

            style     = (RtfStyle)styleTable["Heading"];
            rtfString = style.ToString("Heading", true);
            Assert.AreEqual(@"\s3\rtlpar\qr\f1\fs20\sbasedon1\snext2 Heading", rtfString);

            style     = (RtfStyle)styleTable["Emphasis"];
            rtfString = style.ToString("Emphasis", true);
            Assert.AreEqual(@"\*\cs4\b\f1\fs40\additive Emphasis", rtfString);
        }
Ejemplo n.º 2
0
        public void RtfStyle_ToString_LeftToRight()
        {
            CheckDisposed();

            // Build up a style table for testing
            RtfStyleInfoTable styleTable = new RtfStyleInfoTable(null, false);

            BuildStyleTable(styleTable);

            // Create a "Paragraph" style that is based on Normal (#2)
            DummyRtfStyle paragraph = new DummyRtfStyle("\\MyFavoriteStyle", StyleType.kstParagraph,
                                                        72000,    // space before
                                                        36000,    // space after
                                                        18000,    // first line indent
                                                        9000,     // leading indent
                                                        4500,     // trailing indent
                                                        "Normal", // based-on style name
                                                        null);    // next style name

            styleTable.Add("\\MyFavoriteStyle", paragraph);
            styleTable.ConnectStyles();

            RtfStyle style     = (RtfStyle)styleTable["Normal"];
            string   rtfString = style.ToString("Normal", true);

            Assert.AreEqual(@"\s1\f1\fs20\snext1 Normal", rtfString);

            style     = (RtfStyle)styleTable["Paragraph"];
            rtfString = style.ToString("Paragraph", true);
            Assert.AreEqual(@"\s2\fi360\lin180\rin90\sb1440\sa720\f1\fs20\sbasedon1\snext2 Paragraph", rtfString);

            style     = (RtfStyle)styleTable["Heading"];
            rtfString = style.ToString("Heading", true);
            Assert.AreEqual(@"\s3\f1\fs20\sbasedon1\snext2 Heading", rtfString);

            style     = (RtfStyle)styleTable["Emphasis"];
            rtfString = style.ToString("Emphasis", true);
            Assert.AreEqual(@"\*\cs4\b\f1\fs40\additive Emphasis", rtfString);

            style     = (RtfStyle)styleTable["ExactSpacing"];
            rtfString = style.ToString("ExactSpacing", true);
            Assert.AreEqual(@"\s5\sl-360\slmult0\f1\fs20\sbasedon1\snext2 ExactSpacing", rtfString);

            style     = (RtfStyle)styleTable["AtLeastSpacing"];
            rtfString = style.ToString("AtLeastSpacing", true);
            Assert.AreEqual(@"\s6\sl360\f1\fs20\sbasedon1\snext2 AtLeastSpacing", rtfString);

            style     = (RtfStyle)styleTable["Border"];
            rtfString = style.ToString("Border", true);
            Assert.AreEqual(@"\s7\qj\f2\fs40" +
                            @"\brdrt\brdrs\brdrw10\brsp20\brdrcf1" +
                            @"\brdrb\brdrs\brdrw20\brsp20\brdrcf1" +
                            @"\brdrl\brdrs\brdrw30\brsp80\brdrcf1" +
                            @"\brdrr\brdrs\brdrw40\brsp80\brdrcf1\nosupersub\snext7" +
                            " Border", rtfString);

            style     = (RtfStyle)styleTable["\\MyFavoriteStyle"];
            rtfString = style.ToString("\\MyFavoriteStyle", true);
            Assert.AreEqual(@"\s8\fi360\lin180\rin90\sb1440\sa720\f1\fs20\sbasedon1\snext8 \\MyFavoriteStyle", rtfString);
        }
 public bool IsEqual(RtfStyle checkedObject)
 {
     return ( //TODO - fix all below
                (ItsAlignment == checkedObject.ItsAlignment) &&
                (ItsBackColor == checkedObject.ItsBackColor) &&
                (ItsBullet == checkedObject.ItsBullet) &&
                (ItsColor == checkedObject.ItsColor) &&
                //(ItsFont == checkedObject.ItsFont) &&
                (ItsFont.Bold == checkedObject.ItsFont.Bold) &&
                (ItsFont.FontFamily.Name == checkedObject.ItsFont.FontFamily.Name) &&
                (ItsFont.Italic == checkedObject.ItsFont.Italic) &&
                (ItsFont.Size == checkedObject.ItsFont.Size) &&
                (ItsFont.Underline == checkedObject.ItsFont.Underline) &&
                //(ItsFontColors == checkedObject.ItsFontColors) &&
                (ItsIndent == checkedObject.ItsIndent) 
            );
 }
Ejemplo n.º 4
0
        static void AddStyleData(AssembledStyles styles, ref int id, Dictionary <string, int> fonts,
                                 Dictionary <Color, int> colors, Dictionary <Tuple <string, int>, StyleData> styleTable)
        {
            var ss = styles.Stylesheet;

            if (ss == null)
            {
                return;                 // Can't do anything useful with styles.
            }
            var styleName = styles.StyleName;

            if (string.IsNullOrEmpty(styleName))
            {
                return;                 // no style info.
            }
            var ws  = styles.Ws;
            var key = new Tuple <string, int>(styleName, ws);

            if (styleTable.ContainsKey(key))
            {
                return;                 // already know all we need.
            }
            var data = new StyleData {
                Name = styleName, Id = id++
            };

            styleTable[key] = data;
            var style = ss.Style(styleName);

            if (style == null)
            {
                data.StyleTableFormatting = "";                 // Todo: some minimal definition with at least the name
                data.DataFormatting       = "";
            }
            // Todo: if style has any overrides for ws, make a distinct style name.
            var rtfStyle = new RtfStyle(style, ws)
            {
                StyleNumber = data.Id, Fonts = fonts, Colors = colors
            };

            data.StyleTableFormatting = rtfStyle.ToString(styleName, true);
            data.DataFormatting       = rtfStyle.ToString(styleName, false);
        }
Ejemplo n.º 5
0
        private string RtfData(Dictionary <string, int> fonts, Dictionary <Color, int> colors, Dictionary <Tuple <string, int>, StyleData> styles)
        {
            var builder       = new StringBuilder();
            var firstBox      = Start.Para;
            var firstRunIndex = firstBox.Source.RunContaining(Start.RenderParaPosition);
            var firstRun      = firstBox.Source.Runs[firstRunIndex];
            var lastBox       = End.Para;
            int endPosition   = End.RenderParaPosition;

            if (endPosition > 0)
            {
                endPosition--;                 // Refers to last character actually IN the selection.
            }
            var lastRunIndex = lastBox.Source.RunContaining(endPosition);
            var lastRun      = lastBox.Source.Runs[lastRunIndex];
            var style        = firstBox.Style;
            var styleName    = style.StyleName;

            if (!string.IsNullOrEmpty(styleName))
            {
                builder.Append("{");
                var key = new Tuple <string, int>(styleName, style.Ws);
                builder.Append(styles[key].DataFormatting);
                builder.Append(" ");
            }

            RunsDo(aRun =>
            {
                builder.Append("{");
                style     = aRun.Styles;
                styleName = style.StyleName;
                if (!string.IsNullOrEmpty(styleName))
                {
                    var key = new Tuple <string, int>(styleName, style.Ws);
                    builder.Append(styles[key].DataFormatting);
                    builder.Append(" ");
                }
                var renderText = aRun.RenderText;
                if (aRun == lastRun)
                {
                    // truncate the bit beyond the selection
                    renderText = renderText.Substring(0, aRun.RenderLength - (aRun.RenderLim - End.RenderParaPosition));
                }
                if (aRun == firstRun)
                {
                    // truncate the bit before the selection
                    renderText = renderText.Substring(Start.RenderParaPosition - aRun.RenderStart);
                }
                builder.Append(RtfStyle.ConvertString(renderText));
                builder.Append("}");
            }, box =>
            {
                style     = box.Style;
                styleName = style.StyleName;

                builder.Remove(builder.Length - 1, 1);
                builder.Append("\\par}");

                if (!string.IsNullOrEmpty(styleName))
                {
                    builder.Append("{");
                    var key = new Tuple <string, int>(styleName, style.Ws);
                    builder.Append(styles[key].DataFormatting);
                    builder.Append(" ");
                }
            }
                   );
            builder.Remove(builder.Length - 1, 1);
            builder.Append("\\par}");
            return(builder.ToString());
        }
 private void InsertAllChangedStylesIntoCollection(RtfStyle currentRtfCharAttributes, RtfStyle previousRtfCharAttributes, int currentIndex)
 {
     if (currentRtfCharAttributes.ItsAlignment != previousRtfCharAttributes.ItsAlignment)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.TextAlign,
             currentIndex,
             currentRtfCharAttributes.ItsAlignment);
     }
     if (currentRtfCharAttributes.ItsBackColor != previousRtfCharAttributes.ItsBackColor)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.BackgroundColor,
             currentIndex,
             currentRtfCharAttributes.ItsAlignment);
     }
     if (currentRtfCharAttributes.ItsBullet != previousRtfCharAttributes.ItsBullet)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumHtmlTags.UnorderedList,
             currentIndex,
             currentRtfCharAttributes.ItsBullet);
     }
     if (currentRtfCharAttributes.ItsColor != previousRtfCharAttributes.ItsColor)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.FontColor,
             currentIndex,
             currentRtfCharAttributes.ItsColor);
     }
     if (currentRtfCharAttributes.ItsIndent != previousRtfCharAttributes.ItsIndent)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.Position,
             currentIndex,
             currentRtfCharAttributes.ItsIndent);
     }
     if (currentRtfCharAttributes.ItsFont.Bold != previousRtfCharAttributes.ItsFont.Bold)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.FontWeight,
             currentIndex,
             currentRtfCharAttributes.ItsFont.Bold);
     }
     if (currentRtfCharAttributes.ItsFont.Italic != previousRtfCharAttributes.ItsFont.Italic)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.FontStyle,
             currentIndex,
             currentRtfCharAttributes.ItsFont.Italic);
     }
     if (currentRtfCharAttributes.ItsFont.Underline != previousRtfCharAttributes.ItsFont.Underline)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.TextDecoration,
             currentIndex,
             currentRtfCharAttributes.ItsFont.Underline);
     }
     if (currentRtfCharAttributes.ItsFont.Size != previousRtfCharAttributes.ItsFont.Size)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.FontSize,
             currentIndex,
             currentRtfCharAttributes.ItsFont.Size);
     }
     if (currentRtfCharAttributes.ItsFont.FontFamily != previousRtfCharAttributes.ItsFont.FontFamily)
     {
         InsertSpanTagStyledToCollection(
             Constants.EnumCssProperties.FontFamily,
             currentIndex,
             currentRtfCharAttributes.ItsFont.FontFamily);
     }
 }
        public string ConvertRtfTextToHtmlText()
        {
            string content = RichTextBox1.Text;
            int currentIndex = 0;
            var currentRtfCharAttributes = new RtfStyle();
            var previousRtfCharAttributes = new RtfStyle();
            
            RetrieveCharAttributesByIndex(currentIndex, ref previousRtfCharAttributes, ref currentRtfCharAttributes);

            string returnValue = content;

            while (++currentIndex < content.Length)
            {
                RetrieveCharAttributesByIndex(currentIndex, ref previousRtfCharAttributes, ref currentRtfCharAttributes);

                bool styleChanged = (!currentRtfCharAttributes.IsEqual(previousRtfCharAttributes));
                
                if (styleChanged)
                {
                    InsertAllChangedStylesIntoCollection(currentRtfCharAttributes, previousRtfCharAttributes, currentIndex);
                }
            }

            int length = HtmlTagsByLocations["tag_start"].Keys.Count;
            
            while (length > 0)
            {
                returnValue = InsertTagToHtmlTextAndUpdateKey(returnValue);
                length = HtmlTagsByLocations["tag_start"].Keys.Count + HtmlTagsByLocations["tag_end"].Keys.Count;
            }

            returnValue = GeneratBaseHtmlString(returnValue);
            return returnValue;
        }
        private void RetrieveCharAttributesByIndex(int currentIndex,
            ref RtfStyle previousRtfCharStyle,
            ref RtfStyle currentRtfCharStyle)
        {
            if (currentIndex > 0)
                previousRtfCharStyle = currentRtfCharStyle;

            RichTextBox1.SelectionStart = currentIndex;
            RichTextBox1.SelectionLength = 1;

            currentRtfCharStyle = new RtfStyle(
                RichTextBox1.SelectionAlignment,
                RichTextBox1.SelectionIndent,
                RichTextBox1.SelectionFont,
                RichTextBox1.SelectionColor,
                RichTextBox1.SelectionBullet,
                RichTextBox1.SelectionBackColor); 
        }
            public static bool AttributeIsEqual(object theCurrentAttribute, RtfStyle theNextRtfCharStyle)
            {
                bool returnValue = false;

                try
                {

                    if (theCurrentAttribute == null) throw new ArgumentNullException("theCurrentAttribute");
                    if (theNextRtfCharStyle == null) throw new ArgumentNullException("theNextRtfCharStyle");

                    Type type = theCurrentAttribute.GetType();

                    string currentValue;
                    string nextValue;

                    switch (type.Name)
                    {
                        case "HorizontalAlignment":
                            returnValue = (theNextRtfCharStyle.ItsAlignment.CompareTo(theCurrentAttribute) == 0);
                            break;
                        case "int":
                            returnValue = (theNextRtfCharStyle.ItsIndent.CompareTo(theCurrentAttribute) == 0);
                            break;
                        case "Font":
                            currentValue = ConvertAttributeValueToString(theCurrentAttribute);
                            nextValue = ConvertAttributeValueToString(theNextRtfCharStyle.ItsFont);
                            returnValue = (currentValue == nextValue);
                            break;
                        case "FontFamily":
                            currentValue = ConvertAttributeValueToString(theCurrentAttribute);
                            nextValue = ConvertAttributeValueToString(theNextRtfCharStyle.ItsFont.FontFamily.Name);
                            returnValue = (currentValue == nextValue);
                            break;
                        case "Color":
                            currentValue = ((Color)theCurrentAttribute).ToString();
                            nextValue = theNextRtfCharStyle.ItsColor.ToString();
                            returnValue = (currentValue == nextValue);
                            break;
                        case "bool":
                            returnValue = (theNextRtfCharStyle.ItsFont.Bold == (bool)theCurrentAttribute);
                            break;
                        case "Colors":
                            currentValue = ConvertAttributeValueToString(theCurrentAttribute);
                            nextValue = ConvertAttributeValueToString(theNextRtfCharStyle.ItsFontColors);
                            returnValue = (currentValue == nextValue);
                            break;
                        default:
                            break;
                    }
                }
                catch (Exception exception)
                {

                    Messages.ExceptionMessage(exception);
                }

                return returnValue;
            }
        private bool NextCharAttributeValueIsEqual(int nextTagIdx, object theCurrentAttribute)
        {
            RichTextBox1.SelectionStart = nextTagIdx;
            RichTextBox1.SelectionLength = 1;

            var nextRtfCharAttributes = new RtfStyle();
            var currentRtfCharAttributes = new RtfStyle();
            RetrieveCharAttributesByIndex(nextTagIdx, ref currentRtfCharAttributes, ref nextRtfCharAttributes);

            return RtfStyle.AttributeIsEqual(theCurrentAttribute, nextRtfCharAttributes);
        }