Пример #1
0
        public static void DrawText(this ID2D1DeviceContext context,
                                    string text,
                                    IDWriteTextFormat format,
                                    D2D_RECT_F rect,
                                    ID2D1Brush defaultFillBrush,
                                    D2D1_DRAW_TEXT_OPTIONS options      = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE,
                                    DWRITE_MEASURING_MODE measuringMode = DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (defaultFillBrush == null)
            {
                throw new ArgumentNullException(nameof(defaultFillBrush));
            }

            if (text == null)
            {
                return;
            }

            context.DrawTextW(text, (uint)text.Length, format, ref rect, defaultFillBrush, options, measuringMode);
        }
Пример #2
0
        public static IComObject <T> CreateTextLayout <T>(this IDWriteFactory factory,
                                                          IDWriteTextFormat format,
                                                          string text,
                                                          int textLength  = 0,
                                                          float maxWidth  = float.MaxValue,
                                                          float maxHeight = float.MaxValue
                                                          ) where T : IDWriteTextLayout
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            textLength = textLength <= 0 ? text.Length : textLength;
            factory.CreateTextLayout(text, (uint)textLength, format, maxWidth, maxHeight, out var layout).ThrowOnError();
            return(new ComObject <T>((T)layout));
        }
        public static void DrawText(this ID2D1DeviceContext device,
                                    string text,
                                    IDWriteTextFormat format,
                                    D2D_RECT_F rect,
                                    ID2D1Brush brush,
                                    D2D1_DRAW_TEXT_OPTIONS options      = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE,
                                    DWRITE_MEASURING_MODE measuringMode = DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (text == null)
            {
                return;
            }

#pragma warning disable CA2010 // Always consume the value returned by methods marked with PreserveSigAttribute
            device.DrawTextW(text, (uint)text.Length, format, ref rect, brush, options, measuringMode);
#pragma warning restore CA2010 // Always consume the value returned by methods marked with PreserveSigAttribute
        }
Пример #4
0
        public static void DrawText(this ID2D1RenderTarget renderTarget,
                                    string text,
                                    IDWriteTextFormat format,
                                    D2D_RECT_F rect,
                                    ID2D1Brush brush,
                                    D2D1_DRAW_TEXT_OPTIONS options      = D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NONE,
                                    DWRITE_MEASURING_MODE measuringMode = DWRITE_MEASURING_MODE.DWRITE_MEASURING_MODE_NATURAL)
        {
            if (renderTarget == null)
            {
                throw new ArgumentNullException(nameof(renderTarget));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            if (text == null)
            {
                return;
            }

            renderTarget.DrawTextW(text, (uint)text.Length, format, ref rect, brush, options, measuringMode);
        }
        public static IComObject <IDWriteFontCollection> GetFontCollection(this IDWriteTextFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            format.GetFontCollection(out var collection).ThrowOnError();
            return(new ComObject <IDWriteFontCollection>(collection));
        }
        public static string GetFontFamilyName(this IDWriteTextFormat format)
        {
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            var len = format.GetFontFamilyNameLength();
            var s   = new string('\0', (int)len);

            format.GetFontFamilyName(s, len + 1).ThrowOnError();
            return(s);
        }
Пример #7
0
        static void Main()
        {
            using (var window = new LayeredRenderWindow()
            {
                Text = "Hello World", DragMoveEnabled = true
            })
            {
                IDWriteTextFormat bottomRightFont = window.XResource.DWriteFactory.CreateTextFormat("Consolas", 16.0f);
                bottomRightFont.FlowDirection = FlowDirection.BottomToTop;
                bottomRightFont.TextAlignment = TextAlignment.Trailing;

                IDWriteTextFormat bottomLeftFont = window.XResource.DWriteFactory.CreateTextFormat("Consolas", FontWeight.Normal, FontStyle.Italic, FontStretch.Normal, 24.0f);
                bottomLeftFont.FlowDirection = FlowDirection.BottomToTop;
                bottomLeftFont.TextAlignment = TextAlignment.Leading;

                window.Draw += Draw;
                RenderLoop.Run(window, () => window.Render(1, 0));

                void Draw(RenderWindow _, ID2D1DeviceContext target)
                {
                    XResource res = window.XResource;

                    target.Clear(Color4.Transparent);
                    RectangleF rectangle = new RectangleF(0, 0, target.Size.Width, target.Size.Height);

                    target.DrawRectangle(
                        rectangle,
                        res.GetColor(Color4.Blue));

                    target.DrawText("😀😁😂🤣😃😄😅😆😉😊😋😎",
                                    res.TextFormats[36], rectangle, res.GetColor(Color4.Blue),
                                    DrawTextOptions.EnableColorFont);

                    target.DrawText($"{window.XResource.DurationSinceStart:mm':'ss'.'ff}\nFPS: {window.RenderTimer.FramesPerSecond:F1}",
                                    bottomRightFont, rectangle, res.GetColor(Color4.Red));

                    target.DrawText("Hello World",
                                    bottomLeftFont, rectangle, res.GetColor(Color4.Purple));
                }
            }
        }
Пример #8
0
 /// <summary>
 /// Draws the specified text using the format information provided by an <see cref="IDWriteTextFormat" /> object.
 /// </summary>
 /// <remarks>
 /// To create an <see cref="IDWriteTextFormat"/> object, create an <see cref="IDWriteFactory"/> and call its CreateTextFormat method.
 /// </remarks>
 /// <param name="text">A reference to an array of Unicode characters 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="defaultForegroundBrush">The brush used to paint the text. </param>
 public void DrawText(string text, IDWriteTextFormat textFormat, RectF layoutRect, ID2D1Brush defaultForegroundBrush)
 {
     DrawText(text, text.Length, textFormat, layoutRect, defaultForegroundBrush, DrawTextOptions.None, MeasuringMode.Natural);
 }
Пример #9
0
 internal TextFormat(IntPtr objPtr)
 {
     comObject =
         (IDWriteTextFormat) Marshal.GetObjectForIUnknown(objPtr);
     Handle = objPtr;
     InitComMethods();
 }
Пример #10
0
 private void Release()
 {
     if(comObject == null)
     {
         throw new InvalidOperationException("The com interface instance has been released!");
     }
     Marshal.ReleaseComObject(comObject);
     comObject = null;
 }
Пример #11
0
        public static IComObject <IDWriteInlineObject> CreateEllipsisTrimmingSign(this IDWriteFactory factory, IDWriteTextFormat format)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }

            factory.CreateEllipsisTrimmingSign(format, out var sign).ThrowOnError();
            return(new ComObject <IDWriteInlineObject>(sign));
        }
Пример #12
0
        public void DrawText(string text, IDWriteTextFormat textFormat, Rect layoutRect, ID2D1Brush defaultFillBrush, DrawTextOptions options = DrawTextOptions.None, MeasuringMode measuringMode = MeasuringMode.Natural)
        {
            Guard.NotNullOrEmpty(text, nameof(text));

            DrawText(text, text.Length, textFormat, layoutRect, defaultFillBrush, options, measuringMode);
        }
Пример #13
0
 internal DWriteTextFormat(IDWriteTextFormat handle)
 {
     this.handle = handle;
 }
Пример #14
0
        static void Main()
        {
            using (var res = new XResource())
                using (var form = new Form()
                {
                    Text = "Hello World"
                })
                {
                    var timer = new RenderTimer();
                    IDWriteTextFormat bottomRightFont = res.DWriteFactory.CreateTextFormat("Consolas", 16.0f);
                    bottomRightFont.FlowDirection = FlowDirection.BottomToTop;
                    bottomRightFont.TextAlignment = TextAlignment.Trailing;

                    IDWriteTextFormat bottomLeftFont = res.DWriteFactory.CreateTextFormat("Consolas", FontWeight.Normal, FontStyle.Italic, FontStretch.Normal, 24.0f);
                    bottomLeftFont.FlowDirection = FlowDirection.BottomToTop;
                    bottomLeftFont.TextAlignment = TextAlignment.Leading;

                    form.Resize += (o, e) =>
                    {
                        if (form.WindowState != FormWindowState.Minimized && res.DeviceAvailable)
                        {
                            res.Resize();
                        }
                    };

                    RenderLoop.Run(form, () => Render());

                    void Render()
                    {
                        if (!res.DeviceAvailable)
                        {
                            res.InitializeDevice(form.Handle);
                        }

                        var target = res.RenderTarget;

                        timer.BeginFrame();
                        target.BeginDraw();
                        Draw(target);
                        target.EndDraw();
                        res.SwapChain.Present(1, 0);
                        timer.EndFrame();
                    }

                    void Draw(ID2D1DeviceContext target)
                    {
                        target.Clear(Color4.CornflowerBlue);
                        RectangleF rectangle = new RectangleF(0, 0, target.Size.Width, target.Size.Height);

                        target.DrawRectangle(
                            new RectangleF(10, 10, target.Size.Width - 20, target.Size.Height - 20),
                            res.GetColor(Color4.Blue));

                        target.DrawText("😀😁😂🤣😃😄😅😆😉😊😋😎",
                                        res.TextFormats[36], rectangle, res.GetColor(Color4.Blue),
                                        DrawTextOptions.EnableColorFont);

                        target.DrawText("FPS: " + timer.FramesPerSecond.ToString("F1"),
                                        bottomRightFont, rectangle, res.GetColor(Color4.Red));

                        target.DrawText("Hello World",
                                        bottomLeftFont, rectangle, res.GetColor(Color4.Purple));
                    }
                }
        }
Пример #15
0
        /// <summary>
        /// Draws the specified text using the format information provided by an <see cref="T:SharpDX.DirectWrite.TextFormat" /> object.
        /// </summary>
        /// <remarks>
        /// To create an <see cref="IDWriteTextFormat"/> object, create an <see cref="IDWriteFactory"/> and call its CreateTextFormat method.
        /// </remarks>
        /// <param name="text">A reference to an array of Unicode characters 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="defaultForegroundBrush">The brush used to paint the text. </param>
        /// <param name="options">A value that indicates whether the text should be snapped to pixel boundaries and whether the text should be clipped to the layout rectangle. The default value is <see cref="F:SharpDX.Direct2D1.DrawTextOptions.None" />, which indicates that text should be snapped to pixel boundaries and it should not be clipped to the layout rectangle. </param>
        /// <param name="measuringMode">A value that indicates how glyph metrics are used to measure text when it is formatted.  The default value is DWRITE_MEASURING_MODE_NATURAL.  </param>
        public void DrawText(string text, IDWriteTextFormat textFormat, RawRectangleF layoutRect, ID2D1Brush defaultForegroundBrush, DrawTextOptions options, MeasuringMode measuringMode)
        {
            Guard.NotNullOrEmpty(text, nameof(text));

            DrawText(text, text.Length, textFormat, layoutRect, defaultForegroundBrush, options, measuringMode);
        }
Пример #16
0
 /// <summary>
 /// Draws the specified text using the format information provided by an <see cref="T:SharpDX.DirectWrite.TextFormat" /> object.
 /// </summary>
 /// <remarks>
 /// To create an <see cref="IDWriteTextFormat"/> object, create an <see cref="IDWriteFactory"/> and call its CreateTextFormat method.
 /// </remarks>
 /// <param name="text">A reference to an array of Unicode characters 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="defaultForegroundBrush">The brush used to paint the text. </param>
 /// <param name="options">A value that indicates whether the text should be snapped to pixel boundaries and whether the text should be clipped to the layout rectangle. The default value is <see cref="F:SharpDX.Direct2D1.DrawTextOptions.None" />, which indicates that text should be snapped to pixel boundaries and it should not be clipped to the layout rectangle. </param>
 /// <param name="measuringMode">A value that indicates how glyph metrics are used to measure text when it is formatted.  The default value is DWRITE_MEASURING_MODE_NATURAL.  </param>
 public void DrawText(string text, IDWriteTextFormat textFormat, RectF layoutRect, ID2D1Brush defaultForegroundBrush, DrawTextOptions options, MeasuringMode measuringMode)
 {
     DrawText(text, text.Length, textFormat, layoutRect, defaultForegroundBrush, options, measuringMode);
 }
 internal DWriteTextFormat(IDWriteTextFormat handle)
 {
     this.handle = handle;
 }