private static string GetRichText(PsdTextFrame textFrame, BaseTextVObject textVObject)
        {
            var paragraphs = textFrame.Paragraphs.ToList();

            var sb = new StringBuilder();

            var paragraphStyle = "";

            if (paragraphs.Count > 0)
            {
                paragraphStyle = GetParagraphStyle(paragraphs.First());
                paragraphs.RemoveAt(0);
            }

            if (string.IsNullOrEmpty(paragraphStyle))
            {
                sb.Append("<p>");
            }
            else
            {
                sb.AppendFormat(@"<p style=""{0}"">", paragraphStyle);
            }

            foreach (var span in textFrame.FormattedText)
            {
                var spanStyle = GetSpanStyle(span, textVObject);

                var paraTextList = span.String.Split('\r').ToList();

                var i = 0;
                foreach (var ptext in paraTextList)
                {
                    var isLast = ++i == paraTextList.Count;

                    var content = Common.XmlEscape(NormalizeString(!isLast && ptext == "" ? " " : ptext, span.Caps));

                    var lines = content.Split('\n');
                    foreach (var line in lines)
                    {
                        if (!string.IsNullOrEmpty(line))
                        {
                            if (!string.IsNullOrEmpty(spanStyle))
                            {
                                sb.AppendFormat(@"<span style=""{0}"">{1}</span>", spanStyle, line);
                            }
                            else
                            {
                                sb.AppendFormat(@"<span>{0}</span>", line);
                            }
                        }

                        if (line != lines.Last())
                        {
                            sb.Append("<br/>");
                        }
                    }

                    if (paragraphs.Count > 0 && !isLast)
                    {
                        paragraphStyle = GetParagraphStyle(paragraphs.First());
                        paragraphs.RemoveAt(0);

                        if (!string.IsNullOrEmpty(paragraphStyle))
                        {
                            sb.AppendFormat(@"</p><p style=""{0}"">", paragraphStyle);
                        }
                        else
                        {
                            sb.Append("</p><p>");
                        }
                    }
                }
            }

            sb.Append("</p>");

            return(sb.ToString());
        }
Example #2
0
    private static string GetLayerText(PsdTextFrame textFrame)
    {
        // Fix line break

        return(textFrame.Text.Replace("\r\n", "\n").Replace("\r", "\n"));
    }
        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);
        }