コード例 #1
0
        public static Task Render(IElement element, IFrameContext context, RendererDirect2D render)
        {
            if (TransformHelper.IsHidden(element))
            {
                return(Task.CompletedTask);
            }

            using (TransformHelper.Create(render, element, context))
                using (LayerHelper.Create(render, element, context))
                {
                    var font      = element.GetFont(context);
                    var fill      = element.GetFill(context);
                    var fillBrush = render.CreateBrush(element, context, fill.Brush, fill.Opacity);

                    if (fillBrush == null)
                    {
                        return(Task.CompletedTask);
                    }

                    foreach (var text in element.Children.OfType <ITextContent>())
                    {
                        using (var textLayout = render.CreateTextLayout(font, text.Content, 999999))
                        {
                            var frameSize = context.Size;
                            var textSize  = new PxSize(textLayout.Metrics.WidthIncludingTrailingWhitespace, textLayout.Metrics.Height);
                            var scale     = CalcScale(frameSize, textSize);

                            var matrix = PxMatrix.Translation(
                                scale,
                                scale,
                                (frameSize.Width - (scale * textSize.Width)) / 2f,
                                (frameSize.Height - (scale * textSize.Height)) / 2f
                                );

                            using (TransformHelper.Create(render, matrix))
                            {
                                render.Target.DrawTextLayout(
                                    new DXM.RawVector2(0, 0),
                                    textLayout,
                                    fillBrush,
                                    D2D1.DrawTextOptions.EnableColorFont
                                    );
                            }
                        }
                    }
                    return(Task.CompletedTask);
                }
        }
コード例 #2
0
        public static Task Render(IElement element, IFrameContext context, RendererDirect2D render)
        {
            using (TransformHelper.Create(render, element, context))
                using (LayerHelper.Create(render, element, context))
                {
                    var x = 0f;
                    var y = 0f;

                    IBrush     brush         = null;
                    IBrush     sbrush        = null;
                    var        fillOpacity   = -1f;
                    D2D1.Brush fillBrush     = null;
                    D2D1.Brush strokeBrush   = null;
                    var        strokeOpacity = -1f;

                    foreach (var span in GetSpans(element, context))
                    {
                        if (span.X.HasValue)
                        {
                            x = span.X.Value;
                        }
                        if (span.Y.HasValue)
                        {
                            y = span.Y.Value;
                        }

                        var c       = span.Items.Count;
                        var layouts = new List <DW.TextLayout>(c);
                        var w       = 0f;
                        for (var i = 0; i < c; i++)
                        {
                            var ti = span.Items[i];

                            var l = render.CreateTextLayout(ti.Font, ti.Text, 9999999);
                            layouts.Add(l);
                            w += ti.Dx + l.Metrics.WidthIncludingTrailingWhitespace;
                        }

                        x -= span.TextAnchor == TextAnchor.Middle
                        ? w / 2
                        : span.TextAnchor == TextAnchor.End
                            ? w
                            : 0;

                        for (var i = 0; i < c; i++)
                        {
                            var ti = span.Items[i];
                            var l  = layouts[i];

                            if (brush != ti.Fill.Brush || !MathEx.NearEqual(fillOpacity, ti.Fill.Opacity))
                            {
                                fillBrush   = render.CreateBrush(element, context, ti.Fill.Brush, ti.Fill.Opacity);
                                brush       = ti.Fill.Brush;
                                fillOpacity = ti.Fill.Opacity;
                            }

                            if (sbrush != ti.Stroke.Brush || !MathEx.NearEqual(strokeOpacity, ti.Stroke.Opacity))
                            {
                                strokeBrush = render.CreateBrush(element, context, ti.Stroke.Brush, ti.Stroke.Opacity);
                                sbrush      = ti.Stroke.Brush;
                                fillOpacity = ti.Fill.Opacity;
                            }

                            x += ti.Dx;
                            y += ti.Dy;


                            if (fillBrush != null)
                            {
                                if (ti.Stroke.Brush != null)
                                {
                                    l.Draw(new CustomColorRenderer(render.Target, fillBrush, strokeBrush, ti.Stroke.Width), x,
                                           y - CalcBaselineOffset(l, ti.DominantBaseline));
                                }
                                else
                                {
                                    render.Target.DrawTextLayout(
                                        new DXM.RawVector2(x, y - CalcBaselineOffset(l, ti.DominantBaseline)),
                                        l,
                                        fillBrush,
                                        D2D1.DrawTextOptions.EnableColorFont
                                        );
                                }
                            }

                            x += l.Metrics.WidthIncludingTrailingWhitespace;

                            l.Dispose();
                        }
                    }

                    return(Task.CompletedTask);
                }
        }