Expose TextIndicator-backed snapshot and rect gathering on DOMNode
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 25 Jul 2015 23:11:16 +0000 (23:11 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 25 Jul 2015 23:11:16 +0000 (23:11 +0000)
https://bugs.webkit.org/show_bug.cgi?id=147298
<rdar://problem/21905839>

Reviewed by Sam Weinig.

* bindings/objc/DOM.mm:
(-[DOMNode getPreviewSnapshotImage:andRects:]):
* bindings/objc/DOMExtensions.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@187391 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/bindings/objc/DOM.mm
Source/WebCore/bindings/objc/DOMExtensions.h

index 1633f50..4cc06fb 100644 (file)
@@ -1,3 +1,15 @@
+2015-07-25  Tim Horton  <timothy_horton@apple.com>
+
+        Expose TextIndicator-backed snapshot and rect gathering on DOMNode
+        https://bugs.webkit.org/show_bug.cgi?id=147298
+        <rdar://problem/21905839>
+
+        Reviewed by Sam Weinig.
+
+        * bindings/objc/DOM.mm:
+        (-[DOMNode getPreviewSnapshotImage:andRects:]):
+        * bindings/objc/DOMExtensions.h:
+
 2015-07-25  Ryosuke Niwa  <rniwa@webkit.org>
 
         Another build fix. Removed the unused argument.
index 33b6deb..7786781 100644 (file)
@@ -50,6 +50,7 @@
 #import "Range.h"
 #import "RenderImage.h"
 #import "ScriptController.h"
+#import "TextIndicator.h"
 #import "WebScriptObjectPrivate.h"
 #import <JavaScriptCore/APICast.h>
 #import <wtf/HashMap.h>
@@ -541,6 +542,48 @@ id <DOMEventTarget> kit(WebCore::EventTarget* eventTarget)
 
 #endif // PLATFORM(IOS)
 
+- (void)getPreviewSnapshotImage:(CGImageRef*)cgImage andRects:(NSArray **)rects
+{
+    if (!cgImage || !rects)
+        return;
+
+    *cgImage = nullptr;
+    *rects = nullptr;
+
+    Node* coreNode = core(self);
+
+    Ref<Range> range = rangeOfContents(*coreNode);
+
+    float deviceScaleFactor = coreNode->document().deviceScaleFactor();
+    const float margin = 4 * deviceScaleFactor;
+    RefPtr<TextIndicator> textIndicator = TextIndicator::createWithRange(range, TextIndicatorPresentationTransition::None, margin);
+
+    if (!textIndicator)
+        return;
+
+    if (Image* image = textIndicator->contentImage())
+        *cgImage = (CGImageRef)CFAutorelease(CGImageRetain(image->getCGImageRef()));
+
+    RetainPtr<NSMutableArray> rectArray = adoptNS([[NSMutableArray alloc] init]);
+
+    if (*cgImage) {
+        FloatPoint origin = textIndicator->textBoundingRectInRootViewCoordinates().location();
+        for (const FloatRect& rect : textIndicator->textRectsInBoundingRectCoordinates()) {
+            CGRect cgRect = rect;
+            cgRect.origin.x += origin.x();
+            cgRect.origin.y += origin.y();
+            cgRect = coreNode->document().frame()->view()->contentsToWindow(enclosingIntRect(cgRect));
+            [rectArray addObject:[NSValue value:&cgRect withObjCType:@encode(CGRect)]];
+        }
+    } else {
+        CGRect cgRect = CGRectInset(range->boundingRect(), -margin, -margin);
+        cgRect = coreNode->document().frame()->view()->contentsToWindow(enclosingIntRect(cgRect));
+        [rectArray addObject:[NSValue value:&cgRect withObjCType:@encode(CGRect)]];
+    }
+
+    *rects = rectArray.autorelease();
+}
+
 @end
 
 @implementation DOMNode (DOMNodeExtensionsPendingPublic)
index c8bf27c..14744e9 100644 (file)
@@ -109,6 +109,8 @@ typedef struct _WKQuad {
 - (DOMNode *)nextFocusNode;
 - (DOMNode *)previousFocusNode;
 #endif
+
+- (void)getPreviewSnapshotImage:(CGImageRef*)cgImage andRects:(NSArray **)rects;
 @end
 
 @interface DOMElement (DOMElementAppKitExtensions)