2015-05-03 22:32:37 +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: */
|
2013-01-06 13:32:02 +04: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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/SVGLineElement.h"
|
2018-02-02 16:21:33 +03:00
|
|
|
#include "mozilla/dom/SVGLengthBinding.h"
|
2013-01-06 13:32:02 +04:00
|
|
|
#include "mozilla/dom/SVGLineElementBinding.h"
|
2013-11-02 15:10:38 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2013-01-06 13:32:02 +04:00
|
|
|
|
2018-12-21 14:43:29 +03:00
|
|
|
NS_IMPL_NS_NEW_SVG_ELEMENT(Line)
|
2013-01-06 13:32:02 +04:00
|
|
|
|
2013-11-02 15:10:38 +04:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2013-01-06 13:32:02 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* SVGLineElement::WrapNode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return SVGLineElement_Binding::Wrap(aCx, this, aGivenProto);
|
2013-01-06 13:32:02 +04:00
|
|
|
}
|
|
|
|
|
2018-12-21 11:58:14 +03:00
|
|
|
SVGElement::LengthInfo SVGLineElement::sLengthInfo[4] = {
|
2018-03-29 12:45:24 +03:00
|
|
|
{nsGkAtoms::x1, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
|
|
|
|
SVGContentUtils::X},
|
|
|
|
{nsGkAtoms::y1, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
|
|
|
|
SVGContentUtils::Y},
|
|
|
|
{nsGkAtoms::x2, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
|
|
|
|
SVGContentUtils::X},
|
|
|
|
{nsGkAtoms::y2, 0, SVGLength_Binding::SVG_LENGTHTYPE_NUMBER,
|
|
|
|
SVGContentUtils::Y},
|
2013-01-06 13:32:02 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Implementation
|
|
|
|
|
2018-09-21 23:45:49 +03:00
|
|
|
SVGLineElement::SVGLineElement(
|
|
|
|
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
|
|
|
|
: SVGLineElementBase(std::move(aNodeInfo)) {}
|
2013-01-06 13:32:02 +04:00
|
|
|
|
2015-08-01 11:09:00 +03:00
|
|
|
void SVGLineElement::MaybeAdjustForZeroLength(float aX1, float aY1, float& aX2,
|
|
|
|
float aY2) {
|
|
|
|
if (aX1 == aX2 && aY1 == aY2) {
|
|
|
|
SVGContentUtils::AutoStrokeOptions strokeOptions;
|
|
|
|
SVGContentUtils::GetStrokeOptions(&strokeOptions, this, nullptr, nullptr,
|
|
|
|
SVGContentUtils::eIgnoreStrokeDashing);
|
|
|
|
|
|
|
|
if (strokeOptions.mLineCap != CapStyle::BUTT) {
|
|
|
|
float tinyLength =
|
|
|
|
strokeOptions.mLineWidth / SVG_ZERO_LENGTH_PATH_FIX_FACTOR;
|
|
|
|
aX2 += tinyLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-06 13:32:02 +04:00
|
|
|
//----------------------------------------------------------------------
|
2018-05-30 05:58:49 +03:00
|
|
|
// nsINode methods
|
2013-01-06 13:32:02 +04:00
|
|
|
|
|
|
|
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGLineElement)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2019-03-19 03:01:03 +03:00
|
|
|
already_AddRefed<DOMSVGAnimatedLength> SVGLineElement::X1() {
|
2013-01-06 13:32:02 +04:00
|
|
|
return mLengthAttributes[ATTR_X1].ToDOMAnimatedLength(this);
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:01:03 +03:00
|
|
|
already_AddRefed<DOMSVGAnimatedLength> SVGLineElement::Y1() {
|
2013-01-06 13:32:02 +04:00
|
|
|
return mLengthAttributes[ATTR_Y1].ToDOMAnimatedLength(this);
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:01:03 +03:00
|
|
|
already_AddRefed<DOMSVGAnimatedLength> SVGLineElement::X2() {
|
2013-01-06 13:32:02 +04:00
|
|
|
return mLengthAttributes[ATTR_X2].ToDOMAnimatedLength(this);
|
|
|
|
}
|
|
|
|
|
2019-03-19 03:01:03 +03:00
|
|
|
already_AddRefed<DOMSVGAnimatedLength> SVGLineElement::Y2() {
|
2013-01-06 13:32:02 +04:00
|
|
|
return mLengthAttributes[ATTR_Y2].ToDOMAnimatedLength(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsIContent methods
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(bool)
|
2017-10-03 01:05:19 +03:00
|
|
|
SVGLineElement::IsAttributeMapped(const nsAtom* name) const {
|
2013-01-06 13:32:02 +04:00
|
|
|
static const MappedAttributeEntry* const map[] = {sMarkersMap};
|
|
|
|
|
|
|
|
return FindAttributeDependence(name, map) ||
|
|
|
|
SVGLineElementBase::IsAttributeMapped(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-12-21 11:58:14 +03:00
|
|
|
// SVGElement methods
|
2013-01-06 13:32:02 +04:00
|
|
|
|
2018-12-21 11:58:14 +03:00
|
|
|
SVGElement::LengthAttributesInfo SVGLineElement::GetLengthInfo() {
|
2013-01-06 13:32:02 +04:00
|
|
|
return LengthAttributesInfo(mLengthAttributes, sLengthInfo,
|
|
|
|
ArrayLength(sLengthInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-12-18 14:11:47 +03:00
|
|
|
// SVGGeometryElement methods
|
2013-01-06 13:32:02 +04:00
|
|
|
|
2019-01-26 16:01:31 +03:00
|
|
|
void SVGLineElement::GetMarkPoints(nsTArray<SVGMark>* aMarks) {
|
2013-01-06 13:32:02 +04:00
|
|
|
float x1, y1, x2, y2;
|
|
|
|
|
|
|
|
GetAnimatedLengthValues(&x1, &y1, &x2, &y2, nullptr);
|
|
|
|
|
2019-04-26 00:46:33 +03:00
|
|
|
float angle = std::atan2(y2 - y1, x2 - x1);
|
2013-01-06 13:32:02 +04:00
|
|
|
|
2019-01-26 16:01:31 +03:00
|
|
|
aMarks->AppendElement(SVGMark(x1, y1, angle, SVGMark::eStart));
|
|
|
|
aMarks->AppendElement(SVGMark(x2, y2, angle, SVGMark::eEnd));
|
2013-01-06 13:32:02 +04:00
|
|
|
}
|
|
|
|
|
2014-10-22 15:29:05 +04:00
|
|
|
void SVGLineElement::GetAsSimplePath(SimplePath* aSimplePath) {
|
|
|
|
float x1, y1, x2, y2;
|
|
|
|
GetAnimatedLengthValues(&x1, &y1, &x2, &y2, nullptr);
|
2015-08-01 11:09:00 +03:00
|
|
|
|
|
|
|
MaybeAdjustForZeroLength(x1, y1, x2, y2);
|
2014-10-22 15:29:05 +04:00
|
|
|
aSimplePath->SetLine(x1, y1, x2, y2);
|
|
|
|
}
|
|
|
|
|
2014-07-06 00:53:04 +04:00
|
|
|
already_AddRefed<Path> SVGLineElement::BuildPath(PathBuilder* aBuilder) {
|
2013-11-02 15:10:38 +04:00
|
|
|
float x1, y1, x2, y2;
|
|
|
|
GetAnimatedLengthValues(&x1, &y1, &x2, &y2, nullptr);
|
|
|
|
|
2015-08-01 11:09:00 +03:00
|
|
|
MaybeAdjustForZeroLength(x1, y1, x2, y2);
|
2014-10-04 15:13:30 +04:00
|
|
|
aBuilder->MoveTo(Point(x1, y1));
|
|
|
|
aBuilder->LineTo(Point(x2, y2));
|
2013-11-02 15:10:38 +04:00
|
|
|
|
2014-10-04 15:13:30 +04:00
|
|
|
return aBuilder->Finish();
|
2013-11-02 15:10:38 +04:00
|
|
|
}
|
|
|
|
|
2015-09-01 07:17:00 +03:00
|
|
|
bool SVGLineElement::GetGeometryBounds(Rect* aBounds,
|
|
|
|
const StrokeOptions& aStrokeOptions,
|
|
|
|
const Matrix& aToBoundsSpace,
|
|
|
|
const Matrix* aToNonScalingStrokeSpace) {
|
2015-01-22 12:36:08 +03:00
|
|
|
float x1, y1, x2, y2;
|
|
|
|
GetAnimatedLengthValues(&x1, &y1, &x2, &y2, nullptr);
|
|
|
|
|
2015-02-03 21:36:32 +03:00
|
|
|
if (aStrokeOptions.mLineWidth <= 0) {
|
2016-09-08 19:26:03 +03:00
|
|
|
*aBounds = Rect(aToBoundsSpace.TransformPoint(Point(x1, y1)), Size());
|
|
|
|
aBounds->ExpandToEnclose(aToBoundsSpace.TransformPoint(Point(x2, y2)));
|
2015-01-22 12:36:08 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-02 21:10:00 +03:00
|
|
|
// transform from non-scaling-stroke space to the space in which we compute
|
|
|
|
// bounds
|
|
|
|
Matrix nonScalingToBounds;
|
|
|
|
if (aToNonScalingStrokeSpace) {
|
2015-09-30 09:05:00 +03:00
|
|
|
MOZ_ASSERT(!aToNonScalingStrokeSpace->IsSingular());
|
2015-09-02 21:10:00 +03:00
|
|
|
Matrix nonScalingToUser = aToNonScalingStrokeSpace->Inverse();
|
|
|
|
nonScalingToBounds = nonScalingToUser * aToBoundsSpace;
|
|
|
|
}
|
|
|
|
|
2015-02-03 21:36:32 +03:00
|
|
|
if (aStrokeOptions.mLineCap == CapStyle::ROUND) {
|
2015-09-02 21:10:00 +03:00
|
|
|
if (!aToBoundsSpace.IsRectilinear() ||
|
|
|
|
(aToNonScalingStrokeSpace &&
|
|
|
|
!aToNonScalingStrokeSpace->IsRectilinear())) {
|
2015-01-22 12:36:08 +03:00
|
|
|
// TODO: handle this case.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Rect bounds(Point(x1, y1), Size());
|
|
|
|
bounds.ExpandToEnclose(Point(x2, y2));
|
2015-09-02 21:10:00 +03:00
|
|
|
if (aToNonScalingStrokeSpace) {
|
|
|
|
bounds = aToNonScalingStrokeSpace->TransformBounds(bounds);
|
|
|
|
bounds.Inflate(aStrokeOptions.mLineWidth / 2.f);
|
|
|
|
*aBounds = nonScalingToBounds.TransformBounds(bounds);
|
|
|
|
} else {
|
|
|
|
bounds.Inflate(aStrokeOptions.mLineWidth / 2.f);
|
|
|
|
*aBounds = aToBoundsSpace.TransformBounds(bounds);
|
|
|
|
}
|
2015-01-22 12:36:08 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-02 21:10:00 +03:00
|
|
|
// Handle butt and square linecap, normal and non-scaling stroke cases
|
|
|
|
// together: start with endpoints (x1, y1), (x2, y2) in the stroke space,
|
|
|
|
// compute the four corners of the stroked line, transform the corners to
|
|
|
|
// bounds space, and compute bounds there.
|
|
|
|
|
|
|
|
if (aToNonScalingStrokeSpace) {
|
|
|
|
Point nonScalingSpaceP1, nonScalingSpaceP2;
|
2016-09-08 19:26:03 +03:00
|
|
|
nonScalingSpaceP1 = aToNonScalingStrokeSpace->TransformPoint(Point(x1, y1));
|
|
|
|
nonScalingSpaceP2 = aToNonScalingStrokeSpace->TransformPoint(Point(x2, y2));
|
2015-09-02 21:10:00 +03:00
|
|
|
x1 = nonScalingSpaceP1.x;
|
|
|
|
y1 = nonScalingSpaceP1.y;
|
|
|
|
x2 = nonScalingSpaceP2.x;
|
|
|
|
y2 = nonScalingSpaceP2.y;
|
|
|
|
}
|
|
|
|
|
2015-01-22 12:36:08 +03:00
|
|
|
Float length = Float(NS_hypot(x2 - x1, y2 - y1));
|
|
|
|
Float xDelta;
|
|
|
|
Float yDelta;
|
2015-09-02 21:10:00 +03:00
|
|
|
Point points[4];
|
2015-01-22 12:36:08 +03:00
|
|
|
|
2015-02-03 21:36:32 +03:00
|
|
|
if (aStrokeOptions.mLineCap == CapStyle::BUTT) {
|
2015-01-22 12:36:08 +03:00
|
|
|
if (length == 0.f) {
|
|
|
|
xDelta = yDelta = 0.f;
|
|
|
|
} else {
|
2015-02-03 21:36:32 +03:00
|
|
|
Float ratio = aStrokeOptions.mLineWidth / 2.f / length;
|
2015-01-22 12:36:08 +03:00
|
|
|
xDelta = ratio * (y2 - y1);
|
|
|
|
yDelta = ratio * (x2 - x1);
|
|
|
|
}
|
2015-09-02 21:10:00 +03:00
|
|
|
points[0] = Point(x1 - xDelta, y1 + yDelta);
|
|
|
|
points[1] = Point(x1 + xDelta, y1 - yDelta);
|
|
|
|
points[2] = Point(x2 + xDelta, y2 - yDelta);
|
|
|
|
points[3] = Point(x2 - xDelta, y2 + yDelta);
|
2015-01-22 12:36:08 +03:00
|
|
|
} else {
|
2015-02-03 21:36:32 +03:00
|
|
|
MOZ_ASSERT(aStrokeOptions.mLineCap == CapStyle::SQUARE);
|
2015-01-22 12:36:08 +03:00
|
|
|
if (length == 0.f) {
|
2015-02-03 21:36:32 +03:00
|
|
|
xDelta = yDelta = aStrokeOptions.mLineWidth / 2.f;
|
2015-09-02 21:10:00 +03:00
|
|
|
points[0] = Point(x1 - xDelta, y1 + yDelta);
|
|
|
|
points[1] = Point(x1 - xDelta, y1 - yDelta);
|
|
|
|
points[2] = Point(x1 + xDelta, y1 - yDelta);
|
|
|
|
points[3] = Point(x1 + xDelta, y1 + yDelta);
|
2015-01-22 12:36:08 +03:00
|
|
|
} else {
|
2015-02-03 21:36:32 +03:00
|
|
|
Float ratio = aStrokeOptions.mLineWidth / 2.f / length;
|
2015-09-02 21:10:00 +03:00
|
|
|
yDelta = ratio * (x2 - x1);
|
|
|
|
xDelta = ratio * (y2 - y1);
|
|
|
|
points[0] = Point(x1 - yDelta - xDelta, y1 - xDelta + yDelta);
|
|
|
|
points[1] = Point(x1 - yDelta + xDelta, y1 - xDelta - yDelta);
|
|
|
|
points[2] = Point(x2 + yDelta + xDelta, y2 + xDelta - yDelta);
|
|
|
|
points[3] = Point(x2 + yDelta - xDelta, y2 + xDelta + yDelta);
|
2015-01-22 12:36:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 21:10:00 +03:00
|
|
|
const Matrix& toBoundsSpace =
|
|
|
|
aToNonScalingStrokeSpace ? nonScalingToBounds : aToBoundsSpace;
|
2015-01-22 12:36:08 +03:00
|
|
|
|
2016-09-08 19:26:03 +03:00
|
|
|
*aBounds = Rect(toBoundsSpace.TransformPoint(points[0]), Size());
|
2015-01-22 12:36:08 +03:00
|
|
|
for (uint32_t i = 1; i < 4; ++i) {
|
2016-09-08 19:26:03 +03:00
|
|
|
aBounds->ExpandToEnclose(toBoundsSpace.TransformPoint(points[i]));
|
2015-01-22 12:36:08 +03:00
|
|
|
}
|
2015-09-02 21:10:00 +03:00
|
|
|
|
2015-01-22 12:36:08 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-06 13:32:02 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|