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: */
|
2012-05-21 15:12:37 +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/. */
|
2010-02-19 00:51:00 +03:00
|
|
|
|
|
|
|
#include "SVGViewBoxSMILType.h"
|
2019-01-23 16:48:08 +03:00
|
|
|
|
|
|
|
#include "mozilla/SMILValue.h"
|
2010-02-19 00:51:00 +03:00
|
|
|
#include "nsDebug.h"
|
2019-01-23 16:48:08 +03:00
|
|
|
#include "SVGViewBox.h"
|
2010-02-19 00:51:00 +03:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/*static*/
|
|
|
|
SVGViewBoxSMILType SVGViewBoxSMILType::sSingleton;
|
2010-02-19 00:51:00 +03:00
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
void SVGViewBoxSMILType::Init(SMILValue& aValue) const {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aValue.IsNull(), "Unexpected value type");
|
2010-02-19 00:51:00 +03:00
|
|
|
|
2019-01-06 20:52:55 +03:00
|
|
|
aValue.mU.mPtr = new SVGViewBoxRect();
|
2010-02-19 00:51:00 +03:00
|
|
|
aValue.mType = this;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
void SVGViewBoxSMILType::Destroy(SMILValue& aValue) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aValue.mType == this, "Unexpected SMIL value");
|
2019-01-06 20:52:55 +03:00
|
|
|
delete static_cast<SVGViewBoxRect*>(aValue.mU.mPtr);
|
2012-07-30 18:20:58 +04:00
|
|
|
aValue.mU.mPtr = nullptr;
|
2018-12-27 13:21:40 +03:00
|
|
|
aValue.mType = SMILNullType::Singleton();
|
2010-02-19 00:51:00 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
nsresult SVGViewBoxSMILType::Assign(SMILValue& aDest,
|
|
|
|
const SMILValue& aSrc) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aDest.mType == aSrc.mType, "Incompatible SMIL types");
|
|
|
|
MOZ_ASSERT(aDest.mType == this, "Unexpected SMIL value");
|
2010-02-19 00:51:00 +03:00
|
|
|
|
2019-01-06 20:52:55 +03:00
|
|
|
const SVGViewBoxRect* src = static_cast<const SVGViewBoxRect*>(aSrc.mU.mPtr);
|
|
|
|
SVGViewBoxRect* dst = static_cast<SVGViewBoxRect*>(aDest.mU.mPtr);
|
2010-02-19 00:51:00 +03:00
|
|
|
*dst = *src;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
bool SVGViewBoxSMILType::IsEqual(const SMILValue& aLeft,
|
|
|
|
const SMILValue& aRight) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aLeft.mType == aRight.mType, "Incompatible SMIL types");
|
|
|
|
MOZ_ASSERT(aLeft.mType == this, "Unexpected type for SMIL value");
|
2010-02-21 00:13:11 +03:00
|
|
|
|
2019-01-06 20:52:55 +03:00
|
|
|
const SVGViewBoxRect* leftBox =
|
|
|
|
static_cast<const SVGViewBoxRect*>(aLeft.mU.mPtr);
|
|
|
|
const SVGViewBoxRect* rightBox = static_cast<SVGViewBoxRect*>(aRight.mU.mPtr);
|
2010-02-21 00:13:11 +03:00
|
|
|
return *leftBox == *rightBox;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
nsresult SVGViewBoxSMILType::Add(SMILValue& aDest, const SMILValue& aValueToAdd,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aValueToAdd.mType == aDest.mType, "Trying to add invalid types");
|
|
|
|
MOZ_ASSERT(aValueToAdd.mType == this, "Unexpected source type");
|
2010-02-19 00:51:00 +03:00
|
|
|
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=541884#c3 and the two
|
|
|
|
// comments that follow that one for arguments for and against allowing
|
|
|
|
// viewBox to be additive.
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
nsresult SVGViewBoxSMILType::ComputeDistance(const SMILValue& aFrom,
|
|
|
|
const SMILValue& aTo,
|
2010-02-19 00:51:00 +03:00
|
|
|
double& aDistance) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aFrom.mType == aTo.mType, "Trying to compare different types");
|
|
|
|
MOZ_ASSERT(aFrom.mType == this, "Unexpected source type");
|
2010-02-19 00:51:00 +03:00
|
|
|
|
2019-01-06 20:52:55 +03:00
|
|
|
const SVGViewBoxRect* from =
|
|
|
|
static_cast<const SVGViewBoxRect*>(aFrom.mU.mPtr);
|
|
|
|
const SVGViewBoxRect* to = static_cast<const SVGViewBoxRect*>(aTo.mU.mPtr);
|
2010-02-19 00:51:00 +03:00
|
|
|
|
2013-02-26 20:58:06 +04:00
|
|
|
if (from->none || to->none) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-02-19 00:51:00 +03:00
|
|
|
// We use the distances between the edges rather than the difference between
|
|
|
|
// the x, y, width and height for the "distance". This is necessary in
|
|
|
|
// order for the "distance" result that we calculate to be the same for a
|
|
|
|
// given change in the left side as it is for an equal change in the opposite
|
|
|
|
// side. See https://bugzilla.mozilla.org/show_bug.cgi?id=541884#c12
|
|
|
|
|
|
|
|
float dLeft = to->x - from->x;
|
|
|
|
float dTop = to->y - from->y;
|
|
|
|
float dRight = (to->x + to->width) - (from->x + from->width);
|
|
|
|
float dBottom = (to->y + to->height) - (from->y + from->height);
|
|
|
|
|
|
|
|
aDistance =
|
|
|
|
sqrt(dLeft * dLeft + dTop * dTop + dRight * dRight + dBottom * dBottom);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
nsresult SVGViewBoxSMILType::Interpolate(const SMILValue& aStartVal,
|
|
|
|
const SMILValue& aEndVal,
|
2010-02-19 00:51:00 +03:00
|
|
|
double aUnitDistance,
|
2019-01-23 16:48:08 +03:00
|
|
|
SMILValue& aResult) const {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aStartVal.mType == aEndVal.mType,
|
|
|
|
"Trying to interpolate different types");
|
|
|
|
MOZ_ASSERT(aStartVal.mType == this, "Unexpected types for interpolation");
|
|
|
|
MOZ_ASSERT(aResult.mType == this, "Unexpected result type");
|
2013-02-26 20:58:06 +04:00
|
|
|
|
2019-01-06 20:52:55 +03:00
|
|
|
const SVGViewBoxRect* start =
|
|
|
|
static_cast<const SVGViewBoxRect*>(aStartVal.mU.mPtr);
|
|
|
|
const SVGViewBoxRect* end =
|
|
|
|
static_cast<const SVGViewBoxRect*>(aEndVal.mU.mPtr);
|
2013-02-26 20:58:06 +04:00
|
|
|
|
|
|
|
if (start->none || end->none) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:52:55 +03:00
|
|
|
SVGViewBoxRect* current = static_cast<SVGViewBoxRect*>(aResult.mU.mPtr);
|
2010-02-19 00:51:00 +03:00
|
|
|
|
|
|
|
float x = (start->x + (end->x - start->x) * aUnitDistance);
|
|
|
|
float y = (start->y + (end->y - start->y) * aUnitDistance);
|
|
|
|
float width = (start->width + (end->width - start->width) * aUnitDistance);
|
|
|
|
float height =
|
|
|
|
(start->height + (end->height - start->height) * aUnitDistance);
|
|
|
|
|
2019-01-06 20:52:55 +03:00
|
|
|
*current = SVGViewBoxRect(x, y, width, height);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2010-02-19 00:51:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|