Example #1
0
        /// <summary>
        /// Creates a TextLayout with the specified parameter</summary>
        /// <param name="text">The string to create a new D2dTextLayout object from</param>
        /// <param name="textFormat">The text format to apply to the string</param>
        /// <param name="layoutWidth">Text layout width</param>
        /// <param name="layoutHeight">Text layout height</param>
        /// <returns>A new instance of D2dTextLayout</returns>
        public static D2dTextLayout CreateTextLayout(string text, D2dTextFormat textFormat, float layoutWidth, float layoutHeight)
        {
            var textlayout = new TextLayout(s_dwFactory, text, textFormat.NativeTextFormat,
                                            layoutWidth, layoutHeight);

            return(new D2dTextLayout(text, textlayout));
        }
Example #2
0
        /// <summary>
        /// Creates a TextLayout with the specified parameter</summary>
        /// <param name="text">The string to create a new D2dTextLayout object from</param>
        /// <param name="textFormat">The text format to apply to the string</param>
        /// <param name="layoutWidth">Text layout width</param>
        /// <param name="layoutHeight">Text layout height</param>
        /// <param name="transform">2D Matrix used to transform the text</param>
        /// <returns>A new instance of D2dTextLayout</returns>
        public static D2dTextLayout CreateTextLayout(string text, D2dTextFormat textFormat, float layoutWidth, float layoutHeight, Matrix3x2F transform)
        {
            var matrix = Matrix.Identity;

            matrix.M11 = transform.M11;
            matrix.M12 = transform.M12;
            matrix.M21 = transform.M21;
            matrix.M22 = transform.M22;
            matrix.M31 = transform.DX;
            matrix.M32 = transform.DY;

            var textlayout = new TextLayout(s_dwFactory, text, textFormat.NativeTextFormat,
                                            layoutWidth, layoutHeight, 1, matrix, true);

            if (textFormat.Underlined)
            {
                textlayout.SetUnderline(true, new SharpDX.DirectWrite.TextRange(0, text.Length));
            }
            if (textFormat.Strikeout)
            {
                textlayout.SetStrikethrough(true, new SharpDX.DirectWrite.TextRange(0, text.Length));
            }


            var d2dTextLayout = new D2dTextLayout(text, textlayout);

            d2dTextLayout.DrawTextOptions = textFormat.DrawTextOptions;
            return(d2dTextLayout);
        }
Example #3
0
 /// <summary>
 /// Measures the specified string when drawn with the specified System.Drawing.Font</summary>
 /// <param name="text">String to measure</param>
 /// <param name="textFormat">D2dTextFormat that defines the font and format of the string</param>
 /// <param name="maxSize">The maximum x and y dimensions</param>
 /// <returns>This method returns a System.Drawing.SizeF structure that represents the
 /// size, in DIP (Device Independent Pixels) units of the string specified by the text parameter as drawn
 /// with the format parameter</returns>
 public static SizeF MeasureText(string text, D2dTextFormat textFormat, SizeF maxSize)
 {
     if (string.IsNullOrEmpty(text))
     {
         return(SizeF.Empty);
     }
     return(s_gfx.MeasureText(text, textFormat, maxSize));
 }
Example #4
0
        private void CreateSharedResources()
        {
            // create text format object for various test modes
            m_generalTextFormat = D2dFactory.CreateTextFormat("Calibri"
                , D2dFontWeight.Bold, D2dFontStyle.Normal, D2dFactory.FontSizeToPixel(16));

            m_generalTextFormat.WordWrapping = D2dWordWrapping.Wrap;
        }
Example #5
0
        /// <summary>
        /// Creates a TextLayout with the specified parameter</summary>
        /// <param name="text">The string to create a new D2dTextLayout object from</param>
        /// <param name="textFormat">The text format to apply to the string</param>
        /// <param name="layoutWidth">Text layout width</param>
        /// <param name="layoutHeight">Text layout height</param>
        /// <param name="transform">2D Matrix used to transform the text</param>
        /// <returns>A new instance of D2dTextLayout</returns>
        public static D2dTextLayout CreateTextLayout(string text, D2dTextFormat textFormat, float layoutWidth, float layoutHeight, Matrix3x2F transform)
        {
            var matrix = new Matrix3x2
            {
                M11 = transform.M11,
                M12 = transform.M12,
                M21 = transform.M21,
                M22 = transform.M22,
                M31 = transform.DX,
                M32 = transform.DY
            };

            var textlayout = new TextLayout(s_dwFactory, text, textFormat.NativeTextFormat,
                                            layoutWidth, layoutHeight, 1, matrix, true);

            return(new D2dTextLayout(text, textlayout));
        }
Example #6
0
        /// <summary>
        /// Creates a TextLayout with the specified parameter</summary>
        /// <param name="text">The string to create a new D2dTextLayout object from</param>
        /// <param name="textFormat">The text format to apply to the string</param>
        /// <param name="layoutWidth">Text layout width</param>
        /// <param name="layoutHeight">Text layout height</param>
        /// <returns>A new instance of D2dTextLayout</returns>
        public static D2dTextLayout CreateTextLayout(string text, D2dTextFormat textFormat, float layoutWidth, float layoutHeight)
        {
            var textlayout = new TextLayout(s_dwFactory, text, textFormat.NativeTextFormat,
                                            layoutWidth, layoutHeight);

            if (textFormat.Underlined)
            {
                textlayout.SetUnderline(true, new SharpDX.DirectWrite.TextRange(0, text.Length));
            }
            if (textFormat.Strikeout)
            {
                textlayout.SetStrikethrough(true, new SharpDX.DirectWrite.TextRange(0, text.Length));
            }

            var d2dTextLayout = new D2dTextLayout(text, textlayout);

            d2dTextLayout.DrawTextOptions = textFormat.DrawTextOptions;
            return(d2dTextLayout);
        }
Example #7
0
        /// <summary>
        /// Draws a horizontal chart scale</summary>
        /// <param name="g">The Direct2D graphics object</param>
        /// <param name="transform">Graph (world) to window's client (screen) transform</param>
        /// <param name="graphRect">Graph rectangle</param>
        /// <param name="top">Whether or not the scale should be aligned along the top of the rectangle</param>
        /// <param name="majorSpacing">Spacing, in pixels, between major tick marks</param>
        /// <param name="minimumGraphStep">Minimum spacing, in graph (world) space, between ticks.
        /// For example, 1.0 would limit ticks to being drawn on whole integers.</param>
        /// <param name="lineBrush">Scale line pen</param>
        /// <param name="textFormat">Text format</param>
        /// <param name="textBrush">Text brush</param>
        public static void DrawHorizontalScale(
            this D2dGraphics g,
            Matrix transform,
            RectangleF graphRect,
            bool top,
            int majorSpacing,
            float minimumGraphStep,
            D2dBrush lineBrush,
            D2dTextFormat textFormat,
            D2dBrush textBrush)
        {
            double xScale = transform.Elements[0];
            RectangleF clientRect = Transform(transform, graphRect);

            double tickEnd, majorTickStart, minorTickStart, textStart;

            if (top)
            {
                tickEnd = clientRect.Top + 1;
                majorTickStart = tickEnd + 12;
                minorTickStart = tickEnd + 6;
                textStart = tickEnd + 8;
            }
            else
            {
                tickEnd = clientRect.Bottom - 1;
                majorTickStart = tickEnd - 12;
                minorTickStart = tickEnd - 6;
                textStart = tickEnd - 19;
            }

            double min = Math.Min(graphRect.Left, graphRect.Right);
            double max = Math.Max(graphRect.Left, graphRect.Right);

            double tickAnchor = CalculateTickAnchor(min, max);
            double majorGraphStep = CalculateStep(
                min, max, Math.Abs(clientRect.Right - clientRect.Left), majorSpacing, minimumGraphStep);
            int numMinorTicks = CalculateNumMinorTicks(majorGraphStep, minimumGraphStep, 5);
            double cMinorStep = (majorGraphStep / numMinorTicks) * xScale;
            if (majorGraphStep > 0)
            {
                double offset = tickAnchor - min;
                offset = offset - MathUtil.Remainder(offset, majorGraphStep);

                // draw leading minor ticks
                double cmx;
                cmx = ((tickAnchor - (offset + majorGraphStep)) - min) * xScale + clientRect.Left + cMinorStep;
                for (int i = 0; i < numMinorTicks - 1 && cmx < clientRect.Right; i++)
                {
                    // cull minor ticks outside of the view
                    if (cmx > clientRect.Left)
                    {
                        g.DrawLine((float)cmx, (float)minorTickStart, (float)cmx, (float)tickEnd, lineBrush);
                    }
                    cmx += cMinorStep;
                }

                for (double x = tickAnchor - offset; x < max; x += majorGraphStep)
                {
                    double cx = (x - min) * xScale + clientRect.Left;
                    g.DrawLine((float)cx, (float)majorTickStart, (float)cx, (float)tickEnd, lineBrush);
                    string xString = String.Format("{0:G8}", Math.Round(x, 6));
                    SizeF textSize = g.MeasureText(xString, textFormat);
                    var textRect = new RectangleF(new PointF((float)cx + 1, (float)textStart), textSize);
                    g.DrawText(xString, textFormat, textRect, textBrush);

                    // draw minor ticks
                    cmx = cx + cMinorStep;
                    for (int i = 0; i < numMinorTicks - 1 && cmx < clientRect.Right; i++)
                    {
                        g.DrawLine((float)cmx, (float)minorTickStart, (float)cmx, (float)tickEnd, lineBrush);
                        cmx += cMinorStep;
                    }
                }
            }
        }
Example #8
0
 /// <summary>
 /// Measures the specified string when drawn with the specified System.Drawing.Font</summary>
 /// <param name="text">String to measure</param>
 /// <param name="format">D2dTextFormat that defines the font and format of the string</param>
 /// <param name="maxSize">The maximum x and y dimensions</param>
 /// <returns>This method returns a System.Drawing.SizeF structure that represents the
 /// size, in DIP (Device Independent Pixels) units of the string specified by the text parameter as drawn 
 /// with the format parameter</returns>
 public SizeF MeasureText(string text, D2dTextFormat format, SizeF maxSize)
 {
     using (var layout = new SharpDX.DirectWrite.TextLayout(D2dFactory.NativeDwFactory,
             text, format.NativeTextFormat, maxSize.Width, maxSize.Height))
     {
         var metrics = layout.Metrics;
         return new SizeF(metrics.WidthIncludingTrailingWhitespace, metrics.Height);
     }
 }
Example #9
0
 /// <summary>
 /// Measures the specified string when drawn with the specified System.Drawing.Font</summary>
 /// <param name="text">String to measure</param>
 /// <param name="format">D2dTextFormat that defines the font and format of the string</param>
 /// <returns>This method returns a System.Drawing.SizeF structure that represents the
 /// size, in DIP (Device Independent Pixels) units of the string specified by the text parameter as drawn 
 /// with the format parameter</returns>
 public SizeF MeasureText(string text, D2dTextFormat format)
 {
     return MeasureText(text, format, new SizeF(2024, 2024));
 }
Example #10
0
 /// <summary>
 /// Draws the specified text using the format information provided by an SharpDX.DirectWrite.TextFormat object</summary>
 /// <param name="text">A string to draw</param>
 /// <param name="textFormat">An object that describes formatting details of the text to draw, such as
 /// the font, the font size, and flow direction</param>
 /// <param name="layoutRect">The size and position of the area in which the text is drawn</param>
 /// <param name="color">The color used to paint the text</param>
 public void DrawText(string text, D2dTextFormat textFormat, RectangleF layoutRect, Color color)
 {
     m_solidColorBrush.Color = color;
     DrawText(text, textFormat, layoutRect, m_solidColorBrush);
 }
Example #11
0
 /// <summary>
 /// Draws the specified text using the format information provided by an SharpDX.DirectWrite.TextFormat object</summary>
 /// <param name="text">A string to draw</param>
 /// <param name="textFormat">An object that describes formatting details of the text to draw, such as
 /// the font, the font size, and flow direction</param>
 /// <param name="layoutRect">The size and position of the area in which the text is drawn</param>
 /// <param name="brush">The brush used to paint the text</param>
 public void DrawText(string text, D2dTextFormat textFormat, RectangleF layoutRect, D2dBrush brush)
 {
     using (var layout = new SharpDX.DirectWrite.TextLayout(D2dFactory.NativeDwFactory, text, textFormat.NativeTextFormat, layoutRect.Width, layoutRect.Height))
     {
         if (textFormat.Underlined)
             layout.SetUnderline(true, new SharpDX.DirectWrite.TextRange(0, text.Length));
         if (textFormat.Strikeout)
             layout.SetStrikethrough(true, new SharpDX.DirectWrite.TextRange(0, text.Length));
         m_renderTarget.DrawTextLayout(
             layoutRect.Location.ToSharpDX(),
             layout, brush.NativeBrush,
             (DrawTextOptions)textFormat.DrawTextOptions);
     }
 }
Example #12
0
 /// <summary>
 /// Draws the specified text using the format information provided at the specified location</summary>
 /// <param name="text">The text string to draw</param>
 /// <param name="textFormat">The text format object to use</param>
 /// <param name="upperLeft">Upper left corner of the text</param>
 /// <param name="color">The color to use to draw the text</param>
 public void DrawText(string text, D2dTextFormat textFormat, PointF upperLeft, Color color)
 {
     m_solidColorBrush.Color = color;
     DrawText(text, textFormat, upperLeft, m_solidColorBrush);
 }
Example #13
0
 /// <summary>
 /// Draws the specified text using the format information provided at the specified location</summary>
 /// <param name="text">The text string to draw</param>
 /// <param name="textFormat">The text format object to use</param>
 /// <param name="upperLeft">Upper left corner of the text</param>
 /// <param name="brush">The brush to use to draw the text</param>
 public void DrawText(string text, D2dTextFormat textFormat, PointF upperLeft, D2dBrush brush)
 {
     var rtsize = Size;
     using (var layout
         = new SharpDX.DirectWrite.TextLayout(D2dFactory.NativeDwFactory,
             text, textFormat.NativeTextFormat, rtsize.Width, rtsize.Height))
     {
         layout.TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading;
         layout.ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near;
         if (textFormat.Underlined)
             layout.SetUnderline(true,new SharpDX.DirectWrite.TextRange(0, text.Length));
         if (textFormat.Strikeout)
             layout.SetStrikethrough(true,new SharpDX.DirectWrite.TextRange(0, text.Length));
         m_renderTarget.DrawTextLayout(upperLeft.ToSharpDX(),
             layout,
             brush.NativeBrush,
             (DrawTextOptions)textFormat.DrawTextOptions);
     }
 }
Example #14
0
        /// <summary>
        /// Draws a horizontal chart scale</summary>
        /// <param name="g">The Direct2D graphics object</param>
        /// <param name="transform">Graph (world) to window's client (screen) transform</param>
        /// <param name="graphRect">Graph rectangle</param>
        /// <param name="top">Whether or not the scale should be aligned along the top of the rectangle</param>
        /// <param name="majorSpacing">Spacing, in pixels, between major tick marks</param>
        /// <param name="minimumGraphStep">Minimum spacing, in graph (world) space, between ticks.
        /// For example, 1.0 would limit ticks to being drawn on whole integers.</param>
        /// <param name="lineBrush">Scale line pen</param>
        /// <param name="textFormat">Text format</param>
        /// <param name="textBrush">Text brush</param>
        public static void DrawHorizontalScale(
            this D2dGraphics g,
            Matrix transform,
            RectangleF graphRect,
            bool top,
            int majorSpacing,
            float minimumGraphStep,
            D2dBrush lineBrush,
            D2dTextFormat textFormat,
            D2dBrush textBrush)
        {
            double     xScale     = transform.Elements[0];
            RectangleF clientRect = Transform(transform, graphRect);

            double tickEnd, majorTickStart, minorTickStart, textStart;

            if (top)
            {
                tickEnd        = clientRect.Top + 1;
                majorTickStart = tickEnd + 12;
                minorTickStart = tickEnd + 6;
                textStart      = tickEnd + 8;
            }
            else
            {
                tickEnd        = clientRect.Bottom - 1;
                majorTickStart = tickEnd - 12;
                minorTickStart = tickEnd - 6;
                textStart      = tickEnd - 19;
            }

            double min = Math.Min(graphRect.Left, graphRect.Right);
            double max = Math.Max(graphRect.Left, graphRect.Right);

            double tickAnchor     = CalculateTickAnchor(min, max);
            double majorGraphStep = CalculateStep(
                min, max, Math.Abs(clientRect.Right - clientRect.Left), majorSpacing, minimumGraphStep);
            int    numMinorTicks = CalculateNumMinorTicks(majorGraphStep, minimumGraphStep, 5);
            double cMinorStep    = (majorGraphStep / numMinorTicks) * xScale;

            if (majorGraphStep > 0)
            {
                double offset = tickAnchor - min;
                offset = offset - MathUtil.Remainder(offset, majorGraphStep);

                // draw leading minor ticks
                double cmx;
                cmx = ((tickAnchor - (offset + majorGraphStep)) - min) * xScale + clientRect.Left + cMinorStep;
                for (int i = 0; i < numMinorTicks - 1 && cmx < clientRect.Right; i++)
                {
                    // cull minor ticks outside of the view
                    if (cmx > clientRect.Left)
                    {
                        g.DrawLine((float)cmx, (float)minorTickStart, (float)cmx, (float)tickEnd, lineBrush);
                    }
                    cmx += cMinorStep;
                }

                for (double x = tickAnchor - offset; x < max; x += majorGraphStep)
                {
                    double cx = (x - min) * xScale + clientRect.Left;
                    g.DrawLine((float)cx, (float)majorTickStart, (float)cx, (float)tickEnd, lineBrush);
                    string xString  = String.Format("{0:G8}", Math.Round(x, 6));
                    SizeF  textSize = g.MeasureText(xString, textFormat);
                    var    textRect = new RectangleF(new PointF((float)cx + 1, (float)textStart), textSize);
                    g.DrawText(xString, textFormat, textRect, textBrush);

                    // draw minor ticks
                    cmx = cx + cMinorStep;
                    for (int i = 0; i < numMinorTicks - 1 && cmx < clientRect.Right; i++)
                    {
                        g.DrawLine((float)cmx, (float)minorTickStart, (float)cmx, (float)tickEnd, lineBrush);
                        cmx += cMinorStep;
                    }
                }
            }
        }
Example #15
0
 /// <summary>
 /// Creates a TextLayout with the specified parameter</summary>
 /// <param name="text">The string to create a new D2dTextLayout object from</param>
 /// <param name="textFormat">The text format to apply to the string</param>
 /// <param name="transform">2D Matrix used to transform the text</param>
 /// <returns>A new instance of D2dTextLayout</returns>
 public static D2dTextLayout CreateTextLayout(string text, D2dTextFormat textFormat, Matrix3x2F transform)
 {
     return(CreateTextLayout(text, textFormat, 2048, 2048, transform));
 }
Example #16
0
 /// <summary>
 /// Creates a TextLayout with the specified parameter</summary>
 /// <param name="text">The string to create a new D2dTextLayout object from</param>
 /// <param name="textFormat">The text format to apply to the string</param>
 /// <returns>A new instance of D2dTextLayout</returns>
 public static D2dTextLayout CreateTextLayout(string text, D2dTextFormat textFormat)
 {
     return(CreateTextLayout(text, textFormat, 2048, 2048));
 }