/// <summary>
 /// Adds the text to the existing paragraph.
 /// </summary>
 /// <param name="txt">Text string.</param>
 /// <param name="options">Text formatting options.</param>
 public void Write(string txt, MTextFormattingOptions options)
 {
     if (options == null)
     {
         this.text += txt;
     }
     else
     {
         this.text += options.FormatText(txt);
     }
 }
示例#2
0
        /// <summary>
        /// Adds the text to the current paragraph.
        /// </summary>
        /// <param name="txt">Text string.</param>
        /// <param name="options">Text formatting options.</param>
        public void Write(string txt, MTextFormattingOptions options)
        {
            if (options == null)
            {
                this.text += txt;
                return;
            }

            string formattedText    = txt;
            double baseHeightFactor = options.HeightFactor;

            if (options.Superscript)
            {
                formattedText     = string.Format("\\S{0}^ ;", formattedText);
                baseHeightFactor *= options.SuperSubScriptHeightFactor;
            }
            if (options.Subscript)
            {
                formattedText     = string.Format("\\S^ {0};", formattedText);
                baseHeightFactor *= options.SuperSubScriptHeightFactor;
            }

            string f;

            if (string.IsNullOrEmpty(options.FontName))
            {
                f = this.style.IsTrueType ? this.style.FontFamilyName : this.style.FontFile;
            }
            else
            {
                f = options.FontName;
            }

            if (options.Bold && options.Italic)
            {
                formattedText = string.Format("\\F{0}|b1|i1;{1}", f, formattedText);
            }
            else if (options.Bold && !options.Italic)
            {
                formattedText = string.Format("\\F{0}|b1|i0;{1}", f, formattedText);
            }
            else if (!options.Bold && options.Italic)
            {
                formattedText = string.Format("\\F{0}|i1|b0;{1}", f, formattedText);
            }
            else
            {
                formattedText = string.Format("\\F{0}|b0|i0;{1}", f, formattedText);
            }

            if (options.Overline)
            {
                formattedText = string.Format("\\O{0}\\o", formattedText);
            }
            if (options.Underline)
            {
                formattedText = string.Format("\\L{0}\\l", formattedText);
            }
            if (options.StrikeThrough)
            {
                formattedText = string.Format("\\K{0}\\k", formattedText);
            }
            if (options.Color != null)
            {
                // The DXF is not consistent in the way it converts a true color to its 24-bit representation,
                // while stuff like layer colors it follows BGR order, when formatting text it uses RGB order.
                formattedText = options.Color.UseTrueColor
                    ? string.Format("\\C{0};\\c{1};{2}", options.Color.Index, BitConverter.ToInt32(new byte[] { options.Color.R, options.Color.G, options.Color.B, 0 }, 0), formattedText)
                    : string.Format("\\C{0};{1}", options.Color.Index, formattedText);
            }

            if (!MathHelper.IsOne(baseHeightFactor))
            {
                formattedText = string.Format("\\H{0}x;{1}", baseHeightFactor.ToString(CultureInfo.InvariantCulture), formattedText);
            }
            if (!MathHelper.IsZero(options.ObliqueAngle))
            {
                formattedText = string.Format("\\Q{0};{1}", options.ObliqueAngle.ToString(CultureInfo.InvariantCulture), formattedText);
            }
            if (!MathHelper.IsOne(options.CharacterSpaceFactor))
            {
                formattedText = string.Format("\\T{0};{1}", options.CharacterSpaceFactor.ToString(CultureInfo.InvariantCulture), formattedText);
            }
            if (!MathHelper.IsOne(options.WidthFactor))
            {
                formattedText = string.Format("\\W{0};{1}", options.WidthFactor.ToString(CultureInfo.InvariantCulture), formattedText);
            }

            this.text += "{" + formattedText + "}";
        }
示例#3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="text"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="db"></param>
        /// <param name="r"></param>
        public void Draw(object doc, Test2d.XText text, double dx, double dy, ImmutableArray<Test2d.ShapeProperty> db, Test2d.Record r)
        {
            var _doc = doc as DxfDocument;

            var tbind = text.BindToTextProperty(db, r);
            if (string.IsNullOrEmpty(tbind))
                return;

            var style = text.Style;
            var stroke = GetColor(style.Stroke);
            var strokeTansparency = GetTransparency(style.Stroke);

            var attachmentPoint = default(MTextAttachmentPoint);
            double x, y;
            var rect = Test2d.Rect2.Create(text.TopLeft, text.BottomRight, dx, dy);

            switch (text.Style.TextStyle.TextHAlignment)
            {
                default:
                case Test2d.TextHAlignment.Left:
                    x = rect.X;
                    break;
                case Test2d.TextHAlignment.Center:
                    x = rect.X + rect.Width / 2.0;
                    break;
                case Test2d.TextHAlignment.Right:
                    x = rect.X + rect.Width;
                    break;
            }

            switch (text.Style.TextStyle.TextVAlignment)
            {
                default:
                case Test2d.TextVAlignment.Top:
                    y = rect.Y;
                    break;
                case Test2d.TextVAlignment.Center:
                    y = rect.Y + rect.Height / 2.0;
                    break;
                case Test2d.TextVAlignment.Bottom:
                    y = rect.Y + rect.Height;
                    break;
            }

            switch (text.Style.TextStyle.TextVAlignment)
            {
                default:
                case Test2d.TextVAlignment.Top:
                    switch (text.Style.TextStyle.TextHAlignment)
                    {
                        default:
                        case Test2d.TextHAlignment.Left:
                            attachmentPoint = MTextAttachmentPoint.TopLeft;
                            break;
                        case Test2d.TextHAlignment.Center:
                            attachmentPoint = MTextAttachmentPoint.TopCenter;
                            break;
                        case Test2d.TextHAlignment.Right:
                            attachmentPoint = MTextAttachmentPoint.TopRight;
                            break;
                    }
                    break;
                case Test2d.TextVAlignment.Center:
                    switch (text.Style.TextStyle.TextHAlignment)
                    {
                        default:
                        case Test2d.TextHAlignment.Left:
                            attachmentPoint = MTextAttachmentPoint.MiddleLeft;
                            break;
                        case Test2d.TextHAlignment.Center:
                            attachmentPoint = MTextAttachmentPoint.MiddleCenter;
                            break;
                        case Test2d.TextHAlignment.Right:
                            attachmentPoint = MTextAttachmentPoint.MiddleRight;
                            break;
                    }
                    break;
                case Test2d.TextVAlignment.Bottom:
                    switch (text.Style.TextStyle.TextHAlignment)
                    {
                        default:
                        case Test2d.TextHAlignment.Left:
                            attachmentPoint = MTextAttachmentPoint.BottomLeft;
                            break;
                        case Test2d.TextHAlignment.Center:
                            attachmentPoint = MTextAttachmentPoint.BottomCenter;
                            break;
                        case Test2d.TextHAlignment.Right:
                            attachmentPoint = MTextAttachmentPoint.BottomRight;
                            break;
                    }
                    break;
            }

            var ts = new TextStyle(style.TextStyle.FontName, style.TextStyle.FontFile);
            var dxfMText = new MText(
                new Vector3(ToDxfX(x), ToDxfY(y), 0),
                text.Style.TextStyle.FontSize * 72.0 / 96.0,
                rect.Width,
                ts);
            dxfMText.AttachmentPoint = attachmentPoint;

            var fs = text.Style.TextStyle.FontStyle;
            var options = new MTextFormattingOptions(dxfMText.Style);
            options.Bold = fs.Flags.HasFlag(Test2d.FontStyleFlags.Bold);
            options.Italic = fs.Flags.HasFlag(Test2d.FontStyleFlags.Italic);
            options.Underline = fs.Flags.HasFlag(Test2d.FontStyleFlags.Underline);
            options.StrikeThrough = fs.Flags.HasFlag(Test2d.FontStyleFlags.Strikeout);

            options.Aligment = MTextFormattingOptions.TextAligment.Default;
            options.Color = null;
            dxfMText.Write(tbind, options);

            dxfMText.Layer = _currentLayer;
            dxfMText.Transparency.Value = strokeTansparency;
            dxfMText.Color = stroke;

            _doc.AddEntity(dxfMText);
        }
示例#4
0
        /// <summary>
        /// Formats a text string to represent a fraction.
        /// </summary>
        /// <param name="numerator">Fraction numerator.</param>
        /// <param name="denominator">Fraction denominator.</param>
        /// <param name="fractionType">Style of the fraction.</param>
        /// <param name="options">Text formatting options.</param>
        /// <returns>A text string that represents the fraction.</returns>
        /// <remarks>
        /// In fractions the characters '/' and '#' are reserved if you need to write them you must write "\/" and "\#", respectively.<br />
        /// Do not combine fractions with super or subscript options, it is not supported (stacking commands cannot be nested).
        /// </remarks>
        public void WriteFraction(string numerator, string denominator, FractionFormatType fractionType, MTextFormattingOptions options)
        {
            string txt = string.Empty;

            switch (fractionType)
            {
            case FractionFormatType.Diagonal:
                txt = "\\S" + numerator + "#" + denominator + ";";
                break;

            case FractionFormatType.Horizontal:
                txt = "\\S" + numerator + "/" + denominator + ";";
                break;

            case FractionFormatType.NotStacked:
                txt = numerator + "/" + denominator;
                break;
            }

            this.Write(txt, options);
        }
示例#5
0
        private static void WriteMText()
        {
            DxfDocument dxf = new DxfDocument();

            //xData sample
            XData xdata = new XData(new ApplicationRegistry("netDxf"));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.String, "extended data with netDxf"));
            xdata.XDataRecord.Add(XDataRecord.OpenControlString);
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionX, 0));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionY, 0));
            xdata.XDataRecord.Add(new XDataRecord(XDataCode.WorldSpacePositionZ, 0));
            xdata.XDataRecord.Add(XDataRecord.CloseControlString);

            //text
            TextStyle style = new TextStyle("Times.ttf");
            //TextStyle style = TextStyle.Default;
            MText mText = new MText(new Vector3(3,2,0), 1.0f, 100.0f, style);
            mText.Layer = new Layer("Multiline Text");
            //mText.Layer.Color.Index = 8;
            mText.Rotation = 0;
            mText.LineSpacingFactor = 1.0;
            mText.ParagraphHeightFactor = 1.0;

            //mText.AttachmentPoint = MTextAttachmentPoint.TopCenter;
            //mText.Write("Hello World!");
            //mText.Write(" we keep writting on the same line.");
            //mText.WriteLine("This text is in a new line");

            //mText.Write("Hello World! ");
            //for (int i = 0; i < 50; i++)
            //{
            //    mText.Write("1234567890");
            //}
            //mText.Write(" This text is over the limit of the 250 character chunk");
            //mText.NewParagraph();
            //mText.Write("This is a text in a new paragraph");
            //mText.Write(" and we continue writing in the previous paragraph");
            //mText.NewParagraph();
            MTextFormattingOptions options = new MTextFormattingOptions(mText.Style);
            options.Bold = true;
            mText.Write("Bold text in mText.Style", options);
            mText.EndParagraph();
            options.Italic = true;
            mText.Write("Bold and italic text in mText.Style", options);
            mText.EndParagraph();
            options.Bold = false;
            options.FontName = "Arial";
            options.Color = AciColor.Blue;
            mText.ParagraphHeightFactor = 2;
            mText.Write("Italic text in Arial", options);
            mText.EndParagraph();
            options.Italic = false;
            options.Color = null; // back to the default text color
            mText.Write("Normal text in Arial with the default paragraph height factor", options);
            mText.EndParagraph();
            mText.ParagraphHeightFactor = 1;
            mText.Write("No formatted text uses mText.Style");
            mText.Write(" and the text continues in the same paragraph.");
            mText.EndParagraph();

            //options.HeightPercentage = 2.5;
            //options.Color = AciColor.Red;
            //options.Overstrike = true;
            //options.Underline = true;
            //options.FontFile = "times.ttf";
            //options.ObliqueAngle = 15;
            //options.CharacterSpacePercentage = 2.35;
            //options.WidthFactor = 1.8;
            
            //for unknown reasons the aligment doesn't seem to change anything
            //mText.Write("Formatted text", options);
            //options.Aligment = MTextFormattingOptions.TextAligment.Center;
            //mText.Write("Center", options);
            //options.Aligment = MTextFormattingOptions.TextAligment.Top;
            //mText.Write("Top", options);
            //options.Aligment = MTextFormattingOptions.TextAligment.Bottom;
            //mText.Write("Bottom", options);

            mText.XData.Add(xdata);
            
            dxf.AddEntity(mText);

            dxf.Save("MText sample.dxf");

        }
示例#6
0
        private static void MTextEntity()
        {
            TextStyle style = new TextStyle("Arial");

            MText text1 = new MText(Vector2.Zero, 10, 0, style);
            // you can set manually the text value with all available formatting commands
            text1.Value = "{\\C71;\\c10938556;Text} with true color\\P{\\C140;Text} with indexed color";

            MText text2 = new MText(new Vector2(0, 30), 10, 0, style);
            // or use the Write() method
            MTextFormattingOptions op = new MTextFormattingOptions(text2.Style);
            op.Color = new AciColor(188, 232, 166); // using true color
            text2.Write("Text", op);
            op.Color = null; // set color to the default defined in text2.Style
            text2.Write(" with true color");
            text2.EndParagraph();
            op.Color = new AciColor(140); // using index color
            text2.Write("Text", op); // set color to the default defined in text2.Style
            op.Color = null;
            text2.Write(" with indexed color");

            // both text1 and text2 should yield to the same result
            DxfDocument dxf = new DxfDocument(DxfVersion.AutoCad2010);
            dxf.AddEntity(text1);
            dxf.AddEntity(text2);

            dxf.Save("MText format.dxf");

            // now you can retrieve the MText text value without any formatting codes, control characters like tab '\t' will be preserved in the result,
            // the new paragraph command "\P" will be converted to new line feed '\r\n'.
            Console.WriteLine(text1.PlainText());
            Console.WriteLine();
            Console.WriteLine(text2.PlainText());
            Console.WriteLine();
            Console.WriteLine("Press a key to finish...");
            Console.ReadKey();

        }
示例#7
0
 /// <summary>
 /// Adds the text to the existing paragraph. 
 /// </summary>
 /// <param name="text">Text string.</param>
 /// <param name="options">Text formatting options.</param>
 public void Write(string text, MTextFormattingOptions options = null)
 {
     if (options == null)
         this.value += text;
     else
         this.value += options.FormatText(text);
 }
示例#8
0
        /// <inheritdoc/>
        public override void Draw(object dc, Core2D.Shapes.XText text, double dx, double dy, ImmutableArray<Core2D.Data.XProperty> db, Core2D.Data.Database.XRecord r)
        {
            var dxf = dc as DxfDocument;

            var tbind = text.BindText(db, r);
            if (string.IsNullOrEmpty(tbind))
                return;

            var style = text.Style;
            var stroke = ToColor(style.Stroke);
            var strokeTansparency = ToTransparency(style.Stroke);

            var attachmentPoint = default(MTextAttachmentPoint);
            double x, y;
            var rect = Core2D.Math.Rect2.Create(text.TopLeft, text.BottomRight, dx, dy);

            switch (text.Style.TextStyle.TextHAlignment)
            {
                default:
                case Core2D.Style.TextHAlignment.Left:
                    x = rect.X;
                    break;
                case Core2D.Style.TextHAlignment.Center:
                    x = rect.X + rect.Width / 2.0;
                    break;
                case Core2D.Style.TextHAlignment.Right:
                    x = rect.X + rect.Width;
                    break;
            }

            switch (text.Style.TextStyle.TextVAlignment)
            {
                default:
                case Core2D.Style.TextVAlignment.Top:
                    y = rect.Y;
                    break;
                case Core2D.Style.TextVAlignment.Center:
                    y = rect.Y + rect.Height / 2.0;
                    break;
                case Core2D.Style.TextVAlignment.Bottom:
                    y = rect.Y + rect.Height;
                    break;
            }

            switch (text.Style.TextStyle.TextVAlignment)
            {
                default:
                case Core2D.Style.TextVAlignment.Top:
                    switch (text.Style.TextStyle.TextHAlignment)
                    {
                        default:
                        case Core2D.Style.TextHAlignment.Left:
                            attachmentPoint = MTextAttachmentPoint.TopLeft;
                            break;
                        case Core2D.Style.TextHAlignment.Center:
                            attachmentPoint = MTextAttachmentPoint.TopCenter;
                            break;
                        case Core2D.Style.TextHAlignment.Right:
                            attachmentPoint = MTextAttachmentPoint.TopRight;
                            break;
                    }
                    break;
                case Core2D.Style.TextVAlignment.Center:
                    switch (text.Style.TextStyle.TextHAlignment)
                    {
                        default:
                        case Core2D.Style.TextHAlignment.Left:
                            attachmentPoint = MTextAttachmentPoint.MiddleLeft;
                            break;
                        case Core2D.Style.TextHAlignment.Center:
                            attachmentPoint = MTextAttachmentPoint.MiddleCenter;
                            break;
                        case Core2D.Style.TextHAlignment.Right:
                            attachmentPoint = MTextAttachmentPoint.MiddleRight;
                            break;
                    }
                    break;
                case Core2D.Style.TextVAlignment.Bottom:
                    switch (text.Style.TextStyle.TextHAlignment)
                    {
                        default:
                        case Core2D.Style.TextHAlignment.Left:
                            attachmentPoint = MTextAttachmentPoint.BottomLeft;
                            break;
                        case Core2D.Style.TextHAlignment.Center:
                            attachmentPoint = MTextAttachmentPoint.BottomCenter;
                            break;
                        case Core2D.Style.TextHAlignment.Right:
                            attachmentPoint = MTextAttachmentPoint.BottomRight;
                            break;
                    }
                    break;
            }

            var ts = new TextStyle(style.TextStyle.FontName, style.TextStyle.FontFile);
            var dxfMText = new MText(
                new Vector3(ToDxfX(x), ToDxfY(y), 0),
                text.Style.TextStyle.FontSize * _targetDpi / _sourceDpi,
                rect.Width,
                ts);
            dxfMText.AttachmentPoint = attachmentPoint;

            var options = new MTextFormattingOptions(dxfMText.Style);
            var fs = text.Style.TextStyle.FontStyle;
            if (fs != null)
            {
                options.Bold = fs.Flags.HasFlag(Core2D.Style.FontStyleFlags.Bold);
                options.Italic = fs.Flags.HasFlag(Core2D.Style.FontStyleFlags.Italic);
                options.Underline = fs.Flags.HasFlag(Core2D.Style.FontStyleFlags.Underline);
                options.StrikeThrough = fs.Flags.HasFlag(Core2D.Style.FontStyleFlags.Strikeout);
            }

            options.Aligment = MTextFormattingOptions.TextAligment.Default;
            options.Color = null;
            dxfMText.Write(tbind, options);

            dxfMText.Layer = _currentLayer;
            dxfMText.Transparency.Value = strokeTansparency;
            dxfMText.Color = stroke;

            dxf.AddEntity(dxfMText);
        }