2012-01-09 14:15:10 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-09 14:15:10 -08:00
|
|
|
|
|
|
|
#include "PathCairo.h"
|
|
|
|
#include <math.h>
|
|
|
|
#include "DrawTargetCairo.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "PathHelpers.h"
|
|
|
|
#include "HelpersCairo.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
PathBuilderCairo::PathBuilderCairo(FillRule aFillRule)
|
|
|
|
: mFillRule(aFillRule)
|
2012-01-09 14:15:10 -08:00
|
|
|
{
|
2012-07-24 03:18:38 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
void
|
|
|
|
PathBuilderCairo::MoveTo(const Point &aPoint)
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_path_data_t data;
|
|
|
|
data.header.type = CAIRO_PATH_MOVE_TO;
|
|
|
|
data.header.length = 2;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = aPoint.x;
|
|
|
|
data.point.y = aPoint.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
|
|
|
|
mBeginPoint = mCurrentPoint = aPoint;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCairo::LineTo(const Point &aPoint)
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_path_data_t data;
|
|
|
|
data.header.type = CAIRO_PATH_LINE_TO;
|
|
|
|
data.header.length = 2;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = aPoint.x;
|
|
|
|
data.point.y = aPoint.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
|
|
|
|
mCurrentPoint = aPoint;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCairo::BezierTo(const Point &aCP1,
|
|
|
|
const Point &aCP2,
|
|
|
|
const Point &aCP3)
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_path_data_t data;
|
|
|
|
data.header.type = CAIRO_PATH_CURVE_TO;
|
|
|
|
data.header.length = 4;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = aCP1.x;
|
|
|
|
data.point.y = aCP1.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = aCP2.x;
|
|
|
|
data.point.y = aCP2.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = aCP3.x;
|
|
|
|
data.point.y = aCP3.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
|
|
|
|
mCurrentPoint = aCP3;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCairo::QuadraticBezierTo(const Point &aCP1,
|
|
|
|
const Point &aCP2)
|
|
|
|
{
|
|
|
|
// We need to elevate the degree of this quadratic Bézier to cubic, so we're
|
|
|
|
// going to add an intermediate control point, and recompute control point 1.
|
|
|
|
// The first and last control points remain the same.
|
|
|
|
// This formula can be found on http://fontforge.sourceforge.net/bezier.html
|
|
|
|
Point CP0 = CurrentPoint();
|
|
|
|
Point CP1 = (CP0 + aCP1 * 2.0) / 3.0;
|
|
|
|
Point CP2 = (aCP2 + aCP1 * 2.0) / 3.0;
|
|
|
|
Point CP3 = aCP2;
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_path_data_t data;
|
|
|
|
data.header.type = CAIRO_PATH_CURVE_TO;
|
|
|
|
data.header.length = 4;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = CP1.x;
|
|
|
|
data.point.y = CP1.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = CP2.x;
|
|
|
|
data.point.y = CP2.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
data.point.x = CP3.x;
|
|
|
|
data.point.y = CP3.y;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
|
|
|
|
mCurrentPoint = aCP2;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCairo::Close()
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_path_data_t data;
|
|
|
|
data.header.type = CAIRO_PATH_CLOSE_PATH;
|
|
|
|
data.header.length = 1;
|
|
|
|
mPathData.push_back(data);
|
|
|
|
|
|
|
|
mCurrentPoint = mBeginPoint;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathBuilderCairo::Arc(const Point &aOrigin, float aRadius, float aStartAngle,
|
|
|
|
float aEndAngle, bool aAntiClockwise)
|
|
|
|
{
|
|
|
|
ArcToBezier(this, aOrigin, aRadius, aStartAngle, aEndAngle, aAntiClockwise);
|
|
|
|
}
|
|
|
|
|
|
|
|
Point
|
|
|
|
PathBuilderCairo::CurrentPoint() const
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
return mCurrentPoint;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<Path>
|
|
|
|
PathBuilderCairo::Finish()
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
return new PathCairo(mFillRule, mPathData, mCurrentPoint);
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
PathCairo::PathCairo(FillRule aFillRule, std::vector<cairo_path_data_t> &aPathData, const Point &aCurrentPoint)
|
|
|
|
: mFillRule(aFillRule)
|
|
|
|
, mContainingContext(nullptr)
|
|
|
|
, mCurrentPoint(aCurrentPoint)
|
2012-01-09 14:15:10 -08:00
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
mPathData.swap(aPathData);
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
PathCairo::PathCairo(cairo_t *aContext)
|
|
|
|
: mFillRule(FILL_WINDING)
|
|
|
|
, mContainingContext(nullptr)
|
2012-09-03 14:48:04 -07:00
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_path_t *path = cairo_copy_path(aContext);
|
|
|
|
|
|
|
|
// XXX - mCurrentPoint is not properly set here, the same is true for the
|
|
|
|
// D2D Path code, we never require current point when hitting this codepath
|
|
|
|
// but this should be fixed.
|
|
|
|
for (int i = 0; i < path->num_data; i++) {
|
|
|
|
mPathData.push_back(path->data[i]);
|
2012-09-03 14:48:04 -07:00
|
|
|
}
|
2013-09-28 07:20:24 -07:00
|
|
|
|
|
|
|
cairo_path_destroy(path);
|
2012-09-03 14:48:04 -07:00
|
|
|
}
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
PathCairo::~PathCairo()
|
|
|
|
{
|
|
|
|
if (mContainingContext) {
|
|
|
|
cairo_destroy(mContainingContext);
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 14:15:10 -08:00
|
|
|
|
|
|
|
TemporaryRef<PathBuilder>
|
|
|
|
PathCairo::CopyToBuilder(FillRule aFillRule) const
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
|
|
|
|
|
|
|
|
builder->mPathData = mPathData;
|
|
|
|
builder->mCurrentPoint = mCurrentPoint;
|
|
|
|
|
|
|
|
return builder;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<PathBuilder>
|
|
|
|
PathCairo::TransformedCopyToBuilder(const Matrix &aTransform, FillRule aFillRule) const
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
|
2012-09-03 14:48:04 -07:00
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
AppendPathToBuilder(builder, &aTransform);
|
|
|
|
builder->mCurrentPoint = aTransform * mCurrentPoint;
|
|
|
|
|
|
|
|
return builder;
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PathCairo::ContainsPoint(const Point &aPoint, const Matrix &aTransform) const
|
|
|
|
{
|
|
|
|
Matrix inverse = aTransform;
|
|
|
|
inverse.Invert();
|
|
|
|
Point transformed = inverse * aPoint;
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
EnsureContainingContext();
|
|
|
|
|
|
|
|
return cairo_in_fill(mContainingContext, transformed.x, transformed.y);
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
2012-10-29 08:54:53 -07:00
|
|
|
bool
|
|
|
|
PathCairo::StrokeContainsPoint(const StrokeOptions &aStrokeOptions,
|
|
|
|
const Point &aPoint,
|
|
|
|
const Matrix &aTransform) const
|
|
|
|
{
|
|
|
|
Matrix inverse = aTransform;
|
|
|
|
inverse.Invert();
|
|
|
|
Point transformed = inverse * aPoint;
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
EnsureContainingContext();
|
|
|
|
|
|
|
|
SetCairoStrokeOptions(mContainingContext, aStrokeOptions);
|
|
|
|
|
|
|
|
return cairo_in_stroke(mContainingContext, transformed.x, transformed.y);
|
2012-10-29 08:54:53 -07:00
|
|
|
}
|
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
Rect
|
|
|
|
PathCairo::GetBounds(const Matrix &aTransform) const
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
EnsureContainingContext();
|
2012-09-03 14:48:04 -07:00
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
double x1, y1, x2, y2;
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_path_extents(mContainingContext, &x1, &y1, &x2, &y2);
|
2012-09-28 10:21:40 -07:00
|
|
|
Rect bounds(Float(x1), Float(y1), Float(x2 - x1), Float(y2 - y1));
|
2012-01-09 14:15:10 -08:00
|
|
|
return aTransform.TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect
|
|
|
|
PathCairo::GetStrokedBounds(const StrokeOptions &aStrokeOptions,
|
|
|
|
const Matrix &aTransform) const
|
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
EnsureContainingContext();
|
2012-09-03 14:48:04 -07:00
|
|
|
|
2012-01-09 14:15:10 -08:00
|
|
|
double x1, y1, x2, y2;
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
SetCairoStrokeOptions(mContainingContext, aStrokeOptions);
|
2012-01-09 14:15:10 -08:00
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
cairo_stroke_extents(mContainingContext, &x1, &y1, &x2, &y2);
|
2012-09-28 10:21:40 -07:00
|
|
|
Rect bounds((Float)x1, (Float)y1, (Float)(x2 - x1), (Float)(y2 - y1));
|
2012-01-09 14:15:10 -08:00
|
|
|
return aTransform.TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
2013-09-28 07:20:24 -07:00
|
|
|
void
|
|
|
|
PathCairo::EnsureContainingContext() const
|
2012-01-09 14:15:10 -08:00
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
if (mContainingContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mContainingContext = cairo_create(DrawTargetCairo::GetDummySurface());
|
|
|
|
|
|
|
|
SetPathOnContext(mContainingContext);
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-28 07:20:24 -07:00
|
|
|
PathCairo::SetPathOnContext(cairo_t *aContext) const
|
2012-01-09 14:15:10 -08:00
|
|
|
{
|
2013-09-28 07:20:24 -07:00
|
|
|
// Needs the correct fill rule set.
|
2012-09-03 14:48:04 -07:00
|
|
|
cairo_set_fill_rule(aContext, GfxFillRuleToCairoFillRule(mFillRule));
|
2013-09-28 07:20:24 -07:00
|
|
|
|
|
|
|
cairo_new_path(aContext);
|
|
|
|
|
|
|
|
if (mPathData.size()) {
|
|
|
|
cairo_path_t path;
|
|
|
|
path.data = const_cast<cairo_path_data_t*>(&mPathData.front());
|
|
|
|
path.num_data = mPathData.size();
|
|
|
|
path.status = CAIRO_STATUS_SUCCESS;
|
|
|
|
cairo_append_path(aContext, &path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PathCairo::AppendPathToBuilder(PathBuilderCairo *aBuilder, const Matrix *aTransform) const
|
|
|
|
{
|
|
|
|
if (aTransform) {
|
|
|
|
int i = 0;
|
|
|
|
while (i < mPathData.size()) {
|
|
|
|
uint32_t pointCount = mPathData[i].header.length - 1;
|
|
|
|
aBuilder->mPathData.push_back(mPathData[i]);
|
|
|
|
i++;
|
|
|
|
for (int c = 0; c < pointCount; c++) {
|
|
|
|
cairo_path_data_t data;
|
|
|
|
Point newPoint = *aTransform * Point(mPathData[i].point.x, mPathData[i].point.y);
|
|
|
|
data.point.x = newPoint.x;
|
|
|
|
data.point.y = newPoint.y;
|
|
|
|
aBuilder->mPathData.push_back(data);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < mPathData.size(); i++) {
|
|
|
|
aBuilder->mPathData.push_back(mPathData[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-01-09 14:15:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|