Draw() public method

Draws text using the specified client drawing context.
To draw text with this method, a textLayout object needs to be created by the application using SharpDX.DirectWrite.Factory.CreateTextLayout. After the textLayout object is obtained, the application calls the IDWriteTextLayout::Draw method to draw the text, decorations, and inline objects. The actual drawing is done through the callback interface passed in as the textRenderer argument; there, the corresponding DrawGlyphRun API is called.
public Draw ( TextRenderer renderer, float originX, float originY ) : void
renderer TextRenderer Pointer to the set of callback functions used to draw parts of a text string.
originX float The x-coordinate of the layout's left side.
originY float The y-coordinate of the layout's top side.
return void
Beispiel #1
0
        protected void DrawHintAndInput()
        {
            if (!Paradigm.Config.Gui.InputBarVisibility)
            {
                return;
            }

            /* Draw Hint */
            if (HintText != null)
            {
                SharedBrush.Color = new Color(ForegroundColor.R, ForegroundColor.G, ForegroundColor.B, 0.7F);
                RenderTarget.DrawText(HintText, InputTextFormat, new RawRectangleF(10, 0, Width - 10, InputBoxHeight / 2),
                                      SharedBrush, D2D1.DrawTextOptions.None);
            }

            /* Draw user input text */
            if (InputTextLayout != null)
            {
                var rect = HintText == null
                    ? new RawRectangleF(10, 5, Width - 10, InputBoxHeight - 5)
                    : new RawRectangleF(10, InputBoxHeight / 2, Width - 10, InputBoxHeight);
                InputTextLayout.MaxWidth  = rect.Right - rect.Left;
                InputTextLayout.MaxHeight = rect.Bottom - rect.Top;
                InputTextLayout.Draw(_customColorRenderer, rect.Left, rect.Top);
            }

            /* Draw Input Box Bottom Line */
            SharedBrush.Color = ForegroundColor;
            RenderTarget.DrawLine(new RawVector2(10, InputBoxHeight + 2), new RawVector2(Width - 10, InputBoxHeight + 2), SharedBrush, 2);
        }
Beispiel #2
0
 public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null)
 {
     try
     {
         if (brush == null)
         {
             brush = new SolidBrush(Color.FromRGB(0, 0, 0, 0));
         }
         var layout = new DW.TextLayout(factories.DWFactory, text, WinUniversalPlatform.GetTextFormat(factories, font), (float)frame.Width, (float)frame.Height);
         var h      = layout.Metrics.Height + layout.OverhangMetrics.Top;
         var point  = (frame.TopLeft - h * Point.OneY).ToVector2();
         if (pen == null)
         {
             renderTarget.DrawTextLayout(point, layout, GetBrush(frame, brush));
         }
         else
         {
             using (var pentr = new TextRendererWithPen(factories.D2DFactory, renderTarget))
             {
                 pentr.PenBrush  = GetBrush(pen);
                 pentr.PenWidth  = (float)pen.Width;
                 pentr.PenStyle  = GetStrokeStyle(pen);
                 pentr.FontBrush = GetBrush(frame, brush);
                 layout.Draw(pentr, point.X, point.Y);
             }
         }
     }
     catch
     {
         System.Diagnostics.Debug.WriteLine("drawtexterror");
     }
 }
Beispiel #3
0
        /// <summary>
        /// Builds the text geometry for the given string.
        /// </summary>
        /// <param name="stringToBuild">The string to build within the geometry.</param>
        /// <param name="geometryOptions">Some configuration for geometry creation.</param>
        public void BuildTextGeometry(string stringToBuild, TextGeometryOptions geometryOptions)
        {
            DWrite.Factory writeFactory = GraphicsCore.Current.FactoryDWrite;

            //TODO: Cache font objects

            //Get DirectWrite font weight
            DWrite.FontWeight fontWeight = DWrite.FontWeight.Normal;
            switch (geometryOptions.FontWeight)
            {
            case FontGeometryWeight.Bold:
                fontWeight = DWrite.FontWeight.Bold;
                break;

            default:
                fontWeight = DWrite.FontWeight.Normal;
                break;
            }

            //Get DirectWrite font style
            DWrite.FontStyle fontStyle = DWrite.FontStyle.Normal;
            switch (geometryOptions.FontStyle)
            {
            case FontGeometryStyle.Italic:
                fontStyle = DWrite.FontStyle.Italic;
                break;

            case FontGeometryStyle.Oblique:
                fontStyle = DWrite.FontStyle.Oblique;
                break;

            default:
                fontStyle = DWrite.FontStyle.Normal;
                break;
            }

            //Create the text layout object
            try
            {
                DWrite.TextLayout textLayout = new DWrite.TextLayout(
                    writeFactory, stringToBuild,
                    new DWrite.TextFormat(
                        writeFactory, geometryOptions.FontFamily, fontWeight, fontStyle, geometryOptions.FontSize),
                    float.MaxValue, float.MaxValue, 1f, true);

                //Render the text using the vertex structure text renderer
                using (VertexStructureTextRenderer textRenderer = new VertexStructureTextRenderer(this, geometryOptions))
                {
                    textLayout.Draw(textRenderer, 0f, 0f);
                }
            }
            catch (SharpDX.SharpDXException)
            {
                //TODO: Display some error
            }
        }
 private void DrawText(string text, float locX, float locY, D2D1.Brush fg, D2D1.Brush bg, string font = "", float size = 0)
 {
     using (TextFormat = new DW.TextFormat(DWFactory, font == "" ? Config.CooldownBarTextFont : font, size == 0 ? Config.CooldownBarTextFontSize : size)) {
         TextFormat.WordWrapping = DW.WordWrapping.NoWrap;
         using (DW.TextLayout TextLayout = new DW.TextLayout(DWFactory, text, TextFormat, 500, 500)) {
             using (TextBrush = new TextBrush(fg, bg)) {
                 using (TextRenderer = new TextRenderer(Render, TextBrush)) {
                     TextLayout.SetDrawingEffect(TextBrush, new DW.TextRange(10, 20));
                     TextLayout.Draw(TextRenderer, locX, locY);
                 }
             }
         }
     }
 }
Beispiel #5
0
        static void Main(string[] args)
        {
            mainForm = new RenderForm("Advanced Text rendering demo");

            d2dFactory = new D2DFactory();
            dwFactory = new DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared);
            
            textRenderer = new CustomColorRenderer();

            CreateResources();

            var bgcolor = new Color4(0.1f,0.1f,0.1f,1.0f);

            //This is the offset where we start our text layout
            Vector2 offset = new Vector2(202.0f,250.0f);

            textFormat = new TextFormat(dwFactory, "Arial", FontWeight.Regular, FontStyle.Normal, 16.0f);
            textLayout = new TextLayout(dwFactory, introText, textFormat, 300.0f, 200.0f);

            //Apply various modifications to text
            textLayout.SetUnderline(true, new TextRange(0, 5));
            textLayout.SetDrawingEffect(greenBrush, new TextRange(10, 20));
            textLayout.SetFontSize(24.0f, new TextRange(6, 4));
            textLayout.SetFontFamilyName("Comic Sans MS", new TextRange(11,7));

            //Measure full layout
            var textSize = textLayout.Metrics;
            fullTextBackground = new RectangleF(textSize.Left + offset.X, textSize.Top + offset.Y, textSize.Width, textSize.Height);

            //Measure text to apply background to
            var metrics = textLayout.HitTestTextRange(53, 4, 0.0f, 0.0f)[0];
            textRegionRect = new RectangleF(metrics.Left + offset.X, metrics.Top + offset.Y, metrics.Width, metrics.Height);

            //Assign render target and brush to our custom renderer
            textRenderer.AssignResources(renderTarget, defaultBrush);

            RenderLoop.Run(mainForm, () =>
            {
                renderTarget.BeginDraw();
                renderTarget.Clear(bgcolor);

                renderTarget.FillRectangle(fullTextBackground, backgroundBrush);

                renderTarget.FillRectangle(textRegionRect, redBrush);

                textLayout.Draw(textRenderer, offset.X, offset.Y);

                try
                {
                    renderTarget.EndDraw();
                }
                catch
                {
                    CreateResources();
                }
            });

            d2dFactory.Dispose();
            dwFactory.Dispose();
            renderTarget.Dispose();
        }
Beispiel #6
0
        protected override DX11VertexGeometry GetGeom(DX11RenderContext device, int slice)
        {
            if (d2dFactory == null)
            {
                d2dFactory = new D2DFactory();
                dwFactory = new DWriteFactory(SharpDX.DirectWrite.FactoryType.Shared);
            }

            TextFormat fmt = new TextFormat(dwFactory, this.FFontInput[slice].Name, FFontSize[slice]);

            TextLayout tl = new TextLayout(dwFactory, FText[slice], fmt, 0.0f, 32.0f);
            tl.WordWrapping = WordWrapping.NoWrap;
            tl.TextAlignment = FHAlignment[slice];
            tl.ParagraphAlignment = FVAlignment[slice];

            OutlineRenderer renderer = new OutlineRenderer(d2dFactory);
            Extruder ex = new Extruder(d2dFactory);

            tl.Draw(renderer, 0.0f, 0.0f);

            var result = ex.GetVertices(renderer.GetGeometry(), this.FExtrude[slice]);

            Vector3 min = new Vector3(float.MaxValue);
            Vector3 max = new Vector3(float.MinValue);

            result.ForEach(pn =>
            {
                min.X = pn.Position.X < min.X ? pn.Position.X : min.X;
                min.Y = pn.Position.Y < min.Y ? pn.Position.Y : min.Y;
                min.Z = pn.Position.Z < min.Z ? pn.Position.Z : min.Z;

                max.X = pn.Position.X > max.X ? pn.Position.X : max.X;
                max.Y = pn.Position.Y > max.Y ? pn.Position.Y : max.Y;
                max.Z = pn.Position.Z > max.Z ? pn.Position.Z : max.Z;
            });

            SlimDX.DataStream ds = new SlimDX.DataStream(result.Count * Pos3Norm3VertexSDX.VertexSize, true, true);
            ds.Position = 0;

            ds.WriteRange(result.ToArray());

            ds.Position = 0;

            var vbuffer = new SlimDX.Direct3D11.Buffer(device.Device, ds, new SlimDX.Direct3D11.BufferDescription()
            {
                BindFlags = SlimDX.Direct3D11.BindFlags.VertexBuffer,
                CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None,
                OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.None,
                SizeInBytes = (int)ds.Length,
                Usage = SlimDX.Direct3D11.ResourceUsage.Default
            });

            ds.Dispose();

            DX11VertexGeometry vg = new DX11VertexGeometry(device);
            vg.InputLayout = Pos3Norm3VertexSDX.Layout;
            vg.Topology = SlimDX.Direct3D11.PrimitiveTopology.TriangleList;
            vg.VertexBuffer = vbuffer;
            vg.VertexSize = Pos3Norm3VertexSDX.VertexSize;
            vg.VerticesCount = result.Count;
            vg.HasBoundingBox = true;
            vg.BoundingBox = new SlimDX.BoundingBox(new SlimDX.Vector3(min.X, min.Y, min.Z), new SlimDX.Vector3(max.X, max.Y, max.Z));

            return vg;
        }