2017-10-27 20:33:53 +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: */
|
2014-03-12 05:11:38 +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 "GeometryUtils.h"
|
|
|
|
|
2019-03-29 18:12:47 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2020-07-11 05:20:20 +03:00
|
|
|
#include "mozilla/SVGUtils.h"
|
2018-03-19 22:18:07 +03:00
|
|
|
#include "mozilla/dom/CharacterData.h"
|
2014-03-12 05:11:38 +04:00
|
|
|
#include "mozilla/dom/DOMPointBinding.h"
|
2014-03-12 05:11:38 +04:00
|
|
|
#include "mozilla/dom/GeometryUtilsBinding.h"
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
#include "mozilla/dom/Text.h"
|
2014-03-12 05:11:38 +04:00
|
|
|
#include "mozilla/dom/DOMPoint.h"
|
2014-03-12 05:11:38 +04:00
|
|
|
#include "mozilla/dom/DOMQuad.h"
|
2014-03-12 05:11:38 +04:00
|
|
|
#include "mozilla/dom/DOMRect.h"
|
2020-03-04 03:45:38 +03:00
|
|
|
#include "mozilla/dom/BrowserChild.h"
|
2014-03-12 05:11:38 +04:00
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsCSSFrameConstructor.h"
|
|
|
|
#include "nsLayoutUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
enum GeometryNodeType {
|
|
|
|
GEOMETRY_NODE_ELEMENT,
|
|
|
|
GEOMETRY_NODE_TEXT,
|
|
|
|
GEOMETRY_NODE_DOCUMENT
|
|
|
|
};
|
|
|
|
|
2019-09-12 22:05:32 +03:00
|
|
|
static nsIFrame* GetFrameForNode(nsINode* aNode, GeometryNodeType aType,
|
|
|
|
bool aCreateFramesForSuppressedWhitespace) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aNode->OwnerDoc();
|
2019-09-12 22:05:32 +03:00
|
|
|
if (aType == GEOMETRY_NODE_TEXT && aCreateFramesForSuppressedWhitespace) {
|
2019-03-29 18:12:47 +03:00
|
|
|
if (PresShell* presShell = doc->GetPresShell()) {
|
|
|
|
presShell->FrameConstructor()->EnsureFrameForTextNodeIsCreatedAfterFlush(
|
2018-03-19 22:18:06 +03:00
|
|
|
static_cast<CharacterData*>(aNode));
|
2017-11-01 15:00:04 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-05 10:31:56 +03:00
|
|
|
doc->FlushPendingNotifications(FlushType::Layout);
|
2017-11-01 15:00:04 +03:00
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
switch (aType) {
|
2017-11-01 15:00:04 +03:00
|
|
|
case GEOMETRY_NODE_TEXT:
|
2014-03-12 05:11:38 +04:00
|
|
|
case GEOMETRY_NODE_ELEMENT:
|
|
|
|
return aNode->AsContent()->GetPrimaryFrame();
|
|
|
|
case GEOMETRY_NODE_DOCUMENT: {
|
2019-03-29 18:12:47 +03:00
|
|
|
PresShell* presShell = doc->GetPresShell();
|
2014-03-12 05:11:38 +04:00
|
|
|
return presShell ? presShell->GetRootFrame() : nullptr;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unknown GeometryNodeType");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
static nsIFrame* GetFrameForGeometryNode(
|
2019-09-12 22:05:32 +03:00
|
|
|
const Optional<OwningGeometryNode>& aGeometryNode, nsINode* aDefaultNode,
|
|
|
|
bool aCreateFramesForSuppressedWhitespace) {
|
2014-03-12 05:11:38 +04:00
|
|
|
if (!aGeometryNode.WasPassed()) {
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(aDefaultNode->OwnerDoc(), GEOMETRY_NODE_DOCUMENT,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
const OwningGeometryNode& value = aGeometryNode.Value();
|
2014-03-12 05:11:38 +04:00
|
|
|
if (value.IsElement()) {
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(value.GetAsElement(), GEOMETRY_NODE_ELEMENT,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
if (value.IsDocument()) {
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(value.GetAsDocument(), GEOMETRY_NODE_DOCUMENT,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(value.GetAsText(), GEOMETRY_NODE_TEXT,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
static nsIFrame* GetFrameForGeometryNode(const GeometryNode& aGeometryNode) {
|
2019-09-12 22:05:32 +03:00
|
|
|
// This will create frames for suppressed whitespace nodes.
|
2014-03-12 05:11:38 +04:00
|
|
|
if (aGeometryNode.IsElement()) {
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(&aGeometryNode.GetAsElement(), GEOMETRY_NODE_ELEMENT,
|
|
|
|
true);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
if (aGeometryNode.IsDocument()) {
|
|
|
|
return GetFrameForNode(&aGeometryNode.GetAsDocument(),
|
2019-09-12 22:05:32 +03:00
|
|
|
GEOMETRY_NODE_DOCUMENT, true);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(&aGeometryNode.GetAsText(), GEOMETRY_NODE_TEXT, true);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
|
2019-09-12 22:05:32 +03:00
|
|
|
static nsIFrame* GetFrameForNode(nsINode* aNode,
|
|
|
|
bool aCreateFramesForSuppressedWhitespace) {
|
2014-03-12 05:11:38 +04:00
|
|
|
if (aNode->IsElement()) {
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(aNode, GEOMETRY_NODE_ELEMENT,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
if (aNode == aNode->OwnerDoc()) {
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(aNode, GEOMETRY_NODE_DOCUMENT,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
2018-04-13 01:41:00 +03:00
|
|
|
NS_ASSERTION(aNode->IsText(), "Unknown node type");
|
2019-09-12 22:05:32 +03:00
|
|
|
return GetFrameForNode(aNode, GEOMETRY_NODE_TEXT,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
static nsIFrame* GetFirstNonAnonymousFrameForGeometryNode(
|
2019-09-12 22:05:32 +03:00
|
|
|
const Optional<OwningGeometryNode>& aNode, nsINode* aDefaultNode,
|
|
|
|
bool aCreateFramesForSuppressedWhitespace) {
|
|
|
|
nsIFrame* f = GetFrameForGeometryNode(aNode, aDefaultNode,
|
|
|
|
aCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (f) {
|
|
|
|
f = nsLayoutUtils::GetFirstNonAnonymousFrame(f);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
static nsIFrame* GetFirstNonAnonymousFrameForGeometryNode(
|
|
|
|
const GeometryNode& aNode) {
|
2019-09-12 22:05:32 +03:00
|
|
|
// This will create frames for suppressed whitespace nodes.
|
2014-03-12 05:11:38 +04:00
|
|
|
nsIFrame* f = GetFrameForGeometryNode(aNode);
|
|
|
|
if (f) {
|
|
|
|
f = nsLayoutUtils::GetFirstNonAnonymousFrame(f);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsIFrame* GetFirstNonAnonymousFrameForNode(nsINode* aNode) {
|
2019-09-12 22:05:32 +03:00
|
|
|
// This will create frames for suppressed whitespace nodes.
|
|
|
|
nsIFrame* f = GetFrameForNode(aNode, true);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (f) {
|
|
|
|
f = nsLayoutUtils::GetFirstNonAnonymousFrame(f);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
/**
|
|
|
|
* This can modify aFrame to point to a different frame. This is needed to
|
|
|
|
* handle SVG, where SVG elements can only compute a rect that's valid with
|
|
|
|
* respect to the "outer SVG" frame.
|
|
|
|
*/
|
|
|
|
static nsRect GetBoxRectForFrame(nsIFrame** aFrame, CSSBoxType aType) {
|
|
|
|
nsRect r;
|
2020-07-11 05:20:20 +03:00
|
|
|
nsIFrame* f = SVGUtils::GetOuterSVGFrameAndCoveredRegion(*aFrame, &r);
|
2016-02-25 13:54:09 +03:00
|
|
|
if (f && f != *aFrame) {
|
|
|
|
// For non-outer SVG frames, the BoxType is ignored.
|
2014-03-12 05:11:38 +04:00
|
|
|
*aFrame = f;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = *aFrame;
|
|
|
|
switch (aType) {
|
|
|
|
case CSSBoxType::Content:
|
|
|
|
r = f->GetContentRectRelativeToSelf();
|
|
|
|
break;
|
|
|
|
case CSSBoxType::Padding:
|
|
|
|
r = f->GetPaddingRectRelativeToSelf();
|
|
|
|
break;
|
|
|
|
case CSSBoxType::Border:
|
|
|
|
r = nsRect(nsPoint(0, 0), f->GetSize());
|
|
|
|
break;
|
2018-01-23 02:12:37 +03:00
|
|
|
case CSSBoxType::Margin:
|
|
|
|
r = f->GetMarginRectRelativeToSelf();
|
|
|
|
break;
|
2014-03-12 05:11:38 +04:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "unknown box type");
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AccumulateQuadCallback : public nsLayoutUtils::BoxCallback {
|
|
|
|
public:
|
2019-01-02 16:05:23 +03:00
|
|
|
AccumulateQuadCallback(Document* aParentObject,
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<DOMQuad> >& aResult,
|
2014-03-12 05:11:38 +04:00
|
|
|
nsIFrame* aRelativeToFrame,
|
|
|
|
const nsPoint& aRelativeToBoxTopLeft,
|
|
|
|
CSSBoxType aBoxType)
|
2018-12-26 05:32:55 +03:00
|
|
|
: mParentObject(ToSupports(aParentObject)),
|
2014-03-12 05:11:38 +04:00
|
|
|
mResult(aResult),
|
|
|
|
mRelativeToFrame(aRelativeToFrame),
|
|
|
|
mRelativeToBoxTopLeft(aRelativeToBoxTopLeft),
|
|
|
|
mBoxType(aBoxType) {
|
2016-02-02 12:47:15 +03:00
|
|
|
if (mBoxType == CSSBoxType::Margin) {
|
|
|
|
// Don't include the caption margin when computing margins for a
|
|
|
|
// table
|
|
|
|
mIncludeCaptionBoxForTable = false;
|
|
|
|
}
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
|
|
|
|
2022-01-28 19:50:14 +03:00
|
|
|
void AddBox(nsIFrame* aFrame) override {
|
2014-03-12 05:11:38 +04:00
|
|
|
nsIFrame* f = aFrame;
|
2017-04-30 18:30:08 +03:00
|
|
|
if (mBoxType == CSSBoxType::Margin && f->IsTableFrame()) {
|
2016-06-16 15:14:08 +03:00
|
|
|
// Margin boxes for table frames should be taken from the table wrapper
|
2016-02-02 12:47:15 +03:00
|
|
|
// frame, since that has the margin.
|
|
|
|
f = f->GetParent();
|
|
|
|
}
|
2014-03-12 05:11:38 +04:00
|
|
|
nsRect box = GetBoxRectForFrame(&f, mBoxType);
|
|
|
|
nsPoint appUnits[4] = {box.TopLeft(), box.TopRight(), box.BottomRight(),
|
|
|
|
box.BottomLeft()};
|
|
|
|
CSSPoint points[4];
|
|
|
|
for (uint32_t i = 0; i < 4; ++i) {
|
|
|
|
points[i] =
|
|
|
|
CSSPoint(nsPresContext::AppUnitsToFloatCSSPixels(appUnits[i].x),
|
|
|
|
nsPresContext::AppUnitsToFloatCSSPixels(appUnits[i].y));
|
|
|
|
}
|
2022-01-28 19:50:14 +03:00
|
|
|
nsLayoutUtils::TransformResult rv = nsLayoutUtils::TransformPoints(
|
|
|
|
RelativeTo{f}, RelativeTo{mRelativeToFrame}, 4, points);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (rv == nsLayoutUtils::TRANSFORM_SUCCEEDED) {
|
|
|
|
CSSPoint delta(
|
|
|
|
nsPresContext::AppUnitsToFloatCSSPixels(mRelativeToBoxTopLeft.x),
|
|
|
|
nsPresContext::AppUnitsToFloatCSSPixels(mRelativeToBoxTopLeft.y));
|
|
|
|
for (uint32_t i = 0; i < 4; ++i) {
|
|
|
|
points[i] -= delta;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PodArrayZero(points);
|
|
|
|
}
|
|
|
|
mResult.AppendElement(new DOMQuad(mParentObject, points));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports* mParentObject;
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<DOMQuad> >& mResult;
|
2014-03-12 05:11:38 +04:00
|
|
|
nsIFrame* mRelativeToFrame;
|
|
|
|
nsPoint mRelativeToBoxTopLeft;
|
|
|
|
CSSBoxType mBoxType;
|
|
|
|
};
|
|
|
|
|
|
|
|
static nsPresContext* FindTopLevelPresContext(nsPresContext* aPC) {
|
|
|
|
bool isChrome = aPC->IsChrome();
|
|
|
|
nsPresContext* pc = aPC;
|
|
|
|
for (;;) {
|
|
|
|
nsPresContext* parent = pc->GetParentPresContext();
|
|
|
|
if (!parent || parent->IsChrome() != isChrome) {
|
|
|
|
return pc;
|
|
|
|
}
|
|
|
|
pc = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 23:43:59 +03:00
|
|
|
static bool CheckFramesInSameTopLevelBrowsingContext(nsIFrame* aFrame1,
|
|
|
|
nsIFrame* aFrame2,
|
|
|
|
CallerType aCallerType) {
|
2014-03-12 05:11:38 +04:00
|
|
|
nsPresContext* pc1 = aFrame1->PresContext();
|
|
|
|
nsPresContext* pc2 = aFrame2->PresContext();
|
|
|
|
if (pc1 == pc2) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-01 23:43:59 +03:00
|
|
|
if (aCallerType == CallerType::System) {
|
2014-03-12 05:11:38 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (FindTopLevelPresContext(pc1) == FindTopLevelPresContext(pc2)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetBoxQuads(nsINode* aNode, const dom::BoxQuadOptions& aOptions,
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<DOMQuad> >& aResult, CallerType aCallerType,
|
2014-03-12 05:11:38 +04:00
|
|
|
ErrorResult& aRv) {
|
2019-09-12 22:05:32 +03:00
|
|
|
nsIFrame* frame =
|
|
|
|
GetFrameForNode(aNode, aOptions.mCreateFramesForSuppressedWhitespace);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (!frame) {
|
|
|
|
// No boxes to return
|
|
|
|
return;
|
|
|
|
}
|
2017-03-01 20:03:14 +03:00
|
|
|
AutoWeakFrame weakFrame(frame);
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* ownerDoc = aNode->OwnerDoc();
|
2019-09-12 22:05:32 +03:00
|
|
|
nsIFrame* relativeToFrame = GetFirstNonAnonymousFrameForGeometryNode(
|
|
|
|
aOptions.mRelativeTo, ownerDoc,
|
|
|
|
aOptions.mCreateFramesForSuppressedWhitespace);
|
2014-05-13 00:25:22 +04:00
|
|
|
// The first frame might be destroyed now if the above call lead to an
|
|
|
|
// EnsureFrameForTextNode call. We need to get the first frame again
|
|
|
|
// when that happens and re-check it.
|
|
|
|
if (!weakFrame.IsAlive()) {
|
2019-09-12 22:05:32 +03:00
|
|
|
frame =
|
|
|
|
GetFrameForNode(aNode, aOptions.mCreateFramesForSuppressedWhitespace);
|
2014-05-13 00:25:22 +04:00
|
|
|
if (!frame) {
|
|
|
|
// No boxes to return
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 05:11:38 +04:00
|
|
|
if (!relativeToFrame) {
|
2020-02-03 23:35:54 +03:00
|
|
|
// XXXbz There's no spec for this.
|
|
|
|
return aRv.ThrowNotFoundError("No box to get quads relative to");
|
2014-03-12 05:11:38 +04:00
|
|
|
}
|
2017-02-01 23:43:59 +03:00
|
|
|
if (!CheckFramesInSameTopLevelBrowsingContext(frame, relativeToFrame,
|
|
|
|
aCallerType)) {
|
2020-02-03 23:35:54 +03:00
|
|
|
aRv.ThrowNotFoundError(
|
|
|
|
"Can't get quads relative to a box in a different toplevel browsing "
|
|
|
|
"context");
|
2014-03-12 05:11:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// GetBoxRectForFrame can modify relativeToFrame so call it first.
|
|
|
|
nsPoint relativeToTopLeft =
|
|
|
|
GetBoxRectForFrame(&relativeToFrame, CSSBoxType::Border).TopLeft();
|
|
|
|
AccumulateQuadCallback callback(ownerDoc, aResult, relativeToFrame,
|
|
|
|
relativeToTopLeft, aOptions.mBox);
|
2020-04-01 01:34:14 +03:00
|
|
|
|
|
|
|
// Bug 1624653: Refactor this to get boxes in layer pixels, which we will
|
|
|
|
// then convert into CSS units.
|
2014-03-12 05:11:38 +04:00
|
|
|
nsLayoutUtils::GetAllInFlowBoxes(frame, &callback);
|
|
|
|
}
|
|
|
|
|
2020-03-04 03:45:38 +03:00
|
|
|
void GetBoxQuadsFromWindowOrigin(nsINode* aNode,
|
|
|
|
const dom::BoxQuadOptions& aOptions,
|
|
|
|
nsTArray<RefPtr<DOMQuad> >& aResult,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
// We want the quads relative to the window. To do this, we ignore the
|
|
|
|
// provided aOptions.mRelativeTo and instead use the document node of
|
|
|
|
// the top-most in-process document. Later, we'll check if there is a
|
|
|
|
// browserChild associated with that document, and if so, transform the
|
|
|
|
// calculated quads with the browserChild's to-parent matrix, which
|
|
|
|
// will get us to top-level coordinates.
|
|
|
|
if (aOptions.mRelativeTo.WasPassed()) {
|
|
|
|
return aRv.ThrowNotSupportedError(
|
|
|
|
"Can't request quads in window origin space relative to another "
|
|
|
|
"node.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're going to call GetBoxQuads with our parameters, but we supply
|
|
|
|
// a new BoxQuadOptions object that uses the top in-process document
|
|
|
|
// as the relativeTo target.
|
|
|
|
BoxQuadOptions bqo(aOptions);
|
|
|
|
|
|
|
|
RefPtr<Document> topInProcessDoc =
|
2021-05-05 12:42:20 +03:00
|
|
|
nsContentUtils::GetInProcessSubtreeRootDocument(aNode->OwnerDoc());
|
2020-03-04 03:45:38 +03:00
|
|
|
|
|
|
|
OwningGeometryNode ogn;
|
|
|
|
ogn.SetAsDocument() = topInProcessDoc;
|
|
|
|
bqo.mRelativeTo.Construct(ogn);
|
|
|
|
|
2020-04-01 01:34:14 +03:00
|
|
|
// Bug 1624653: Refactor this to get boxes in layer pixels, which we can
|
|
|
|
// transform directly with the GetChildToParentConversionMatrix below,
|
|
|
|
// and convert to CSS units as a final step.
|
2020-03-04 03:45:38 +03:00
|
|
|
GetBoxQuads(aNode, bqo, aResult, CallerType::System, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have aResult filled with DOMQuads with values relative to the
|
|
|
|
// top in-process document. See if topInProcessDoc is associated with a
|
|
|
|
// BrowserChild, and if it is, get its transformation matrix and use that
|
|
|
|
// to transform the DOMQuads in place to make them relative to the window
|
|
|
|
// origin.
|
|
|
|
nsIDocShell* docShell = topInProcessDoc->GetDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
return aRv.ThrowInvalidStateError(
|
|
|
|
"Returning untranslated quads because top in process document has "
|
|
|
|
"no docshell.");
|
|
|
|
}
|
|
|
|
|
|
|
|
BrowserChild* browserChild = BrowserChild::GetFrom(docShell);
|
|
|
|
if (!browserChild) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* presContext = docShell->GetPresContext();
|
|
|
|
if (!presContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int32_t appUnitsPerDevPixel = presContext->AppUnitsPerDevPixel();
|
|
|
|
|
|
|
|
LayoutDeviceToLayoutDeviceMatrix4x4 matrix =
|
|
|
|
browserChild->GetChildToParentConversionMatrix();
|
|
|
|
|
|
|
|
// For each DOMQuad in aResult, change the css units into layer pixels,
|
|
|
|
// then transform them by matrix, then change them back into css units
|
|
|
|
// and overwrite the original points.
|
2020-04-01 01:34:14 +03:00
|
|
|
LayoutDeviceToCSSScale ld2cScale((float)appUnitsPerDevPixel /
|
|
|
|
(float)AppUnitsPerCSSPixel());
|
2020-03-04 03:45:38 +03:00
|
|
|
CSSToLayoutDeviceScale c2ldScale = ld2cScale.Inverse();
|
|
|
|
|
|
|
|
for (auto& quad : aResult) {
|
|
|
|
for (uint32_t i = 0; i < 4; i++) {
|
|
|
|
DOMPoint* p = quad->Point(i);
|
|
|
|
CSSPoint cp(p->X(), p->Y());
|
|
|
|
|
|
|
|
LayoutDevicePoint windowLdp = matrix.TransformPoint(cp * c2ldScale);
|
|
|
|
|
|
|
|
CSSPoint windowCp = windowLdp * ld2cScale;
|
|
|
|
p->SetX(windowCp.x);
|
|
|
|
p->SetY(windowCp.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 05:11:38 +04:00
|
|
|
static void TransformPoints(nsINode* aTo, const GeometryNode& aFrom,
|
|
|
|
uint32_t aPointCount, CSSPoint* aPoints,
|
2017-02-01 23:43:59 +03:00
|
|
|
const ConvertCoordinateOptions& aOptions,
|
|
|
|
CallerType aCallerType, ErrorResult& aRv) {
|
2014-03-12 05:11:38 +04:00
|
|
|
nsIFrame* fromFrame = GetFirstNonAnonymousFrameForGeometryNode(aFrom);
|
2017-03-01 20:03:14 +03:00
|
|
|
AutoWeakFrame weakFrame(fromFrame);
|
2014-03-12 05:11:38 +04:00
|
|
|
nsIFrame* toFrame = GetFirstNonAnonymousFrameForNode(aTo);
|
2014-05-13 00:25:22 +04:00
|
|
|
// The first frame might be destroyed now if the above call lead to an
|
|
|
|
// EnsureFrameForTextNode call. We need to get the first frame again
|
|
|
|
// when that happens.
|
|
|
|
if (fromFrame && !weakFrame.IsAlive()) {
|
|
|
|
fromFrame = GetFirstNonAnonymousFrameForGeometryNode(aFrom);
|
|
|
|
}
|
2014-03-12 05:11:38 +04:00
|
|
|
if (!fromFrame || !toFrame) {
|
2020-02-03 23:35:54 +03:00
|
|
|
aRv.ThrowNotFoundError(
|
|
|
|
"Can't transform coordinates between nonexistent boxes");
|
2014-03-12 05:11:38 +04:00
|
|
|
return;
|
|
|
|
}
|
2017-02-01 23:43:59 +03:00
|
|
|
if (!CheckFramesInSameTopLevelBrowsingContext(fromFrame, toFrame,
|
|
|
|
aCallerType)) {
|
2020-02-03 23:35:54 +03:00
|
|
|
aRv.ThrowNotFoundError(
|
|
|
|
"Can't transform coordinates between boxes in different toplevel "
|
|
|
|
"browsing contexts");
|
2014-03-12 05:11:38 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPoint fromOffset =
|
|
|
|
GetBoxRectForFrame(&fromFrame, aOptions.mFromBox).TopLeft();
|
|
|
|
nsPoint toOffset = GetBoxRectForFrame(&toFrame, aOptions.mToBox).TopLeft();
|
|
|
|
CSSPoint fromOffsetGfx(nsPresContext::AppUnitsToFloatCSSPixels(fromOffset.x),
|
|
|
|
nsPresContext::AppUnitsToFloatCSSPixels(fromOffset.y));
|
|
|
|
for (uint32_t i = 0; i < aPointCount; ++i) {
|
|
|
|
aPoints[i] += fromOffsetGfx;
|
|
|
|
}
|
2022-01-28 19:50:14 +03:00
|
|
|
nsLayoutUtils::TransformResult rv = nsLayoutUtils::TransformPoints(
|
|
|
|
RelativeTo{fromFrame}, RelativeTo{toFrame}, aPointCount, aPoints);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (rv == nsLayoutUtils::TRANSFORM_SUCCEEDED) {
|
|
|
|
CSSPoint toOffsetGfx(nsPresContext::AppUnitsToFloatCSSPixels(toOffset.x),
|
|
|
|
nsPresContext::AppUnitsToFloatCSSPixels(toOffset.y));
|
|
|
|
for (uint32_t i = 0; i < aPointCount; ++i) {
|
|
|
|
aPoints[i] -= toOffsetGfx;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PodZero(aPoints, aPointCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMQuad> ConvertQuadFromNode(
|
|
|
|
nsINode* aTo, dom::DOMQuad& aQuad, const GeometryNode& aFrom,
|
|
|
|
const dom::ConvertCoordinateOptions& aOptions, CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
CSSPoint points[4];
|
|
|
|
for (uint32_t i = 0; i < 4; ++i) {
|
|
|
|
DOMPoint* p = aQuad.Point(i);
|
|
|
|
if (p->W() != 1.0 || p->Z() != 0.0) {
|
2020-02-03 23:35:54 +03:00
|
|
|
aRv.ThrowInvalidStateError("Point is not 2d");
|
2014-03-12 05:11:38 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
points[i] = CSSPoint(p->X(), p->Y());
|
|
|
|
}
|
2017-02-01 23:43:59 +03:00
|
|
|
TransformPoints(aTo, aFrom, 4, points, aOptions, aCallerType, aRv);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMQuad> result = new DOMQuad(aTo->GetParentObject().mObject, points);
|
2014-03-12 05:11:38 +04:00
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMQuad> ConvertRectFromNode(
|
|
|
|
nsINode* aTo, dom::DOMRectReadOnly& aRect, const GeometryNode& aFrom,
|
|
|
|
const dom::ConvertCoordinateOptions& aOptions, CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
CSSPoint points[4];
|
|
|
|
double x = aRect.X(), y = aRect.Y(), w = aRect.Width(), h = aRect.Height();
|
|
|
|
points[0] = CSSPoint(x, y);
|
|
|
|
points[1] = CSSPoint(x + w, y);
|
|
|
|
points[2] = CSSPoint(x + w, y + h);
|
|
|
|
points[3] = CSSPoint(x, y + h);
|
2017-02-01 23:43:59 +03:00
|
|
|
TransformPoints(aTo, aFrom, 4, points, aOptions, aCallerType, aRv);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMQuad> result = new DOMQuad(aTo->GetParentObject().mObject, points);
|
2014-03-12 05:11:38 +04:00
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DOMPoint> ConvertPointFromNode(
|
|
|
|
nsINode* aTo, const dom::DOMPointInit& aPoint, const GeometryNode& aFrom,
|
|
|
|
const dom::ConvertCoordinateOptions& aOptions, CallerType aCallerType,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (aPoint.mW != 1.0 || aPoint.mZ != 0.0) {
|
2020-02-03 23:35:54 +03:00
|
|
|
aRv.ThrowInvalidStateError("Point is not 2d");
|
2014-03-12 05:11:38 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
CSSPoint point(aPoint.mX, aPoint.mY);
|
2017-02-01 23:43:59 +03:00
|
|
|
TransformPoints(aTo, aFrom, 1, &point, aOptions, aCallerType, aRv);
|
2014-03-12 05:11:38 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMPoint> result =
|
|
|
|
new DOMPoint(aTo->GetParentObject().mObject, point.x, point.y);
|
2014-03-12 05:11:38 +04:00
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|