Esempio n. 1
0
        public string UpdateByPlainTextVObjectData(string canvasData, string vObjectData)
        {
            var plainText = new PlainTextVObject {
                Data = vObjectData
            };

            using (var canvas = new CanvasSlim {
                Data = canvasData
            })
            {
                UpdateTextVObject(plainText, canvas);
                return(plainText.Data);
            }
        }
Esempio n. 2
0
        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);
        }