protected internal override void UpdateSize()
        {
            if (ValidRect)
            {
                return;
            }

            var dpi      = Canvas != null ? 96 * Canvas.Zoom : 72;
            var textRect = new RotatedRectangleF(MeasureText(dpi));

            textRect.Scale(72 / dpi, 72 / dpi);

            // Move text rectangle to the baseline location because GetBlackBox returns an empty rectangle (in location 0:0) when text is empty
            if (textRect.IsEmpty)
            {
                textRect.CenterX = BaselineLocation.X;
                textRect.CenterY = BaselineLocation.Y;
            }

            textRect.RotateAt(Angle, new PointF(BaselineLocation));

            Path = Math.Path.CreateRectanglePath(textRect.ToRectangleF());
            Transform.Clear(keepAngle: true);
            ActualAngle = Angle;

            ValidRect = true;
        }
        protected internal override RotatedRectangleF GetDrawingRectangle(float dpi = 72)
        {
            var rectangle = new RotatedRectangleF(MeasureText(dpi));

            rectangle.RotateAt(Angle, GetDrawingFirstPoint(dpi));

            return(rectangle);
        }
Beispiel #3
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                var rectangle = new RotatedRectangleF();

                if (reader.TokenType == JsonToken.Null)
                {
                    return(rectangle);
                }

                foreach (var property in (JObject)serializer.Deserialize(reader))
                {
                    switch (property.Key)
                    {
                    case "CenterX":
                        rectangle.CenterX = property.Value.Value <float>();
                        break;

                    case "CenterY":
                        rectangle.CenterY = property.Value.Value <float>();
                        break;

                    case "Width":
                        rectangle.Width = property.Value.Value <float>();
                        break;

                    case "Height":
                        rectangle.Height = property.Value.Value <float>();
                        break;

                    case "Angle":
                        rectangle.Angle = property.Value.Value <float>();
                        break;
                    }
                }

                return(rectangle);
            }
        protected internal override void UpdateSize()
        {
            if (ValidRect)
            {
                return;
            }

            RotatedRectangleF textRect;

            if (!TextPath.IsEmpty)
            {
                var dpi = Canvas != null ? 96 * Canvas.Zoom : 72;
                textRect = new RotatedRectangleF(MeasureText(dpi));
                textRect.Scale(72 / dpi, 72 / dpi);

                var firstPoint = TextPath.GetFirstPoint();

                // Move text rectangle to the text path's first point because GetBlackBox returns an empty rectangle (in location 0:0) when text is empty
                if (textRect.IsEmpty)
                {
                    textRect.CenterX = firstPoint.X;
                    textRect.CenterY = firstPoint.Y;
                }

                textRect.RotateAt(Angle, firstPoint);
            }
            else
            {
                textRect = new RotatedRectangleF();
            }

            Path = Path.CreateRectanglePath(textRect.ToRectangleF());
            Transform.Clear(keepAngle: true);
            ActualAngle = Transform.Angle;
            ValidRect   = true;
        }
        protected override Text CreateAdvancedText(string text, Graphics graphics)
        {
            var font = CreateFont(graphics);

            var dpi  = graphics.DpiX;
            var rect = GetDrawingRectangle(dpi);

            rect.Width  -= RenderingMargin * 2;
            rect.Height -= RenderingMargin * 2;

            var boundedText = new BoundedText(text, font, rect.ToRectangleF())
            {
                Alignment = VOAligmnentToAdvanced(Alignment),
                Underline = Underline,
                Tracking  = Tracking,
                Leading   = Utils.EqualsOfFloatNumbers(0, Leading) ? font.Size * 1.2f : Leading,
                Paragraph =
                {
                    FirstLineIndent = ParagraphSettings.FirstLineIndent,
                    SpaceAfter      = ParagraphSettings.SpaceAfter,
                    SpaceBefore     = ParagraphSettings.SpaceBefore,
                    OverlapLines    = true
                },
                Vertical = IsVertical
            };

            if (!Utils.EqualsOfFloatNumbers(1, HorizontalScale) || !Utils.EqualsOfFloatNumbers(1, VerticalScale))
            {
                var rectangle = new RotatedRectangleF(boundedText.Rectangle);
                rectangle.Scale(1 / HorizontalScale, 1 / VerticalScale);

                boundedText.Rectangle = rectangle.ToRectangleF();
                boundedText.Transform.Scale(HorizontalScale, VerticalScale);
            }

            if (!string.IsNullOrEmpty(text))
            {
                ExtendRectangle(boundedText, graphics);
            }

            var rectHeight = rect.Height;
            var textHeight = boundedText.GetBlackBox().Height;

            var verticalOffset = 0f;

            switch (VerticalAlignment)
            {
            case TextVerticalAlignment.Center:
                verticalOffset = (rectHeight - textHeight) / 2 - RenderingMargin;
                break;

            case TextVerticalAlignment.Bottom:
                verticalOffset = rectHeight - textHeight - RenderingMargin;
                break;
            }

            if (verticalOffset > 0)
            {
                if (!Utils.EqualsOfFloatNumbers(1, VerticalScale))
                {
                    verticalOffset *= 1 / VerticalScale;
                }

                var rectangle = boundedText.Rectangle;
                rectangle.Y      += verticalOffset;
                rectangle.Height -= verticalOffset;

                boundedText.Rectangle = rectangle;
            }

            var wrappingPath = GetWrappingPath(dpi);

            if (wrappingPath.Points.Count > 0)
            {
                boundedText.WrappingPaths.Add(wrappingPath);
            }

            return(boundedText);
        }
Beispiel #6
0
 public PlaceholderVObject(ContentVObject content, RotatedRectangleF placeholder)
     : this(content, Math.Path.CreateRectanglePath(placeholder.ToRectangleF()), placeholder.Angle)
 {
 }
        private static VObject CreateTextVObject(
            PsdTextFrame frame,
            bool forceRichText = false,
            bool getRichTextForBoundedTextOnly = false)
        {
            BaseTextVObject textVObject;

            TextAlignment aligment;

            switch (frame.Justification)
            {
            case TextJustification.Left:
                aligment = TextAlignment.Left;
                break;

            case TextJustification.LastLeft:
                aligment = TextAlignment.LastLeft;
                break;

            case TextJustification.Right:
                aligment = TextAlignment.Right;
                break;

            case TextJustification.LastRight:
                aligment = TextAlignment.LastRight;
                break;

            case TextJustification.All:
                aligment = TextAlignment.Justify;
                break;

            case TextJustification.Center:
                aligment = TextAlignment.Center;
                break;

            case TextJustification.LastCenter:
                aligment = TextAlignment.LastCenter;
                break;

            default:
                aligment = TextAlignment.Left;
                break;
            }

            var transform = GetTransformFromPsdMatrix(frame.Transform);

            // Curved text
            if (frame.Path != null)
            {
                // Get the first point of transformed path and move raw path to this point.
                var path = Path.FromAdvancedPath(frame.Raw.Path);
                if (!path.IsEmpty)
                {
                    path.Scale(72 / frame.DpiX, 72 / frame.DpiY);
                    path.Scale(transform.ScaleX, transform.ScaleY);

                    var transformedPath = Path.FromAdvancedPath(frame.Path);
                    transformedPath.Scale(72 / frame.DpiX, 72 / frame.DpiY);
                    var transformedFirstPoint = transformedPath.GetFirstPoint();

                    var firstPoint = path.GetFirstPoint();
                    path.Translate(transformedFirstPoint.X - firstPoint.X, transformedFirstPoint.Y - firstPoint.Y);
                }

                textVObject = new CurvedTextVObject {
                    TextPath = path, FitToPath = true, Angle = transform.Angle, PathStart = frame.PathStartPoint, PathEnd = frame.PathEndPoint
                };
            }
            else if (Utils.EqualsOfFloatNumbers(frame.TextBox.Width, 0) && Utils.EqualsOfFloatNumbers(frame.TextBox.Height, 0))
            {
                var baselineLocation = new PointF(
                    Common.ConvertPixelsToPoints(frame.DpiX, frame.TextBox.X),
                    Common.ConvertPixelsToPoints(frame.DpiY, frame.TextBox.Y));

                textVObject = new PlainTextVObject {
                    BaselineLocation = baselineLocation, Angle = transform.Angle, IsVertical = frame.IsVertical
                };
            }
            else
            {
                var textBoxShift = frame.IsVertical ? frame.Raw.TextBox.Width * (float)transform.ScaleX : 0;

                var rectangle = new RotatedRectangleF(new RectangleF(
                                                          Common.ConvertPixelsToPoints(frame.DpiX, frame.TextBox.X - textBoxShift),
                                                          Common.ConvertPixelsToPoints(frame.DpiY, frame.TextBox.Y),
                                                          Common.ConvertPixelsToPoints(frame.DpiX, frame.Raw.TextBox.Width * (float)transform.ScaleX),
                                                          Common.ConvertPixelsToPoints(frame.DpiY, frame.Raw.TextBox.Height * (float)transform.ScaleY)
                                                          ));

                rectangle.RotateAt(transform.Angle, rectangle.Location);

                textVObject = new BoundedTextVObject {
                    Rectangle = rectangle, IsVertical = frame.IsVertical
                };
            }

            var boundedText = textVObject as BoundedTextVObject;

            // Empty text is allowed in boundedTextVObject only
            if (boundedText == null && string.IsNullOrEmpty(frame.Text))
            {
                return(null);
            }

            textVObject.Alignment       = aligment;
            textVObject.Tracking        = RoundFloat(frame.Tracking, 1);
            textVObject.Leading         = RoundFloat(frame.Leading, 1);
            textVObject.VerticalScale   = frame.VerticalScale * (float)transform.ScaleY;
            textVObject.HorizontalScale = frame.HorizontalScale * (float)transform.ScaleX;
            textVObject.TextColor       = frame.Color;
            textVObject.Opacity         = frame.Opacity;

            textVObject.Font.Size           = RoundFloat(frame.Raw.FontSize, 2);
            textVObject.Font.PostScriptName = frame.FontName;

            if (textVObject is CurvedTextVObject)
            {
                ((CurvedTextVObject)textVObject).OriginalFontSize = textVObject.Font.Size;
            }

            var nonDefaultParagraphs = frame.Paragraphs.Count > 1 && frame.Paragraphs.Any(p => !Utils.EqualsOfFloatNumbers(0, p.FirstLineIndent) ||
                                                                                          !Utils.EqualsOfFloatNumbers(0, p.SpaceAfterParagraph) || !Utils.EqualsOfFloatNumbers(0, p.SpaceBeforeParagraph)) ||
                                       frame.Paragraphs.Count == 1 && frame.Paragraphs.First().FirstLineIndent > 0;

            if ((boundedText == null && !getRichTextForBoundedTextOnly && (forceRichText || frame.FormattedText.Count > 1)) ||
                (boundedText != null && (forceRichText || frame.FormattedText.Count > 1 || nonDefaultParagraphs)))
            {
                textVObject.Text       = GetRichText(frame, textVObject);
                textVObject.IsRichText = true;
            }
            else
            {
                textVObject.Text = Common.XmlEscape(NormalizeString(frame.Text ?? "", frame.Caps));

                textVObject.Font.FauxBold   = frame.FauxBold;
                textVObject.Font.FauxItalic = frame.FauxItalic;
                textVObject.Underline       = frame.Underline;

                if (boundedText != null)
                {
                    boundedText.ParagraphSettings = new ParagraphSettings
                    {
                        FirstLineIndent = RoundFloat(frame.Paragraph.FirstLineIndent, 1),
                        SpaceAfter      = RoundFloat(frame.Paragraph.SpaceAfterParagraph, 1),
                        SpaceBefore     = RoundFloat(frame.Paragraph.SpaceBeforeParagraph, 1)
                    };
                }

                textVObject.IsRichText = false;
            }

            return(textVObject);
        }