Пример #1
0
        public override void Render(DrawingContext context)
        {
            var y = 0.0;

            for (var i = (int)_offset.Y; i < itemCount; ++i)
            {
                using (var line = new FormattedText(
                    "Item " + (i + 1),
                    TextBlock.GetFontFamily(this),
                    TextBlock.GetFontSize(this),
                    TextBlock.GetFontStyle(this),
                    TextAlignment.Left,
                    TextBlock.GetFontWeight(this)))
                {
                    context.DrawText(Brushes.Black, new Point(-_offset.X, y), line);
                    y += _lineSize.Height;
                }
            }
        }
Пример #2
0
        public static AM.FormattedText ToFormattedText(this Paint paint, string text)
        {
            var typeface      = paint.Typeface?.ToTypeface();
            var textAlignment = paint.TextAlign.ToTextAlignment();
            var fontSize      = paint.TextSize;
            // TODO: paint.TextEncoding
            // TODO: paint.LcdRenderText
            // TODO: paint.SubpixelText

            var ft = new AM.FormattedText()
            {
                Text          = text,
                Typeface      = typeface,
                FontSize      = fontSize,
                TextAlignment = textAlignment,
                TextWrapping  = AM.TextWrapping.NoWrap
            };

            return(ft);
        }
Пример #3
0
        public override void Render(DrawingContext context, TextInfo textInfo)
        {
            if (textView.TextDocument != null)
            {
                Width = textInfo.CharWidth * textInfo.NumLines.ToString().Length + 12;

                if (textView != null && textView.VisualLines.Count > 0)
                {
                    var firstLine = textView.VisualLines.First().DocumentLine.LineNumber;
                    var lastLine = textView.VisualLines.Last().DocumentLine.LineNumber;

                    DocumentLine currentLine = null;

                    if (textView.SelectionStart == textView.SelectionEnd && textView.CaretIndex >= 0 && textView.CaretIndex <= textView.TextDocument.TextLength)
                    {
                        currentLine = textView.TextDocument.GetLineByOffset(textView.CaretIndex);
                    }

                    for (var i = 0; i < textInfo.NumLines && i + firstLine <= textView.TextDocument.LineCount && i + firstLine <= lastLine; i++)
                    {
                        using (
                            var formattedText = new FormattedText((i + firstLine).ToString(), "Consolas", textView.FontSize, FontStyle.Normal,
                                TextAlignment.Right, FontWeight.Normal)
                            { Constraint = new Size(Width, Bounds.Height) })
                        {
                            IBrush textColor = foreground;

                            if (currentLine != null)
                            {
                                if ((i + firstLine) == currentLine.LineNumber)
                                {
                                    textColor = currentLineForeground;
                                }
                            }

                            context.DrawText(textColor, new Point(-8, textInfo.LineHeight * i), formattedText);
                        }
                    }                    
                }
            }
        }
Пример #4
0
        /// <inheritdoc/>
        public override void Draw(object dc, TextShape text, double dx, double dy, object db, object r)
        {
            var _gfx = dc as AM.DrawingContext;

            var properties = (ImmutableArray <Property>)db;
            var record     = (Record)r;
            var tbind      = text.BindText(properties, record);

            if (string.IsNullOrEmpty(tbind))
            {
                return;
            }

            AM.IBrush brush = ToBrush(text.Style.Stroke);

            var fontStyle  = AM.FontStyle.Normal;
            var fontWeight = AM.FontWeight.Normal;

            //var fontDecoration = PM.FontDecoration.None;

            if (text.Style.TextStyle.FontStyle != null)
            {
                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Italic))
                {
                    fontStyle |= AM.FontStyle.Italic;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Bold))
                {
                    fontWeight |= AM.FontWeight.Bold;
                }

                // TODO: Implement font decoration after Avalonia adds support.

                /*
                 * if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Underline))
                 * {
                 *  fontDecoration |= PM.FontDecoration.Underline;
                 * }
                 *
                 * if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Strikeout))
                 * {
                 *  fontDecoration |= PM.FontDecoration.Strikethrough;
                 * }
                 */
            }

            if (text.Style.TextStyle.FontSize >= 0.0)
            {
                var tf = new AM.Typeface(
                    text.Style.TextStyle.FontName,
                    text.Style.TextStyle.FontSize * _textScaleFactor,
                    fontStyle,
                    fontWeight);

                var ft = new AM.FormattedText()
                {
                    Typeface      = tf,
                    Text          = tbind,
                    TextAlignment = AM.TextAlignment.Left,
                    Wrapping      = AM.TextWrapping.NoWrap
                };

                var rect   = CreateRect(text.TopLeft, text.BottomRight, dx, dy);
                var size   = ft.Measure();
                var origin = GetTextOrigin(text.Style, ref rect, ref size);

                _gfx.DrawText(brush, origin, ft);
            }
        }
Пример #5
0
        /// <summary>
        /// Invalidates <see cref="FormattedText"/>.
        /// </summary>
        protected void InvalidateFormattedText()
        {
            if (_formattedText != null)
            {
                _constraint = _formattedText.Constraint;
                _formattedText.Dispose();
                _formattedText = null;
            }

            InvalidateMeasure();
        }
Пример #6
0
 /// <summary>
 /// Creates the <see cref="FormattedText"/> used to render the text.
 /// </summary>
 /// <param name="constraint">The constraint of the text.</param>
 /// <returns>A <see cref="FormattedText"/> object.</returns>
 protected virtual FormattedText CreateFormattedText(Size constraint)
 {
     var result = new FormattedText(
         Text ?? string.Empty,
         FontFamily,
         FontSize,
         FontStyle,
         TextAlignment,
         FontWeight,
         TextWrapping);
     result.Constraint = constraint;
     return result;
 }
Пример #7
0
        protected override Size MeasureOverride(Size constraint)
        {
            if (this.formattedText == null)
            {
                this.formattedText = this.CreateFormattedText();
            }

            return new Size(this.formattedText.Width, this.formattedText.Height);
        }
Пример #8
0
 public void DrawText(IBrush foreground, Point origin, FormattedText text)
 {
     using (var paint = CreatePaint(foreground, text.Measure()))
     {
         var textImpl = text.PlatformImpl as FormattedTextImpl;
         textImpl.Draw(this, Canvas, origin.ToSKPoint(), paint);
     }
 }
Пример #9
0
        protected override Size MeasureOverride(Size availableSize)
        {
            var text = Text;

            if (!string.IsNullOrEmpty(text))
            {
                return base.MeasureOverride(availableSize);
            }
            else
            {
                // TODO: Pretty sure that measuring "X" isn't the right way to do this...
                using (var formattedText = new FormattedText(
                    "X",
                    FontFamily,
                    FontSize,
                    FontStyle,
                    TextAlignment,
                    FontWeight))
                {
                    return formattedText.Measure();
                }
            }
        }
Пример #10
0
 /// <summary>
 /// Draws text.
 /// </summary>
 /// <param name="foreground">The foreground brush.</param>
 /// <param name="origin">The upper-left corner of the text.</param>
 /// <param name="text">The text.</param>
 public void DrawText(IBrush foreground, Point origin, FormattedText text)
     => _impl.DrawText(foreground, origin, text);
Пример #11
0
        /// <inheritdoc/>
        public override void Draw(object dc, XText text, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _gfx = dc as AM.DrawingContext;

            var tbind = text.BindText(db, r);
            if (string.IsNullOrEmpty(tbind))
                return;

            AM.IBrush brush = ToBrush(text.Style.Stroke);

            var fontStyle = AM.FontStyle.Normal;
            var fontWeight = AM.FontWeight.Normal;
            //var fontDecoration = PM.FontDecoration.None;

            if (text.Style.TextStyle.FontStyle != null)
            {
                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Italic))
                {
                    fontStyle |= AM.FontStyle.Italic;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Bold))
                {
                    fontWeight |= AM.FontWeight.Bold;
                }

                // TODO: Implement font decoration after Avalonia adds support.
                /*
                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Underline))
                {
                    fontDecoration |= PM.FontDecoration.Underline;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Strikeout))
                {
                    fontDecoration |= PM.FontDecoration.Strikethrough;
                }
                */
            }

            if (text.Style.TextStyle.FontSize >= 0.0)
            {
                var ft = new AM.FormattedText(
                    tbind,
                    text.Style.TextStyle.FontName,
                    text.Style.TextStyle.FontSize * _textScaleFactor,
                    fontStyle,
                    AM.TextAlignment.Left,
                    fontWeight);

                var rect = CreateRect(text.TopLeft, text.BottomRight, dx, dy);
                var size = ft.Measure();
                var origin = GetTextOrigin(text.Style, ref rect, ref size);

                _gfx.DrawText(brush, origin, ft);

                ft.Dispose();
            }
        }
Пример #12
0
 void UpdateText(int fps)
 {
     _text?.Dispose();
     _text = new FormattedText("FPS: " + fps, "Arial", 15, FontStyle.Normal, TextAlignment.Left,
         FontWeight.Normal);
 }
Пример #13
0
 public void InvalidateText()
 {
     this.formattedText = null;
     this.InvalidateMeasure();
 }
 public override void DrawText(FormattedText formattedText, Point origin)
 {
     this.target.DrawText(
         formattedText.Text ?? string.Empty,
         ((Direct2D1FormattedText)formattedText.PlatformImpl).DirectWriteTextLayout,
         new RectangleF((float)origin.X, (float)origin.Y, float.PositiveInfinity, float.PositiveInfinity),
         formattedText.Foreground.ToSharpDX(this.target),
         DrawTextOptions.None);
 }
Пример #15
0
 protected override Size MeasureOverride(Size availableSize)
 {
     using (var line = new FormattedText(
         "Item 100",
         TextBlock.GetFontFamily(this),
         TextBlock.GetFontSize(this),
         TextBlock.GetFontStyle(this),
         TextAlignment.Left,
         TextBlock.GetFontWeight(this)))
     {
         line.Constraint = availableSize;
         _lineSize = line.Measure();
         return new Size(_lineSize.Width, _lineSize.Height * itemCount);
     }
 }
Пример #16
0
        /// <summary>
        /// Draws text.
        /// </summary>
        /// <param name="foreground">The foreground brush.</param>
        /// <param name="origin">The upper-left corner of the text.</param>
        /// <param name="text">The text.</param>
        public void DrawText(IBrush foreground, Point origin, FormattedText text)
        {
            var layout = ((FormattedTextImpl)text.PlatformImpl).Layout;
            _context.MoveTo(origin.X, origin.Y);

            using (var b = SetBrush(foreground, new Size(0, 0))) 
            {
                Pango.CairoHelper.ShowLayout(_context, layout);
            }
        }
Пример #17
0
 /// <summary>
 /// Creates the <see cref="FormattedText"/> used to render the text.
 /// </summary>
 /// <param name="constraint">The constraint of the text.</param>
 /// <returns>A <see cref="FormattedText"/> object.</returns>
 protected override FormattedText CreateFormattedText(Size constraint)
 {
     var result = new FormattedText(
         StripAccessKey(Text),
         FontFamily,
         FontSize,
         FontStyle,
         TextAlignment,
         FontWeight);
     result.Constraint = constraint;
     return result;
 }
Пример #18
0
 public abstract void DrawText(FormattedText formattedText, Point origin);
Пример #19
0
        protected void UpdateTextGeometry()
        {
            BoundText = Text.GetProperty(nameof(TextShapeViewModel.Text)) is string boundText ? boundText : Text.Text;

            if (BoundText is null)
            {
                return;
            }

            if (Style.TextStyle.FontSize < 0.0)
            {
                return;
            }

            var fontStyle  = AM.FontStyle.Normal;
            var fontWeight = AM.FontWeight.Normal;

            if (Style.TextStyle.FontStyle.HasFlag(FontStyleFlags.Italic))
            {
                fontStyle |= AM.FontStyle.Italic;
            }

            if (Style.TextStyle.FontStyle.HasFlag(FontStyleFlags.Bold))
            {
                fontWeight |= AM.FontWeight.Bold;
            }

            // TODO: Cache Typeface
            // TODO: Cache FormattedText

            Typeface = new AM.Typeface(Style.TextStyle.FontName, fontStyle, fontWeight);

            var textAlignment = Style.TextStyle.TextHAlignment switch
            {
                TextHAlignment.Right => AM.TextAlignment.Right,
                TextHAlignment.Center => AM.TextAlignment.Center,
                _ => AM.TextAlignment.Left,
            };

            FormattedText = new AM.FormattedText()
            {
                Typeface      = Typeface,
                Text          = BoundText,
                TextAlignment = textAlignment,
                TextWrapping  = AM.TextWrapping.NoWrap,
                FontSize      = Style.TextStyle.FontSize,
                Constraint    = Rect.Size
            };

            var size = FormattedText.Bounds.Size;
            var rect = Rect;

            // NOTE: Using AM.TextAlignment
            var originX = rect.X;
            //var originX = Style.TextStyle.TextHAlignment switch
            //{
            //    TextHAlignment.Left => rect.X,
            //    TextHAlignment.Right => rect.Right - size.Width,
            //    _ => (rect.Left + rect.Width / 2.0) - (size.Width / 2.0)
            //};

            var originY = Style.TextStyle.TextVAlignment switch
            {
                TextVAlignment.Top => rect.Y,
                TextVAlignment.Bottom => rect.Bottom - size.Height,
                _ => (rect.Bottom - rect.Height / 2f) - (size.Height / 2f)
            };

            Origin = new A.Point(originX, originY);
        }
Пример #20
0
        /// <summary>
        /// Draws text.
        /// </summary>
        /// <param name="foreground">The foreground brush.</param>
        /// <param name="origin">The upper-left corner of the text.</param>
        /// <param name="text">The text.</param>
        public void DrawText(IBrush foreground, Point origin, FormattedText text)
        {
            if (!string.IsNullOrEmpty(text.Text))
            {
                var impl = (FormattedTextImpl)text.PlatformImpl;

                using (var brush = CreateBrush(foreground, impl.Measure()))
                using (var renderer = new AvaloniaTextRenderer(this, _renderTarget, brush.PlatformBrush))
                {
                    if (brush.PlatformBrush != null)
                    {
                        impl.TextLayout.Draw(renderer, (float)origin.X, (float)origin.Y);
                    }
                }
            }
        }
Пример #21
0
        protected internal override void OnRender(DrawingContext drawingContext)
        {
            Rect rect = new Rect(new Size(this.ActualWidth, this.ActualHeight));

            if (this.formattedText == null)
            {
                this.formattedText = this.CreateFormattedText();
            }

            drawingContext.DrawRectangle(
                this.Background,
                null,
                rect);

            drawingContext.DrawText(this.formattedText, new Point());
        }
Пример #22
0
 /// <summary>
 /// Draws text.
 /// </summary>
 /// <param name="foreground">The foreground brush.</param>
 /// <param name="origin">The upper-left corner of the text.</param>
 /// <param name="text">The text.</param>
 public void DrawText(IBrush foreground, Point origin, FormattedText text)
 => _impl.DrawText(foreground, origin, text);