private const float Epsilon = 0.00001F; // Prevents divide by zero public static bool CropSelection(ShapeRange shapeRange) { bool hasChange = false; Shape refObj = shapeRange[1]; float refScaleWidth = ShapeUtil.GetScaleWidth(refObj); float refScaleHeight = ShapeUtil.GetScaleHeight(refObj); float cropTop = Epsilon; float cropBottom = Epsilon; float cropLeft = Epsilon; float cropRight = Epsilon; if (!ShapeUtil.IsShape(refObj)) { cropTop = Math.Max(refObj.PictureFormat.CropTop, Epsilon); cropBottom = Math.Max(refObj.PictureFormat.CropBottom, Epsilon); cropLeft = Math.Max(refObj.PictureFormat.CropLeft, Epsilon); cropRight = Math.Max(refObj.PictureFormat.CropRight, Epsilon); } float refShapeCroppedHeight = cropTop + cropBottom; float refShapeCroppedWidth = cropLeft + cropRight; for (int i = 2; i <= shapeRange.Count; i++) { float heightToCrop = shapeRange[i].Height - refObj.Height; float widthToCrop = shapeRange[i].Width - refObj.Width; if (heightToCrop <= 0 && widthToCrop <= 0) { continue; } hasChange = true; float scaleWidth = ShapeUtil.GetScaleWidth(shapeRange[i]); float scaleHeight = ShapeUtil.GetScaleHeight(shapeRange[i]); if (CropLabSettings.AnchorPosition == AnchorPosition.Reference) { shapeRange[i].PictureFormat.CropTop += Math.Max(0, heightToCrop * cropTop / refShapeCroppedHeight / scaleHeight); shapeRange[i].PictureFormat.CropLeft += Math.Max(0, widthToCrop * cropLeft / refShapeCroppedWidth / scaleWidth); shapeRange[i].PictureFormat.CropRight += Math.Max(0, widthToCrop * cropRight / refShapeCroppedWidth / scaleWidth); shapeRange[i].PictureFormat.CropBottom += Math.Max(0, heightToCrop * cropBottom / refShapeCroppedHeight / scaleHeight); } else { shapeRange[i].PictureFormat.CropTop += CropLabSettings.GetAnchorY() * heightToCrop / scaleHeight; shapeRange[i].PictureFormat.CropLeft += CropLabSettings.GetAnchorX() * widthToCrop / scaleWidth; shapeRange[i].PictureFormat.CropRight += (1 - CropLabSettings.GetAnchorX()) * widthToCrop / scaleWidth; shapeRange[i].PictureFormat.CropBottom += (1 - CropLabSettings.GetAnchorY()) * heightToCrop / scaleHeight; } } return(hasChange); }
private void CropSpotlightPictureToSlide(ref PowerPoint.Shape shapeToCrop) { float scaleFactorWidth = ShapeUtil.GetScaleWidth(shapeToCrop); float scaleFactorHeight = ShapeUtil.GetScaleHeight(shapeToCrop); if (shapeToCrop.Left < 0) { shapeToCrop.PictureFormat.CropLeft += ((0.0f - shapeToCrop.Left) / scaleFactorWidth); } if (shapeToCrop.Left + shapeToCrop.Width > PowerPointPresentation.Current.SlideWidth) { shapeToCrop.PictureFormat.CropRight += ((shapeToCrop.Left + shapeToCrop.Width - PowerPointPresentation.Current.SlideWidth) / scaleFactorWidth); } if (shapeToCrop.Top < 0) { shapeToCrop.PictureFormat.CropTop += ((0.0f - shapeToCrop.Top) / scaleFactorHeight); } if (shapeToCrop.Top + shapeToCrop.Height > PowerPointPresentation.Current.SlideHeight) { shapeToCrop.PictureFormat.CropBottom += ((shapeToCrop.Top + shapeToCrop.Height - PowerPointPresentation.Current.SlideHeight) / scaleFactorHeight); } }