Ejemplo n.º 1
0
        public static string RenderToFile(BaseTextVObject vObject, float dpi)
        {
            var fileId = CreateFileId(vObject.GetMD5(dpi));

            if (Configuration.FileCache.FileExists(fileId))
            {
                return(fileId);
            }

            try
            {
                var bitmap = GetTextBitmap(vObject, dpi, out fileId);
                Configuration.FileCache.WriteToStream(fileId, stream =>
                {
                    bitmap.Save(stream, Common.GetWriterSettings(_fileFormat));
                    bitmap.Dispose();
                });
            }
            catch (Exception)
            {
                Configuration.FileCache.RemoveFile(fileId);
                throw;
            }

            return(fileId);
        }
Ejemplo n.º 2
0
        private static string GetSpanStyle(ITextSettings span, BaseTextVObject text)
        {
            var style = new StringBuilder();

            if (span.FontName != text.Font.PostScriptName)
            {
                style.AppendFormat("font-name:{0};", span.FontName);
            }

            if (span.FauxBold)
            {
                style.Append("bold:true;");
            }

            if (span.FauxItalic)
            {
                style.Append("italic:true;");
            }

            if (span.Underline != text.Underline)
            {
                style.AppendFormat("underline:{0};", span.Underline.ToString(CultureInfo.InvariantCulture).ToLower(CultureInfo.CurrentCulture));
            }

            var spanColor = span.Color;

            if (span.Color != text.TextColor)
            {
                var colorString = ColorToFormatString(spanColor);
                if (!string.IsNullOrEmpty(colorString))
                {
                    style.AppendFormat("color:{0};", colorString);
                }
            }

            var fontSize = RoundFloat(span.FontSize / text.VerticalScale, 2);

            if (!Utils.EqualsOfFloatNumbers(fontSize, text.Font.Size))
            {
                style.AppendFormat("font-size:{0}pt;", fontSize.ToString(CultureInfo.InvariantCulture));
            }

            var tracking = RoundFloat(span.Tracking, 1);

            if (!Utils.EqualsOfFloatNumbers(tracking, text.Tracking))
            {
                style.AppendFormat("tracking:{0};", tracking.ToString(CultureInfo.InvariantCulture));
            }

            return(style.ToString().Trim());
        }
Ejemplo n.º 3
0
        private static void UpdateTextVObject(BaseTextVObject vObject, ICanvas canvas)
        {
            var layer = new Layer();

            layer.VObjects.Add(vObject);
            canvas.Layers.Add(layer);

            var colorManagement = vObject.GetColorManagement(true);

            if (colorManagement != null)
            {
                colorManagement.InitPreviewColorMap(vObject.GetColors());
            }

            vObject.CurrentFileId = TextRenderer.RenderToFile(vObject, canvas.ScreenXDpi * canvas.Zoom);
        }
Ejemplo n.º 4
0
        private static Bitmap GetTextBitmap(BaseTextVObject vObject, float dpi, Point location, bool extendBitmap, out string id)
        {
            if (vObject == null)
            {
                throw new ArgumentNullException("vObject");
            }

            if (dpi <= 0)
            {
                throw new ArgumentOutOfRangeException("dpi");
            }

            var rect   = vObject.MeasureText(dpi);
            var width  = (int)System.Math.Ceiling(rect.Width + (extendBitmap ? location.X * 2 : 0));
            var height = (int)System.Math.Ceiling(rect.Height + (extendBitmap ? location.Y * 2 : 0));

            if (rect.IsEmpty)
            {
                throw new InvalidOperationException("TextRenderer cannot write a text bitmap because it is empty");
            }

            var scale           = dpi / vObject.TargetDpi ?? 1;
            var colorManagement = vObject.GetColorManagement(true);
            var textBitmap      = new Bitmap(width, height, ColorManagement.GetPixelFormat(_colorSpace), RgbColor.Transparent)
            {
                DpiX         = dpi / scale,
                DpiY         = dpi / scale,
                ColorProfile = ColorManagement.GetProfile(colorManagement, _colorSpace, true)
            };

            using (var graphics = textBitmap.GetAdvancedGraphics())
            {
                graphics.Transform.Translate(-rect.X + location.X, -rect.Y + location.Y);

                if (!Utils.EqualsOfFloatNumbers(scale, 1))
                {
                    graphics.Transform.Scale(scale, scale);
                }

                var angle   = vObject.Angle;
                var opacity = vObject.Opacity;

                var curvedText = vObject as CurvedTextVObject;
                if (curvedText != null)
                {
                    curvedText.TextPath.RotateAt(-angle, curvedText.TextPath.GetFirstPoint());
                }

                vObject.Transform.Update(null, null, null, null, 0);
                vObject.Opacity = 1;

                vObject.DrawText(graphics, textBitmap, colorManagement);

                if (curvedText != null)
                {
                    curvedText.TextPath.RotateAt(angle, curvedText.TextPath.GetFirstPoint());
                }

                vObject.Transform.Update(null, null, null, null, angle);
                vObject.Opacity = opacity;
            }

            textBitmap.DpiX = dpi;
            textBitmap.DpiY = dpi;

            id = CreateFileId(vObject.GetMD5(dpi));

            return(textBitmap);
        }
Ejemplo n.º 5
0
 private static Bitmap GetTextBitmap(BaseTextVObject vObject, float dpi, out string id)
 {
     return(GetTextBitmap(vObject, dpi, new Point(1, 1), false, out id));
 }
Ejemplo n.º 6
0
        internal static Bitmap GetTextBitmap(BaseTextVObject vObject, float dpi, Point location, bool extendBitmap = false)
        {
            string id;

            return(GetTextBitmap(vObject, dpi, location, extendBitmap, out id));
        }
Ejemplo n.º 7
0
 public ProtectedFontSettings(BaseTextVObject textVObject)
 {
     _textVObject = textVObject;
 }
Ejemplo n.º 8
0
        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());
        }