GraphicsContextCairo.cpp [plain text]
#include "config.h"
#include "GraphicsContext.h"
#if PLATFORM(CAIRO)
#include "AffineTransform.h"
#include "CairoPath.h"
#include "FloatRect.h"
#include "Font.h"
#include "IntRect.h"
#include "NotImplemented.h"
#include "Path.h"
#include "SimpleFontData.h"
#include <cairo.h>
#include <math.h>
#include <stdio.h>
#include <wtf/MathExtras.h>
#if PLATFORM(GTK)
#include <gdk/gdk.h>
#include <pango/pango.h>
#elif PLATFORM(WIN)
#include <cairo-win32.h>
#endif
#include "GraphicsContextPlatformPrivateCairo.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
namespace WebCore {
static inline void setColor(cairo_t* cr, const Color& col)
{
float red, green, blue, alpha;
col.getRGBA(red, green, blue, alpha);
cairo_set_source_rgba(cr, red, green, blue, alpha);
}
static inline void fillRectSourceOver(cairo_t* cr, const FloatRect& rect, const Color& col)
{
setColor(cr, col);
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_fill(cr);
}
GraphicsContext::GraphicsContext(PlatformGraphicsContext* cr)
: m_common(createGraphicsContextPrivate())
, m_data(new GraphicsContextPlatformPrivate)
{
m_data->cr = cairo_reference(cr);
setPaintingDisabled(!cr);
}
GraphicsContext::~GraphicsContext()
{
destroyGraphicsContextPrivate(m_common);
delete m_data;
}
AffineTransform GraphicsContext::getCTM() const
{
cairo_t* cr = platformContext();
cairo_matrix_t m;
cairo_get_matrix(cr, &m);
return m;
}
cairo_t* GraphicsContext::platformContext() const
{
return m_data->cr;
}
void GraphicsContext::savePlatformState()
{
cairo_save(m_data->cr);
}
void GraphicsContext::restorePlatformState()
{
cairo_restore(m_data->cr);
}
void GraphicsContext::drawRect(const IntRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
if (fillColor().alpha())
fillRectSourceOver(cr, rect, fillColor());
if (strokeStyle() != NoStroke) {
setColor(cr, strokeColor());
FloatRect r(rect);
r.inflate(-.5f);
cairo_rectangle(cr, r.x(), r.y(), r.width(), r.height());
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);
}
cairo_restore(cr);
}
static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle style)
{
if (style == DottedStroke || style == DashedStroke) {
if (p1.x() == p2.x()) {
p1.setY(p1.y() + strokeWidth);
p2.setY(p2.y() - strokeWidth);
}
else {
p1.setX(p1.x() + strokeWidth);
p2.setX(p2.x() - strokeWidth);
}
}
if (static_cast<int>(strokeWidth) % 2) {
if (p1.x() == p2.x()) {
p1.setX(p1.x() + 0.5);
p2.setX(p2.x() + 0.5);
}
else {
p1.setY(p1.y() + 0.5);
p2.setY(p2.y() + 0.5);
}
}
}
void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
{
if (paintingDisabled())
return;
StrokeStyle style = strokeStyle();
if (style == NoStroke)
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
float width = strokeThickness();
if (width < 1)
width = 1;
FloatPoint p1 = point1;
FloatPoint p2 = point2;
bool isVerticalLine = (p1.x() == p2.x());
adjustLineToPixelBoundaries(p1, p2, width, style);
cairo_set_line_width(cr, width);
int patWidth = 0;
switch (style) {
case NoStroke:
case SolidStroke:
break;
case DottedStroke:
patWidth = static_cast<int>(width);
break;
case DashedStroke:
patWidth = 3*static_cast<int>(width);
break;
}
setColor(cr, strokeColor());
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
if (patWidth) {
if (isVerticalLine) {
fillRectSourceOver(cr, FloatRect(p1.x() - width/2, p1.y() - width, width, width), strokeColor());
fillRectSourceOver(cr, FloatRect(p2.x() - width/2, p2.y(), width, width), strokeColor());
} else {
fillRectSourceOver(cr, FloatRect(p1.x() - width, p1.y() - width/2, width, width), strokeColor());
fillRectSourceOver(cr, FloatRect(p2.x(), p2.y() - width/2, width, width), strokeColor());
}
int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2*static_cast<int>(width);
int remainder = distance%patWidth;
int coverage = distance-remainder;
int numSegments = coverage/patWidth;
float patternOffset = 0;
if (patWidth == 1)
patternOffset = 1.0;
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 (!evenNumberOfSegments) {
if (remainder)
patternOffset = (patWidth - remainder)/2;
}
}
double dash = patWidth;
cairo_set_dash(cr, &dash, 1, patternOffset);
}
cairo_move_to(cr, p1.x(), p1.y());
cairo_line_to(cr, p2.x(), p2.y());
cairo_stroke(cr);
cairo_restore(cr);
}
void GraphicsContext::drawEllipse(const IntRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
float yRadius = .5 * rect.height();
float xRadius = .5 * rect.width();
cairo_translate(cr, rect.x() + xRadius, rect.y() + yRadius);
cairo_scale(cr, xRadius, yRadius);
cairo_arc(cr, 0., 0., 1., 0., 2 * M_PI);
cairo_restore(cr);
if (fillColor().alpha()) {
setColor(cr, fillColor());
cairo_fill_preserve(cr);
}
if (strokeStyle() != NoStroke) {
setColor(cr, strokeColor());
cairo_set_line_width(cr, strokeThickness());
cairo_stroke(cr);
}
cairo_new_path(cr);
}
void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
{
if (paintingDisabled())
return;
if (strokeStyle() == NoStroke)
return;
int x = rect.x();
int y = rect.y();
float w = rect.width();
#if 0 // FIXME: unused so far
float h = rect.height();
float scaleFactor = h / w;
float reverseScaleFactor = w / h;
#endif
float r = w / 2;
float fa = startAngle;
float falen = fa + angleSpan;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_arc_negative(cr, x + r, y + r, r, -fa * M_PI/180, -falen * M_PI/180);
setColor(cr, strokeColor());
cairo_set_line_width(cr, strokeThickness());
cairo_stroke(cr);
cairo_restore(cr);
}
void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias)
{
if (paintingDisabled())
return;
if (npoints <= 1)
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_set_antialias(cr, shouldAntialias ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
cairo_move_to(cr, points[0].x(), points[0].y());
for (size_t i = 1; i < npoints; i++)
cairo_line_to(cr, points[i].x(), points[i].y());
cairo_close_path(cr);
if (fillColor().alpha()) {
setColor(cr, fillColor());
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_fill_preserve(cr);
}
if (strokeStyle() != NoStroke) {
setColor(cr, strokeColor());
cairo_set_line_width(cr, strokeThickness());
cairo_stroke(cr);
}
cairo_new_path(cr);
cairo_restore(cr);
}
void GraphicsContext::fillRect(const IntRect& rect, const Color& color)
{
if (paintingDisabled())
return;
if (color.alpha())
fillRectSourceOver(m_data->cr, rect, color);
}
void GraphicsContext::fillRect(const FloatRect& rect, const Color& color)
{
if (paintingDisabled())
return;
if (color.alpha())
fillRectSourceOver(m_data->cr, rect, color);
}
void GraphicsContext::clip(const IntRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::drawFocusRing(const Color& color)
{
if (paintingDisabled())
return;
int radius = (focusRingWidth() - 1) / 2;
int offset = radius + focusRingOffset();
const Vector<IntRect>& rects = focusRingRects();
unsigned rectCount = rects.size();
IntRect finalFocusRect;
for (unsigned i = 0; i < rectCount; i++) {
IntRect focusRect = rects[i];
focusRect.inflate(offset);
finalFocusRect.unite(focusRect);
}
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_rectangle(cr, finalFocusRect.x(), finalFocusRect.y(), finalFocusRect.width(), finalFocusRect.height());
Color ringColor(color.red(), color.green(), color.blue(), 127);
setColor(cr, ringColor);
cairo_stroke(cr);
cairo_restore(cr);
}
void GraphicsContext::drawLineForText(const IntPoint& origin, int width, bool printing)
{
if (paintingDisabled())
return;
StrokeStyle savedStrokeStyle = strokeStyle();
setStrokeStyle(SolidStroke);
IntPoint endPoint = origin + IntSize(width, 0);
drawLine(origin, endPoint);
setStrokeStyle(savedStrokeStyle);
}
void GraphicsContext::drawLineForMisspellingOrBadGrammar(const IntPoint& origin, int width, bool grammar)
{
if (paintingDisabled())
return;
#if PLATFORM(GTK)
cairo_t* cr = m_data->cr;
cairo_save(cr);
if (grammar)
cairo_set_source_rgb(cr, 0, 1, 0);
else
cairo_set_source_rgb(cr, 1, 0, 0);
pango_cairo_show_error_underline(cr, origin.x(), origin.y(), width, cMisspellingLineThickness);
cairo_restore(cr);
#else
notImplemented();
#endif
}
FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& frect)
{
FloatRect result;
double x = frect.x();
double y = frect.y();
cairo_t* cr = m_data->cr;
cairo_user_to_device(cr, &x, &y);
x = round(x);
y = round(y);
cairo_device_to_user(cr, &x, &y);
result.setX(static_cast<float>(x));
result.setY(static_cast<float>(y));
x = frect.width();
y = frect.height();
cairo_user_to_device_distance(cr, &x, &y);
x = round(x);
y = round(y);
cairo_device_to_user_distance(cr, &x, &y);
result.setWidth(static_cast<float>(x));
result.setHeight(static_cast<float>(y));
return result;
}
void GraphicsContext::translate(float x, float y)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_translate(cr, x, y);
}
IntPoint GraphicsContext::origin()
{
cairo_matrix_t matrix;
cairo_t* cr = m_data->cr;
cairo_get_matrix(cr, &matrix);
return IntPoint(static_cast<int>(matrix.x0), static_cast<int>(matrix.y0));
}
void GraphicsContext::setPlatformFillColor(const Color& col)
{
}
void GraphicsContext::setPlatformStrokeColor(const Color& col)
{
}
void GraphicsContext::setPlatformStrokeThickness(float strokeThickness)
{
if (paintingDisabled())
return;
cairo_set_line_width(m_data->cr, strokeThickness);
}
void GraphicsContext::setPlatformStrokeStyle(const StrokeStyle& strokeStyle)
{
static double dashPattern[] = {5.0, 5.0};
static double dotPattern[] = {1.0, 1.0};
if (paintingDisabled())
return;
switch (strokeStyle) {
case NoStroke:
cairo_set_line_width(m_data->cr, 0);
break;
case SolidStroke:
cairo_set_dash(m_data->cr, 0, 0, 0);
break;
case DottedStroke:
cairo_set_dash(m_data->cr, dotPattern, 2, 0);
break;
case DashedStroke:
cairo_set_dash(m_data->cr, dashPattern, 2, 0);
break;
default:
notImplemented();
break;
}
}
void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
{
notImplemented();
}
#if PLATFORM(GTK)
void GraphicsContext::concatCTM(const AffineTransform& transform)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
const cairo_matrix_t* matrix = reinterpret_cast<const cairo_matrix_t*>(&transform);
cairo_transform(cr, matrix);
}
#endif
void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
{
if (paintingDisabled())
return;
clip(rect);
Path p;
FloatRect r(rect);
p.addEllipse(r);
r.inflate(-thickness);
p.addEllipse(r);
addPath(p);
cairo_t* cr = m_data->cr;
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::setShadow(IntSize const&, int, Color const&)
{
notImplemented();
}
void GraphicsContext::clearShadow()
{
notImplemented();
}
void GraphicsContext::beginTransparencyLayer(float opacity)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_push_group(cr);
m_data->layers.append(opacity);
}
void GraphicsContext::endTransparencyLayer()
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_pop_group_to_source(cr);
cairo_paint_with_alpha(cr, m_data->layers.last());
m_data->layers.removeLast();
}
void GraphicsContext::clearRect(const FloatRect& rect)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_fill(cr);
cairo_restore(cr);
}
void GraphicsContext::strokeRect(const FloatRect& rect, float width)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
setColor(cr, strokeColor());
cairo_set_line_width(cr, width);
cairo_stroke(cr);
cairo_restore(cr);
}
void GraphicsContext::setLineCap(LineCap lineCap)
{
if (paintingDisabled())
return;
cairo_line_cap_t cairoCap = CAIRO_LINE_CAP_BUTT;
switch (lineCap) {
case ButtCap:
break;
case RoundCap:
cairoCap = CAIRO_LINE_CAP_ROUND;
break;
case SquareCap:
cairoCap = CAIRO_LINE_CAP_SQUARE;
break;
}
cairo_set_line_cap(m_data->cr, cairoCap);
}
void GraphicsContext::setLineJoin(LineJoin lineJoin)
{
if (paintingDisabled())
return;
cairo_line_join_t cairoJoin = CAIRO_LINE_JOIN_MITER;
switch (lineJoin) {
case MiterJoin:
break;
case RoundJoin:
cairoJoin = CAIRO_LINE_JOIN_ROUND;
break;
case BevelJoin:
cairoJoin = CAIRO_LINE_JOIN_BEVEL;
break;
}
cairo_set_line_join(m_data->cr, cairoJoin);
}
void GraphicsContext::setMiterLimit(float miter)
{
if (paintingDisabled())
return;
cairo_set_miter_limit(m_data->cr, miter);
}
void GraphicsContext::setAlpha(float)
{
notImplemented();
}
static inline cairo_operator_t toCairoOperator(CompositeOperator op)
{
switch (op) {
case CompositeClear:
return CAIRO_OPERATOR_CLEAR;
case CompositeCopy:
return CAIRO_OPERATOR_SOURCE;
case CompositeSourceOver:
return CAIRO_OPERATOR_OVER;
case CompositeSourceIn:
return CAIRO_OPERATOR_IN;
case CompositeSourceOut:
return CAIRO_OPERATOR_OUT;
case CompositeSourceAtop:
return CAIRO_OPERATOR_ATOP;
case CompositeDestinationOver:
return CAIRO_OPERATOR_DEST_OVER;
case CompositeDestinationIn:
return CAIRO_OPERATOR_DEST_IN;
case CompositeDestinationOut:
return CAIRO_OPERATOR_DEST_OUT;
case CompositeDestinationAtop:
return CAIRO_OPERATOR_DEST_ATOP;
case CompositeXOR:
return CAIRO_OPERATOR_XOR;
case CompositePlusDarker:
return CAIRO_OPERATOR_SATURATE;
case CompositeHighlight:
return CAIRO_OPERATOR_OVER;
case CompositePlusLighter:
return CAIRO_OPERATOR_ADD;
default:
return CAIRO_OPERATOR_SOURCE;
}
}
void GraphicsContext::setCompositeOperation(CompositeOperator op)
{
if (paintingDisabled())
return;
cairo_set_operator(m_data->cr, toCairoOperator(op));
}
void GraphicsContext::beginPath()
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_new_path(cr);
}
void GraphicsContext::addPath(const Path& path)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_path_t* p = cairo_copy_path(path.platformPath()->m_cr);
cairo_append_path(cr, p);
cairo_path_destroy(p);
}
void GraphicsContext::clip(const Path& path)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_path_t* p = cairo_copy_path(path.platformPath()->m_cr);
cairo_append_path(cr, p);
cairo_path_destroy(p);
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_WINDING);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::clipOut(const Path& path)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
double x1, y1, x2, y2;
cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
cairo_rectangle(cr, x1, y1, x2 - x1, y2 - y1);
addPath(path);
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::rotate(float radians)
{
if (paintingDisabled())
return;
cairo_rotate(m_data->cr, radians);
}
void GraphicsContext::scale(const FloatSize& size)
{
if (paintingDisabled())
return;
cairo_scale(m_data->cr, size.width(), size.height());
}
void GraphicsContext::clipOut(const IntRect& r)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
double x1, y1, x2, y2;
cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
cairo_rectangle(cr, x1, x2, x2 - x1, y2 - y1);
cairo_rectangle(cr, r.x(), r.y(), r.width(), r.height());
cairo_fill_rule_t savedFillRule = cairo_get_fill_rule(cr);
cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
cairo_clip(cr);
cairo_set_fill_rule(cr, savedFillRule);
}
void GraphicsContext::clipOutEllipseInRect(const IntRect& r)
{
if (paintingDisabled())
return;
Path p;
p.addEllipse(r);
clipOut(p);
}
void GraphicsContext::fillRoundedRect(const IntRect& r, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color)
{
if (paintingDisabled())
return;
cairo_t* cr = m_data->cr;
cairo_save(cr);
beginPath();
addPath(Path::createRoundedRectangle(r, topLeft, topRight, bottomLeft, bottomRight));
setColor(cr, color);
cairo_fill(cr);
cairo_restore(cr);
}
#if PLATFORM(GTK)
void GraphicsContext::setGdkExposeEvent(GdkEventExpose* expose)
{
m_data->expose = expose;
}
GdkEventExpose* GraphicsContext::gdkExposeEvent() const
{
return m_data->expose;
}
GdkDrawable* GraphicsContext::gdkDrawable() const
{
if (!m_data->expose)
return 0;
return GDK_DRAWABLE(m_data->expose->window);
}
IntPoint GraphicsContext::translatePoint(const IntPoint& point) const
{
cairo_matrix_t tm;
cairo_get_matrix(m_data->cr, &tm);
double x = point.x();
double y = point.y();
cairo_matrix_transform_point(&tm, &x, &y);
return IntPoint(x, y);
}
#endif
void GraphicsContext::setUseAntialiasing(bool enable)
{
if (paintingDisabled())
return;
cairo_set_antialias(m_data->cr, enable ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
}
}
#endif // PLATFORM(CAIRO)