Exemple #1
0
        internal override void Format(Area area, FormatInfo previousFormatInfo)
        {
            var fullPath = _image.GetFilePath(_documentRenderer.WorkingDirectory);

            _imageFilePath = GetFileName(fullPath, out _index);
            // The Image is stored in the string if path starts with "base64:", otherwise we check whether the file exists.
            if (!_imageFilePath.StartsWith("base64:") &&
                !File.Exists(_imageFilePath))
            {
                _failure = ImageFailure.FileNotFound;
                Debug.WriteLine(Messages2.ImageNotFound(_image.Name), "warning");
            }
            ImageFormatInfo formatInfo = (ImageFormatInfo)_renderInfo.FormatInfo;

            formatInfo.Failure   = _failure;
            formatInfo.ImagePath = fullPath;
            CalculateImageDimensions();
            base.Format(area, previousFormatInfo);
        }
Exemple #2
0
        private void CalculateImageDimensions()
        {
            ImageFormatInfo formatInfo = (ImageFormatInfo)this.renderInfo.FormatInfo;

              if (formatInfo.failure == ImageFailure.None)
              {
            XImage xImage = null;
            try
            {
            xImage = image.IsStreamBased ? XImage.FromStream(image.ImageStream) : XImage.FromFile(formatInfo.ImagePath);
            }
            catch (InvalidOperationException ex)
            {
              Trace.WriteLine(Messages.InvalidImageType(ex.Message));
              formatInfo.failure = ImageFailure.InvalidType;
            }

            try
            {
              XUnit usrWidth = image.Width.Point;
              XUnit usrHeight = image.Height.Point;
              bool usrWidthSet = !this.image.IsNull("Width");
              bool usrHeightSet = !this.image.IsNull("Height");

              XUnit resultWidth = usrWidth;
              XUnit resultHeight = usrHeight;

              double xPixels = xImage.PixelWidth;
              bool usrResolutionSet = !image.IsNull("Resolution");

              double horzRes = usrResolutionSet ? (double)image.Resolution : xImage.HorizontalResolution;
              XUnit inherentWidth = XUnit.FromInch(xPixels / horzRes);
              double yPixels = xImage.PixelHeight;
              double vertRes = usrResolutionSet ? (double)image.Resolution : xImage.VerticalResolution;
              XUnit inherentHeight = XUnit.FromInch(yPixels / vertRes);

              bool lockRatio = this.image.IsNull("LockAspectRatio") ? true : image.LockAspectRatio;

              double scaleHeight = this.image.ScaleHeight;
              double scaleWidth = this.image.ScaleWidth;
              bool scaleHeightSet = !this.image.IsNull("ScaleHeight");
              bool scaleWidthSet = !this.image.IsNull("ScaleWidth");

              if (lockRatio && !(scaleHeightSet && scaleWidthSet))
              {
            if (usrWidthSet && !usrHeightSet)
            {
              resultHeight = inherentHeight / inherentWidth * usrWidth;
            }
            else if (usrHeightSet && !usrWidthSet)
            {
              resultWidth = inherentWidth / inherentHeight * usrHeight;
            }
            else if (!usrHeightSet && !usrWidthSet)
            {
              resultHeight = inherentHeight;
              resultWidth = inherentWidth;
            }

            if (scaleHeightSet)
            {
              resultHeight = resultHeight * scaleHeight;
              resultWidth = resultWidth * scaleHeight;
            }
            if (scaleWidthSet)
            {
              resultHeight = resultHeight * scaleWidth;
              resultWidth = resultWidth * scaleWidth;
            }
              }
              else
              {
            if (!usrHeightSet)
              resultHeight = inherentHeight;

            if (!usrWidthSet)
              resultWidth = inherentWidth;

            if (scaleHeightSet)
              resultHeight = resultHeight * scaleHeight;
            if (scaleWidthSet)
              resultWidth = resultWidth * scaleWidth;
              }

              formatInfo.CropWidth = (int)xPixels;
              formatInfo.CropHeight = (int)yPixels;
              if (!this.image.IsNull("PictureFormat"))
              {
            PictureFormat picFormat = this.image.PictureFormat;
            //Cropping in pixels.
            XUnit cropLeft = picFormat.CropLeft.Point;
            XUnit cropRight = picFormat.CropRight.Point;
            XUnit cropTop = picFormat.CropTop.Point;
            XUnit cropBottom = picFormat.CropBottom.Point;
            formatInfo.CropX = (int)(horzRes * cropLeft.Inch);
            formatInfo.CropY = (int)(vertRes * cropTop.Inch);
            formatInfo.CropWidth -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch);
            formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch);

            //Scaled cropping of the height and width.
            double xScale = resultWidth / inherentWidth;
            double yScale = resultHeight / inherentHeight;

            cropLeft = xScale * cropLeft;
            cropRight = xScale * cropRight;
            cropTop = yScale * cropTop;
            cropBottom = yScale * cropBottom;

            resultHeight = resultHeight - cropTop - cropBottom;
            resultWidth = resultWidth - cropLeft - cropRight;
              }
              if (resultHeight <= 0 || resultWidth <= 0)
              {
            formatInfo.Width = XUnit.FromCentimeter(2.5);
            formatInfo.Height = XUnit.FromCentimeter(2.5);
            Trace.WriteLine(Messages.EmptyImageSize);
            this.failure = ImageFailure.EmptySize;
              }
              else
              {
            formatInfo.Width = resultWidth;
            formatInfo.Height = resultHeight;
              }
            }
            catch (Exception ex)
            {
              Trace.WriteLine(Messages.ImageNotReadable(this.image.Name, ex.Message));
              formatInfo.failure = ImageFailure.NotRead;
            }
            finally
            {
              if (xImage != null)
            xImage.Dispose();
            }
              }
              if (formatInfo.failure != ImageFailure.None)
              {
            if (!this.image.IsNull("Width"))
              formatInfo.Width = this.image.Width.Point;
            else
              formatInfo.Width = XUnit.FromCentimeter(2.5);

            if (!this.image.IsNull("Height"))
              formatInfo.Height = this.image.Height.Point;
            else
              formatInfo.Height = XUnit.FromCentimeter(2.5);
            return;
              }
        }
Exemple #3
0
 internal override void Format(Area area, FormatInfo previousFormatInfo)
 {
     if (!this.image.IsStreamBased)
     {
     this.imageFilePath = image.GetFilePath(this.documentRenderer.WorkingDirectory);
     //if (!File.Exists(this.imageFilePath))
     if (!XImage.ExistsFile(this.imageFilePath))
     {
         this.failure = ImageFailure.FileNotFound;
         Trace.WriteLine(Messages.ImageNotFound(this.image.Name), "warning");
     }
     }
     ImageFormatInfo formatInfo = (ImageFormatInfo)this.renderInfo.FormatInfo;
       formatInfo.failure = this.failure;
       formatInfo.ImagePath = this.imageFilePath;
       CalculateImageDimensions();
       base.Format(area, previousFormatInfo);
 }
Exemple #4
0
        private void CalculateImageDimensions()
        {
            ImageFormatInfo formatInfo = (ImageFormatInfo)_renderInfo.FormatInfo;

            if (formatInfo.Failure == ImageFailure.None)
            {
                FixedPage xImage = null;
                try
                {
                    //xImage = XImage.FromFile(_imageFilePath);
                    xImage = GetFixedPage(_imageFilePath, _index);
                }
                catch (InvalidOperationException ex)
                {
                    Debug.WriteLine(Messages2.InvalidImageType(ex.Message));
                    formatInfo.Failure = ImageFailure.InvalidType;
                }

                if (formatInfo.Failure == ImageFailure.None)
                {
                    try
                    {
                        XUnit usrWidth     = _image.Width.Point;
                        XUnit usrHeight    = _image.Height.Point;
                        bool  usrWidthSet  = !_image._width.IsNull;
                        bool  usrHeightSet = !_image._height.IsNull;

                        XUnit resultWidth  = usrWidth;
                        XUnit resultHeight = usrHeight;

                        Debug.Assert(xImage != null);
                        double xPixels          = xImage.Width;
                        bool   usrResolutionSet = !_image._resolution.IsNull;

                        double horzRes = usrResolutionSet ? _image.Resolution : 96;
                        double vertRes = usrResolutionSet ? _image.Resolution : 96;

// ReSharper disable CompareOfFloatsByEqualityOperator
                        if (horzRes == 0 && vertRes == 0)
                        {
                            horzRes = 72;
                            vertRes = 72;
                        }
                        else if (horzRes == 0)
                        {
                            Debug.Assert(false, "How can this be?");
                            horzRes = 72;
                        }
                        else if (vertRes == 0)
                        {
                            Debug.Assert(false, "How can this be?");
                            vertRes = 72;
                        }
                        // ReSharper restore CompareOfFloatsByEqualityOperator

                        XUnit  inherentWidth  = XUnit.FromInch(xPixels / horzRes);
                        double yPixels        = xImage.Height;
                        XUnit  inherentHeight = XUnit.FromInch(yPixels / vertRes);

                        //bool lockRatio = _image.IsNull("LockAspectRatio") ? true : _image.LockAspectRatio;
                        bool lockRatio = _image._lockAspectRatio.IsNull || _image.LockAspectRatio;

                        double scaleHeight = _image.ScaleHeight;
                        double scaleWidth  = _image.ScaleWidth;
                        //bool scaleHeightSet = !_image.IsNull("ScaleHeight");
                        //bool scaleWidthSet = !_image.IsNull("ScaleWidth");
                        bool scaleHeightSet = !_image._scaleHeight.IsNull;
                        bool scaleWidthSet  = !_image._scaleWidth.IsNull;

                        if (lockRatio && !(scaleHeightSet && scaleWidthSet))
                        {
                            if (usrWidthSet && !usrHeightSet)
                            {
                                resultHeight = inherentHeight / inherentWidth * usrWidth;
                            }
                            else if (usrHeightSet && !usrWidthSet)
                            {
                                resultWidth = inherentWidth / inherentHeight * usrHeight;
                            }
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
                            else if (!usrHeightSet && !usrWidthSet)
                            {
                                resultHeight = inherentHeight;
                                resultWidth  = inherentWidth;
                            }

                            if (scaleHeightSet)
                            {
                                resultHeight = resultHeight * scaleHeight;
                                resultWidth  = resultWidth * scaleHeight;
                            }
                            if (scaleWidthSet)
                            {
                                resultHeight = resultHeight * scaleWidth;
                                resultWidth  = resultWidth * scaleWidth;
                            }
                        }
                        else
                        {
                            if (!usrHeightSet)
                            {
                                resultHeight = inherentHeight;
                            }

                            if (!usrWidthSet)
                            {
                                resultWidth = inherentWidth;
                            }

                            if (scaleHeightSet)
                            {
                                resultHeight = resultHeight * scaleHeight;
                            }
                            if (scaleWidthSet)
                            {
                                resultWidth = resultWidth * scaleWidth;
                            }
                        }

                        formatInfo.CropWidth  = (int)xPixels;
                        formatInfo.CropHeight = (int)yPixels;
                        if (_image._pictureFormat != null && !_image._pictureFormat.IsNull())
                        {
                            PictureFormat picFormat = _image.PictureFormat;
                            //Cropping in pixels.
                            XUnit cropLeft   = picFormat.CropLeft.Point;
                            XUnit cropRight  = picFormat.CropRight.Point;
                            XUnit cropTop    = picFormat.CropTop.Point;
                            XUnit cropBottom = picFormat.CropBottom.Point;
                            formatInfo.CropX       = (int)(horzRes * cropLeft.Inch);
                            formatInfo.CropY       = (int)(vertRes * cropTop.Inch);
                            formatInfo.CropWidth  -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch);
                            formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch);

                            //Scaled cropping of the height and width.
                            double xScale = resultWidth / inherentWidth;
                            double yScale = resultHeight / inherentHeight;

                            cropLeft   = xScale * cropLeft;
                            cropRight  = xScale * cropRight;
                            cropTop    = yScale * cropTop;
                            cropBottom = yScale * cropBottom;

                            resultHeight = resultHeight - cropTop - cropBottom;
                            resultWidth  = resultWidth - cropLeft - cropRight;
                        }
                        if (resultHeight <= 0 || resultWidth <= 0)
                        {
                            formatInfo.Width  = XUnit.FromCentimeter(2.5);
                            formatInfo.Height = XUnit.FromCentimeter(2.5);
                            Debug.WriteLine(Messages2.EmptyImageSize);
                            _failure = ImageFailure.EmptySize;
                        }
                        else
                        {
                            formatInfo.Width  = resultWidth;
                            formatInfo.Height = resultHeight;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(Messages2.ImageNotReadable(_image.Name, ex.Message));
                        formatInfo.Failure = ImageFailure.NotRead;
                    }
                }
            }
            if (formatInfo.Failure != ImageFailure.None)
            {
                if (!_image._width.IsNull)
                {
                    formatInfo.Width = _image.Width.Point;
                }
                else
                {
                    formatInfo.Width = XUnit.FromCentimeter(2.5);
                }

                if (!_image._height.IsNull)
                {
                    formatInfo.Height = _image.Height.Point;
                }
                else
                {
                    formatInfo.Height = XUnit.FromCentimeter(2.5);
                }
            }
        }