GraphicsContextCG.cpp [plain text]
#define _USE_MATH_DEFINES 1
#include "config.h"
#include "GraphicsContext.h"
#include "TransformationMatrix.h"
#include "FloatConversion.h"
#include "GraphicsContextPrivate.h"
#include "GraphicsContextPlatformPrivateCG.h"
#include "ImageBuffer.h"
#include "KURL.h"
#include "Path.h"
#include "Pattern.h"
#include <CoreGraphics/CGBitmapContext.h>
#include <CoreGraphics/CGPDFContext.h>
#include <wtf/MathExtras.h>
#include <wtf/OwnArrayPtr.h>
#include <wtf/RetainPtr.h>
#if PLATFORM(MAC) && !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
#define HAVE_CG_INTERPOLATION_MEDIUM 1
#endif
using namespace std;
namespace WebCore {
static void setCGFillColor(CGContextRef context, const Color& color)
{
CGFloat red, green, blue, alpha;
color.getRGBA(red, green, blue, alpha);
CGContextSetRGBFillColor(context, red, green, blue, alpha);
}
static void setCGStrokeColor(CGContextRef context, const Color& color)
{
CGFloat red, green, blue, alpha;
color.getRGBA(red, green, blue, alpha);
CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
}
GraphicsContext::GraphicsContext(CGContextRef cgContext)
: m_common(createGraphicsContextPrivate())
, m_data(new GraphicsContextPlatformPrivate(cgContext))
{
setPaintingDisabled(!cgContext);
if (cgContext) {
setPlatformFillColor(fillColor());
setPlatformStrokeColor(strokeColor());
}
}
GraphicsContext::~GraphicsContext()
{
destroyGraphicsContextPrivate(m_common);
delete m_data;
}
CGContextRef GraphicsContext::platformContext() const
{
ASSERT(!paintingDisabled());
ASSERT(m_data->m_cgContext);
return m_data->m_cgContext;
}
void GraphicsContext::savePlatformState()
{
CGContextSaveGState(platformContext());
m_data->save();
}
void GraphicsContext::restorePlatformState()
{
CGContextRestoreGState(platformContext());
m_data->restore();
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::drawRect(const IntRect& rect)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextFillRect(context, rect);
if (strokeStyle() != NoStroke) {
Color oldFillColor = fillColor();
if (oldFillColor != strokeColor())
setCGFillColor(context, strokeColor());
CGRect rects[4] = {
FloatRect(rect.x(), rect.y(), rect.width(), 1),
FloatRect(rect.x(), rect.bottom() - 1, rect.width(), 1),
FloatRect(rect.x(), rect.y() + 1, 1, rect.height() - 2),
FloatRect(rect.right() - 1, rect.y() + 1, 1, rect.height() - 2)
};
CGContextFillRects(context, rects, 4);
if (oldFillColor != strokeColor())
setCGFillColor(context, oldFillColor);
}
}
void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
{
if (paintingDisabled())
return;
if (strokeStyle() == NoStroke)
return;
float width = strokeThickness();
FloatPoint p1 = point1;
FloatPoint p2 = point2;
bool isVerticalLine = (p1.x() == p2.x());
if (strokeStyle() == DottedStroke || strokeStyle() == DashedStroke) {
if (isVerticalLine) {
p1.move(0, width);
p2.move(0, -width);
} else {
p1.move(width, 0);
p2.move(-width, 0);
}
}
if (((int)width) % 2) {
if (isVerticalLine) {
p1.move(0.5f, 0.0f);
p2.move(0.5f, 0.0f);
} else {
p1.move(0.0f, 0.5f);
p2.move(0.0f, 0.5f);
}
}
int patWidth = 0;
switch (strokeStyle()) {
case NoStroke:
case SolidStroke:
break;
case DottedStroke:
patWidth = (int)width;
break;
case DashedStroke:
patWidth = 3 * (int)width;
break;
}
CGContextRef context = platformContext();
if (shouldAntialias())
CGContextSetShouldAntialias(context, false);
if (patWidth) {
CGContextSaveGState(context);
setCGFillColor(context, strokeColor()); if (isVerticalLine) {
CGContextFillRect(context, FloatRect(p1.x() - width / 2, p1.y() - width, width, width));
CGContextFillRect(context, FloatRect(p2.x() - width / 2, p2.y(), width, width));
} else {
CGContextFillRect(context, FloatRect(p1.x() - width, p1.y() - width / 2, width, width));
CGContextFillRect(context, FloatRect(p2.x(), p2.y() - width / 2, width, width));
}
int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2*(int)width;
int remainder = distance % patWidth;
int coverage = distance - remainder;
int numSegments = coverage / patWidth;
float patternOffset = 0.0f;
if (patWidth == 1)
patternOffset = 1.0f;
else {
bool evenNumberOfSegments = numSegments % 2 == 0;
if (remainder)
evenNumberOfSegments = !evenNumberOfSegments;
if (evenNumberOfSegments) {
if (remainder) {
patternOffset += patWidth - remainder;
patternOffset += remainder / 2;
} else
patternOffset = patWidth / 2;
} else {
if (remainder)
patternOffset = (patWidth - remainder)/2;
}
}
const CGFloat dottedLine[2] = { patWidth, patWidth };
CGContextSetLineDash(context, patternOffset, dottedLine, 2);
}
CGContextBeginPath(context);
CGContextMoveToPoint(context, p1.x(), p1.y());
CGContextAddLineToPoint(context, p2.x(), p2.y());
CGContextStrokePath(context);
if (patWidth)
CGContextRestoreGState(context);
if (shouldAntialias())
CGContextSetShouldAntialias(context, true);
}
void GraphicsContext::drawEllipse(const IntRect& rect)
{
ASSERT(rect.width() == rect.height());
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextBeginPath(context);
float r = (float)rect.width() / 2;
CGContextAddArc(context, rect.x() + r, rect.y() + r, r, 0.0f, 2.0f * piFloat, 0);
CGContextClosePath(context);
drawPath();
}
void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
{
if (paintingDisabled() || strokeStyle() == NoStroke || strokeThickness() <= 0.0f)
return;
CGContextRef context = platformContext();
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextSetShouldAntialias(context, false);
int x = rect.x();
int y = rect.y();
float w = (float)rect.width();
float h = (float)rect.height();
float scaleFactor = h / w;
float reverseScaleFactor = w / h;
if (w != h)
scale(FloatSize(1, scaleFactor));
float hRadius = w / 2;
float vRadius = h / 2;
float fa = startAngle;
float falen = fa + angleSpan;
float start = -fa * piFloat / 180.0f;
float end = -falen * piFloat / 180.0f;
CGContextAddArc(context, x + hRadius, (y + vRadius) * reverseScaleFactor, hRadius, start, end, true);
if (w != h)
scale(FloatSize(1, reverseScaleFactor));
float width = strokeThickness();
int patWidth = 0;
switch (strokeStyle()) {
case DottedStroke:
patWidth = (int)(width / 2);
break;
case DashedStroke:
patWidth = 3 * (int)(width / 2);
break;
default:
break;
}
if (patWidth) {
int distance;
if (hRadius == vRadius)
distance = static_cast<int>((piFloat * hRadius) / 2.0f);
else distance = static_cast<int>((piFloat * sqrtf((hRadius * hRadius + vRadius * vRadius) / 2.0f)) / 2.0f);
int remainder = distance % patWidth;
int coverage = distance - remainder;
int numSegments = coverage / patWidth;
float patternOffset = 0.0f;
if (patWidth == 1)
patternOffset = 1.0f;
else {
bool evenNumberOfSegments = numSegments % 2 == 0;
if (remainder)
evenNumberOfSegments = !evenNumberOfSegments;
if (evenNumberOfSegments) {
if (remainder) {
patternOffset += patWidth - remainder;
patternOffset += remainder / 2.0f;
} else
patternOffset = patWidth / 2.0f;
} else {
if (remainder)
patternOffset = (patWidth - remainder) / 2.0f;
}
}
const CGFloat dottedLine[2] = { patWidth, patWidth };
CGContextSetLineDash(context, patternOffset, dottedLine, 2);
}
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool antialiased)
{
if (paintingDisabled())
return;
if (npoints <= 1)
return;
CGContextRef context = platformContext();
if (antialiased != shouldAntialias())
CGContextSetShouldAntialias(context, antialiased);
CGContextBeginPath(context);
CGContextMoveToPoint(context, points[0].x(), points[0].y());
for (size_t i = 1; i < npoints; i++)
CGContextAddLineToPoint(context, points[i].x(), points[i].y());
CGContextClosePath(context);
drawPath();
if (antialiased != shouldAntialias())
CGContextSetShouldAntialias(context, shouldAntialias());
}
void GraphicsContext::applyStrokePattern()
{
CGContextRef cgContext = platformContext();
CGPatternRef platformPattern = m_common->state.strokePattern.get()->createPlatformPattern(getCTM());
if (!platformPattern)
return;
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(0);
CGContextSetStrokeColorSpace(cgContext, patternSpace);
CGColorSpaceRelease(patternSpace);
const CGFloat patternAlpha = 1;
CGContextSetStrokePattern(cgContext, platformPattern, &patternAlpha);
CGPatternRelease(platformPattern);
}
void GraphicsContext::applyFillPattern()
{
CGContextRef cgContext = platformContext();
CGPatternRef platformPattern = m_common->state.fillPattern.get()->createPlatformPattern(getCTM());
if (!platformPattern)
return;
CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(0);
CGContextSetFillColorSpace(cgContext, patternSpace);
CGColorSpaceRelease(patternSpace);
const CGFloat patternAlpha = 1;
CGContextSetFillPattern(cgContext, platformPattern, &patternAlpha);
CGPatternRelease(platformPattern);
}
static inline bool calculateDrawingMode(const GraphicsContextState& state, CGPathDrawingMode& mode)
{
bool shouldFill = state.fillColorSpace == PatternColorSpace || state.fillColor.alpha();
bool shouldStroke = state.strokeColorSpace == PatternColorSpace || (state.strokeStyle != NoStroke && state.strokeColor.alpha());
bool useEOFill = state.fillRule == RULE_EVENODD;
if (shouldFill) {
if (shouldStroke) {
if (useEOFill)
mode = kCGPathEOFillStroke;
else
mode = kCGPathFillStroke;
} else { if (useEOFill)
mode = kCGPathEOFill;
else
mode = kCGPathFill;
}
} else {
mode = kCGPathStroke;
}
return shouldFill || shouldStroke;
}
void GraphicsContext::drawPath()
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
const GraphicsContextState& state = m_common->state;
if (state.fillColorSpace == GradientColorSpace || state.strokeColorSpace == GradientColorSpace) {
fillPath();
strokePath();
return;
}
if (state.fillColorSpace == PatternColorSpace)
applyFillPattern();
if (state.strokeColorSpace == PatternColorSpace)
applyStrokePattern();
CGPathDrawingMode drawingMode;
if (calculateDrawingMode(state, drawingMode))
CGContextDrawPath(context, drawingMode);
}
static inline void fillPathWithFillRule(CGContextRef context, WindRule fillRule)
{
if (fillRule == RULE_EVENODD)
CGContextEOFillPath(context);
else
CGContextFillPath(context);
}
void GraphicsContext::fillPath()
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
switch (m_common->state.fillColorSpace) {
case SolidColorSpace:
fillPathWithFillRule(context, fillRule());
break;
case PatternColorSpace:
applyFillPattern();
fillPathWithFillRule(context, fillRule());
break;
case GradientColorSpace:
CGContextSaveGState(context);
if (fillRule() == RULE_EVENODD)
CGContextEOClip(context);
else
CGContextClip(context);
CGContextConcatCTM(context, m_common->state.fillGradient->gradientSpaceTransform());
CGContextDrawShading(context, m_common->state.fillGradient->platformGradient());
CGContextRestoreGState(context);
break;
}
}
void GraphicsContext::strokePath()
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
switch (m_common->state.strokeColorSpace) {
case SolidColorSpace:
CGContextStrokePath(context);
break;
case PatternColorSpace:
applyStrokePattern();
CGContextStrokePath(context);
break;
case GradientColorSpace:
CGContextSaveGState(context);
CGContextReplacePathWithStrokedPath(context);
CGContextClip(context);
CGContextConcatCTM(context, m_common->state.strokeGradient->gradientSpaceTransform());
CGContextDrawShading(context, m_common->state.strokeGradient->platformGradient());
CGContextRestoreGState(context);
break;
}
}
void GraphicsContext::fillRect(const FloatRect& rect)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
switch (m_common->state.fillColorSpace) {
case SolidColorSpace:
CGContextFillRect(context, rect);
break;
case PatternColorSpace:
applyFillPattern();
CGContextFillRect(context, rect);
break;
case GradientColorSpace:
CGContextSaveGState(context);
CGContextClipToRect(context, rect);
CGContextConcatCTM(context, m_common->state.fillGradient->gradientSpaceTransform());
CGContextDrawShading(context, m_common->state.fillGradient->platformGradient());
CGContextRestoreGState(context);
break;
}
}
void GraphicsContext::fillRect(const FloatRect& rect, const Color& color)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
Color oldFillColor = fillColor();
if (oldFillColor != color)
setCGFillColor(context, color);
CGContextFillRect(context, rect);
if (oldFillColor != color)
setCGFillColor(context, oldFillColor);
}
void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
Color oldFillColor = fillColor();
if (oldFillColor != color)
setCGFillColor(context, color);
addPath(Path::createRoundedRectangle(rect, topLeft, topRight, bottomLeft, bottomRight));
fillPath();
if (oldFillColor != color)
setCGFillColor(context, oldFillColor);
}
void GraphicsContext::clip(const FloatRect& rect)
{
if (paintingDisabled())
return;
CGContextClipToRect(platformContext(), rect);
m_data->clip(rect);
}
void GraphicsContext::clipOut(const IntRect& rect)
{
if (paintingDisabled())
return;
CGRect rects[2] = { CGContextGetClipBoundingBox(platformContext()), rect };
CGContextBeginPath(platformContext());
CGContextAddRects(platformContext(), rects, 2);
CGContextEOClip(platformContext());
}
void GraphicsContext::clipOutEllipseInRect(const IntRect& rect)
{
if (paintingDisabled())
return;
CGContextBeginPath(platformContext());
CGContextAddRect(platformContext(), CGContextGetClipBoundingBox(platformContext()));
CGContextAddEllipseInRect(platformContext(), rect);
CGContextEOClip(platformContext());
}
void GraphicsContext::clipPath(WindRule clipRule)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
if (!CGContextIsPathEmpty(context)) {
if (clipRule == RULE_EVENODD)
CGContextEOClip(context);
else
CGContextClip(context);
}
}
void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
{
if (paintingDisabled())
return;
clip(rect);
CGContextRef context = platformContext();
CGContextAddEllipseInRect(context, CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()));
CGContextAddEllipseInRect(context, CGRectMake(rect.x() + thickness, rect.y() + thickness,
rect.width() - (thickness * 2), rect.height() - (thickness * 2)));
CGContextEOClip(context);
}
void GraphicsContext::clipToImageBuffer(const FloatRect& rect, const ImageBuffer* imageBuffer)
{
if (paintingDisabled())
return;
CGContextTranslateCTM(platformContext(), rect.x(), rect.y() + rect.height());
CGContextScaleCTM(platformContext(), 1, -1);
CGContextClipToMask(platformContext(), FloatRect(FloatPoint(), rect.size()), imageBuffer->image()->getCGImageRef());
CGContextScaleCTM(platformContext(), 1, -1);
CGContextTranslateCTM(platformContext(), -rect.x(), -rect.y() - rect.height());
}
void GraphicsContext::beginTransparencyLayer(float opacity)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextSaveGState(context);
CGContextSetAlpha(context, opacity);
CGContextBeginTransparencyLayer(context, 0);
m_data->beginTransparencyLayer();
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::endTransparencyLayer()
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextEndTransparencyLayer(context);
CGContextRestoreGState(context);
m_data->endTransparencyLayer();
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::setPlatformShadow(const IntSize& size, int blur, const Color& color)
{
if (paintingDisabled())
return;
CGFloat width = size.width();
CGFloat height = size.height();
CGFloat blurRadius = blur;
CGContextRef context = platformContext();
if (!m_common->state.shadowsIgnoreTransforms) {
CGAffineTransform transform = CGContextGetCTM(context);
CGFloat A = transform.a * transform.a + transform.b * transform.b;
CGFloat B = transform.a * transform.c + transform.b * transform.d;
CGFloat C = B;
CGFloat D = transform.c * transform.c + transform.d * transform.d;
CGFloat smallEigenvalue = narrowPrecisionToCGFloat(sqrt(0.5 * ((A + D) - sqrt(4 * B * C + (A - D) * (A - D)))));
blurRadius = min(blur * smallEigenvalue, narrowPrecisionToCGFloat(1000.0));
CGSize sizeInDeviceSpace = CGSizeApplyAffineTransform(size, transform);
width = sizeInDeviceSpace.width;
height = sizeInDeviceSpace.height;
}
static const CGFloat extraShadowOffset = narrowPrecisionToCGFloat(1.0 / 128);
if (width > 0)
width += extraShadowOffset;
else if (width < 0)
width -= extraShadowOffset;
if (height > 0)
height += extraShadowOffset;
else if (height < 0)
height -= extraShadowOffset;
if (!color.isValid())
CGContextSetShadow(context, CGSizeMake(width, height), blurRadius);
else {
CGColorRef colorCG = createCGColor(color);
CGContextSetShadowWithColor(context,
CGSizeMake(width, height),
blurRadius,
colorCG);
CGColorRelease(colorCG);
}
}
void GraphicsContext::clearPlatformShadow()
{
if (paintingDisabled())
return;
CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
}
void GraphicsContext::setMiterLimit(float limit)
{
if (paintingDisabled())
return;
CGContextSetMiterLimit(platformContext(), limit);
}
void GraphicsContext::setAlpha(float alpha)
{
if (paintingDisabled())
return;
CGContextSetAlpha(platformContext(), alpha);
}
void GraphicsContext::clearRect(const FloatRect& r)
{
if (paintingDisabled())
return;
CGContextClearRect(platformContext(), r);
}
void GraphicsContext::strokeRect(const FloatRect& r, float lineWidth)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
switch (m_common->state.strokeColorSpace) {
case SolidColorSpace:
CGContextStrokeRectWithWidth(context, r, lineWidth);
break;
case PatternColorSpace:
applyStrokePattern();
CGContextStrokeRectWithWidth(context, r, lineWidth);
break;
case GradientColorSpace:
CGContextSaveGState(context);
setStrokeThickness(lineWidth);
CGContextAddRect(context, r);
CGContextReplacePathWithStrokedPath(context);
CGContextClip(context);
CGContextDrawShading(context, m_common->state.strokeGradient->platformGradient());
CGContextRestoreGState(context);
break;
}
}
void GraphicsContext::setLineCap(LineCap cap)
{
if (paintingDisabled())
return;
switch (cap) {
case ButtCap:
CGContextSetLineCap(platformContext(), kCGLineCapButt);
break;
case RoundCap:
CGContextSetLineCap(platformContext(), kCGLineCapRound);
break;
case SquareCap:
CGContextSetLineCap(platformContext(), kCGLineCapSquare);
break;
}
}
void GraphicsContext::setLineDash(const DashArray& dashes, float dashOffset)
{
CGContextSetLineDash(platformContext(), dashOffset, dashes.data(), dashes.size());
}
void GraphicsContext::setLineJoin(LineJoin join)
{
if (paintingDisabled())
return;
switch (join) {
case MiterJoin:
CGContextSetLineJoin(platformContext(), kCGLineJoinMiter);
break;
case RoundJoin:
CGContextSetLineJoin(platformContext(), kCGLineJoinRound);
break;
case BevelJoin:
CGContextSetLineJoin(platformContext(), kCGLineJoinBevel);
break;
}
}
void GraphicsContext::beginPath()
{
CGContextBeginPath(platformContext());
}
void GraphicsContext::addPath(const Path& path)
{
CGContextAddPath(platformContext(), path.platformPath());
}
void GraphicsContext::clip(const Path& path)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextBeginPath(context);
CGContextAddPath(context, path.platformPath());
CGContextClip(context);
m_data->clip(path);
}
void GraphicsContext::clipOut(const Path& path)
{
if (paintingDisabled())
return;
CGContextBeginPath(platformContext());
CGContextAddRect(platformContext(), CGContextGetClipBoundingBox(platformContext()));
CGContextAddPath(platformContext(), path.platformPath());
CGContextEOClip(platformContext());
}
void GraphicsContext::scale(const FloatSize& size)
{
if (paintingDisabled())
return;
CGContextScaleCTM(platformContext(), size.width(), size.height());
m_data->scale(size);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::rotate(float angle)
{
if (paintingDisabled())
return;
CGContextRotateCTM(platformContext(), angle);
m_data->rotate(angle);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::translate(float x, float y)
{
if (paintingDisabled())
return;
CGContextTranslateCTM(platformContext(), x, y);
m_data->translate(x, y);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::concatCTM(const TransformationMatrix& transform)
{
if (paintingDisabled())
return;
CGContextConcatCTM(platformContext(), transform);
m_data->concatCTM(transform);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
TransformationMatrix GraphicsContext::getCTM() const
{
CGAffineTransform t = CGContextGetCTM(platformContext());
return TransformationMatrix(t.a, t.b, t.c, t.d, t.tx, t.ty);
}
FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect)
{
if (m_data->m_userToDeviceTransformKnownToBeIdentity)
return rect;
CGAffineTransform deviceMatrix = CGContextGetUserSpaceToDeviceSpaceTransform(platformContext());
if (CGAffineTransformIsIdentity(deviceMatrix)) {
m_data->m_userToDeviceTransformKnownToBeIdentity = true;
return rect;
}
float deviceScaleX = sqrtf(deviceMatrix.a * deviceMatrix.a + deviceMatrix.b * deviceMatrix.b);
float deviceScaleY = sqrtf(deviceMatrix.c * deviceMatrix.c + deviceMatrix.d * deviceMatrix.d);
CGPoint deviceOrigin = CGPointMake(rect.x() * deviceScaleX, rect.y() * deviceScaleY);
CGPoint deviceLowerRight = CGPointMake((rect.x() + rect.width()) * deviceScaleX,
(rect.y() + rect.height()) * deviceScaleY);
deviceOrigin.x = roundf(deviceOrigin.x);
deviceOrigin.y = roundf(deviceOrigin.y);
deviceLowerRight.x = roundf(deviceLowerRight.x);
deviceLowerRight.y = roundf(deviceLowerRight.y);
if (deviceOrigin.y == deviceLowerRight.y && rect.height() != 0)
deviceLowerRight.y += 1;
if (deviceOrigin.x == deviceLowerRight.x && rect.width() != 0)
deviceLowerRight.x += 1;
FloatPoint roundedOrigin = FloatPoint(deviceOrigin.x / deviceScaleX, deviceOrigin.y / deviceScaleY);
FloatPoint roundedLowerRight = FloatPoint(deviceLowerRight.x / deviceScaleX, deviceLowerRight.y / deviceScaleY);
return FloatRect(roundedOrigin, roundedLowerRight - roundedOrigin);
}
void GraphicsContext::drawLineForText(const IntPoint& point, int width, bool printing)
{
if (paintingDisabled())
return;
if (width <= 0)
return;
float x = point.x();
float y = point.y();
float lineLength = width;
float thickness = max(strokeThickness(), 0.5f);
bool restoreAntialiasMode = false;
if (!printing) {
float adjustedThickness = max(thickness, 1.0f);
CGRect lineRect = roundToDevicePixels(FloatRect(x, y, lineLength, adjustedThickness));
if (lineRect.size.height < thickness * 2.0) {
x = lineRect.origin.x;
y = lineRect.origin.y;
lineLength = lineRect.size.width;
thickness = lineRect.size.height;
if (shouldAntialias()) {
CGContextSetShouldAntialias(platformContext(), false);
restoreAntialiasMode = true;
}
}
}
if (fillColor() != strokeColor())
setCGFillColor(platformContext(), strokeColor());
CGContextFillRect(platformContext(), CGRectMake(x, y, lineLength, thickness));
if (fillColor() != strokeColor())
setCGFillColor(platformContext(), fillColor());
if (restoreAntialiasMode)
CGContextSetShouldAntialias(platformContext(), true);
}
void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
{
if (paintingDisabled())
return;
CFURLRef urlRef = link.createCFURL();
if (urlRef) {
CGContextRef context = platformContext();
CGRect box = CGContextGetClipBoundingBox(context);
IntRect intBox((int)box.origin.x, (int)box.origin.y, (int)box.size.width, (int)box.size.height);
IntRect rect = destRect;
rect.intersect(intBox);
CGPDFContextSetURLForRect(context, urlRef,
CGRectApplyAffineTransform(rect, CGContextGetCTM(context)));
CFRelease(urlRef);
}
}
void GraphicsContext::setImageInterpolationQuality(InterpolationQuality mode)
{
if (paintingDisabled())
return;
CGInterpolationQuality quality = kCGInterpolationDefault;
switch (mode) {
case InterpolationDefault:
quality = kCGInterpolationDefault;
break;
case InterpolationNone:
quality = kCGInterpolationNone;
break;
case InterpolationLow:
quality = kCGInterpolationLow;
break;
case InterpolationMedium:
#if HAVE(CG_INTERPOLATION_MEDIUM)
quality = kCGInterpolationMedium;
break;
#endif
case InterpolationHigh:
quality = kCGInterpolationHigh;
break;
}
CGContextSetInterpolationQuality(platformContext(), quality);
}
InterpolationQuality GraphicsContext::imageInterpolationQuality() const
{
if (paintingDisabled())
return InterpolationDefault;
CGInterpolationQuality quality = CGContextGetInterpolationQuality(platformContext());
switch (quality) {
case kCGInterpolationDefault:
return InterpolationDefault;
case kCGInterpolationNone:
return InterpolationNone;
case kCGInterpolationLow:
return InterpolationLow;
#if HAVE(CG_INTERPOLATION_MEDIUM)
case kCGInterpolationMedium:
return InterpolationMedium;
#endif
case kCGInterpolationHigh:
return InterpolationHigh;
}
return InterpolationDefault;
}
void GraphicsContext::setPlatformTextDrawingMode(int mode)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
switch (mode) {
case cTextInvisible: CGContextSetTextDrawingMode(context, kCGTextInvisible);
break;
case cTextFill: CGContextSetTextDrawingMode(context, kCGTextFill);
break;
case cTextStroke: CGContextSetTextDrawingMode(context, kCGTextStroke);
break;
case 3: CGContextSetTextDrawingMode(context, kCGTextFillStroke);
break;
case cTextClip: CGContextSetTextDrawingMode(context, kCGTextClip);
break;
case 5: CGContextSetTextDrawingMode(context, kCGTextFillClip);
break;
case 6: CGContextSetTextDrawingMode(context, kCGTextStrokeClip);
break;
case 7: CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip);
break;
default:
break;
}
}
void GraphicsContext::setPlatformStrokeColor(const Color& color)
{
if (paintingDisabled())
return;
setCGStrokeColor(platformContext(), color);
}
void GraphicsContext::setPlatformStrokeThickness(float thickness)
{
if (paintingDisabled())
return;
CGContextSetLineWidth(platformContext(), thickness);
}
void GraphicsContext::setPlatformFillColor(const Color& color)
{
if (paintingDisabled())
return;
setCGFillColor(platformContext(), color);
}
void GraphicsContext::setPlatformShouldAntialias(bool enable)
{
if (paintingDisabled())
return;
CGContextSetShouldAntialias(platformContext(), enable);
}
#ifndef BUILDING_ON_TIGER // Tiger's setCompositeOperation() is defined in GraphicsContextMac.mm.
void GraphicsContext::setCompositeOperation(CompositeOperator mode)
{
if (paintingDisabled())
return;
CGBlendMode target = kCGBlendModeNormal;
switch (mode) {
case CompositeClear:
target = kCGBlendModeClear;
break;
case CompositeCopy:
target = kCGBlendModeCopy;
break;
case CompositeSourceOver:
break;
case CompositeSourceIn:
target = kCGBlendModeSourceIn;
break;
case CompositeSourceOut:
target = kCGBlendModeSourceOut;
break;
case CompositeSourceAtop:
target = kCGBlendModeSourceAtop;
break;
case CompositeDestinationOver:
target = kCGBlendModeDestinationOver;
break;
case CompositeDestinationIn:
target = kCGBlendModeDestinationIn;
break;
case CompositeDestinationOut:
target = kCGBlendModeDestinationOut;
break;
case CompositeDestinationAtop:
target = kCGBlendModeDestinationAtop;
break;
case CompositeXOR:
target = kCGBlendModeXOR;
break;
case CompositePlusDarker:
target = kCGBlendModePlusDarker;
break;
case CompositeHighlight:
break;
case CompositePlusLighter:
target = kCGBlendModePlusLighter;
break;
}
CGContextSetBlendMode(platformContext(), target);
}
#endif
}