// // scaling calculations // public static void ApplyMaxNonUniformScaling(PDFTransformationMatrix onmatrix, PDFSize dest, PDFRect viewport) { PDFSize source = viewport.Size; double scalex = dest.Width.PointsValue / source.Width.PointsValue; double scaley = dest.Height.PointsValue / source.Height.PointsValue; double offx = 0; // viewport.X.PointsValue; double offy = 0; // viewport.Y.PointsValue; onmatrix.SetTranslation(offx, offy); onmatrix.SetScale((float)scalex, (float)scaley); }
/// <summary> /// This will render the transformation matrix and then the XObject name operation. /// </summary> /// <param name="context"></param> /// <param name="writer"></param> /// <returns></returns> protected virtual bool OutputDrawingContent(PDFRenderContext context, PDFWriter writer) { if (null != this.OutPutName) { context.Graphics.SaveGraphicsState(); var x = context.Offset.X.RealValue; x = context.Graphics.GetXPosition(x); var y = (context.Offset.Y + this.Height).RealValue; y = context.Graphics.GetYPosition(y); if (this.ClipRect.HasValue) { //var rect = this.ClipRect.Value.Offset(context.Offset.X, context.Offset.Y); //context.Graphics.SetClipRect(rect); } var matrix = new PDFTransformationMatrix(); matrix.SetTranslation((float)x, (float)y); //Set the transformation matrix for the current offset context.Graphics.SetTransformationMatrix(matrix, true, true); if (!this.Matrix.IsIdentity) { context.Graphics.SetTransformationMatrix(this.Matrix, true, true); } context.Graphics.PaintXObject(this.OutPutName); context.Graphics.RestoreGraphicsState(); return(true); } else { return(false); } }
public static void ApplyUniformStretching(PDFTransformationMatrix onmatrix, PDFSize dest, PDFRect viewport, AspectRatioAlign align) { PDFSize source = viewport.Size; double scalex = dest.Width.PointsValue / source.Width.PointsValue; double scaley = dest.Height.PointsValue / source.Height.PointsValue; double offx = 0; // viewport.X.PointsValue; double offy = 0; // viewport.Y.PointsValue; float max = (float)Math.Max(scalex, scaley); double w = source.Width.PointsValue * max; double h = source.Height.PointsValue * max; switch (align) { //YMin case (AspectRatioAlign.xMinYMin): //Scale to fit within the size without overflow and show at the top (so no x or y offset) offx = 0; offy = (dest.Height.PointsValue - h); //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; case (AspectRatioAlign.xMidYMin): offx = (dest.Width.PointsValue - w) / 2; offy = (dest.Height.PointsValue - h); //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; case (AspectRatioAlign.xMaxYMin): //Scale to fit within the size without overflow and show at the top (so no x or y offset) offx = dest.Width.PointsValue - w; offy = (dest.Height.PointsValue - h); //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; //YMid case (AspectRatioAlign.xMinYMid): //Scale to fit within the size without overflow and show at the top (so no x or y offset) offx = 0; offy = (dest.Height.PointsValue - h) / 2; //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; case (AspectRatioAlign.xMidYMid): //Scale to fit within the size without overflow and then position in the middle offx = (dest.Width.PointsValue - w) / 2; offy = (dest.Height.PointsValue - h) / 2; //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; case (AspectRatioAlign.xMaxYMid): //Scale to fit within the size without overflow and show at the top (so no x or y offset) offx = dest.Width.PointsValue - w; offy = (dest.Height.PointsValue - h) / 2; //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; //YMax case (AspectRatioAlign.xMinYMax): //Scale to fit within the size without overflow and show at the top (so no x or y offset) offx = 0; offy = 0; //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; case (AspectRatioAlign.xMidYMax): //Scale to fit within the size without overflow and then position in the middle offx = (dest.Width.PointsValue - w) / 2; offy = 0; //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; case (AspectRatioAlign.xMaxYMax): //Scale to fit within the size without overflow and show at the top (so no x or y offset) offx = dest.Width.PointsValue - w; offy = 0; //plus as from the bottom onmatrix.SetTranslation(offx, offy); onmatrix.SetScale(max, max); break; default: break; } }