#include "config.h"
#if ENABLE(SVG)
#include "CgSupport.h"
#include <CoreGraphics/CoreGraphics.h>
#include "FloatConversion.h"
#include "GraphicsContext.h"
#include "RenderStyle.h"
#include "SVGPaintServer.h"
#include "SVGRenderStyle.h"
#include <wtf/Assertions.h>
namespace WebCore {
CGAffineTransform CGAffineTransformMakeMapBetweenRects(CGRect source, CGRect dest)
{
CGAffineTransform transform = CGAffineTransformMakeTranslation(dest.origin.x - source.origin.x, dest.origin.y - source.origin.y);
transform = CGAffineTransformScale(transform, dest.size.width/source.size.width, dest.size.height/source.size.height);
return transform;
}
void applyStrokeStyleToContext(GraphicsContext* context, RenderStyle* style, const RenderObject* object)
{
context->setStrokeThickness(SVGRenderStyle::cssPrimitiveToLength(object, style->svgStyle()->strokeWidth(), 1.0f));
context->setLineCap(style->svgStyle()->capStyle());
context->setLineJoin(style->svgStyle()->joinStyle());
context->setMiterLimit(style->svgStyle()->strokeMiterLimit());
const DashArray& dashes = dashArrayFromRenderingStyle(style);
double dashOffset = SVGRenderStyle::cssPrimitiveToLength(object, style->svgStyle()->strokeDashOffset(), 0.0f);
CGContextSetLineDash(context->platformContext(), narrowPrecisionToCGFloat(dashOffset), dashes.data(), dashes.size());
}
CGContextRef scratchContext()
{
static CGContextRef scratch = 0;
if (!scratch) {
CFMutableDataRef empty = CFDataCreateMutable(NULL, 0);
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(empty);
scratch = CGPDFContextCreate(consumer, NULL, NULL);
CGDataConsumerRelease(consumer);
CFRelease(empty);
CGFloat black[4] = {0, 0, 0, 1};
CGContextSetFillColor(scratch, black);
CGContextSetStrokeColor(scratch, black);
}
return scratch;
}
FloatRect strokeBoundingBox(const Path& path, RenderStyle* style, const RenderObject* object)
{
CGPathRef cgPath = path.platformPath();
CGContextRef context = scratchContext();
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextAddPath(context, cgPath);
GraphicsContext gc(context);
applyStrokeStyleToContext(&gc, style, object);
CGContextReplacePathWithStrokedPath(context);
if (CGContextIsPathEmpty(context)) {
CGContextRestoreGState(context);
return FloatRect();
}
CGRect box = CGContextGetPathBoundingBox(context);
CGContextRestoreGState(context);
return FloatRect(box);
}
}
#endif // ENABLE(SVG)