private EMImage(EMDocument doc, EMElementOrigin origin, EMElement parent, EMLocalFilePath path, string title, string alt, ImageOptions options)
     : base(doc, origin, parent)
 {
     Path = path;
     Title = title;
     Alt = alt;
     Options = options;
 }
Beispiel #2
0
 public ImageOptions(ImageOptions other)
 {
     Width = other.Width;
     Height = other.Height;
     Align = other.Align;
 }
 public ImageOptions(ImageOptions other)
 {
     Width  = other.Width;
     Height = other.Height;
     Align  = other.Align;
 }
        private string LinkEvaluator(Match match)
        {
            var linkId = match.Groups[1].Value.ToLowerInvariant();

            Urls[linkId] = Markdown.EncodeAmpsAndAngles(match.Groups[2].Value);

            var width = match.Groups[6].Value;
            var height = match.Groups[8].Value;
            var align = match.Groups[10].Value;

            var convert = match.Groups[12].Value;
            var convertType = match.Groups[14].Value;
            var convertQuality = match.Groups[16].Value;
            var hexFillColor = match.Groups[18].Value;

            //Store the refence image attributes
            var imageFormat = new ReferenceAttributes(width, height, convert, convertType, hexFillColor, convertQuality);
            ReferenceAttributes[linkId] = imageFormat;

            if (!String.IsNullOrWhiteSpace(match.Groups[3].Value))
            {
                Titles[linkId] = match.Groups[3].Value.Replace("\"", """);
            }

            ImageAlignment[linkId] = new ImageOptions(width, height, align);

            return "";
        }
        private static EMElement Create(
            TransformationData data,
            EMDocument doc,
            EMElementOrigin origin,
            EMElement parent,
            string url,
            string alt,
            string title,
            string convert,
            string width,
            string height,
            string convertQuality,
            string hexFillColor,
            string convertType,
            ImageOptions options)
        {
            ImageFormat imageFormatType;

            if (String.IsNullOrWhiteSpace(url))
            {
                return EMErrorElement.Create(doc, origin, parent, data, "EmptyLink", origin.Text);
            }

            alt = alt.Replace("\"", """);

            if (title != null)
            {
                title = title.Replace("\"", """);
            }

            if (url.StartsWith("<") && url.EndsWith(">"))
            {
                url = url.Substring(1, url.Length - 2); // Remove <>'s surrounding URL, if present
            }

            if (String.IsNullOrWhiteSpace(alt))
            {
                //if no alt text provided use the file name.
                alt = Regex.Replace(url, @".*[\\|/](.*)", "$1");
            }

            var doConvert = !convert.ToLower().Equals("false") && data.Markdown.DefaultImageDoCompress;

            int widthIntConverted;
            int heightIntConverted;
            int quality;

            Color fillColor;

            if (doConvert)
            {
                //if we are converting try to get the other values
                //try parse the strings for width and height into integers
                try
                {
                    widthIntConverted = Int32.Parse(width);
                }
                catch (FormatException)
                {
                    widthIntConverted = 0;
                }

                try
                {
                    heightIntConverted = Int32.Parse(height);
                }
                catch (FormatException)
                {
                    heightIntConverted = 0;
                }

                try
                {
                    quality = Int32.Parse(convertQuality);
                }
                catch (FormatException)
                {
                    quality = data.Markdown.DefaultImageQuality;
                }

                try
                {
                    fillColor = ImageConversion.GetColorFromHexString(hexFillColor);
                }
                catch (Exception)
                {
                    fillColor = data.Markdown.DefaultImageFillColor;
                }

                if (String.IsNullOrWhiteSpace(convertType))
                {
                    convertType = data.Markdown.DefaultImageFormatExtension;
                    imageFormatType = data.Markdown.DefaultImageFormat;
                }
                else
                {
                    try
                    {
                        imageFormatType = ImageConversion.GetImageFormat(convertType.ToLower());
                    }
                    catch (Exception)
                    {
                        return EMErrorElement.Create(
                            doc,
                            origin,
                            parent,
                            data,
                            "UnsupportedImageFileTypeConversion",
                            Markdown.Unescape(url),
                            convertType.ToLower());
                    }
                }
            }
            else
            {
                //set width and height to zero indicating to converter that we want to use the images values
                widthIntConverted = 0;
                heightIntConverted = 0;

                //set conversion type to itself, but do check it is a supported image type.
                convertType =
                    Regex.Match(Markdown.Unescape(url), @"\.(png|gif|tga|bmp|jpg)", RegexOptions.IgnoreCase).Groups[1].Value;

                try
                {
                    imageFormatType = ImageConversion.GetImageFormat(convertType.ToLower());
                }
                catch (Exception)
                {
                    return EMErrorElement.Create(
                        doc,
                        origin,
                        parent,
                        data,
                        "UnsupportedImageFileTypeConversion",
                        Markdown.Unescape(url),
                        convertType.ToLower());
                }

                quality = 100;
                fillColor = data.Markdown.DefaultImageFillColor;
            }

            if (!String.IsNullOrWhiteSpace(convertType))
            {
                try
                {
                    var path = new EMLocalFilePath(Markdown.Unescape(url), doc, data,
                        fileName => System.IO.Path.GetFileNameWithoutExtension(fileName) + "." + ImageConversion.GetImageExt(imageFormatType));

                    if (!path.IsImage)
                    {
                        throw new EMPathVerificationException(Language.Message("GivenPathIsNotAnImage", url));
                    }

                    data.ImageDetails.Add(
                        new ImageConversion(
                            path.GetSource(),
                            path.GetAbsolutePath(data),
                            widthIntConverted,
                            heightIntConverted,
                            doConvert,
                            imageFormatType,
                            fillColor,
                            quality));

                    return new EMImage(doc, origin, parent, path, title, alt, options);
                }
                catch (EMPathVerificationException e)
                {
                    return new EMErrorElement(doc, origin, parent, e.AddToErrorList(data, origin.Text));
                }
            }

            throw new InvalidOperationException("Should not happen!");
        }