2013-01-06 09:32:01 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/SVGRectElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "mozilla/dom/SVGRectElementBinding.h"
|
2013-11-02 11:10:38 +00:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-10-26 18:00:03 +00:00
|
|
|
#include "mozilla/gfx/Matrix.h"
|
|
|
|
#include "mozilla/gfx/Rect.h"
|
2013-11-02 11:10:38 +00:00
|
|
|
#include "mozilla/gfx/PathHelpers.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2013-01-06 09:32:01 +00:00
|
|
|
|
|
|
|
NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(Rect)
|
|
|
|
|
2013-11-02 11:10:38 +00:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2013-01-06 09:32:01 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-03-11 22:05:58 +00:00
|
|
|
class SVGAnimatedLength;
|
|
|
|
|
2013-01-06 09:32:01 +00:00
|
|
|
JSObject*
|
2014-04-08 22:27:17 +00:00
|
|
|
SVGRectElement::WrapNode(JSContext *aCx)
|
2013-01-06 09:32:01 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return SVGRectElementBinding::Wrap(aCx, this);
|
2013-01-06 09:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGElement::LengthInfo SVGRectElement::sLengthInfo[6] =
|
|
|
|
{
|
|
|
|
{ &nsGkAtoms::x, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
|
|
|
|
{ &nsGkAtoms::y, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
|
|
|
|
{ &nsGkAtoms::width, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
|
|
|
|
{ &nsGkAtoms::height, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y },
|
|
|
|
{ &nsGkAtoms::rx, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::X },
|
|
|
|
{ &nsGkAtoms::ry, 0, nsIDOMSVGLength::SVG_LENGTHTYPE_NUMBER, SVGContentUtils::Y }
|
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Implementation
|
|
|
|
|
2014-06-20 02:01:40 +00:00
|
|
|
SVGRectElement::SVGRectElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2013-01-06 09:32:01 +00:00
|
|
|
: SVGRectElementBase(aNodeInfo)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsIDOMNode methods
|
|
|
|
|
|
|
|
NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGRectElement)
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
already_AddRefed<SVGAnimatedLength>
|
2013-01-06 09:32:01 +00:00
|
|
|
SVGRectElement::X()
|
|
|
|
{
|
2013-01-06 09:32:02 +00:00
|
|
|
return mLengthAttributes[ATTR_X].ToDOMAnimatedLength(this);
|
2013-01-06 09:32:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
already_AddRefed<SVGAnimatedLength>
|
2013-01-06 09:32:01 +00:00
|
|
|
SVGRectElement::Y()
|
|
|
|
{
|
2013-01-06 09:32:02 +00:00
|
|
|
return mLengthAttributes[ATTR_Y].ToDOMAnimatedLength(this);
|
2013-01-06 09:32:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
already_AddRefed<SVGAnimatedLength>
|
2013-01-06 09:32:01 +00:00
|
|
|
SVGRectElement::Width()
|
|
|
|
{
|
2013-01-06 09:32:02 +00:00
|
|
|
return mLengthAttributes[ATTR_WIDTH].ToDOMAnimatedLength(this);
|
2013-01-06 09:32:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
already_AddRefed<SVGAnimatedLength>
|
2013-01-06 09:32:01 +00:00
|
|
|
SVGRectElement::Height()
|
|
|
|
{
|
2013-01-06 09:32:02 +00:00
|
|
|
return mLengthAttributes[ATTR_HEIGHT].ToDOMAnimatedLength(this);
|
2013-01-06 09:32:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
already_AddRefed<SVGAnimatedLength>
|
2013-01-06 09:32:01 +00:00
|
|
|
SVGRectElement::Rx()
|
|
|
|
{
|
2013-01-06 09:32:02 +00:00
|
|
|
return mLengthAttributes[ATTR_RX].ToDOMAnimatedLength(this);
|
2013-01-06 09:32:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 20:56:00 +00:00
|
|
|
already_AddRefed<SVGAnimatedLength>
|
2013-01-06 09:32:01 +00:00
|
|
|
SVGRectElement::Ry()
|
|
|
|
{
|
2013-01-06 09:32:02 +00:00
|
|
|
return mLengthAttributes[ATTR_RY].ToDOMAnimatedLength(this);
|
2013-01-06 09:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsSVGElement methods
|
|
|
|
|
|
|
|
/* virtual */ bool
|
|
|
|
SVGRectElement::HasValidDimensions() const
|
|
|
|
{
|
|
|
|
return mLengthAttributes[ATTR_WIDTH].IsExplicitlySet() &&
|
|
|
|
mLengthAttributes[ATTR_WIDTH].GetAnimValInSpecifiedUnits() > 0 &&
|
|
|
|
mLengthAttributes[ATTR_HEIGHT].IsExplicitlySet() &&
|
|
|
|
mLengthAttributes[ATTR_HEIGHT].GetAnimValInSpecifiedUnits() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSVGElement::LengthAttributesInfo
|
|
|
|
SVGRectElement::GetLengthInfo()
|
|
|
|
{
|
|
|
|
return LengthAttributesInfo(mLengthAttributes, sLengthInfo,
|
|
|
|
ArrayLength(sLengthInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsSVGPathGeometryElement methods
|
|
|
|
|
2014-10-26 18:00:03 +00:00
|
|
|
bool
|
|
|
|
SVGRectElement::GetGeometryBounds(Rect* aBounds, Float aStrokeWidth,
|
|
|
|
const Matrix& aTransform)
|
|
|
|
{
|
|
|
|
Rect r;
|
|
|
|
Float rx, ry;
|
|
|
|
GetAnimatedLengthValues(&r.x, &r.y, &r.width, &r.height, &rx, &ry, nullptr);
|
|
|
|
|
|
|
|
if (r.IsEmpty()) {
|
|
|
|
// Rendering of the element disabled
|
|
|
|
r.SetEmpty(); // make sure width/height are actually zero
|
|
|
|
*aBounds = r;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
rx = std::max(rx, 0.0f);
|
|
|
|
ry = std::max(ry, 0.0f);
|
|
|
|
|
|
|
|
if (rx != 0 || ry != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStrokeWidth > 0.f) {
|
|
|
|
r.Inflate(aStrokeWidth / 2.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aBounds = aTransform.TransformBounds(r);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-22 11:29:05 +00:00
|
|
|
void
|
|
|
|
SVGRectElement::GetAsSimplePath(SimplePath* aSimplePath)
|
|
|
|
{
|
|
|
|
float x, y, width, height, rx, ry;
|
|
|
|
GetAnimatedLengthValues(&x, &y, &width, &height, &rx, &ry, nullptr);
|
|
|
|
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
aSimplePath->Reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rx = std::max(rx, 0.0f);
|
|
|
|
ry = std::max(ry, 0.0f);
|
|
|
|
|
|
|
|
if (rx != 0 || ry != 0) {
|
|
|
|
aSimplePath->Reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aSimplePath->SetRect(x, y, width, height);
|
|
|
|
}
|
|
|
|
|
2013-11-02 11:10:38 +00:00
|
|
|
TemporaryRef<Path>
|
2014-07-05 20:53:04 +00:00
|
|
|
SVGRectElement::BuildPath(PathBuilder* aBuilder)
|
2013-11-02 11:10:38 +00:00
|
|
|
{
|
|
|
|
float x, y, width, height, rx, ry;
|
|
|
|
GetAnimatedLengthValues(&x, &y, &width, &height, &rx, &ry, nullptr);
|
|
|
|
|
2013-11-06 10:05:18 +00:00
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
rx = std::max(rx, 0.0f);
|
|
|
|
ry = std::max(ry, 0.0f);
|
|
|
|
|
|
|
|
if (rx == 0 && ry == 0) {
|
|
|
|
// Optimization for the no rounded corners case.
|
|
|
|
Rect r(x, y, width, height);
|
2014-10-04 11:13:30 +00:00
|
|
|
aBuilder->MoveTo(r.TopLeft());
|
|
|
|
aBuilder->LineTo(r.TopRight());
|
|
|
|
aBuilder->LineTo(r.BottomRight());
|
|
|
|
aBuilder->LineTo(r.BottomLeft());
|
|
|
|
aBuilder->Close();
|
2013-11-06 10:05:18 +00:00
|
|
|
} else {
|
|
|
|
// If either the 'rx' or the 'ry' attribute isn't set, then we have to
|
|
|
|
// set it to the value of the other:
|
|
|
|
bool hasRx = mLengthAttributes[ATTR_RX].IsExplicitlySet();
|
|
|
|
bool hasRy = mLengthAttributes[ATTR_RY].IsExplicitlySet();
|
|
|
|
MOZ_ASSERT(hasRx || hasRy);
|
|
|
|
|
|
|
|
if (hasRx && !hasRy) {
|
|
|
|
ry = rx;
|
|
|
|
} else if (hasRy && !hasRx) {
|
|
|
|
rx = ry;
|
2013-11-02 11:10:38 +00:00
|
|
|
}
|
2013-11-06 10:05:18 +00:00
|
|
|
|
|
|
|
// Clamp rx and ry to half the rect's width and height respectively:
|
|
|
|
rx = std::min(rx, width / 2);
|
|
|
|
ry = std::min(ry, height / 2);
|
|
|
|
|
|
|
|
Size cornerRadii(rx, ry);
|
|
|
|
Size radii[] = { cornerRadii, cornerRadii, cornerRadii, cornerRadii };
|
2014-10-04 11:13:30 +00:00
|
|
|
AppendRoundedRectToPath(aBuilder, Rect(x, y, width, height), radii);
|
2013-11-02 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2014-10-04 11:13:30 +00:00
|
|
|
return aBuilder->Finish();
|
2013-11-02 11:10:38 +00:00
|
|
|
}
|
|
|
|
|
2013-01-06 09:32:01 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|