This source file includes following definitions.
- m_quad
- node
- quad
- boundingBox
- nodeRespondsToTapGesture
- nodeIsZoomTarget
- providesContextMenuItems
- appendQuadsToSubtargetList
- appendBasicSubtargetsForNode
- appendContextSubtargetsForNode
- appendZoomableSubtargets
- parentShadowHostOrOwner
- compileSubtargetList
- compileZoomableSubtargets
- zoomableIntersectionQuotient
- hybridDistanceFunction
- contentsToWindow
- adjustPointToRect
- snapTo
- findNodeWithLowestDistanceMetric
- findBestClickableCandidate
- findBestContextMenuCandidate
- findBestZoomableArea
#include "config.h"
#include "core/page/TouchAdjustment.h"
#include "core/dom/ContainerNode.h"
#include "core/dom/Node.h"
#include "core/dom/NodeRenderStyle.h"
#include "core/dom/Text.h"
#include "core/editing/Editor.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLFrameOwnerElement.h"
#include "core/rendering/RenderBox.h"
#include "core/rendering/RenderObject.h"
#include "core/rendering/RenderText.h"
#include "core/rendering/style/RenderStyle.h"
#include "platform/geometry/FloatPoint.h"
#include "platform/geometry/FloatQuad.h"
#include "platform/geometry/IntSize.h"
#include "platform/text/TextBreakIterator.h"
namespace WebCore {
namespace TouchAdjustment {
const float zeroTolerance = 1e-6f;
class SubtargetGeometry {
public:
SubtargetGeometry(Node* node, const FloatQuad& quad)
: m_node(node)
, m_quad(quad)
{ }
Node* node() const { return m_node; }
FloatQuad quad() const { return m_quad; }
IntRect boundingBox() const { return m_quad.enclosingBoundingBox(); }
private:
Node* m_node;
FloatQuad m_quad;
};
typedef Vector<SubtargetGeometry> SubtargetGeometryList;
typedef bool (*NodeFilter)(Node*);
typedef void (*AppendSubtargetsForNode)(Node*, SubtargetGeometryList&);
typedef float (*DistanceFunction)(const IntPoint&, const IntRect&, const SubtargetGeometry&);
bool nodeRespondsToTapGesture(Node* node)
{
if (node->willRespondToMouseClickEvents() || node->willRespondToMouseMoveEvents())
return true;
if (node->isElementNode()) {
Element* element = toElement(node);
if (element->isMouseFocusable())
return true;
if (element->childrenAffectedByActive() || element->childrenAffectedByHover())
return true;
}
if (RenderStyle* renderStyle = node->renderStyle()) {
if (renderStyle->affectedByActive() || renderStyle->affectedByHover())
return true;
}
return false;
}
bool nodeIsZoomTarget(Node* node)
{
if (node->isTextNode() || node->isShadowRoot())
return false;
ASSERT(node->renderer());
return node->renderer()->isBox();
}
bool providesContextMenuItems(Node* node)
{
ASSERT(node->renderer() || node->isShadowRoot());
if (!node->renderer())
return false;
if (node->isContentEditable())
return true;
if (node->isLink())
return true;
if (node->renderer()->isImage())
return true;
if (node->renderer()->isMedia())
return true;
if (node->renderer()->canBeSelectionLeaf()) {
if (node->renderer()->frame()->editor().behavior().shouldSelectOnContextualMenuClick())
return true;
if (node->renderer()->selectionState() != RenderObject::SelectionNone)
return true;
}
return false;
}
static inline void appendQuadsToSubtargetList(Vector<FloatQuad>& quads, Node* node, SubtargetGeometryList& subtargets)
{
Vector<FloatQuad>::const_iterator it = quads.begin();
const Vector<FloatQuad>::const_iterator end = quads.end();
for (; it != end; ++it)
subtargets.append(SubtargetGeometry(node, *it));
}
static inline void appendBasicSubtargetsForNode(Node* node, SubtargetGeometryList& subtargets)
{
ASSERT(node->renderer());
Vector<FloatQuad> quads;
node->renderer()->absoluteQuads(quads);
appendQuadsToSubtargetList(quads, node, subtargets);
}
static inline void appendContextSubtargetsForNode(Node* node, SubtargetGeometryList& subtargets)
{
ASSERT(node->renderer());
if (!node->isTextNode())
return appendBasicSubtargetsForNode(node, subtargets);
Text* textNode = toText(node);
RenderText* textRenderer = toRenderText(textNode->renderer());
if (textRenderer->frame()->editor().behavior().shouldSelectOnContextualMenuClick()) {
String textValue = textNode->data();
TextBreakIterator* wordIterator = wordBreakIterator(textValue, 0, textValue.length());
int lastOffset = wordIterator->first();
if (lastOffset == -1)
return;
int offset;
while ((offset = wordIterator->next()) != -1) {
if (isWordTextBreak(wordIterator)) {
Vector<FloatQuad> quads;
textRenderer->absoluteQuadsForRange(quads, lastOffset, offset);
appendQuadsToSubtargetList(quads, textNode, subtargets);
}
lastOffset = offset;
}
} else {
if (textRenderer->selectionState() == RenderObject::SelectionNone)
return appendBasicSubtargetsForNode(node, subtargets);
int startPos, endPos;
switch (textRenderer->selectionState()) {
case RenderObject::SelectionInside:
startPos = 0;
endPos = textRenderer->textLength();
break;
case RenderObject::SelectionStart:
textRenderer->selectionStartEnd(startPos, endPos);
endPos = textRenderer->textLength();
break;
case RenderObject::SelectionEnd:
textRenderer->selectionStartEnd(startPos, endPos);
startPos = 0;
break;
case RenderObject::SelectionBoth:
textRenderer->selectionStartEnd(startPos, endPos);
break;
default:
ASSERT_NOT_REACHED();
return;
}
Vector<FloatQuad> quads;
textRenderer->absoluteQuadsForRange(quads, startPos, endPos);
appendQuadsToSubtargetList(quads, textNode, subtargets);
}
}
static inline void appendZoomableSubtargets(Node* node, SubtargetGeometryList& subtargets)
{
RenderBox* renderer = toRenderBox(node->renderer());
ASSERT(renderer);
Vector<FloatQuad> quads;
FloatRect borderBoxRect = renderer->borderBoxRect();
FloatRect contentBoxRect = renderer->contentBoxRect();
quads.append(renderer->localToAbsoluteQuad(borderBoxRect));
if (borderBoxRect != contentBoxRect)
quads.append(renderer->localToAbsoluteQuad(contentBoxRect));
Vector<FloatQuad>::const_iterator it = quads.begin();
const Vector<FloatQuad>::const_iterator end = quads.end();
for (; it != end; ++it)
subtargets.append(SubtargetGeometry(node, *it));
}
static inline Node* parentShadowHostOrOwner(const Node* node)
{
if (Node* ancestor = node->parentOrShadowHostNode())
return ancestor;
if (node->isDocumentNode())
return toDocument(node)->ownerElement();
return 0;
}
void compileSubtargetList(const Vector<RefPtr<Node> >& intersectedNodes, SubtargetGeometryList& subtargets, NodeFilter nodeFilter, AppendSubtargetsForNode appendSubtargetsForNode)
{
HashMap<Node*, Node*> responderMap;
HashSet<Node*> ancestorsToRespondersSet;
Vector<Node*> candidates;
HashSet<Node*> editableAncestors;
for (unsigned i = 0; i < intersectedNodes.size(); ++i) {
Node* node = intersectedNodes[i].get();
Vector<Node*> visitedNodes;
Node* respondingNode = 0;
for (Node* visitedNode = node; visitedNode; visitedNode = visitedNode->parentOrShadowHostNode()) {
respondingNode = responderMap.get(visitedNode);
if (respondingNode)
break;
visitedNodes.append(visitedNode);
if (nodeFilter(visitedNode)) {
respondingNode = visitedNode;
for (visitedNode = parentShadowHostOrOwner(visitedNode); visitedNode; visitedNode = parentShadowHostOrOwner(visitedNode)) {
HashSet<Node*>::AddResult addResult = ancestorsToRespondersSet.add(visitedNode);
if (!addResult.isNewEntry)
break;
}
break;
}
}
for (unsigned j = 0; j < visitedNodes.size(); j++)
responderMap.add(visitedNodes[j], respondingNode);
if (respondingNode)
candidates.append(node);
}
for (unsigned i = 0; i < candidates.size(); i++) {
Node* candidate = candidates[i];
Node* respondingNode = responderMap.get(candidate);
ASSERT(respondingNode);
if (ancestorsToRespondersSet.contains(respondingNode))
continue;
if (editableAncestors.contains(candidate))
continue;
if (candidate->isContentEditable()) {
Node* replacement = candidate;
Node* parent = candidate->parentOrShadowHostNode();
while (parent && parent->isContentEditable()) {
replacement = parent;
if (editableAncestors.contains(replacement)) {
replacement = 0;
break;
}
editableAncestors.add(replacement);
parent = parent->parentOrShadowHostNode();
}
candidate = replacement;
}
if (candidate)
appendSubtargetsForNode(candidate, subtargets);
}
}
void compileZoomableSubtargets(const Vector<RefPtr<Node> >& intersectedNodes, SubtargetGeometryList& subtargets)
{
for (unsigned i = 0; i < intersectedNodes.size(); ++i) {
Node* candidate = intersectedNodes[i].get();
if (nodeIsZoomTarget(candidate))
appendZoomableSubtargets(candidate, subtargets);
}
}
float zoomableIntersectionQuotient(const IntPoint& touchHotspot, const IntRect& touchArea, const SubtargetGeometry& subtarget)
{
IntRect rect = subtarget.boundingBox();
rect = subtarget.node()->document().view()->contentsToWindow(rect);
if (!rect.contains(touchHotspot))
return std::numeric_limits<float>::infinity();
IntRect intersection = rect;
intersection.intersect(touchArea);
return rect.size().area() / (float)intersection.size().area();
}
float hybridDistanceFunction(const IntPoint& touchHotspot, const IntRect& touchRect, const SubtargetGeometry& subtarget)
{
IntRect rect = subtarget.boundingBox();
rect = subtarget.node()->document().view()->contentsToWindow(rect);
float radiusSquared = 0.25f * (touchRect.size().diagonalLengthSquared());
float distanceToAdjustScore = rect.distanceSquaredToPoint(touchHotspot) / radiusSquared;
int maxOverlapWidth = std::min(touchRect.width(), rect.width());
int maxOverlapHeight = std::min(touchRect.height(), rect.height());
float maxOverlapArea = std::max(maxOverlapWidth * maxOverlapHeight, 1);
rect.intersect(touchRect);
float intersectArea = rect.size().area();
float intersectionScore = 1 - intersectArea / maxOverlapArea;
float hybridScore = intersectionScore + distanceToAdjustScore;
return hybridScore;
}
FloatPoint contentsToWindow(FrameView *view, FloatPoint pt)
{
int x = static_cast<int>(pt.x() + 0.5f);
int y = static_cast<int>(pt.y() + 0.5f);
IntPoint adjusted = view->contentsToWindow(IntPoint(x, y));
return FloatPoint(adjusted.x(), adjusted.y());
}
void adjustPointToRect(FloatPoint& point, const FloatRect& rect)
{
if (point.x() < rect.x())
point.setX(rect.x());
else if (point.x() > rect.maxX())
point.setX(rect.maxX());
if (point.y() < rect.y())
point.setY(rect.y());
else if (point.y() > rect.maxY())
point.setY(rect.maxY());
}
bool snapTo(const SubtargetGeometry& geom, const IntPoint& touchPoint, const IntRect& touchArea, IntPoint& adjustedPoint)
{
FrameView* view = geom.node()->document().view();
FloatQuad quad = geom.quad();
if (quad.isRectilinear()) {
IntRect contentBounds = geom.boundingBox();
IntRect bounds = view->contentsToWindow(contentBounds);
if (bounds.contains(touchPoint)) {
adjustedPoint = touchPoint;
return true;
}
if (bounds.intersects(touchArea)) {
bounds.intersect(touchArea);
adjustedPoint = bounds.center();
return true;
}
return false;
}
FloatPoint p1 = contentsToWindow(view, quad.p1());
FloatPoint p2 = contentsToWindow(view, quad.p2());
FloatPoint p3 = contentsToWindow(view, quad.p3());
FloatPoint p4 = contentsToWindow(view, quad.p4());
quad = FloatQuad(p1, p2, p3, p4);
if (quad.containsPoint(touchPoint)) {
adjustedPoint = touchPoint;
return true;
}
FloatPoint center = quad.center();
adjustPointToRect(center, touchArea);
adjustedPoint = roundedIntPoint(center);
return quad.containsPoint(adjustedPoint);
}
bool findNodeWithLowestDistanceMetric(Node*& targetNode, IntPoint& targetPoint, IntRect& targetArea, const IntPoint& touchHotspot, const IntRect& touchArea, SubtargetGeometryList& subtargets, DistanceFunction distanceFunction)
{
targetNode = 0;
float bestDistanceMetric = std::numeric_limits<float>::infinity();
SubtargetGeometryList::const_iterator it = subtargets.begin();
const SubtargetGeometryList::const_iterator end = subtargets.end();
IntPoint adjustedPoint;
for (; it != end; ++it) {
Node* node = it->node();
float distanceMetric = distanceFunction(touchHotspot, touchArea, *it);
if (distanceMetric < bestDistanceMetric) {
if (snapTo(*it, touchHotspot, touchArea, adjustedPoint)) {
targetPoint = adjustedPoint;
targetArea = it->boundingBox();
targetNode = node;
bestDistanceMetric = distanceMetric;
}
} else if (distanceMetric - bestDistanceMetric < zeroTolerance) {
if (snapTo(*it, touchHotspot, touchArea, adjustedPoint)) {
if (node->isDescendantOf(targetNode)) {
targetPoint = adjustedPoint;
targetNode = node;
targetArea = it->boundingBox();
}
}
}
}
if (targetNode) {
targetArea = targetNode->document().view()->contentsToWindow(targetArea);
}
return (targetNode);
}
}
bool findBestClickableCandidate(Node*& targetNode, IntPoint &targetPoint, const IntPoint &touchHotspot, const IntRect &touchArea, const Vector<RefPtr<Node> >& nodes)
{
IntRect targetArea;
TouchAdjustment::SubtargetGeometryList subtargets;
TouchAdjustment::compileSubtargetList(nodes, subtargets, TouchAdjustment::nodeRespondsToTapGesture, TouchAdjustment::appendBasicSubtargetsForNode);
return TouchAdjustment::findNodeWithLowestDistanceMetric(targetNode, targetPoint, targetArea, touchHotspot, touchArea, subtargets, TouchAdjustment::hybridDistanceFunction);
}
bool findBestContextMenuCandidate(Node*& targetNode, IntPoint &targetPoint, const IntPoint &touchHotspot, const IntRect &touchArea, const Vector<RefPtr<Node> >& nodes)
{
IntRect targetArea;
TouchAdjustment::SubtargetGeometryList subtargets;
TouchAdjustment::compileSubtargetList(nodes, subtargets, TouchAdjustment::providesContextMenuItems, TouchAdjustment::appendContextSubtargetsForNode);
return TouchAdjustment::findNodeWithLowestDistanceMetric(targetNode, targetPoint, targetArea, touchHotspot, touchArea, subtargets, TouchAdjustment::hybridDistanceFunction);
}
bool findBestZoomableArea(Node*& targetNode, IntRect& targetArea, const IntPoint& touchHotspot, const IntRect& touchArea, const Vector<RefPtr<Node> >& nodes)
{
IntPoint targetPoint;
TouchAdjustment::SubtargetGeometryList subtargets;
TouchAdjustment::compileZoomableSubtargets(nodes, subtargets);
return TouchAdjustment::findNodeWithLowestDistanceMetric(targetNode, targetPoint, targetArea, touchHotspot, touchArea, subtargets, TouchAdjustment::zoomableIntersectionQuotient);
}
}