//const float defaultViewportScale = 800f; public static SVGMatrix GetFillTransform(SVGFill svgFill, Rect bounds, SVGLength[] gradientStart, SVGLength[] gradientEnd, SVGMatrix fillTransform, SVGMatrix gradientTransform) { SVGMatrix transform = SVGMatrix.identity; SVGLength gradientStartX = gradientStart[0]; SVGLength gradientStartY = gradientStart[1]; SVGLength gradientEndX = gradientEnd[0]; SVGLength gradientEndY = gradientEnd[1]; Rect viewport = svgFill.viewport; //Debug.Log(viewport); if (svgFill.fillType == FILL_TYPE.GRADIENT) { switch (svgFill.gradientType) { case GRADIENT_TYPE.LINEAR: { Vector2 startPoint = GetGradientVector(gradientStartX, gradientStartY, bounds); Vector2 endPoint = GetGradientVector(gradientEndX, gradientEndY, bounds); Vector2 gradientVector = endPoint - startPoint; Vector2 normalizedVector = Vector2.zero; float angle = Mathf.Atan2(gradientVector.y, gradientVector.x) * Mathf.Rad2Deg; Vector2 posDiff = Vector2.Lerp(startPoint, endPoint, 0.5f); float magnitude = gradientVector.magnitude; if (magnitude != 0f) { normalizedVector.x = viewport.width / magnitude; normalizedVector.y = viewport.height / magnitude; } transform = transform.Translate(viewport.center); transform = transform.Scale(normalizedVector.x, normalizedVector.y); transform = transform.Rotate(-angle); transform = transform.Translate(-posDiff); transform = transform.Multiply(gradientTransform.Inverse()); transform = transform.Multiply(fillTransform.Inverse()); break; } case GRADIENT_TYPE.RADIAL: { Vector2 point = GetGradientVector(gradientStartX, gradientStartY, bounds); float radius = GetGradientVector(gradientEndX, gradientEndY, bounds).x; if (gradientEndX.unitType == SVGLengthType.Percentage) { radius *= 0.5f; } float radiusTimesTwo = radius * 2f; Vector2 normalizedVector = Vector2.zero; if (radiusTimesTwo != 0f) { normalizedVector.x = viewport.width / radiusTimesTwo; normalizedVector.y = viewport.height / radiusTimesTwo; } transform = transform.Translate(viewport.center); transform = transform.Scale(normalizedVector.x, normalizedVector.y); transform = transform.Translate(-point); transform = transform.Multiply(gradientTransform.Inverse()); transform = transform.Multiply(fillTransform.Inverse()); break; } case GRADIENT_TYPE.CONICAL: { Vector2 point = GetGradientVector(gradientStartX, gradientStartY, bounds); float radius = GetGradientVector(gradientEndX, gradientEndY, bounds).x; if (gradientEndX.unitType == SVGLengthType.Percentage) { radius *= 0.5f; } float radiusTimesTwo = radius * 2f; Vector2 normalizedVector = Vector2.zero; if (radiusTimesTwo != 0f) { normalizedVector.x = viewport.width / radiusTimesTwo; normalizedVector.y = viewport.height / radiusTimesTwo; } transform = transform.Translate(viewport.center); transform = transform.Scale(normalizedVector.x, normalizedVector.y); transform = transform.Translate(-point); transform = transform.Multiply(gradientTransform.Inverse()); transform = transform.Multiply(fillTransform.Inverse()); break; } } } return(transform); }
public static SVGMatrix GetViewBoxTransform(AttributeList attributeList, ref Rect viewport, bool negotiate = false) { SVGMatrix matrix = SVGMatrix.identity; float x = 0.0f; float y = 0.0f; float w = 0.0f; float h = 0.0f; string preserveAspectRatio = attributeList.GetValue("preserveAspectRatio"); string viewBox = attributeList.GetValue("viewBox"); if (!string.IsNullOrEmpty(viewBox)) { string[] viewBoxValues = SVGStringExtractor.ExtractTransformValue(viewBox); if (viewBoxValues.Length == 4) { Rect contentRect = new Rect( new SVGLength(viewBoxValues[0]).value, new SVGLength(viewBoxValues[1]).value, new SVGLength(viewBoxValues[2]).value, new SVGLength(viewBoxValues[3]).value ); SVGViewport.Align align = SVGViewport.Align.xMidYMid; SVGViewport.MeetOrSlice meetOrSlice = SVGViewport.MeetOrSlice.Meet; if (!string.IsNullOrEmpty(preserveAspectRatio)) { string[] aspectRatioValues = SVGStringExtractor.ExtractStringArray(preserveAspectRatio); align = SVGViewport.GetAlignFromStrings(aspectRatioValues); meetOrSlice = SVGViewport.GetMeetOrSliceFromStrings(aspectRatioValues); } Rect oldViewport = viewport; viewport = SVGViewport.GetViewport(viewport, contentRect, align, meetOrSlice); float sizeX = 0f, sizeY = 0f; if (oldViewport.size.x != 0f) { sizeX = viewport.size.x / oldViewport.size.x; } if (oldViewport.size.y != 0f) { sizeY = viewport.size.y / oldViewport.size.y; } matrix.Scale(sizeX, sizeY); matrix = matrix.Translate(viewport.x - oldViewport.x, viewport.y - oldViewport.y); } } else { if (negotiate) { string attrXString = attributeList.GetValue("x"); string attrYString = attributeList.GetValue("y"); string attrWidthString = attributeList.GetValue("width"); string attrHeightString = attributeList.GetValue("height"); SVGLength attrX = new SVGLength(SVGLengthType.PX, 0f), attrY = new SVGLength(SVGLengthType.PX, 0f), attrWidth = new SVGLength(SVGLengthType.PX, 1f), attrHeight = new SVGLength(SVGLengthType.PX, 1f); if (!string.IsNullOrEmpty(attrXString)) { attrX = new SVGLength(attrXString); } if (!string.IsNullOrEmpty(attrYString)) { attrY = new SVGLength(attrYString); } if (!string.IsNullOrEmpty(attrWidthString)) { attrWidth = new SVGLength(attrWidthString); } if (!string.IsNullOrEmpty(attrHeightString)) { attrHeight = new SVGLength(attrHeightString); } x = attrX.value; y = attrY.value; w = attrWidth.value; h = attrHeight.value; float x_ratio = 1f; if (w != 0f) { x_ratio = attrWidth.value / w; } float y_ratio = 1f; if (h != 0f) { y_ratio = attrHeight.value / h; } matrix = matrix.Scale(x_ratio, y_ratio); matrix = matrix.Translate(x, y); viewport = new Rect(x, y, w, h); // Debug.Log(string.Format("x: {0}, y: {1}, width: {2}, height: {3}, attrWidth: {4}, attrHeight: {5}", x, y, w, h, attrWidth, attrHeight)); } // Debug.Log(string.Format("x: {0}, y: {1}, width: {2}, height: {3}, attrWidth: {4}, attrHeight: {5}", x, y, w, h, attrWidth, attrHeight)); } return(matrix); }
//const float defaultViewportScale = 800f; private static SVGMatrix GetFillTransform(SVGPaintable svgPaintable, Rect bounds) { SVGFill svgFill = svgPaintable.svgFill; SVGMatrix transform = new SVGMatrix(); SVGMatrix gradientMatrix = svgFill.gradientTransform; Rect viewport = svgPaintable.viewport; if (svgFill.fillType == FILL_TYPE.GRADIENT) { switch (svgFill.gradientType) { case GRADIENT_TYPE.LINEAR: { Vector2 startPoint = GetGradientVector(svgFill.gradientStartX, svgFill.gradientStartY, bounds); Vector2 endPoint = GetGradientVector(svgFill.gradientEndX, svgFill.gradientEndY, bounds); Vector2 gradientVector = endPoint - startPoint; Vector2 normalizedVector = Vector2.zero; float angle = Mathf.Atan2(gradientVector.y, gradientVector.x) * Mathf.Rad2Deg; Vector2 posDiff = Vector2.Lerp(startPoint, endPoint, 0.5f); float magnitude = gradientVector.magnitude; if(magnitude != 0f) { normalizedVector.x = viewport.width / magnitude; normalizedVector.y = viewport.height / magnitude; } transform = transform.Translate(viewport.center); transform = transform.ScaleNonUniform(normalizedVector.x, normalizedVector.y); transform = transform.Rotate(-angle); transform = transform.Translate(-posDiff); transform = transform.Multiply(gradientMatrix.Inverse()); transform = transform.Multiply(svgFill.transform.Inverse()); break; } case GRADIENT_TYPE.RADIAL: { Vector2 point = GetGradientVector(svgFill.gradientStartX, svgFill.gradientStartY, bounds); float radius = GetGradientVector(svgFill.gradientEndX, svgFill.gradientEndY, bounds).x; if(svgFill.gradientEndX.unitType == SVGLengthType.Percentage) radius *= 0.5f; float radiusTimesTwo = radius * 2f; Vector2 normalizedVector = Vector2.zero; if(radiusTimesTwo != 0f) { normalizedVector.x = viewport.width / radiusTimesTwo; normalizedVector.y = viewport.height / radiusTimesTwo; } transform = transform.Translate(viewport.center); transform = transform.ScaleNonUniform(normalizedVector.x, normalizedVector.y); transform = transform.Translate(-point); transform = transform.Multiply(gradientMatrix.Inverse()); transform = transform.Multiply(svgFill.transform.Inverse()); break; } case GRADIENT_TYPE.CONICAL: { Vector2 point = GetGradientVector(svgFill.gradientStartX, svgFill.gradientStartY, bounds); float radius = GetGradientVector(svgFill.gradientEndX, svgFill.gradientEndY, bounds).x; if(svgFill.gradientEndX.unitType == SVGLengthType.Percentage) radius *= 0.5f; float radiusTimesTwo = radius * 2f; Vector2 normalizedVector = Vector2.zero; if(radiusTimesTwo != 0f) { normalizedVector.x = viewport.width / radiusTimesTwo; normalizedVector.y = viewport.height / radiusTimesTwo; } transform = transform.Translate(viewport.center); transform = transform.ScaleNonUniform(normalizedVector.x, normalizedVector.y); transform = transform.Translate(-point); transform = transform.Multiply(gradientMatrix.Inverse()); transform = transform.Multiply(svgFill.transform.Inverse()); break; } } } return transform; }
//const float defaultViewportScale = 800f; private static SVGMatrix GetFillTransform(SVGPaintable svgPaintable, Rect bounds) { SVGFill svgFill = svgPaintable.svgFill; SVGMatrix transform = new SVGMatrix(); SVGMatrix gradientMatrix = svgFill.gradientTransform; Rect viewport = svgPaintable.viewport; if (svgFill.fillType == FILL_TYPE.GRADIENT) { switch (svgFill.gradientType) { case GRADIENT_TYPE.LINEAR: { Vector2 startPoint = GetGradientVector(svgFill.gradientStartX, svgFill.gradientStartY, bounds); Vector2 endPoint = GetGradientVector(svgFill.gradientEndX, svgFill.gradientEndY, bounds); Vector2 gradientVector = endPoint - startPoint; Vector2 normalizedVector = Vector2.zero; float angle = Mathf.Atan2(gradientVector.y, gradientVector.x) * Mathf.Rad2Deg; Vector2 posDiff = Vector2.Lerp(startPoint, endPoint, 0.5f); float magnitude = gradientVector.magnitude; if (magnitude != 0f) { normalizedVector.x = viewport.width / magnitude; normalizedVector.y = viewport.height / magnitude; } transform = transform.Translate(viewport.center); transform = transform.ScaleNonUniform(normalizedVector.x, normalizedVector.y); transform = transform.Rotate(-angle); transform = transform.Translate(-posDiff); transform = transform.Multiply(gradientMatrix.Inverse()); transform = transform.Multiply(svgFill.transform.Inverse()); break; } case GRADIENT_TYPE.RADIAL: { Vector2 point = GetGradientVector(svgFill.gradientStartX, svgFill.gradientStartY, bounds); float radius = GetGradientVector(svgFill.gradientEndX, svgFill.gradientEndY, bounds).x; if (svgFill.gradientEndX.unitType == SVGLengthType.Percentage) { radius *= 0.5f; } float radiusTimesTwo = radius * 2f; Vector2 normalizedVector = Vector2.zero; if (radiusTimesTwo != 0f) { normalizedVector.x = viewport.width / radiusTimesTwo; normalizedVector.y = viewport.height / radiusTimesTwo; } transform = transform.Translate(viewport.center); transform = transform.ScaleNonUniform(normalizedVector.x, normalizedVector.y); transform = transform.Translate(-point); transform = transform.Multiply(gradientMatrix.Inverse()); transform = transform.Multiply(svgFill.transform.Inverse()); break; } case GRADIENT_TYPE.CONICAL: { Vector2 point = GetGradientVector(svgFill.gradientStartX, svgFill.gradientStartY, bounds); float radius = GetGradientVector(svgFill.gradientEndX, svgFill.gradientEndY, bounds).x; if (svgFill.gradientEndX.unitType == SVGLengthType.Percentage) { radius *= 0.5f; } float radiusTimesTwo = radius * 2f; Vector2 normalizedVector = Vector2.zero; if (radiusTimesTwo != 0f) { normalizedVector.x = viewport.width / radiusTimesTwo; normalizedVector.y = viewport.height / radiusTimesTwo; } transform = transform.Translate(viewport.center); transform = transform.ScaleNonUniform(normalizedVector.x, normalizedVector.y); transform = transform.Translate(-point); transform = transform.Multiply(gradientMatrix.Inverse()); transform = transform.Multiply(svgFill.transform.Inverse()); break; } } } return(transform); }
public static SVGMatrix GetViewBoxTransform(AttributeList attributeList, ref Rect viewport, bool negotiate = false) { SVGMatrix matrix = new SVGMatrix(); float x = 0.0f; float y = 0.0f; float w = 0.0f; float h = 0.0f; string preserveAspectRatio = attributeList.GetValue("preserveAspectRatio"); string viewBox = attributeList.GetValue("viewBox"); if (!string.IsNullOrEmpty(viewBox)) { string[] viewBoxValues = SVGStringExtractor.ExtractTransformValue(viewBox); if(viewBoxValues.Length == 4) { Rect contentRect = new Rect( new SVGLength(viewBoxValues[0]).value, new SVGLength(viewBoxValues[1]).value, new SVGLength(viewBoxValues[2]).value, new SVGLength(viewBoxValues[3]).value ); SVGViewport.Align align = SVGViewport.Align.xMidYMid; SVGViewport.MeetOrSlice meetOrSlice = SVGViewport.MeetOrSlice.Meet; if(!string.IsNullOrEmpty(preserveAspectRatio)) { string[] aspectRatioValues = SVGStringExtractor.ExtractStringArray(preserveAspectRatio); align = SVGViewport.GetAlignFromStrings(aspectRatioValues); meetOrSlice = SVGViewport.GetMeetOrSliceFromStrings(aspectRatioValues); } Rect oldViewport = viewport; viewport = SVGViewport.GetViewport(viewport, contentRect, align, meetOrSlice); float sizeX = 0f, sizeY = 0f; if(oldViewport.size.x != 0f) sizeX = viewport.size.x / oldViewport.size.x; if(oldViewport.size.y != 0f) sizeY = viewport.size.y / oldViewport.size.y; matrix.ScaleNonUniform(sizeX, sizeY); matrix = matrix.Translate(viewport.x - oldViewport.x, viewport.y - oldViewport.y); } } else { if(negotiate) { string attrXString = attributeList.GetValue("x"); string attrYString = attributeList.GetValue("y"); string attrWidthString = attributeList.GetValue("width"); string attrHeightString = attributeList.GetValue("height"); SVGLength attrX = new SVGLength(SVGLengthType.PX, 0f), attrY = new SVGLength(SVGLengthType.PX, 0f), attrWidth = new SVGLength(SVGLengthType.PX, 1f), attrHeight = new SVGLength(SVGLengthType.PX, 1f); if(!string.IsNullOrEmpty(attrXString)) { attrX = new SVGLength(attrXString); } if(!string.IsNullOrEmpty(attrYString)) { attrY = new SVGLength(attrYString); } if(!string.IsNullOrEmpty(attrWidthString)) { attrWidth = new SVGLength(attrWidthString); } if(!string.IsNullOrEmpty(attrHeightString)) { attrHeight = new SVGLength(attrHeightString); } x = attrX.value; y = attrY.value; w = attrWidth.value; h = attrHeight.value; float x_ratio = 1f; if(w != 0f) x_ratio = attrWidth.value / w; float y_ratio = 1f; if(h != 0f) y_ratio = attrHeight.value / h; matrix = matrix.ScaleNonUniform(x_ratio, y_ratio); matrix = matrix.Translate(x, y); viewport = new Rect(x, y, w, h); // Debug.Log(string.Format("x: {0}, y: {1}, width: {2}, height: {3}, attrWidth: {4}, attrHeight: {5}", x, y, w, h, attrWidth, attrHeight)); } // Debug.Log(string.Format("x: {0}, y: {1}, width: {2}, height: {3}, attrWidth: {4}, attrHeight: {5}", x, y, w, h, attrWidth, attrHeight)); } return matrix; }