Ejemplo n.º 1
0
        public override Result DrawGlyphRun(
            object clientDrawingContext,
            float baselineOriginX,
            float baselineOriginY,
            D2D1.MeasuringMode measuringMode,
            GlyphRun glyphRun,
            GlyphRunDescription glyphRunDescription,
            ComObject clientDrawingEffect
            )
        {
            var fHash     = FontHash(glyphRun.FontFace, glyphRun.FontSize);
            var positionX = (float)Math.Floor(baselineOriginX + 0.5f);
            var positionY = (float)Math.Floor(baselineOriginY + 0.5f);

            Color4     brushColor = Color4.White;
            TextShadow shadow     = new TextShadow();

            if (clientDrawingEffect != null && clientDrawingEffect is ColorDrawingEffect colorFx)
            {
                brushColor = colorFx.Color;
                shadow     = colorFx.Shadow;
            }

            for (int i = 0; i < glyphRun.Indices.Length; i++)
            {
                var glyph = GetGlyph(fHash, glyphRun.FontFace, glyphRun.Indices[i], glyphRun.FontSize);
                if (shadow.Enabled)
                {
                    Quads.Add(new DrawQuad()
                    {
                        Texture     = glyph.Texture,
                        Source      = glyph.Rectangle,
                        Destination = new Rectangle(
                            (int)(glyph.OffsetX + positionX + 2),
                            (int)(glyph.OffsetY + positionY + 2),
                            glyph.Rectangle.Width,
                            glyph.Rectangle.Height
                            ),
                        Color = shadow.Color
                    });
                }
                var q = new DrawQuad()
                {
                    Texture     = glyph.Texture,
                    Source      = glyph.Rectangle,
                    Destination = new Rectangle(
                        (int)(glyph.OffsetX + positionX),
                        (int)(glyph.OffsetY + positionY),
                        glyph.Rectangle.Width,
                        glyph.Rectangle.Height
                        ),
                    Color = brushColor
                };
                Quads.Add(q);
                positionX += glyphRun.Advances[i];
            }
            return(Result.Ok);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// <p> Draws a run of glyphs to a bitmap target at the specified position.</p>
        /// </summary>
        /// <param name="baselineOriginX"><dd>  <p> The horizontal position of the baseline origin, in DIPs, relative to the upper-left corner of the DIB.</p> </dd></param>
        /// <param name="baselineOriginY"><dd>  <p> The vertical position of the baseline origin, in DIPs, relative to the upper-left corner of the DIB.</p> </dd></param>
        /// <param name="measuringMode"><dd>  <p> The measuring method for glyphs in the run, used with the other properties to determine the rendering mode.</p> </dd></param>
        /// <param name="glyphRun"><dd>  <p> The structure containing the properties of the glyph run.</p> </dd></param>
        /// <param name="renderingParams"><dd>  <p> The object that controls rendering behavior.</p> </dd></param>
        /// <param name="textColor"><dd>  <p> The foreground color of the text.</p> </dd></param>
        /// <param name="blackBoxRect"><dd>  <p> The optional rectangle that receives the bounding box (in pixels not DIPs) of all the pixels affected by  drawing the glyph run. The black box rectangle may extend beyond the dimensions of the bitmap.</p> </dd></param>
        /// <returns><p>If this method succeeds, it returns <strong><see cref="SharpDX.Result.Ok"/></strong>. Otherwise, it returns an <strong><see cref="SharpDX.Result"/></strong> error code.</p></returns>
        /// <remarks>
        /// <p>You can use the <strong><see cref="SharpDX.DirectWrite.BitmapRenderTarget.DrawGlyphRun"/></strong> to render to a bitmap from a custom text renderer that you implement.  The custom text renderer should call this method from within the <strong><see cref="SharpDX.DirectWrite.TextRenderer.DrawGlyphRun"/></strong> callback method as shown in the following code.</p><pre>STDMETHODIMP GdiTextRenderer::DrawGlyphRun( __maybenull void* clientDrawingContext, FLOAT baselineOriginX, FLOAT baselineOriginY, <see cref="SharpDX.Direct2D1.MeasuringMode"/> measuringMode, __in <see cref="SharpDX.DirectWrite.GlyphRun"/> const* glyphRun, __in <see cref="SharpDX.DirectWrite.GlyphRunDescription"/> const* glyphRunDescription, <see cref="SharpDX.ComObject"/>* clientDrawingEffect )
        /// { <see cref="SharpDX.Result"/> hr = <see cref="SharpDX.Result.Ok"/>; // Pass on the drawing call to the render target to do the real work. <see cref="SharpDX.Mathematics.Interop.RawRectangle"/> dirtyRect = {0}; hr = pRenderTarget_-&gt;DrawGlyphRun( baselineOriginX, baselineOriginY, measuringMode, glyphRun, pRenderingParams_, RGB(0,200,255), &amp;dirtyRect ); return hr;
        /// }
        /// </pre><p>The <em>baselineOriginX</em>, <em>baslineOriginY</em>, <em>measuringMethod</em>, and <em>glyphRun</em> parameters are provided (as arguments) when the callback method is invoked.  The <em>renderingParams</em>, <em>textColor</em> and <em>blackBoxRect</em> are not.</p><p>Default rendering params can be retrieved by using the <strong><see cref="SharpDX.DirectWrite.Factory.CreateMonitorRenderingParams"/></strong> method.</p><p></p>
        /// </remarks>
        /// <include file='Documentation\CodeComments.xml' path="/comments/comment[@id='IDWriteBitmapRenderTarget::DrawGlyphRun']/*"/>
        /// <msdn-id>dd368167</msdn-id>
        /// <unmanaged>HRESULT IDWriteBitmapRenderTarget::DrawGlyphRun([In] float baselineOriginX,[In] float baselineOriginY,[In] DWRITE_MEASURING_MODE measuringMode,[In] const DWRITE_GLYPH_RUN* glyphRun,[In] IDWriteRenderingParams* renderingParams,[In] int textColor,[Out, Optional] RECT* blackBoxRect)</unmanaged>
        /// <unmanaged-short>IDWriteBitmapRenderTarget::DrawGlyphRun</unmanaged-short>
        public void DrawGlyphRun(float baselineOriginX,
                                 float baselineOriginY,
                                 SharpDX.Direct2D1.MeasuringMode measuringMode,
                                 SharpDX.DirectWrite.GlyphRun glyphRun,
                                 SharpDX.DirectWrite.RenderingParams renderingParams,
                                 RawColorBGRA textColor,
                                 out SharpDX.Mathematics.Interop.RawRectangle blackBoxRect)
        {
            int colorRgb = (textColor.R) | (textColor.G << 8) | (textColor.B << 16);

            DrawGlyphRun(baselineOriginX, baselineOriginY, measuringMode, glyphRun, renderingParams, colorRgb, out blackBoxRect);
        }
Ejemplo n.º 3
0
        public override Result DrawGlyphRun(object clientDrawingContext, float baselineOriginX, float baselineOriginY,
                                            D2D1.MeasuringMode measuringMode, DW.GlyphRun glyphRun, DW.GlyphRunDescription glyphRunDescription,
                                            ComObject clientDrawingEffect)
        {
            using (var pathGeometry = new D2D1.PathGeometry(_renderTarget.Factory))
            {
                using (var geometrySink = pathGeometry.Open())
                    using (var fontFace = glyphRun.FontFace)
                    {
                        if (glyphRun.Indices.Length > 0)
                        {
                            fontFace.GetGlyphRunOutline(
                                glyphRun.FontSize,
                                glyphRun.Indices,
                                glyphRun.Advances,
                                glyphRun.Offsets,
                                glyphRun.Indices.Length,
                                glyphRun.IsSideways,
                                glyphRun.BidiLevel % 2 != 0,
                                geometrySink
                                );
                        }
                        geometrySink.Close();
                    }

                var matrix = new Matrix3x2()
                {
                    M11 = 1,
                    M12 = 0,
                    M21 = 0,
                    M22 = 1,
                    M31 = baselineOriginX,
                    M32 = baselineOriginY
                };

                var sw = _renderTarget.StrokeWidth;
                using (var transformedGeometry =
                           new D2D1.TransformedGeometry(_renderTarget.Factory, pathGeometry, matrix))
                {
                    _renderTarget.StrokeWidth = _strokeWidth;
                    _renderTarget.DrawGeometry(transformedGeometry, _strokeBrush);
                    _renderTarget.FillGeometry(transformedGeometry, _fillBrush);
                }
                _renderTarget.StrokeWidth = sw;
            }

            return(SharpDX.Result.Ok);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Draws the given text on the screen.
        /// </summary>
        /// <param name="textToDraw">The text to draw.</param>
        /// <param name="textFormat">The TextFormat to be used.</param>
        /// <param name="targetRectangle">The target rectangle.</param>
        /// <param name="brush">The brush.</param>
        /// <param name="drawOptions">Some draw options to be passed to Direct2D.</param>
        /// <param name="measuringMode">Sets the measuring mode to be passed to Direct2D.</param>
        public void DrawText(
            string textToDraw, TextFormatResource textFormat, RectangleF targetRectangle, BrushResource brush,
            DrawTextOptions drawOptions = DrawTextOptions.None,
            MeasuringMode measuringMode = MeasuringMode.Natural)
        {
            if (m_renderTarget == null)
            {
                return;
            }

            textToDraw.EnsureNotNull(nameof(textToDraw));
            targetRectangle.EnsureNotEmpty(nameof(targetRectangle));
            brush.EnsureNotNull(nameof(brush));

            D2D.DrawTextOptions drawOptionsD2D   = (D2D.DrawTextOptions)drawOptions;
            D2D.MeasuringMode   measuringModeD2D = (D2D.MeasuringMode)measuringMode;

            m_renderTarget.DrawText(
                textToDraw,
                textFormat.GetTextFormat(m_device),
                targetRectangle.ToDXRectangle(),
                brush.GetBrush(m_device),
                drawOptionsD2D);
        }
Ejemplo n.º 5
0
            public override SharpDX.Result DrawGlyphRun(object clientDrawingContext, float baselineOriginX, float baselineOriginY, D2D1.MeasuringMode measuringMode,
                                                        DW.GlyphRun glyphRun, DW.GlyphRunDescription glyphRunDescription, ComObject effect)
            {
                var sb = _defaultBrush;

                if (effect != null && effect is D2D1.SolidColorBrush solidColorBrush)
                {
                    sb = solidColorBrush;
                }
                try
                {
                    _renderTarget.DrawGlyphRun(new Vector2(baselineOriginX, baselineOriginY), glyphRun, sb, measuringMode);
                    return(SharpDX.Result.Ok);
                }
                catch
                {
                    return(SharpDX.Result.Fail);
                }
            }
Ejemplo n.º 6
0
        public Result DrawGlyphRun(object clientDrawingContext, float baselineOriginX, float baselineOriginY, D2D.MeasuringMode measuringMode, DW.GlyphRun glyphRun, DW.GlyphRunDescription glyphRunDescription, ComObject clientDrawingEffect)
        {
            D2D.RenderTarget render = clientDrawingContext as D2D.RenderTarget;
            if (render == null)
            {
                return(SharpDX.Result.Ok);
            }

            D2D.SolidColorBrush foreBrush = this.factory.GetSolidColorBrush(this.DefaultFore);
            bool isDrawGlyphRun           = true;

            if (clientDrawingEffect != null)
            {
                var drawingForeBrush = clientDrawingEffect as D2D.SolidColorBrush;
                var selectedEffect   = clientDrawingEffect as SelectedEffect;
                var drawingEffect    = clientDrawingEffect as DrawingEffect;

                if (drawingForeBrush != null)
                {
                    foreBrush = drawingForeBrush;
                }
                else if (selectedEffect != null)
                {
                    foreBrush = this.factory.GetSolidColorBrush(selectedEffect.Fore);
                }
                else if (drawingEffect != null)
                {
                    if (drawingEffect.Stroke == HilightType.Url)
                    {
                        foreBrush = this.factory.GetSolidColorBrush(drawingEffect.Fore);
                    }
                }
            }

            if (isDrawGlyphRun)
            {
                render.DrawGlyphRun(new Vector2(baselineOriginX, baselineOriginY),
                                    glyphRun,
                                    foreBrush,
                                    measuringMode);
            }

            return(SharpDX.Result.Ok);
        }