Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1308)

Unified Diff: Source/core/html/canvas/HitRegion.cpp

Issue 300223009: Implement basic parts of hit regions on canvas2d. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/canvas/HitRegion.cpp
diff --git a/Source/core/html/canvas/HitRegion.cpp b/Source/core/html/canvas/HitRegion.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..4d17cae28f9b4276d64aee158e6732f2056220d6
--- /dev/null
+++ b/Source/core/html/canvas/HitRegion.cpp
@@ -0,0 +1,147 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/html/canvas/HitRegion.h"
+
+#include "core/accessibility/AXObject.h"
+#include "core/accessibility/AXObjectCache.h"
+
+namespace WebCore {
+
+HitRegion::HitRegion(const HitRegionOptions& options)
+ : m_id(options.id)
+ , m_control(options.control)
+ , m_path(options.path)
+{
+}
+
+void HitRegion::updateAccessibility(Element* canvas)
+{
+ if (!m_control || !canvas || !canvas->renderer() || !m_control->isDescendantOf(canvas))
+ return;
+
+ if (AXObjectCache* axObjectCache = m_control->document().existingAXObjectCache()) {
+ if (AXObject* obj = axObjectCache->getOrCreate(m_control.get())) {
dmazzoni 2014/06/03 17:26:10 Let's move some of this logic to a new function on
zino 2014/06/08 08:39:49 Done.
+ LayoutRect elementRect = LayoutRect(m_path.boundingRect());
+
+ IntRect canvasRect = canvas->renderer()->absoluteBoundingBoxRect();
+ elementRect.moveBy(canvasRect.location());
+ obj->setElementRect(elementRect);
+
+ obj = obj->parentObject();
+ while (obj && obj->node() != canvas) {
+ obj->setElementRect(elementRect);
fs 2014/06/03 11:47:01 This might be better answered by dmazzoni, but thi
dmazzoni 2014/06/03 17:26:10 Ideally it seems like we'd want container element
fs 2014/06/04 08:13:40 Computing on-demand SGTM.
zino 2014/06/08 08:39:49 Done.
+ obj = obj->parentObject();
+ }
+ }
+ }
+}
+
+bool HitRegion::contains(const LayoutPoint& point) const
+{
+ // FIXME: We should avoid following boundingRect test if fillRule is evenodd.
fs 2014/06/03 11:47:01 Why? AFAICT this broad-phase check is equally good
zino 2014/06/06 06:35:16 You're right. Done.
+ if (!m_path.boundingRect().contains(point))
+ return false;
+
+ return m_path.contains(point, RULE_NONZERO);
+}
+
+void HitRegion::removePixels(const Path& clearArea)
+{
+ m_path.subtractPath(clearArea);
+}
+
+void HitRegionManager::addHitRegion(PassRefPtrWillBeRawPtr<HitRegion> passHitRegion)
+{
+ RefPtrWillBeRawPtr<HitRegion> hitRegion = passHitRegion;
+
+ m_hitRegionList.add(hitRegion);
+
+ if (!hitRegion->id().isEmpty())
+ m_hitRegionIdMap.set(hitRegion->id(), hitRegion);
+
+ if (hitRegion->control())
+ m_hitRegionControlMap.set(hitRegion->control(), hitRegion);
+}
+
+void HitRegionManager::removeHitRegion(HitRegion* hitRegion)
+{
+ if (!hitRegion)
+ return;
+
+ if (!hitRegion->id().isEmpty())
+ m_hitRegionIdMap.remove(hitRegion->id());
+
+ if (hitRegion->control())
+ m_hitRegionControlMap.remove(hitRegion->control());
+
+ m_hitRegionList.remove(hitRegion);
+}
+
+void HitRegionManager::removeHitRegionById(const String& id)
+{
+ if (!id.isEmpty())
+ removeHitRegion(getHitRegionById(id));
+}
+
+void HitRegionManager::removeHitRegionByControl(const Element* control)
+{
+ removeHitRegion(getHitRegionByControl(control));
+}
+
+void HitRegionManager::removeHitRegionsInRect(const FloatRect& rect, const AffineTransform& ctm)
+{
+ Path clearArea;
+ clearArea.addRect(rect);
+ clearArea.transform(ctm);
+
+ HitRegionIterator itEnd = m_hitRegionList.rend();
+
+ for (HitRegionIterator it = m_hitRegionList.rbegin(); it != itEnd; ++it) {
+ RefPtrWillBeRawPtr<HitRegion> hitRegion = *it;
+ hitRegion->removePixels(clearArea);
+ if (hitRegion->path().isEmpty())
+ removeHitRegion(hitRegion.get());
+ }
+}
+
+void HitRegionManager::removeAllHitRegions()
+{
+ m_hitRegionList.clear();
+ m_hitRegionIdMap.clear();
+}
+
+HitRegion* HitRegionManager::getHitRegionById(const String& id) const
+{
+ return m_hitRegionIdMap.get(id);
+}
+
+HitRegion* HitRegionManager::getHitRegionByControl(const Element* control) const
+{
+ if (control)
+ return m_hitRegionControlMap.get(control);
+
+ return 0;
+}
+
+HitRegion* HitRegionManager::getHitRegionAtPoint(const LayoutPoint& point) const
+{
+ HitRegionIterator itEnd = m_hitRegionList.rend();
+
+ for (HitRegionIterator it = m_hitRegionList.rbegin(); it != itEnd; ++it) {
+ RefPtrWillBeRawPtr<HitRegion> hitRegion = *it;
+ if (hitRegion->contains(point))
+ return hitRegion.get();
+ }
+
+ return 0;
+}
+
+unsigned HitRegionManager::getHitRegionsCount() const
+{
+ return m_hitRegionList.size();
+}
+
+} // namespace WebCore

Powered by Google App Engine
This is Rietveld 408576698