Exemple #1
0
        internal static async Task Render(CompositionEngine compositionEngine, RenderTarget renderTarget, FrameworkElement rootElement, TextBlock textBlock)
        {
            using (var textFormat = new TextFormat(
                       compositionEngine.DWriteFactory,
                       textBlock.FontFamily.Source,
                       (float)textBlock.FontSize)
            {
                TextAlignment = textBlock.TextAlignment.ToSharpDX(),
                ParagraphAlignment = ParagraphAlignment.Near
            })
            {
                var rect = textBlock.GetBoundingRect(rootElement).ToSharpDX();
                // For some reason we need a bigger rect for the TextBlock rendering to fit in the same boundaries
                rect.Right++;
                rect.Bottom++;

                using (
                    var textBrush = await textBlock.Foreground.ToSharpDX(renderTarget, rect))
                {
                    if (textBrush == null)
                    {
                        return;
                    }

                    var layer = textBlock.CreateAndPushLayerIfNecessary(renderTarget, rootElement);

                    // You can render the bounding rectangle to debug composition
                    //renderTarget.DrawRectangle(
                    //    rect,
                    //    textBrush);
                    renderTarget.DrawText(
                        textBlock.Text,
                        textFormat,
                        rect,
                        textBrush);

                    if (layer != null)
                    {
                        renderTarget.PopLayer();
                        layer.Dispose();
                    }
                    //}
                }
            }
        }