public override void CacheRenderingRegion(ISvgRenderer renderer) { SvgTransformableElement refEl = ReferencedElement as SvgTransformableElement; if (refEl == null) { return; } if (renderingNode == null) { renderingNode = renderer.GetRenderingNode(this); } // Check if it has already been calculated if (renderingNode != null && renderingNode.ScreenRegion != RectangleF.Empty) { return; } XmlElement refElParent = (XmlElement)refEl.ParentNode; OwnerDocument.Static = true; CopyToReferencedElement(refEl); refElParent.RemoveChild(refEl); this.AppendChild(refEl); refEl.CacheRenderingRegion(renderer); renderingNode.ScreenRegion = refEl.RenderingNode.ScreenRegion; this.RemoveChild(refEl); RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); OwnerDocument.Static = false; }
public ISvgMatrix GetScreenCTM() { ISvgMatrix matrix = new SvgMatrix(); ISvgTransformList svgTList; ISvgMatrix vCTM; // if (this is SvgSvgElement) if (string.Equals(this.Name, "svg", StringComparison.Ordinal)) { vCTM = ((SvgSvgElement)this).ViewBoxTransform; matrix = vCTM; } else if (this.Transform != null) { svgTList = this.Transform.AnimVal; matrix = svgTList.Consolidate().Matrix; } SvgTransformableElement par = ParentNode as SvgTransformableElement; while (par != null) { // TODO: other elements can establish viewports, not just <svg>! //if (par is SvgSvgElement) if (string.Equals(par.Name, "svg", StringComparison.Ordinal)) { vCTM = ((SvgSvgElement)par).ViewBoxTransform; matrix = vCTM.Multiply(matrix); } else { svgTList = par.Transform.AnimVal; matrix = svgTList.Consolidate().Matrix.Multiply(matrix); } par = par.ParentNode as SvgTransformableElement; } // Now scale out the pixels //ISvgSvgElement root = OwnerDocument.RootElement; //float innerWidth = this.OwnerDocument.Window.InnerWidth; //float innerHeight = this.OwnerDocument.Window.InnerHeight; //if (innerWidth != 0 && innerHeight != 0) //{ // float screenRatW = (float)root.Width.AnimVal.Value / innerWidth; // float screenRatH = (float)root.Height.AnimVal.Value / innerHeight; // matrix.ScaleNonUniform(screenRatW, screenRatH); //} return(matrix); }
/// <summary> /// For each given element, the accumulation of all transformations that have been defined /// on the given element and all of its ancestors up to and including the element that /// established the current viewport (usually, the 'svg' element which is the most /// immediate ancestor to the given element) is called the current transformation matrix /// or CTM. /// </summary> /// <returns>A matrix representing the mapping of current user coordinates to viewport /// coordinates.</returns> public ISvgMatrix GetCTM() { ISvgMatrix matrix = new SvgMatrix(); ISvgTransformList svgTList; ISvgMatrix vCTM; // if (this is SvgSvgElement) if (string.Equals(this.Name, "svg", StringComparison.Ordinal)) { vCTM = ((SvgSvgElement)this).ViewBoxTransform; matrix = vCTM; } else if (this.Transform != null) { svgTList = this.Transform.AnimVal; matrix = svgTList.Consolidate().Matrix; } ISvgElement nVE = this.NearestViewportElement; if (nVE != null) { SvgTransformableElement par = ParentNode as SvgTransformableElement; while (par != null && par != nVE) { svgTList = par.Transform.AnimVal; matrix = svgTList.Consolidate().Matrix.Multiply(matrix); par = par.ParentNode as SvgTransformableElement; } // if (par == nVE && nVE is SvgSvgElement) if (par == nVE && string.Equals(nVE.Name, "svg", StringComparison.Ordinal)) { vCTM = ((SvgSvgElement)nVE).ViewBoxTransform; matrix = vCTM.Multiply(matrix); } } return(matrix); }
/// <summary> /// For each given element, the accumulation of all transformations that have been defined /// on the given element and all of its ancestors up to and including the element that /// established the current viewport (usually, the 'svg' element which is the most /// immediate ancestor to the given element) is called the current transformation matrix /// or CTM. /// </summary> /// <returns>A matrix representing the mapping of current user coordinates to viewport /// coordinates.</returns> public ISvgMatrix GetCTM() { ISvgMatrix matrix = new SvgMatrix(); ISvgTransformList svgTList; ISvgMatrix vCTM; if (this is SvgSvgElement) { vCTM = (this as SvgSvgElement).ViewBoxTransform; matrix = vCTM; } else if (this.Transform != null) { svgTList = this.Transform.AnimVal; matrix = svgTList.Consolidate().Matrix; } ISvgElement nVE = this.NearestViewportElement; if (nVE != null) { SvgTransformableElement par = ParentNode as SvgTransformableElement; while (par != null && par != nVE) { svgTList = par.Transform.AnimVal; matrix = svgTList.Consolidate().Matrix.Multiply(matrix); par = par.ParentNode as SvgTransformableElement; } if (par == nVE && nVE is SvgSvgElement) { vCTM = (nVE as SvgSvgElement).ViewBoxTransform; matrix = vCTM.Multiply(matrix); } } return(matrix); }
private SvgRectF GetElementBounds(SvgTransformableElement element, float margin) { SvgRenderingHint hint = element.RenderingHint; if (hint == SvgRenderingHint.Shape || hint == SvgRenderingHint.Text) { GraphicsPath gp = GdiRendering.CreatePath(element); ISvgMatrix svgMatrix = element.GetScreenCTM(); Matrix matrix = new Matrix((float)svgMatrix.A, (float)svgMatrix.B, (float)svgMatrix.C, (float)svgMatrix.D, (float)svgMatrix.E, (float)svgMatrix.F); SvgRectF bounds = SvgConverter.ToRect(gp.GetBounds(matrix)); bounds = SvgRectF.Inflate(bounds, margin, margin); return bounds; } SvgUseElement useElement = element as SvgUseElement; if (useElement != null) { SvgTransformableElement refEl = useElement.ReferencedElement as SvgTransformableElement; if (refEl == null) return SvgRectF.Empty; XmlElement refElParent = (XmlElement)refEl.ParentNode; element.OwnerDocument.Static = true; useElement.CopyToReferencedElement(refEl); element.AppendChild(refEl); SvgRectF bbox = this.GetElementBounds(refEl, margin); element.RemoveChild(refEl); useElement.RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); element.OwnerDocument.Static = false; return bbox; } SvgRectF union = SvgRectF.Empty; SvgTransformableElement transformChild; foreach (XmlNode childNode in element.ChildNodes) { if (childNode is SvgDefsElement) continue; if (childNode is ISvgTransformable) { transformChild = (SvgTransformableElement)childNode; SvgRectF bbox = this.GetElementBounds(transformChild, margin); if (bbox != SvgRectF.Empty) { if (union == SvgRectF.Empty) union = bbox; else union = SvgRectF.Union(union, bbox); } } } return union; }
public RectangleF GetBRect(float margin) { if (this is ISharpGDIPath) { ISharpGDIPath gdiPathElm = (ISharpGDIPath)this; GraphicsPath gp = gdiPathElm.GetGraphicsPath(); SvgMatrix svgMatrix = (SvgMatrix)this.GetScreenCTM(); RectangleF bounds = gp.GetBounds(svgMatrix.ToMatrix()); bounds = RectangleF.Inflate(bounds, margin, margin); return(bounds); } else if (this is ISvgUseElement) { SvgUseElement use = (SvgUseElement)this; SvgTransformableElement refEl = use.ReferencedElement as SvgTransformableElement; if (refEl == null) { return(RectangleF.Empty); } XmlElement refElParent = (XmlElement)refEl.ParentNode; OwnerDocument.Static = true; use.CopyToReferencedElement(refEl); this.AppendChild(refEl); RectangleF bbox = refEl.GetBRect(margin); this.RemoveChild(refEl); use.RestoreReferencedElement(refEl); refElParent.AppendChild(refEl); OwnerDocument.Static = false; return(bbox); } else { RectangleF union = RectangleF.Empty; SvgTransformableElement transformChild; foreach (XmlNode childNode in ChildNodes) { if (childNode is SvgDefsElement) { continue; } if (childNode is ISvgTransformable) { transformChild = (SvgTransformableElement)childNode; RectangleF bbox = transformChild.GetBRect(margin); if (bbox != RectangleF.Empty) { if (union == RectangleF.Empty) { union = bbox; } else { union = RectangleF.Union(union, bbox); } } } } return(union); } }