internal TexturePaintContext(ColorModel cm, AffineTransform xform, int bWidth, int bHeight, int maxw) { this.ColorModel_Renamed = GetInternedColorModel(cm); this.BWidth = bWidth; this.BHeight = bHeight; this.MaxWidth = maxw; try { xform = xform.CreateInverse(); } catch (NoninvertibleTransformException) { xform.SetToScale(0, 0); } this.IncXAcross = Mod(xform.ScaleX, bWidth); this.IncYAcross = Mod(xform.ShearY, bHeight); this.IncXDown = Mod(xform.ShearX, bWidth); this.IncYDown = Mod(xform.ScaleY, bHeight); this.XOrg = xform.TranslateX; this.YOrg = xform.TranslateY; this.Colincx = (int)IncXAcross; this.Colincy = (int)IncYAcross; this.Colincxerr = FractAsInt(IncXAcross); this.Colincyerr = FractAsInt(IncYAcross); this.Rowincx = (int)IncXDown; this.Rowincy = (int)IncYDown; this.Rowincxerr = FractAsInt(IncXDown); this.Rowincyerr = FractAsInt(IncYDown); }
public override void DrawBackground(DrawContext drawContext) { PdfCanvas canvas = drawContext.GetCanvas(); Matrix ctm = canvas.GetGraphicsState().GetCtm(); // Avoid rotation float?angle = this.GetPropertyAsFloat(Property.ROTATION_ANGLE); bool avoidRotation = null != angle && null != this.GetProperty <Background>(Property.BACKGROUND); if (avoidRotation) { AffineTransform transform = new AffineTransform(ctm.Get(0), ctm.Get(1), ctm.Get(3), ctm.Get(4), ctm.Get(6) , ctm.Get(7)); try { transform = transform.CreateInverse(); } catch (NoninvertibleTransformException e) { throw new Exception(e.Message, e); } transform.Concatenate(new AffineTransform()); canvas.ConcatMatrix(transform); DeleteProperty(Property.ROTATION_ANGLE); } base.DrawBackground(drawContext); // restore concat matrix and rotation angle if (avoidRotation) { SetProperty(Property.ROTATION_ANGLE, angle); canvas.ConcatMatrix(new AffineTransform(ctm.Get(0), ctm.Get(1), ctm.Get(3), ctm.Get(4), ctm.Get(6), ctm.Get (7))); } }
private Point[] TransformPoints(Matrix transformationMatrix, params Point[] points) { try { AffineTransform t = new AffineTransform(transformationMatrix.Get(Matrix.I11), transformationMatrix.Get(Matrix .I12), transformationMatrix.Get(Matrix.I21), transformationMatrix.Get(Matrix.I22), transformationMatrix .Get(Matrix.I31), transformationMatrix.Get(Matrix.I32)); t = t.CreateInverse(); Point[] transformed = new Point[points.Length]; t.Transform(points, 0, transformed, 0, points.Length); return(transformed); } catch (NoninvertibleTransformException e) { throw new Exception(e.Message, e); } }
private Point2D[] TransformPoints(Matrix transormationMatrix, bool inverse, params Point2D[] points) { AffineTransform t = new AffineTransform(transormationMatrix[Matrix.I11], transormationMatrix[Matrix.I12], transormationMatrix[Matrix.I21], transormationMatrix[Matrix.I22], transormationMatrix[Matrix.I31], transormationMatrix[Matrix.I32]); Point2D[] transformed = new Point2D[points.Length]; if (inverse) { t = t.CreateInverse(); } t.Transform(points, 0, transformed, 0, points.Length); return(transformed); }
private Point[] TransformPoints(Matrix transformationMatrix, bool inverse, params Point[] points) { AffineTransform t = new AffineTransform(transformationMatrix.Get(Matrix.I11), transformationMatrix.Get(Matrix .I12), transformationMatrix.Get(Matrix.I21), transformationMatrix.Get(Matrix.I22), transformationMatrix .Get(Matrix.I31), transformationMatrix.Get(Matrix.I32)); Point[] transformed = new Point[points.Length]; if (inverse) { try { t = t.CreateInverse(); } catch (NoninvertibleTransformException e) { throw new PdfException(CleanupExceptionMessageConstant.NONINVERTIBLE_MATRIX_CANNOT_BE_PROCESSED, e); } } t.Transform(points, 0, transformed, 0, points.Length); return(transformed); }
/// <summary> /// When in the svg element /// <c>overflow</c> /// is /// <c>visible</c> /// the corresponding formXObject /// should have a BBox (form XObject’s bounding box; see PDF 32000-1:2008 - 8.10.2 Form Dictionaries) /// that should cover the entire svg space (page in pdf) in order to be able to show parts of the element which are outside the current element viewPort. /// </summary> /// <remarks> /// When in the svg element /// <c>overflow</c> /// is /// <c>visible</c> /// the corresponding formXObject /// should have a BBox (form XObject’s bounding box; see PDF 32000-1:2008 - 8.10.2 Form Dictionaries) /// that should cover the entire svg space (page in pdf) in order to be able to show parts of the element which are outside the current element viewPort. /// To do this, we get the inverse matrix of all the current transformation matrix changes and apply it to the root viewPort. /// This allows you to get the root rectangle in the final coordinate system. /// </remarks> /// <param name="context">current context to get canvases and view ports</param> /// <param name="stream">stream to write a BBox</param> private static void WriteBBoxAccordingToVisibleOverflow(SvgDrawContext context, PdfStream stream) { IList <PdfCanvas> canvases = new List <PdfCanvas>(); int canvasesSize = context.Size(); for (int i = 0; i < canvasesSize; i++) { canvases.Add(context.PopCanvas()); } AffineTransform transform = new AffineTransform(); for (int i = canvases.Count - 1; i >= 0; i--) { PdfCanvas canvas = canvases[i]; Matrix matrix = canvas.GetGraphicsState().GetCtm(); transform.Concatenate(new AffineTransform(matrix.Get(0), matrix.Get(1), matrix.Get(3), matrix.Get(4), matrix .Get(6), matrix.Get(7))); context.PushCanvas(canvas); } try { transform = transform.CreateInverse(); } catch (NoninvertibleTransformException) { // Case with zero determiner (see PDF 32000-1:2008 - 8.3.4 Transformation Matrices - NOTE 3) // for example with a, b, c, d in cm equal to 0 stream.Put(PdfName.BBox, new PdfArray(new Rectangle(0, 0, 0, 0))); ILog logger = LogManager.GetLogger(typeof(AbstractBranchSvgNodeRenderer)); logger.Warn(SvgLogMessageConstant.UNABLE_TO_GET_INVERSE_MATRIX_DUE_TO_ZERO_DETERMINANT); return; } Point[] points = context.GetRootViewPort().ToPointsArray(); transform.Transform(points, 0, points, 0, points.Length); Rectangle bbox = Rectangle.CalculateBBox(JavaUtil.ArraysAsList(points)); stream.Put(PdfName.BBox, new PdfArray(bbox)); }
protected internal override void DoDraw(SvgDrawContext context) { if (this.attributesAndStyles != null) { String elementToReUse = this.attributesAndStyles.Get(SvgConstants.Attributes.XLINK_HREF); if (elementToReUse == null) { elementToReUse = this.attributesAndStyles.Get(SvgConstants.Attributes.HREF); } if (elementToReUse != null && !String.IsNullOrEmpty(elementToReUse) && IsValidHref(elementToReUse)) { String normalizedName = SvgTextUtil.FilterReferenceValue(elementToReUse); if (!context.IsIdUsedByUseTagBefore(normalizedName)) { ISvgNodeRenderer template = context.GetNamedObject(normalizedName); //Clone template ISvgNodeRenderer namedObject = template == null ? null : template.CreateDeepCopy(); //Resolve parent inheritance SvgNodeRendererInheritanceResolver iresolver = new SvgNodeRendererInheritanceResolver(); iresolver.ApplyInheritanceToSubTree(this, namedObject); if (namedObject != null) { if (namedObject is AbstractSvgNodeRenderer) { ((AbstractSvgNodeRenderer)namedObject).SetPartOfClipPath(partOfClipPath); } PdfCanvas currentCanvas = context.GetCurrentCanvas(); float x = 0f; float y = 0f; if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.X)) { x = CssUtils.ParseAbsoluteLength(this.attributesAndStyles.Get(SvgConstants.Attributes.X)); } if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.Y)) { y = CssUtils.ParseAbsoluteLength(this.attributesAndStyles.Get(SvgConstants.Attributes.Y)); } AffineTransform inverseMatrix = null; if (!CssUtils.CompareFloats(x, 0) || !CssUtils.CompareFloats(y, 0)) { AffineTransform translation = AffineTransform.GetTranslateInstance(x, y); currentCanvas.ConcatMatrix(translation); if (partOfClipPath) { try { inverseMatrix = translation.CreateInverse(); } catch (NoninvertibleTransformException ex) { LogManager.GetLogger(typeof(UseSvgNodeRenderer)).Warn(SvgLogMessageConstant.NONINVERTIBLE_TRANSFORMATION_MATRIX_USED_IN_CLIP_PATH , ex); } } } // setting the parent of the referenced element to this instance namedObject.SetParent(this); namedObject.Draw(context); // unsetting the parent of the referenced element namedObject.SetParent(null); if (inverseMatrix != null) { currentCanvas.ConcatMatrix(inverseMatrix); } } } } } }
public GradientPaintContext(ColorModel cm, Point2D p1, Point2D p2, AffineTransform xform, Color c1, Color c2, bool cyclic) { // First calculate the distance moved in user space when // we move a single unit along the X & Y axes in device space. Point2D xvec = new Point2D.Double(1, 0); Point2D yvec = new Point2D.Double(0, 1); try { AffineTransform inverse = xform.CreateInverse(); inverse.DeltaTransform(xvec, xvec); inverse.DeltaTransform(yvec, yvec); } catch (NoninvertibleTransformException) { xvec.SetLocation(0, 0); yvec.SetLocation(0, 0); } // Now calculate the (square of the) user space distance // between the anchor points. This value equals: // (UserVec . UserVec) double udx = p2.X - p1.X; double udy = p2.Y - p1.Y; double ulenSq = udx * udx + udy * udy; if (ulenSq <= Double.Epsilon) { Dx = 0; Dy = 0; } else { // Now calculate the proportional distance moved along the // vector from p1 to p2 when we move a unit along X & Y in // device space. // // The length of the projection of the Device Axis Vector is // its dot product with the Unit User Vector: // (DevAxisVec . (UserVec / Len(UserVec)) // // The "proportional" length is that length divided again // by the length of the User Vector: // (DevAxisVec . (UserVec / Len(UserVec))) / Len(UserVec) // which simplifies to: // ((DevAxisVec . UserVec) / Len(UserVec)) / Len(UserVec) // which simplifies to: // (DevAxisVec . UserVec) / LenSquared(UserVec) Dx = (xvec.X * udx + xvec.Y * udy) / ulenSq; Dy = (yvec.X * udx + yvec.Y * udy) / ulenSq; if (cyclic) { Dx = Dx % 1.0; Dy = Dy % 1.0; } else { // We are acyclic if (Dx < 0) { // If we are using the acyclic form below, we need // dx to be non-negative for simplicity of scanning // across the scan lines for the transition points. // To ensure that constraint, we negate the dx/dy // values and swap the points and colors. Point2D p = p1; p1 = p2; p2 = p; Color c = c1; c1 = c2; c2 = c; Dx = -Dx; Dy = -Dy; } } } Point2D dp1 = xform.Transform(p1, null); this.X1 = dp1.X; this.Y1 = dp1.Y; this.Cyclic = cyclic; int rgb1 = c1.RGB; int rgb2 = c2.RGB; int a1 = (rgb1 >> 24) & 0xff; int r1 = (rgb1 >> 16) & 0xff; int g1 = (rgb1 >> 8) & 0xff; int b1 = (rgb1) & 0xff; int da = ((rgb2 >> 24) & 0xff) - a1; int dr = ((rgb2 >> 16) & 0xff) - r1; int dg = ((rgb2 >> 8) & 0xff) - g1; int db = ((rgb2) & 0xff) - b1; if (a1 == 0xff && da == 0) { Model = Xrgbmodel; if (cm is DirectColorModel) { DirectColorModel dcm = (DirectColorModel)cm; int tmp = dcm.AlphaMask; if ((tmp == 0 || tmp == 0xff) && dcm.RedMask == 0xff && dcm.GreenMask == 0xff00 && dcm.BlueMask == 0xff0000) { Model = Xbgrmodel; tmp = r1; r1 = b1; b1 = tmp; tmp = dr; dr = db; db = tmp; } } } else { Model = ColorModel.RGBdefault; } Interp = new int[cyclic ? 513 : 257]; for (int i = 0; i <= 256; i++) { float rel = i / 256.0f; int rgb = (((int)(a1 + da * rel)) << 24) | (((int)(r1 + dr * rel)) << 16) | (((int)(g1 + dg * rel)) << 8) | (((int)(b1 + db * rel))); Interp[i] = rgb; if (cyclic) { Interp[512 - i] = rgb; } } }