Ejemplo n.º 1
0
        private void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            EnsureLayout(sender);

            if (ShowLayoutRectangles)
            {
                foreach (var layoutRectangle in layoutRectangles)
                {
                    args.DrawingSession.DrawRectangle(layoutRectangle, Colors.Gray);
                }
            }

            if (ShowSelectionBox)
            {
                args.DrawingSession.DrawRectangle(layoutBox, Colors.LightGray, 2.0f, dashedStroke);
            }

            var brush = new CanvasSolidColorBrush(sender, Colors.LightSkyBlue);

            foreach (LayoutBox l in layoutBoxes)
            {
                if (l.GlyphRuns.Count > 0)
                {
                    float layoutAdvance = 0;
                    foreach (var g in l.GlyphRuns)
                    {
                        layoutAdvance += g.GetAdvance();
                    }

                    float x = (float)l.Rectangle.Left;


                    if (CurrentTextDirection == TextDirection.RightToLeft)
                    {
                        x = (float)l.Rectangle.Right - layoutAdvance;
                    }

                    int[] bidiOrdering = l.ProduceBidiOrdering();

                    foreach (int glyphRunIndex in bidiOrdering)
                    {
                        GlyphRun g = l.GlyphRuns[glyphRunIndex];

                        //
                        // The Arabic test string contains control characters. A typical text renderer will just not draw these.
                        //
                        if (g.FormattingSpan.Script.Shape == CanvasScriptShape.NoVisual)
                        {
                            continue;
                        }

                        if (g.Glyphs.Count > 0)
                        {
                            float advance = g.GetAdvance();

                            Vector2 position;
                            position.X = x;
                            position.Y = (float)l.Rectangle.Bottom;

                            if (g.FormattingSpan.BidiLevel % 2 != 0)
                            {
                                position.X += advance;
                            }

                            args.DrawingSession.DrawGlyphRun(
                                position,
                                g.FormattingSpan.FontFace,
                                g.FormattingSpan.FontSize,
                                g.Glyphs.ToArray(),
                                false, // isSideways
                                g.FormattingSpan.BidiLevel,
                                brush);

                            x += advance;
                        }
                    }
                }
            }
        }