2017-10-28 02:10:06 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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-10 02:15:10 +04: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 18:20:24 +04:00
|
|
|
PathBuilderCairo::PathBuilderCairo(FillRule aFillRule) : mFillRule(aFillRule) {}
|
2012-07-24 14:18:38 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathBuilderCairo::MoveTo(const Point& aPoint) {
|
2013-09-28 18:20:24 +04: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-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathBuilderCairo::LineTo(const Point& aPoint) {
|
2013-09-28 18:20:24 +04: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-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathBuilderCairo::BezierTo(const Point& aCP1, const Point& aCP2,
|
|
|
|
const Point& aCP3) {
|
2013-09-28 18:20:24 +04: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-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathBuilderCairo::QuadraticBezierTo(const Point& aCP1, const Point& aCP2) {
|
2012-01-10 02:15:10 +04:00
|
|
|
// 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 18:20:24 +04: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-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PathBuilderCairo::Close() {
|
2013-09-28 18:20:24 +04: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-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathBuilderCairo::Arc(const Point& aOrigin, float aRadius,
|
2012-01-10 02:15:10 +04:00
|
|
|
float aStartAngle, float aEndAngle,
|
|
|
|
bool aAntiClockwise) {
|
2013-12-13 16:14:36 +04:00
|
|
|
ArcToBezier(this, aOrigin, Size(aRadius, aRadius), aStartAngle, aEndAngle,
|
|
|
|
aAntiClockwise);
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Path> PathBuilderCairo::Finish() {
|
2019-07-06 11:18:28 +03:00
|
|
|
return MakeAndAddRef<PathCairo>(mFillRule, mPathData, mCurrentPoint,
|
|
|
|
mBeginPoint);
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
PathCairo::PathCairo(FillRule aFillRule,
|
2019-05-01 11:47:10 +03:00
|
|
|
std::vector<cairo_path_data_t>& aPathData,
|
2019-07-06 11:18:28 +03:00
|
|
|
const Point& aCurrentPoint, const Point& aBeginPoint)
|
2013-09-28 18:20:24 +04:00
|
|
|
: mFillRule(aFillRule),
|
|
|
|
mContainingContext(nullptr),
|
2019-06-05 10:55:46 +03:00
|
|
|
mCurrentPoint(aCurrentPoint),
|
|
|
|
mBeginPoint(aBeginPoint) {
|
2013-09-28 18:20:24 +04:00
|
|
|
mPathData.swap(aPathData);
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
PathCairo::PathCairo(cairo_t* aContext)
|
2014-01-10 23:06:17 +04:00
|
|
|
: mFillRule(FillRule::FILL_WINDING), mContainingContext(nullptr) {
|
2019-05-01 11:47:10 +03:00
|
|
|
cairo_path_t* path = cairo_copy_path(aContext);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
|
|
|
// 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-04 01:48:04 +04:00
|
|
|
}
|
2013-09-28 18:20:24 +04:00
|
|
|
|
|
|
|
cairo_path_destroy(path);
|
2012-09-04 01:48:04 +04:00
|
|
|
}
|
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
PathCairo::~PathCairo() {
|
|
|
|
if (mContainingContext) {
|
|
|
|
cairo_destroy(mContainingContext);
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 02:15:10 +04:00
|
|
|
|
2015-06-17 17:00:52 +03:00
|
|
|
already_AddRefed<PathBuilder> PathCairo::CopyToBuilder(
|
2012-01-10 02:15:10 +04:00
|
|
|
FillRule aFillRule) const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
|
|
|
builder->mPathData = mPathData;
|
|
|
|
builder->mCurrentPoint = mCurrentPoint;
|
2019-06-05 10:55:46 +03:00
|
|
|
builder->mBeginPoint = mBeginPoint;
|
2013-09-28 18:20:24 +04:00
|
|
|
|
2014-06-13 20:09:23 +04:00
|
|
|
return builder.forget();
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<PathBuilder> PathCairo::TransformedCopyToBuilder(
|
2019-05-01 11:47:10 +03:00
|
|
|
const Matrix& aTransform, FillRule aFillRule) const {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilderCairo> builder = new PathBuilderCairo(aFillRule);
|
2012-09-04 01:48:04 +04:00
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
AppendPathToBuilder(builder, &aTransform);
|
2016-09-08 19:26:03 +03:00
|
|
|
builder->mCurrentPoint = aTransform.TransformPoint(mCurrentPoint);
|
2019-06-05 10:55:46 +03:00
|
|
|
builder->mBeginPoint = aTransform.TransformPoint(mBeginPoint);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
2014-06-13 20:09:23 +04:00
|
|
|
return builder.forget();
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool PathCairo::ContainsPoint(const Point& aPoint,
|
|
|
|
const Matrix& aTransform) const {
|
2012-01-10 02:15:10 +04:00
|
|
|
Matrix inverse = aTransform;
|
|
|
|
inverse.Invert();
|
2016-09-08 19:26:03 +03:00
|
|
|
Point transformed = inverse.TransformPoint(aPoint);
|
2012-01-10 02:15:10 +04:00
|
|
|
|
2015-09-21 20:33:06 +03:00
|
|
|
EnsureContainingContext(aTransform);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
|
|
|
return cairo_in_fill(mContainingContext, transformed.x, transformed.y);
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
bool PathCairo::StrokeContainsPoint(const StrokeOptions& aStrokeOptions,
|
|
|
|
const Point& aPoint,
|
|
|
|
const Matrix& aTransform) const {
|
2012-10-29 19:54:53 +04:00
|
|
|
Matrix inverse = aTransform;
|
|
|
|
inverse.Invert();
|
2016-09-08 19:26:03 +03:00
|
|
|
Point transformed = inverse.TransformPoint(aPoint);
|
2012-10-29 19:54:53 +04:00
|
|
|
|
2015-09-21 20:33:06 +03:00
|
|
|
EnsureContainingContext(aTransform);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
|
|
|
SetCairoStrokeOptions(mContainingContext, aStrokeOptions);
|
|
|
|
|
|
|
|
return cairo_in_stroke(mContainingContext, transformed.x, transformed.y);
|
2012-10-29 19:54:53 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Rect PathCairo::GetBounds(const Matrix& aTransform) const {
|
2015-09-21 20:33:06 +03:00
|
|
|
EnsureContainingContext(aTransform);
|
2012-09-04 01:48:04 +04:00
|
|
|
|
2012-01-10 02:15:10 +04:00
|
|
|
double x1, y1, x2, y2;
|
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
cairo_path_extents(mContainingContext, &x1, &y1, &x2, &y2);
|
2012-09-28 21:21:40 +04:00
|
|
|
Rect bounds(Float(x1), Float(y1), Float(x2 - x1), Float(y2 - y1));
|
2012-01-10 02:15:10 +04:00
|
|
|
return aTransform.TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
Rect PathCairo::GetStrokedBounds(const StrokeOptions& aStrokeOptions,
|
|
|
|
const Matrix& aTransform) const {
|
2015-09-21 20:33:06 +03:00
|
|
|
EnsureContainingContext(aTransform);
|
2012-09-04 01:48:04 +04:00
|
|
|
|
2012-01-10 02:15:10 +04:00
|
|
|
double x1, y1, x2, y2;
|
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
SetCairoStrokeOptions(mContainingContext, aStrokeOptions);
|
2012-01-10 02:15:10 +04:00
|
|
|
|
2013-09-28 18:20:24 +04:00
|
|
|
cairo_stroke_extents(mContainingContext, &x1, &y1, &x2, &y2);
|
2012-09-28 21:21:40 +04:00
|
|
|
Rect bounds((Float)x1, (Float)y1, (Float)(x2 - x1), (Float)(y2 - y1));
|
2012-01-10 02:15:10 +04:00
|
|
|
return aTransform.TransformBounds(bounds);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathCairo::StreamToSink(PathSink* aSink) const {
|
2013-11-07 13:11:26 +04:00
|
|
|
for (size_t i = 0; i < mPathData.size(); i++) {
|
|
|
|
switch (mPathData[i].header.type) {
|
|
|
|
case CAIRO_PATH_MOVE_TO:
|
|
|
|
i++;
|
|
|
|
aSink->MoveTo(Point(mPathData[i].point.x, mPathData[i].point.y));
|
|
|
|
break;
|
|
|
|
case CAIRO_PATH_LINE_TO:
|
|
|
|
i++;
|
|
|
|
aSink->LineTo(Point(mPathData[i].point.x, mPathData[i].point.y));
|
|
|
|
break;
|
|
|
|
case CAIRO_PATH_CURVE_TO:
|
|
|
|
aSink->BezierTo(
|
|
|
|
Point(mPathData[i + 1].point.x, mPathData[i + 1].point.y),
|
|
|
|
Point(mPathData[i + 2].point.x, mPathData[i + 2].point.y),
|
|
|
|
Point(mPathData[i + 3].point.x, mPathData[i + 3].point.y));
|
|
|
|
i += 3;
|
|
|
|
break;
|
|
|
|
case CAIRO_PATH_CLOSE_PATH:
|
|
|
|
aSink->Close();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Corrupt path data!
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathCairo::EnsureContainingContext(const Matrix& aTransform) const {
|
2013-09-28 18:20:24 +04:00
|
|
|
if (mContainingContext) {
|
2015-09-21 20:33:06 +03:00
|
|
|
if (mContainingTransform.ExactlyEquals(aTransform)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mContainingContext = cairo_create(DrawTargetCairo::GetDummySurface());
|
2013-09-28 18:20:24 +04:00
|
|
|
}
|
|
|
|
|
2015-09-21 20:33:06 +03:00
|
|
|
mContainingTransform = aTransform;
|
|
|
|
|
|
|
|
cairo_matrix_t mat;
|
|
|
|
GfxMatrixToCairoMatrix(mContainingTransform, mat);
|
|
|
|
cairo_set_matrix(mContainingContext, &mat);
|
2013-09-28 18:20:24 +04:00
|
|
|
|
|
|
|
SetPathOnContext(mContainingContext);
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathCairo::SetPathOnContext(cairo_t* aContext) const {
|
2013-09-28 18:20:24 +04:00
|
|
|
// Needs the correct fill rule set.
|
2012-09-04 01:48:04 +04:00
|
|
|
cairo_set_fill_rule(aContext, GfxFillRuleToCairoFillRule(mFillRule));
|
2013-09-28 18:20:24 +04:00
|
|
|
|
|
|
|
cairo_new_path(aContext);
|
|
|
|
|
2017-08-03 13:02:35 +03:00
|
|
|
if (!mPathData.empty()) {
|
2013-09-28 18:20:24 +04:00
|
|
|
cairo_path_t path;
|
2019-05-01 11:47:10 +03:00
|
|
|
path.data = const_cast<cairo_path_data_t*>(&mPathData.front());
|
2013-09-28 18:20:24 +04:00
|
|
|
path.num_data = mPathData.size();
|
|
|
|
path.status = CAIRO_STATUS_SUCCESS;
|
|
|
|
cairo_append_path(aContext, &path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void PathCairo::AppendPathToBuilder(PathBuilderCairo* aBuilder,
|
|
|
|
const Matrix* aTransform) const {
|
2013-09-28 18:20:24 +04:00
|
|
|
if (aTransform) {
|
2013-10-11 19:55:03 +04:00
|
|
|
size_t i = 0;
|
2013-09-28 18:20:24 +04:00
|
|
|
while (i < mPathData.size()) {
|
|
|
|
uint32_t pointCount = mPathData[i].header.length - 1;
|
|
|
|
aBuilder->mPathData.push_back(mPathData[i]);
|
|
|
|
i++;
|
2013-10-11 19:55:03 +04:00
|
|
|
for (uint32_t c = 0; c < pointCount; c++) {
|
2013-09-28 18:20:24 +04:00
|
|
|
cairo_path_data_t data;
|
2016-09-08 19:26:03 +03:00
|
|
|
Point newPoint = aTransform->TransformPoint(
|
|
|
|
Point(mPathData[i].point.x, mPathData[i].point.y));
|
2013-09-28 18:20:24 +04:00
|
|
|
data.point.x = newPoint.x;
|
|
|
|
data.point.y = newPoint.y;
|
|
|
|
aBuilder->mPathData.push_back(data);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-11 19:55:03 +04:00
|
|
|
for (size_t i = 0; i < mPathData.size(); i++) {
|
2013-09-28 18:20:24 +04:00
|
|
|
aBuilder->mPathData.push_back(mPathData[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-01-10 02:15:10 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|