void SVGAnimateMotionElement::resetToBaseValue(const String&) { if (!hasValidAttributeType()) return; AffineTransform* transform = targetElement()->supplementalTransform(); if (!transform) return; transform->makeIdentity(); }
void SVGAnimateMotionElement::calculateAnimatedValue(float percentage, unsigned, SVGSMILElement*) { SVGElement* targetElement = this->targetElement(); if (!targetElement) return; AffineTransform* transform = targetElement->supplementalTransform(); if (!transform) return; if (RenderObject* targetRenderer = targetElement->renderer()) targetRenderer->setNeedsTransformUpdate(); if (!isAdditive()) transform->makeIdentity(); // FIXME: Implement accumulate. if (animationMode() == PathAnimation) { ASSERT(!animationPath().isEmpty()); Path path = animationPath(); float positionOnPath = path.length() * percentage; bool ok; FloatPoint position = path.pointAtLength(positionOnPath, ok); if (ok) { transform->translate(position.x(), position.y()); RotateMode rotateMode = this->rotateMode(); if (rotateMode == RotateAuto || rotateMode == RotateAutoReverse) { float angle = path.normalAngleAtLength(positionOnPath, ok); if (rotateMode == RotateAutoReverse) angle += 180; transform->rotate(angle); } } return; } FloatSize diff = m_toPoint - m_fromPoint; transform->translate(diff.width() * percentage + m_fromPoint.x(), diff.height() * percentage + m_fromPoint.y()); }
void SVGAnimateMotionElement::calculateAnimatedValue(float percentage, unsigned repeatCount, SVGSMILElement*) { SVGElement* targetElement = this->targetElement(); if (!targetElement) return; AffineTransform* transform = targetElement->supplementalTransform(); if (!transform) return; if (RenderObject* targetRenderer = targetElement->renderer()) targetRenderer->setNeedsTransformUpdate(); if (!isAdditive()) transform->makeIdentity(); if (animationMode() != PathAnimation) { FloatPoint toPointAtEndOfDuration = m_toPoint; if (isAccumulated() && repeatCount && m_hasToPointAtEndOfDuration) toPointAtEndOfDuration = m_toPointAtEndOfDuration; float animatedX = 0; animateAdditiveNumber(percentage, repeatCount, m_fromPoint.x(), m_toPoint.x(), toPointAtEndOfDuration.x(), animatedX); float animatedY = 0; animateAdditiveNumber(percentage, repeatCount, m_fromPoint.y(), m_toPoint.y(), toPointAtEndOfDuration.y(), animatedY); transform->translate(animatedX, animatedY); return; } buildTransformForProgress(transform, percentage); // Handle accumulate="sum". if (isAccumulated() && repeatCount) { for (unsigned i = 0; i < repeatCount; ++i) buildTransformForProgress(transform, 1); } }
/* This function uses tiling to improve the performance of the shadow drawing of rounded rectangles. The code basically does the following steps: 1. Calculate the size of the shadow template, a rectangle that contains all the necessary tiles to draw the complete shadow. 2. If that size is smaller than the real rectangle render the new template rectangle and its shadow in a new surface, in other case render the shadow of the real rectangle in the destination surface. 3. Calculate the sizes and positions of the tiles and their destinations and use drawPattern to render the final shadow. The code divides the rendering in 8 tiles: 1 | 2 | 3 ----------- 4 | | 5 ----------- 6 | 7 | 8 The corners are directly copied from the template rectangle to the real one and the side tiles are 1 pixel width, we use them as tiles to cover the destination side. The corner tiles are bigger than just the side of the rounded corner, we need to increase it because the modifications caused by the corner over the blur effect. We fill the central part with solid color to complete the shadow. */ void ContextShadow::drawRectShadow(GraphicsContext* context, const IntRect& rect, const IntSize& topLeftRadius, const IntSize& topRightRadius, const IntSize& bottomLeftRadius, const IntSize& bottomRightRadius) { float radiusTwice = m_blurDistance * 2; // Find the space the corners need inside the rect for its shadows. int internalShadowWidth = radiusTwice + max(topLeftRadius.width(), bottomLeftRadius.width()) + max(topRightRadius.width(), bottomRightRadius.width()); int internalShadowHeight = radiusTwice + max(topLeftRadius.height(), topRightRadius.height()) + max(bottomLeftRadius.height(), bottomRightRadius.height()); cairo_t* cr = context->platformContext()->cr(); float globalAlpha = context->platformContext()->globalAlpha(); // drawShadowedRect still does not work with rotations. // https://bugs.webkit.org/show_bug.cgi?id=45042 if ((!context->getCTM().isIdentityOrTranslationOrFlipped()) || (internalShadowWidth > rect.width()) || (internalShadowHeight > rect.height()) || (m_type != BlurShadow)) { drawRectShadowWithoutTiling(context, rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, globalAlpha); return; } // Calculate size of the template shadow buffer. IntSize shadowBufferSize = IntSize(rect.width() + radiusTwice, rect.height() + radiusTwice); // Determine dimensions of shadow rect. FloatRect shadowRect = FloatRect(rect.location(), shadowBufferSize); shadowRect.move(- m_blurDistance, - m_blurDistance); // Size of the tiling side. int sideTileWidth = 1; // The length of a side of the buffer is the enough space for four blur radii, // the radii of the corners, and then 1 pixel to draw the side tiles. IntSize shadowTemplateSize = IntSize(sideTileWidth + radiusTwice + internalShadowWidth, sideTileWidth + radiusTwice + internalShadowHeight); // Reduce the size of what we have to draw with the clip area. double x1, x2, y1, y2; cairo_clip_extents(cr, &x1, &y1, &x2, &y2); calculateLayerBoundingRect(context, shadowRect, IntRect(x1, y1, x2 - x1, y2 - y1)); if ((shadowTemplateSize.width() * shadowTemplateSize.height() > m_sourceRect.width() * m_sourceRect.height())) { drawRectShadowWithoutTiling(context, rect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius, globalAlpha); return; } shadowRect.move(m_offset.width(), m_offset.height()); m_layerImage = getScratchBuffer(shadowTemplateSize); // Draw shadow into a new ImageBuffer. m_layerContext = cairo_create(m_layerImage); // Clear the surface first. cairo_set_operator(m_layerContext, CAIRO_OPERATOR_CLEAR); cairo_paint(m_layerContext); cairo_set_operator(m_layerContext, CAIRO_OPERATOR_OVER); // Draw the rectangle. IntRect templateRect = IntRect(m_blurDistance, m_blurDistance, shadowTemplateSize.width() - radiusTwice, shadowTemplateSize.height() - radiusTwice); Path path; path.addRoundedRect(templateRect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius); appendWebCorePathToCairoContext(m_layerContext, path); cairo_set_source_rgba(m_layerContext, 0, 0, 0, globalAlpha); cairo_fill(m_layerContext); // Blur the image. cairo_surface_flush(m_layerImage); blurLayerImage(cairo_image_surface_get_data(m_layerImage), shadowTemplateSize, cairo_image_surface_get_stride(m_layerImage)); cairo_surface_mark_dirty(m_layerImage); // Mask the image with the shadow color. cairo_set_operator(m_layerContext, CAIRO_OPERATOR_IN); setSourceRGBAFromColor(m_layerContext, m_color); cairo_paint(m_layerContext); cairo_destroy(m_layerContext); m_layerContext = 0; // Fill the internal part of the shadow. shadowRect.inflate(-radiusTwice); if (!shadowRect.isEmpty()) { cairo_save(cr); path.clear(); path.addRoundedRect(shadowRect, topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius); appendWebCorePathToCairoContext(cr, path); setSourceRGBAFromColor(cr, m_color); cairo_fill(cr); cairo_restore(cr); } shadowRect.inflate(radiusTwice); // Draw top side. FloatRect tileRect = FloatRect(radiusTwice + topLeftRadius.width(), 0, sideTileWidth, radiusTwice); FloatRect destRect = tileRect; destRect.move(shadowRect.x(), shadowRect.y()); destRect.setWidth(shadowRect.width() - topLeftRadius.width() - topRightRadius.width() - m_blurDistance * 4); FloatPoint phase = getPhase(destRect, tileRect); AffineTransform patternTransform; patternTransform.makeIdentity(); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Draw the bottom side. tileRect = FloatRect(radiusTwice + bottomLeftRadius.width(), shadowTemplateSize.height() - radiusTwice, sideTileWidth, radiusTwice); destRect = tileRect; destRect.move(shadowRect.x(), shadowRect.y() + radiusTwice + rect.height() - shadowTemplateSize.height()); destRect.setWidth(shadowRect.width() - bottomLeftRadius.width() - bottomRightRadius.width() - m_blurDistance * 4); phase = getPhase(destRect, tileRect); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Draw the right side. tileRect = FloatRect(shadowTemplateSize.width() - radiusTwice, radiusTwice + topRightRadius.height(), radiusTwice, sideTileWidth); destRect = tileRect; destRect.move(shadowRect.x() + radiusTwice + rect.width() - shadowTemplateSize.width(), shadowRect.y()); destRect.setHeight(shadowRect.height() - topRightRadius.height() - bottomRightRadius.height() - m_blurDistance * 4); phase = getPhase(destRect, tileRect); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Draw the left side. tileRect = FloatRect(0, radiusTwice + topLeftRadius.height(), radiusTwice, sideTileWidth); destRect = tileRect; destRect.move(shadowRect.x(), shadowRect.y()); destRect.setHeight(shadowRect.height() - topLeftRadius.height() - bottomLeftRadius.height() - m_blurDistance * 4); phase = FloatPoint(destRect.x() - tileRect.x(), destRect.y() - tileRect.y()); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Draw the top left corner. tileRect = FloatRect(0, 0, radiusTwice + topLeftRadius.width(), radiusTwice + topLeftRadius.height()); destRect = tileRect; destRect.move(shadowRect.x(), shadowRect.y()); phase = getPhase(destRect, tileRect); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Draw the top right corner. tileRect = FloatRect(shadowTemplateSize.width() - radiusTwice - topRightRadius.width(), 0, radiusTwice + topRightRadius.width(), radiusTwice + topRightRadius.height()); destRect = tileRect; destRect.move(shadowRect.x() + rect.width() - shadowTemplateSize.width() + radiusTwice, shadowRect.y()); phase = getPhase(destRect, tileRect); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Draw the bottom right corner. tileRect = FloatRect(shadowTemplateSize.width() - radiusTwice - bottomRightRadius.width(), shadowTemplateSize.height() - radiusTwice - bottomRightRadius.height(), radiusTwice + bottomRightRadius.width(), radiusTwice + bottomRightRadius.height()); destRect = tileRect; destRect.move(shadowRect.x() + rect.width() - shadowTemplateSize.width() + radiusTwice, shadowRect.y() + rect.height() - shadowTemplateSize.height() + radiusTwice); phase = getPhase(destRect, tileRect); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Draw the bottom left corner. tileRect = FloatRect(0, shadowTemplateSize.height() - radiusTwice - bottomLeftRadius.height(), radiusTwice + bottomLeftRadius.width(), radiusTwice + bottomLeftRadius.height()); destRect = tileRect; destRect.move(shadowRect.x(), shadowRect.y() + rect.height() - shadowTemplateSize.height() + radiusTwice); phase = getPhase(destRect, tileRect); drawPatternToCairoContext(cr, m_layerImage, shadowTemplateSize, tileRect, patternTransform, phase, CAIRO_OPERATOR_OVER, destRect); // Schedule a purge of the scratch buffer. scheduleScratchBufferPurge(); }
// This works by converting the SVG arc to "simple" beziers. // Partly adapted from Niko's code in kdelibs/kdecore/svgicons. // See also SVG implementation notes: http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter bool SVGPathParser::decomposeArcToCubic(float angle, float rx, float ry, FloatPoint& point1, FloatPoint& point2, bool largeArcFlag, bool sweepFlag) { FloatSize midPointDistance = point1 - point2; midPointDistance.scale(0.5f); AffineTransform pointTransform; pointTransform.rotate(-angle); FloatPoint transformedMidPoint = pointTransform.mapPoint(FloatPoint(midPointDistance.width(), midPointDistance.height())); float squareRx = rx * rx; float squareRy = ry * ry; float squareX = transformedMidPoint.x() * transformedMidPoint.x(); float squareY = transformedMidPoint.y() * transformedMidPoint.y(); // Check if the radii are big enough to draw the arc, scale radii if not. // http://www.w3.org/TR/SVG/implnote.html#ArcCorrectionOutOfRangeRadii float radiiScale = squareX / squareRx + squareY / squareRy; if (radiiScale > 1) { rx *= sqrtf(radiiScale); ry *= sqrtf(radiiScale); } pointTransform.makeIdentity(); pointTransform.scale(1 / rx, 1 / ry); pointTransform.rotate(-angle); point1 = pointTransform.mapPoint(point1); point2 = pointTransform.mapPoint(point2); FloatSize delta = point2 - point1; float d = delta.width() * delta.width() + delta.height() * delta.height(); float scaleFactorSquared = std::max(1 / d - 0.25f, 0.f); float scaleFactor = sqrtf(scaleFactorSquared); if (sweepFlag == largeArcFlag) scaleFactor = -scaleFactor; delta.scale(scaleFactor); FloatPoint centerPoint = point1 + point2; centerPoint.scale(0.5f); centerPoint.move(-delta.height(), delta.width()); float theta1 = FloatPoint(point1 - centerPoint).slopeAngleRadians(); float theta2 = FloatPoint(point2 - centerPoint).slopeAngleRadians(); float thetaArc = theta2 - theta1; if (thetaArc < 0 && sweepFlag) thetaArc += 2 * piFloat; else if (thetaArc > 0 && !sweepFlag) thetaArc -= 2 * piFloat; pointTransform.makeIdentity(); pointTransform.rotate(angle); pointTransform.scale(rx, ry); // Some results of atan2 on some platform implementations are not exact enough. So that we get more // cubic curves than expected here. Adding 0.001f reduces the count of sgements to the correct count. int segments = ceilf(fabsf(thetaArc / (piOverTwoFloat + 0.001f))); for (int i = 0; i < segments; ++i) { float startTheta = theta1 + i * thetaArc / segments; float endTheta = theta1 + (i + 1) * thetaArc / segments; float t = (8 / 6.f) * tanf(0.25f * (endTheta - startTheta)); if (!std::isfinite(t)) return false; float sinStartTheta = sinf(startTheta); float cosStartTheta = cosf(startTheta); float sinEndTheta = sinf(endTheta); float cosEndTheta = cosf(endTheta); point1 = FloatPoint(cosStartTheta - t * sinStartTheta, sinStartTheta + t * cosStartTheta); point1.move(centerPoint.x(), centerPoint.y()); FloatPoint targetPoint = FloatPoint(cosEndTheta, sinEndTheta); targetPoint.move(centerPoint.x(), centerPoint.y()); point2 = targetPoint; point2.move(t * sinEndTheta, -t * cosEndTheta); m_consumer.curveToCubic(pointTransform.mapPoint(point1), pointTransform.mapPoint(point2), pointTransform.mapPoint(targetPoint), AbsoluteCoordinates); } return true; }
// This works by converting the SVG arc to "simple" beziers. // Partly adapted from Niko's code in kdelibs/kdecore/svgicons. // See also SVG implementation notes: // http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter bool SVGPathNormalizer::decomposeArcToCubic(const FloatPoint& currentPoint, const PathSegmentData& arcSegment) { // If rx = 0 or ry = 0 then this arc is treated as a straight line segment (a // "lineto") joining the endpoints. // http://www.w3.org/TR/SVG/implnote.html#ArcOutOfRangeParameters float rx = fabsf(arcSegment.arcRadii().x()); float ry = fabsf(arcSegment.arcRadii().y()); if (!rx || !ry) return false; // If the current point and target point for the arc are identical, it should // be treated as a zero length path. This ensures continuity in animations. if (arcSegment.targetPoint == currentPoint) return false; float angle = arcSegment.arcAngle(); FloatSize midPointDistance = currentPoint - arcSegment.targetPoint; midPointDistance.scale(0.5f); AffineTransform pointTransform; pointTransform.rotate(-angle); FloatPoint transformedMidPoint = pointTransform.mapPoint( FloatPoint(midPointDistance.width(), midPointDistance.height())); float squareRx = rx * rx; float squareRy = ry * ry; float squareX = transformedMidPoint.x() * transformedMidPoint.x(); float squareY = transformedMidPoint.y() * transformedMidPoint.y(); // Check if the radii are big enough to draw the arc, scale radii if not. // http://www.w3.org/TR/SVG/implnote.html#ArcCorrectionOutOfRangeRadii float radiiScale = squareX / squareRx + squareY / squareRy; if (radiiScale > 1) { rx *= sqrtf(radiiScale); ry *= sqrtf(radiiScale); } pointTransform.makeIdentity(); pointTransform.scale(1 / rx, 1 / ry); pointTransform.rotate(-angle); FloatPoint point1 = pointTransform.mapPoint(currentPoint); FloatPoint point2 = pointTransform.mapPoint(arcSegment.targetPoint); FloatSize delta = point2 - point1; float d = delta.width() * delta.width() + delta.height() * delta.height(); float scaleFactorSquared = std::max(1 / d - 0.25f, 0.f); float scaleFactor = sqrtf(scaleFactorSquared); if (arcSegment.arcSweep == arcSegment.arcLarge) scaleFactor = -scaleFactor; delta.scale(scaleFactor); FloatPoint centerPoint = point1 + point2; centerPoint.scale(0.5f, 0.5f); centerPoint.move(-delta.height(), delta.width()); float theta1 = FloatPoint(point1 - centerPoint).slopeAngleRadians(); float theta2 = FloatPoint(point2 - centerPoint).slopeAngleRadians(); float thetaArc = theta2 - theta1; if (thetaArc < 0 && arcSegment.arcSweep) thetaArc += twoPiFloat; else if (thetaArc > 0 && !arcSegment.arcSweep) thetaArc -= twoPiFloat; pointTransform.makeIdentity(); pointTransform.rotate(angle); pointTransform.scale(rx, ry); // Some results of atan2 on some platform implementations are not exact // enough. So that we get more cubic curves than expected here. Adding 0.001f // reduces the count of sgements to the correct count. int segments = ceilf(fabsf(thetaArc / (piOverTwoFloat + 0.001f))); for (int i = 0; i < segments; ++i) { float startTheta = theta1 + i * thetaArc / segments; float endTheta = theta1 + (i + 1) * thetaArc / segments; float t = (8 / 6.f) * tanf(0.25f * (endTheta - startTheta)); if (!std::isfinite(t)) return false; float sinStartTheta = sinf(startTheta); float cosStartTheta = cosf(startTheta); float sinEndTheta = sinf(endTheta); float cosEndTheta = cosf(endTheta); point1 = FloatPoint(cosStartTheta - t * sinStartTheta, sinStartTheta + t * cosStartTheta); point1.move(centerPoint.x(), centerPoint.y()); FloatPoint targetPoint = FloatPoint(cosEndTheta, sinEndTheta); targetPoint.move(centerPoint.x(), centerPoint.y()); point2 = targetPoint; point2.move(t * sinEndTheta, -t * cosEndTheta); PathSegmentData cubicSegment; cubicSegment.command = PathSegCurveToCubicAbs; cubicSegment.point1 = pointTransform.mapPoint(point1); cubicSegment.point2 = pointTransform.mapPoint(point2); cubicSegment.targetPoint = pointTransform.mapPoint(targetPoint); m_consumer->emitSegment(cubicSegment); } return true; }