Text shape.
Inheritance: Core2D.Shape.BaseShape
コード例 #1
0
ファイル: TextSelection.cs プロジェクト: Core2D/Core2D
 /// <summary>
 /// Initialize new instance of <see cref="TextSelection"/> class.
 /// </summary>
 /// <param name="layer">The selection shapes layer.</param>
 /// <param name="shape">The selected shape.</param>
 /// <param name="style">The selection shapes style.</param>
 /// <param name="point">The selection point shape.</param>
 public TextSelection(XLayer layer, XText shape, ShapeStyle style, BaseShape point)
 {
     _layer = layer;
     _text = shape;
     _style = style;
     _point = point;
 }
コード例 #2
0
ファイル: ToolText.cs プロジェクト: Core2D/Core2D
        /// <inheritdoc/>
        public override void LeftDown(double x, double y)
        {
            base.LeftDown(x, y);
            var editor = _serviceProvider.GetService<ProjectEditor>();
            double sx = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(x, editor.Project.Options.SnapX) : x;
            double sy = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(y, editor.Project.Options.SnapY) : y;
            switch (_currentState)
            {
                case ToolState.None:
                    {
                        var style = editor.Project.CurrentStyleLibrary.Selected;
                        _text = XText.Create(
                            sx, sy,
                            editor.Project.Options.CloneStyle ? style.Clone() : style,
                            editor.Project.Options.PointShape,
                            "Text",
                            editor.Project.Options.DefaultIsStroked);

                        var result = editor.TryToGetConnectionPoint(sx, sy);
                        if (result != null)
                        {
                            _text.TopLeft = result;
                        }

                        editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Add(_text);
                        editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                        ToStateOne();
                        Move(_text);
                        _currentState = ToolState.One;
                        editor.CancelAvailable = true;
                    }
                    break;
                case ToolState.One:
                    {
                        if (_text != null)
                        {
                            _text.BottomRight.X = sx;
                            _text.BottomRight.Y = sy;

                            var result = editor.TryToGetConnectionPoint(sx, sy);
                            if (result != null)
                            {
                                _text.BottomRight = result;
                            }

                            editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Remove(_text);
                            Remove();
                            Finalize(_text);
                            editor.Project.AddShape(editor.Project.CurrentContainer.CurrentLayer, _text);
                            _currentState = ToolState.None;
                            editor.CancelAvailable = false;
                        }
                    }
                    break;
            }
        }
コード例 #3
0
ファイル: WpfRenderer.cs プロジェクト: Core2D/Core2D
        /// <inheritdoc/>
        public override void Draw(object dc, XText text, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _dc = dc as DrawingContext;

            var style = text.Style;
            if (style == null)
                return;

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

            double thickness = style.Thickness / _state.ZoomX;
            double half = thickness / 2.0;

            Tuple<Brush, Pen> styleCached = _styleCache.Get(style);
            Brush fill;
            Pen stroke;
            if (styleCached != null)
            {
                fill = styleCached.Item1;
                stroke = styleCached.Item2;
            }
            else
            {
                fill = CreateBrush(style.Fill);
                stroke = CreatePen(style, thickness);
                _styleCache.Set(style, Tuple.Create(fill, stroke));
            }

            var rect = CreateRect(text.TopLeft, text.BottomRight, dx, dy);

            Tuple<string, FormattedText, ShapeStyle> tcache = _textCache.Get(text);
            FormattedText ft;
            string ct;
            if (tcache != null && string.Compare(tcache.Item1, tbind) == 0 && tcache.Item3 == style)
            {
                ct = tcache.Item1;
                ft = tcache.Item2;
                _dc.DrawText(ft, GetTextOrigin(style, ref rect, ft));
            }
            else
            {
                var ci = CultureInfo.InvariantCulture;

                var fontStyle = System.Windows.FontStyles.Normal;
                var fontWeight = FontWeights.Regular;

                if (style.TextStyle.FontStyle != null)
                {
                    if (style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Italic))
                    {
                        fontStyle = System.Windows.FontStyles.Italic;
                    }

                    if (style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Bold))
                    {
                        fontWeight = FontWeights.Bold;
                    }
                }

                var tf = new Typeface(new FontFamily(style.TextStyle.FontName), fontStyle, fontWeight, FontStretches.Normal);

                ft = new FormattedText(
                    tbind,
                    ci,
                    ci.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight,
                    tf,
                    style.TextStyle.FontSize > 0.0 ? style.TextStyle.FontSize : double.Epsilon,
                    stroke.Brush, null, TextFormattingMode.Ideal);

                if (style.TextStyle.FontStyle != null)
                {
                    if (style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Underline)
                    || style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Strikeout))
                    {
                        var decorations = new TextDecorationCollection();

                        if (style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Underline))
                        {
                            decorations = new TextDecorationCollection(
                                decorations.Union(TextDecorations.Underline));
                        }

                        if (style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Strikeout))
                        {
                            decorations = new TextDecorationCollection(
                                decorations.Union(TextDecorations.Strikethrough));
                        }

                        ft.SetTextDecorations(decorations);
                    }
                }

                _textCache.Set(text, Tuple.Create(tbind, ft, style));

                _dc.DrawText(ft, GetTextOrigin(style, ref rect, ft));
            }
        }
コード例 #4
0
ファイル: XGroupTests.cs プロジェクト: Core2D/Core2D
        public void GetPoints_Returns_Shapes_And_Connector_Points()
        {
            var target = new XGroup();

            var text = new XText();
            text.Data.Properties = text.Data.Properties.Add(new XProperty());
            target.Shapes = target.Shapes.Add(text);

            var point = new XPoint();
            point.Data.Properties = point.Data.Properties.Add(new XProperty());
            target.Connectors = target.Connectors.Add(point);

            Assert.Equal(3, target.GetPoints().Count());
        }
コード例 #5
0
ファイル: SkiaRenderer.cs プロジェクト: Core2D/Core2D
        /// <inheritdoc/>
        public override void Draw(object dc, XText text, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var canvas = dc as SKCanvas;

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

            SKTypefaceStyle style = SKTypefaceStyle.Normal;
            if (text.Style.TextStyle.FontStyle != null)
            {
                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Bold))
                {
                    style |= SKTypefaceStyle.Bold;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Italic))
                {
                    style |= SKTypefaceStyle.Italic;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Underline))
                {
                    // TODO: Add support for FontStyleFlags.Underline
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(FontStyleFlags.Strikeout))
                {
                    // TODO: Add support for FontStyleFlags.Strikeout
                }
            }

            using (var pen = ToSKPaintBrush(text.Style.Stroke))
            using (var tf = SKTypeface.FromFamilyName(text.Style.TextStyle.FontName, style))
            {
                pen.TextEncoding = SKTextEncoding.Utf16;
                pen.TextSize = _scaleToPage(text.Style.TextStyle.FontSize * _targetDpi / _sourceDpi);

                var fm = pen.FontMetrics;
                float offset = -(fm.Top + fm.Bottom);

                var rect = CreateRect(text.TopLeft, text.BottomRight, dx, dy, _scaleToPage);
                SKRect bounds = new SKRect();
                pen.MeasureText(tbind, ref bounds);
                SKPoint origin = GetTextOrigin(text.Style, ref rect, ref bounds);

                canvas.DrawText(tbind, origin.X, origin.Y + offset, pen);
            }
        }
コード例 #6
0
ファイル: VdxRenderer.cs プロジェクト: Core2D/Core2D
 /// <inheritdoc/>
 public override void Draw(object dc, XText text, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
 {
     // TODO: Implement Draw text.
 }
コード例 #7
0
ファイル: ShapeHitTestPoint.cs プロジェクト: Core2D/Core2D
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="v"></param>
        /// <param name="threshold"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static BaseShape HitTestText(XText text, Vector2 v, double threshold, double dx, double dy)
        {
            if (ShapeBounds.GetPointBounds(text.TopLeft, threshold, dx, dy).Contains(v))
            {
                return text.TopLeft;
            }

            if (ShapeBounds.GetPointBounds(text.BottomRight, threshold, dx, dy).Contains(v))
            {
                return text.BottomRight;
            }

            if (ShapeBounds.GetTextBounds(text, dx, dy).Contains(v))
            {
                return text;
            }

            return null;
        }
コード例 #8
0
ファイル: AvaloniaRenderer.cs プロジェクト: Core2D/Core2D
        /// <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();
            }
        }
コード例 #9
0
ファイル: ShapeRenderer.cs プロジェクト: Core2D/Core2D
 /// <summary>
 /// Draws a <see cref="XText"/> shape using drawing context.
 /// </summary>
 /// <param name="dc">The native drawing context.</param>
 /// <param name="text">The <see cref="XText"/> shape.</param>
 /// <param name="dx">The X coordinate offset.</param>
 /// <param name="dy">The Y coordinate offset.</param>
 /// <param name="db">The properties database.</param>
 /// <param name="r">The data record.</param>
 public abstract void Draw(object dc, XText text, double dx, double dy, ImmutableArray<XProperty> db, XRecord r);
コード例 #10
0
ファイル: WinFormsRenderer.cs プロジェクト: Core2D/Core2D
        /// <inheritdoc/>
        public override void Draw(object dc, XText text, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _gfx = dc as Graphics;

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

            Brush brush = ToSolidBrush(text.Style.Stroke);

            var fontStyle = System.Drawing.FontStyle.Regular;
            if (text.Style.TextStyle.FontStyle != null)
            {
                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Bold))
                {
                    fontStyle |= System.Drawing.FontStyle.Bold;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Italic))
                {
                    fontStyle |= System.Drawing.FontStyle.Italic;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Underline))
                {
                    fontStyle |= System.Drawing.FontStyle.Underline;
                }

                if (text.Style.TextStyle.FontStyle.Flags.HasFlag(Core2D.Style.FontStyleFlags.Strikeout))
                {
                    fontStyle |= System.Drawing.FontStyle.Strikeout;
                }
            }

            Font font = new Font(
                text.Style.TextStyle.FontName,
                (float)(text.Style.TextStyle.FontSize * _textScaleFactor),
                fontStyle);

            var rect = CreateRect(
                text.TopLeft,
                text.BottomRight,
                dx, dy);

            var srect = new RectangleF(
                _scaleToPage(rect.X),
                _scaleToPage(rect.Y),
                _scaleToPage(rect.Width),
                _scaleToPage(rect.Height));

            var format = new StringFormat();
            switch (text.Style.TextStyle.TextHAlignment)
            {
                case TextHAlignment.Left:
                    format.Alignment = StringAlignment.Near;
                    break;
                case TextHAlignment.Center:
                    format.Alignment = StringAlignment.Center;
                    break;
                case TextHAlignment.Right:
                    format.Alignment = StringAlignment.Far;
                    break;
            }

            switch (text.Style.TextStyle.TextVAlignment)
            {
                case TextVAlignment.Top:
                    format.LineAlignment = StringAlignment.Near;
                    break;
                case TextVAlignment.Center:
                    format.LineAlignment = StringAlignment.Center;
                    break;
                case TextVAlignment.Bottom:
                    format.LineAlignment = StringAlignment.Far;
                    break;
            }

            format.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip;
            format.Trimming = StringTrimming.None;

            _gfx.DrawString(
                tbind,
                font,
                ToSolidBrush(text.Style.Stroke),
                srect,
                format);

            brush.Dispose();
            font.Dispose();
        }
コード例 #11
0
ファイル: XTextTests.cs プロジェクト: Core2D/Core2D
 public void Inherits_From_BaseShape()
 {
     var target = new XText();
     Assert.True(target is BaseShape);
 }
コード例 #12
0
ファイル: ShapeBounds.cs プロジェクト: Core2D/Core2D
 /// <summary>
 /// Get the bounding rectangle for <see cref="XText"/> shape.
 /// </summary>
 /// <param name="text"></param>
 /// <param name="dx"></param>
 /// <param name="dy"></param>
 /// <returns></returns>
 public static Rect2 GetTextBounds(XText text, double dx, double dy)
 {
     return Rect2.Create(text.TopLeft, text.BottomRight, dx, dy);
 }
コード例 #13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <param name="rect"></param>
        /// <param name="selected"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static bool HitTestText(XText text, Rect2 rect, ISet<BaseShape> selected, double dx, double dy)
        {
            if (ShapeBounds.GetTextBounds(text, dx, dy).IntersectsWith(rect))
            {
                if (selected != null)
                {
                    selected.Add(text);
                    return false;
                }
                else
                {
                    return true;
                }
            }

            return false;
        }