Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "GeometryUtils.h"
8
9
#include "mozilla/PresShell.h"
10
#include "mozilla/dom/CharacterData.h"
11
#include "mozilla/dom/DOMPointBinding.h"
12
#include "mozilla/dom/GeometryUtilsBinding.h"
13
#include "mozilla/dom/Element.h"
14
#include "mozilla/dom/Text.h"
15
#include "mozilla/dom/DOMPoint.h"
16
#include "mozilla/dom/DOMQuad.h"
17
#include "mozilla/dom/DOMRect.h"
18
#include "nsIFrame.h"
19
#include "nsCSSFrameConstructor.h"
20
#include "nsLayoutUtils.h"
21
#include "nsSVGUtils.h"
22
23
using namespace mozilla;
24
using namespace mozilla::dom;
25
26
namespace mozilla {
27
28
enum GeometryNodeType {
29
GEOMETRY_NODE_ELEMENT,
30
GEOMETRY_NODE_TEXT,
31
GEOMETRY_NODE_DOCUMENT
32
};
33
34
static nsIFrame* GetFrameForNode(nsINode* aNode, GeometryNodeType aType,
35
bool aCreateFramesForSuppressedWhitespace) {
36
Document* doc = aNode->OwnerDoc();
37
if (aType == GEOMETRY_NODE_TEXT && aCreateFramesForSuppressedWhitespace) {
38
if (PresShell* presShell = doc->GetPresShell()) {
39
presShell->FrameConstructor()->EnsureFrameForTextNodeIsCreatedAfterFlush(
40
static_cast<CharacterData*>(aNode));
41
}
42
}
43
doc->FlushPendingNotifications(FlushType::Layout);
44
45
switch (aType) {
46
case GEOMETRY_NODE_TEXT:
47
case GEOMETRY_NODE_ELEMENT:
48
return aNode->AsContent()->GetPrimaryFrame();
49
case GEOMETRY_NODE_DOCUMENT: {
50
PresShell* presShell = doc->GetPresShell();
51
return presShell ? presShell->GetRootFrame() : nullptr;
52
}
53
default:
54
MOZ_ASSERT(false, "Unknown GeometryNodeType");
55
return nullptr;
56
}
57
}
58
59
static nsIFrame* GetFrameForGeometryNode(
60
const Optional<OwningGeometryNode>& aGeometryNode, nsINode* aDefaultNode,
61
bool aCreateFramesForSuppressedWhitespace) {
62
if (!aGeometryNode.WasPassed()) {
63
return GetFrameForNode(aDefaultNode->OwnerDoc(), GEOMETRY_NODE_DOCUMENT,
64
aCreateFramesForSuppressedWhitespace);
65
}
66
67
const OwningGeometryNode& value = aGeometryNode.Value();
68
if (value.IsElement()) {
69
return GetFrameForNode(value.GetAsElement(), GEOMETRY_NODE_ELEMENT,
70
aCreateFramesForSuppressedWhitespace);
71
}
72
if (value.IsDocument()) {
73
return GetFrameForNode(value.GetAsDocument(), GEOMETRY_NODE_DOCUMENT,
74
aCreateFramesForSuppressedWhitespace);
75
}
76
return GetFrameForNode(value.GetAsText(), GEOMETRY_NODE_TEXT,
77
aCreateFramesForSuppressedWhitespace);
78
}
79
80
static nsIFrame* GetFrameForGeometryNode(const GeometryNode& aGeometryNode) {
81
// This will create frames for suppressed whitespace nodes.
82
if (aGeometryNode.IsElement()) {
83
return GetFrameForNode(&aGeometryNode.GetAsElement(), GEOMETRY_NODE_ELEMENT,
84
true);
85
}
86
if (aGeometryNode.IsDocument()) {
87
return GetFrameForNode(&aGeometryNode.GetAsDocument(),
88
GEOMETRY_NODE_DOCUMENT, true);
89
}
90
return GetFrameForNode(&aGeometryNode.GetAsText(), GEOMETRY_NODE_TEXT, true);
91
}
92
93
static nsIFrame* GetFrameForNode(nsINode* aNode,
94
bool aCreateFramesForSuppressedWhitespace) {
95
if (aNode->IsElement()) {
96
return GetFrameForNode(aNode, GEOMETRY_NODE_ELEMENT,
97
aCreateFramesForSuppressedWhitespace);
98
}
99
if (aNode == aNode->OwnerDoc()) {
100
return GetFrameForNode(aNode, GEOMETRY_NODE_DOCUMENT,
101
aCreateFramesForSuppressedWhitespace);
102
}
103
NS_ASSERTION(aNode->IsText(), "Unknown node type");
104
return GetFrameForNode(aNode, GEOMETRY_NODE_TEXT,
105
aCreateFramesForSuppressedWhitespace);
106
}
107
108
static nsIFrame* GetFirstNonAnonymousFrameForGeometryNode(
109
const Optional<OwningGeometryNode>& aNode, nsINode* aDefaultNode,
110
bool aCreateFramesForSuppressedWhitespace) {
111
nsIFrame* f = GetFrameForGeometryNode(aNode, aDefaultNode,
112
aCreateFramesForSuppressedWhitespace);
113
if (f) {
114
f = nsLayoutUtils::GetFirstNonAnonymousFrame(f);
115
}
116
return f;
117
}
118
119
static nsIFrame* GetFirstNonAnonymousFrameForGeometryNode(
120
const GeometryNode& aNode) {
121
// This will create frames for suppressed whitespace nodes.
122
nsIFrame* f = GetFrameForGeometryNode(aNode);
123
if (f) {
124
f = nsLayoutUtils::GetFirstNonAnonymousFrame(f);
125
}
126
return f;
127
}
128
129
static nsIFrame* GetFirstNonAnonymousFrameForNode(nsINode* aNode) {
130
// This will create frames for suppressed whitespace nodes.
131
nsIFrame* f = GetFrameForNode(aNode, true);
132
if (f) {
133
f = nsLayoutUtils::GetFirstNonAnonymousFrame(f);
134
}
135
return f;
136
}
137
138
/**
139
* This can modify aFrame to point to a different frame. This is needed to
140
* handle SVG, where SVG elements can only compute a rect that's valid with
141
* respect to the "outer SVG" frame.
142
*/
143
static nsRect GetBoxRectForFrame(nsIFrame** aFrame, CSSBoxType aType) {
144
nsRect r;
145
nsIFrame* f = nsSVGUtils::GetOuterSVGFrameAndCoveredRegion(*aFrame, &r);
146
if (f && f != *aFrame) {
147
// For non-outer SVG frames, the BoxType is ignored.
148
*aFrame = f;
149
return r;
150
}
151
152
f = *aFrame;
153
switch (aType) {
154
case CSSBoxType::Content:
155
r = f->GetContentRectRelativeToSelf();
156
break;
157
case CSSBoxType::Padding:
158
r = f->GetPaddingRectRelativeToSelf();
159
break;
160
case CSSBoxType::Border:
161
r = nsRect(nsPoint(0, 0), f->GetSize());
162
break;
163
case CSSBoxType::Margin:
164
r = f->GetMarginRectRelativeToSelf();
165
break;
166
default:
167
MOZ_ASSERT(false, "unknown box type");
168
return r;
169
}
170
171
return r;
172
}
173
174
class AccumulateQuadCallback : public nsLayoutUtils::BoxCallback {
175
public:
176
AccumulateQuadCallback(Document* aParentObject,
177
nsTArray<RefPtr<DOMQuad> >& aResult,
178
nsIFrame* aRelativeToFrame,
179
const nsPoint& aRelativeToBoxTopLeft,
180
CSSBoxType aBoxType)
181
: mParentObject(ToSupports(aParentObject)),
182
mResult(aResult),
183
mRelativeToFrame(aRelativeToFrame),
184
mRelativeToBoxTopLeft(aRelativeToBoxTopLeft),
185
mBoxType(aBoxType) {
186
if (mBoxType == CSSBoxType::Margin) {
187
// Don't include the caption margin when computing margins for a
188
// table
189
mIncludeCaptionBoxForTable = false;
190
}
191
}
192
193
virtual void AddBox(nsIFrame* aFrame) override {
194
nsIFrame* f = aFrame;
195
if (mBoxType == CSSBoxType::Margin && f->IsTableFrame()) {
196
// Margin boxes for table frames should be taken from the table wrapper
197
// frame, since that has the margin.
198
f = f->GetParent();
199
}
200
nsRect box = GetBoxRectForFrame(&f, mBoxType);
201
nsPoint appUnits[4] = {box.TopLeft(), box.TopRight(), box.BottomRight(),
202
box.BottomLeft()};
203
CSSPoint points[4];
204
for (uint32_t i = 0; i < 4; ++i) {
205
points[i] =
206
CSSPoint(nsPresContext::AppUnitsToFloatCSSPixels(appUnits[i].x),
207
nsPresContext::AppUnitsToFloatCSSPixels(appUnits[i].y));
208
}
209
nsLayoutUtils::TransformResult rv =
210
nsLayoutUtils::TransformPoints(f, mRelativeToFrame, 4, points);
211
if (rv == nsLayoutUtils::TRANSFORM_SUCCEEDED) {
212
CSSPoint delta(
213
nsPresContext::AppUnitsToFloatCSSPixels(mRelativeToBoxTopLeft.x),
214
nsPresContext::AppUnitsToFloatCSSPixels(mRelativeToBoxTopLeft.y));
215
for (uint32_t i = 0; i < 4; ++i) {
216
points[i] -= delta;
217
}
218
} else {
219
PodArrayZero(points);
220
}
221
mResult.AppendElement(new DOMQuad(mParentObject, points));
222
}
223
224
nsISupports* mParentObject;
225
nsTArray<RefPtr<DOMQuad> >& mResult;
226
nsIFrame* mRelativeToFrame;
227
nsPoint mRelativeToBoxTopLeft;
228
CSSBoxType mBoxType;
229
};
230
231
static nsPresContext* FindTopLevelPresContext(nsPresContext* aPC) {
232
bool isChrome = aPC->IsChrome();
233
nsPresContext* pc = aPC;
234
for (;;) {
235
nsPresContext* parent = pc->GetParentPresContext();
236
if (!parent || parent->IsChrome() != isChrome) {
237
return pc;
238
}
239
pc = parent;
240
}
241
}
242
243
static bool CheckFramesInSameTopLevelBrowsingContext(nsIFrame* aFrame1,
244
nsIFrame* aFrame2,
245
CallerType aCallerType) {
246
nsPresContext* pc1 = aFrame1->PresContext();
247
nsPresContext* pc2 = aFrame2->PresContext();
248
if (pc1 == pc2) {
249
return true;
250
}
251
if (aCallerType == CallerType::System) {
252
return true;
253
}
254
if (FindTopLevelPresContext(pc1) == FindTopLevelPresContext(pc2)) {
255
return true;
256
}
257
return false;
258
}
259
260
void GetBoxQuads(nsINode* aNode, const dom::BoxQuadOptions& aOptions,
261
nsTArray<RefPtr<DOMQuad> >& aResult, CallerType aCallerType,
262
ErrorResult& aRv) {
263
nsIFrame* frame =
264
GetFrameForNode(aNode, aOptions.mCreateFramesForSuppressedWhitespace);
265
if (!frame) {
266
// No boxes to return
267
return;
268
}
269
AutoWeakFrame weakFrame(frame);
270
Document* ownerDoc = aNode->OwnerDoc();
271
nsIFrame* relativeToFrame = GetFirstNonAnonymousFrameForGeometryNode(
272
aOptions.mRelativeTo, ownerDoc,
273
aOptions.mCreateFramesForSuppressedWhitespace);
274
// The first frame might be destroyed now if the above call lead to an
275
// EnsureFrameForTextNode call. We need to get the first frame again
276
// when that happens and re-check it.
277
if (!weakFrame.IsAlive()) {
278
frame =
279
GetFrameForNode(aNode, aOptions.mCreateFramesForSuppressedWhitespace);
280
if (!frame) {
281
// No boxes to return
282
return;
283
}
284
}
285
if (!relativeToFrame) {
286
aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
287
return;
288
}
289
if (!CheckFramesInSameTopLevelBrowsingContext(frame, relativeToFrame,
290
aCallerType)) {
291
aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
292
return;
293
}
294
// GetBoxRectForFrame can modify relativeToFrame so call it first.
295
nsPoint relativeToTopLeft =
296
GetBoxRectForFrame(&relativeToFrame, CSSBoxType::Border).TopLeft();
297
AccumulateQuadCallback callback(ownerDoc, aResult, relativeToFrame,
298
relativeToTopLeft, aOptions.mBox);
299
nsLayoutUtils::GetAllInFlowBoxes(frame, &callback);
300
}
301
302
static void TransformPoints(nsINode* aTo, const GeometryNode& aFrom,
303
uint32_t aPointCount, CSSPoint* aPoints,
304
const ConvertCoordinateOptions& aOptions,
305
CallerType aCallerType, ErrorResult& aRv) {
306
nsIFrame* fromFrame = GetFirstNonAnonymousFrameForGeometryNode(aFrom);
307
AutoWeakFrame weakFrame(fromFrame);
308
nsIFrame* toFrame = GetFirstNonAnonymousFrameForNode(aTo);
309
// The first frame might be destroyed now if the above call lead to an
310
// EnsureFrameForTextNode call. We need to get the first frame again
311
// when that happens.
312
if (fromFrame && !weakFrame.IsAlive()) {
313
fromFrame = GetFirstNonAnonymousFrameForGeometryNode(aFrom);
314
}
315
if (!fromFrame || !toFrame) {
316
aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
317
return;
318
}
319
if (!CheckFramesInSameTopLevelBrowsingContext(fromFrame, toFrame,
320
aCallerType)) {
321
aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
322
return;
323
}
324
325
nsPoint fromOffset =
326
GetBoxRectForFrame(&fromFrame, aOptions.mFromBox).TopLeft();
327
nsPoint toOffset = GetBoxRectForFrame(&toFrame, aOptions.mToBox).TopLeft();
328
CSSPoint fromOffsetGfx(nsPresContext::AppUnitsToFloatCSSPixels(fromOffset.x),
329
nsPresContext::AppUnitsToFloatCSSPixels(fromOffset.y));
330
for (uint32_t i = 0; i < aPointCount; ++i) {
331
aPoints[i] += fromOffsetGfx;
332
}
333
nsLayoutUtils::TransformResult rv =
334
nsLayoutUtils::TransformPoints(fromFrame, toFrame, aPointCount, aPoints);
335
if (rv == nsLayoutUtils::TRANSFORM_SUCCEEDED) {
336
CSSPoint toOffsetGfx(nsPresContext::AppUnitsToFloatCSSPixels(toOffset.x),
337
nsPresContext::AppUnitsToFloatCSSPixels(toOffset.y));
338
for (uint32_t i = 0; i < aPointCount; ++i) {
339
aPoints[i] -= toOffsetGfx;
340
}
341
} else {
342
PodZero(aPoints, aPointCount);
343
}
344
}
345
346
already_AddRefed<DOMQuad> ConvertQuadFromNode(
347
nsINode* aTo, dom::DOMQuad& aQuad, const GeometryNode& aFrom,
348
const dom::ConvertCoordinateOptions& aOptions, CallerType aCallerType,
349
ErrorResult& aRv) {
350
CSSPoint points[4];
351
for (uint32_t i = 0; i < 4; ++i) {
352
DOMPoint* p = aQuad.Point(i);
353
if (p->W() != 1.0 || p->Z() != 0.0) {
354
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
355
return nullptr;
356
}
357
points[i] = CSSPoint(p->X(), p->Y());
358
}
359
TransformPoints(aTo, aFrom, 4, points, aOptions, aCallerType, aRv);
360
if (aRv.Failed()) {
361
return nullptr;
362
}
363
RefPtr<DOMQuad> result = new DOMQuad(aTo->GetParentObject().mObject, points);
364
return result.forget();
365
}
366
367
already_AddRefed<DOMQuad> ConvertRectFromNode(
368
nsINode* aTo, dom::DOMRectReadOnly& aRect, const GeometryNode& aFrom,
369
const dom::ConvertCoordinateOptions& aOptions, CallerType aCallerType,
370
ErrorResult& aRv) {
371
CSSPoint points[4];
372
double x = aRect.X(), y = aRect.Y(), w = aRect.Width(), h = aRect.Height();
373
points[0] = CSSPoint(x, y);
374
points[1] = CSSPoint(x + w, y);
375
points[2] = CSSPoint(x + w, y + h);
376
points[3] = CSSPoint(x, y + h);
377
TransformPoints(aTo, aFrom, 4, points, aOptions, aCallerType, aRv);
378
if (aRv.Failed()) {
379
return nullptr;
380
}
381
RefPtr<DOMQuad> result = new DOMQuad(aTo->GetParentObject().mObject, points);
382
return result.forget();
383
}
384
385
already_AddRefed<DOMPoint> ConvertPointFromNode(
386
nsINode* aTo, const dom::DOMPointInit& aPoint, const GeometryNode& aFrom,
387
const dom::ConvertCoordinateOptions& aOptions, CallerType aCallerType,
388
ErrorResult& aRv) {
389
if (aPoint.mW != 1.0 || aPoint.mZ != 0.0) {
390
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
391
return nullptr;
392
}
393
CSSPoint point(aPoint.mX, aPoint.mY);
394
TransformPoints(aTo, aFrom, 1, &point, aOptions, aCallerType, aRv);
395
if (aRv.Failed()) {
396
return nullptr;
397
}
398
RefPtr<DOMPoint> result =
399
new DOMPoint(aTo->GetParentObject().mObject, point.x, point.y);
400
return result.forget();
401
}
402
403
} // namespace mozilla