[iOS][WK2] Implement WKPDFView smart magnification
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 27 Jun 2014 02:11:00 +0000 (02:11 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 27 Jun 2014 02:11:00 +0000 (02:11 +0000)
https://bugs.webkit.org/show_bug.cgi?id=134269
<rdar://problem/17272825>

Reviewed by Dan Bernstein.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _currentContentView]):
(contentZoomScale):
Factor _currentContentView out of contentZoomScale.

(-[WKWebView _zoomToRect:WebCore::atScale:origin:WebCore::]):
(-[WKWebView _scrollToRect:WebCore::origin:WebCore::minimumScrollDistance:]):
(-[WKWebView _contentRectForUserInteraction]):
Use it in a bunch more places so we query the right view when zooming/scrolling for smart magnification.

* UIProcess/ios/WKPDFView.h:
* UIProcess/ios/WKPDFView.mm:
(-[WKPDFView scrollViewDidScroll:]):
(-[WKPDFView _revalidateViews]):
(-[WKPDFView zoom:to:atPoint:kind:]):
(-[WKPDFView resetZoom:]):
Implement zoom:to:atPoint:kind: and resetZoom: UIPDFPageViewDelegate methods.
Convert the rects/points and forward them on to the WKWebView to do the zoom.
resetZoom: doesn't provide the gesture origin, so we zoom out using the view center as our origin.
Avoid parenting new UIPDFPageViews while starting a zoom; they'll end up with a bizarre
animation on them and go flying across the screen (even before _isAnimatingZoom is set).

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

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit2/UIProcess/ios/WKPDFView.h
Source/WebKit2/UIProcess/ios/WKPDFView.mm

index c8a8af813875b52418bfb344ffba7910daaece07..46b0f93b3b9a02bf1a3598cd16a8ee4681c824a6 100644 (file)
@@ -1,3 +1,33 @@
+2014-06-26  Timothy Horton  <timothy_horton@apple.com>
+
+        [iOS][WK2] Implement WKPDFView smart magnification
+        https://bugs.webkit.org/show_bug.cgi?id=134269
+        <rdar://problem/17272825>
+
+        Reviewed by Dan Bernstein.
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView _currentContentView]):
+        (contentZoomScale):
+        Factor _currentContentView out of contentZoomScale.
+
+        (-[WKWebView _zoomToRect:WebCore::atScale:origin:WebCore::]):
+        (-[WKWebView _scrollToRect:WebCore::origin:WebCore::minimumScrollDistance:]):
+        (-[WKWebView _contentRectForUserInteraction]):
+        Use it in a bunch more places so we query the right view when zooming/scrolling for smart magnification.
+
+        * UIProcess/ios/WKPDFView.h:
+        * UIProcess/ios/WKPDFView.mm:
+        (-[WKPDFView scrollViewDidScroll:]):
+        (-[WKPDFView _revalidateViews]):
+        (-[WKPDFView zoom:to:atPoint:kind:]):
+        (-[WKPDFView resetZoom:]):
+        Implement zoom:to:atPoint:kind: and resetZoom: UIPDFPageViewDelegate methods.
+        Convert the rects/points and forward them on to the WKWebView to do the zoom.
+        resetZoom: doesn't provide the gesture origin, so we zoom out using the view center as our origin.
+        Avoid parenting new UIPDFPageViews while starting a zoom; they'll end up with a bizarre
+        animation on them and go flying across the screen (even before _isAnimatingZoom is set).
+
 2014-06-26  Tim Horton  <timothy_horton@apple.com>
 
         [WK2] Pinch-zoom shadows can overlap headers and footers
index 5177a037e331e7bb6cb4c7171b1f0092279d2f08..4ae4f9883940f1964ed309c4187193b861a9bc60 100644 (file)
@@ -582,6 +582,11 @@ static CGSize roundScrollViewContentSize(const WebKit::WebPageProxy& page, CGSiz
     return CGSizeMake(floorToDevicePixel(contentSize.width, deviceScaleFactor), floorToDevicePixel(contentSize.height, deviceScaleFactor));
 }
 
+- (UIView *)_currentContentView
+{
+    return _customContentView ? _customContentView.get() : _contentView.get();
+}
+
 - (void)_setHasCustomContentView:(BOOL)pageHasCustomContentView loadedMIMEType:(const WTF::String&)mimeType
 {
     if (pageHasCustomContentView) {
@@ -642,13 +647,7 @@ static CGSize roundScrollViewContentSize(const WebKit::WebPageProxy& page, CGSiz
 
 static CGFloat contentZoomScale(WKWebView* webView)
 {
-    UIView *zoomView;
-    if (webView->_customContentView)
-        zoomView = webView->_customContentView.get();
-    else
-        zoomView = webView->_contentView.get();
-
-    CGFloat scale = [[zoomView layer] affineTransform].a;
+    CGFloat scale = [[webView._currentContentView layer] affineTransform].a;
     ASSERT(scale == [webView->_scrollView zoomScale]);
     return scale;
 }
@@ -916,7 +915,7 @@ static void changeContentOffsetBoundedInValidRange(UIScrollView *scrollView, Web
 {
     // FIMXE: Some of this could be shared with _scrollToRect.
     const double visibleRectScaleChange = contentZoomScale(self) / scale;
-    const WebCore::FloatRect visibleRect([self convertRect:self.bounds toView:_contentView.get()]);
+    const WebCore::FloatRect visibleRect([self convertRect:self.bounds toView:self._currentContentView]);
     const WebCore::FloatRect unobscuredRect([self _contentRectForUserInteraction]);
 
     const WebCore::FloatSize topLeftObscuredInsetAfterZoom((unobscuredRect.minXMinYCorner() - visibleRect.minXMinYCorner()) * visibleRectScaleChange);
@@ -967,7 +966,7 @@ static WebCore::FloatPoint constrainContentOffset(WebCore::FloatPoint contentOff
 {
     WebCore::FloatRect unobscuredContentRect([self _contentRectForUserInteraction]);
     WebCore::FloatPoint unobscuredContentOffset = unobscuredContentRect.location();
-    WebCore::FloatSize contentSize([_contentView bounds].size);
+    WebCore::FloatSize contentSize([self._currentContentView bounds].size);
 
     // Center the target rect in the scroll view.
     // If the target doesn't fit in the scroll view, center on the gesture location instead.
@@ -1279,7 +1278,7 @@ static WebCore::FloatPoint constrainContentOffset(WebCore::FloatPoint contentOff
     UIEdgeInsets obscuredInsets = _obscuredInsets;
     obscuredInsets.bottom = std::max(_obscuredInsets.bottom, _inputViewBounds.size.height);
     CGRect unobscuredRect = UIEdgeInsetsInsetRect(self.bounds, obscuredInsets);
-    return [self convertRect:unobscuredRect toView:_contentView.get()];
+    return [self convertRect:unobscuredRect toView:self._currentContentView];
 }
 
 - (void)_updateVisibleContentRects
index a8cfa6c24606805b9c2b936015bf7f4a54fafe8a..0d9c097f97d6ffec8db96e144aa7c5b94346a52e 100644 (file)
 #if PLATFORM(IOS)
 
 #import "WKWebViewContentProvider.h"
+#import <CorePDF/UIPDFPageView.h>
 #import <UIKit/UIView.h>
 
-@interface WKPDFView : UIView <WKWebViewContentProvider>
+@interface WKPDFView : UIView <WKWebViewContentProvider, UIPDFPageViewDelegate>
 
 @property (nonatomic, readonly) NSString *suggestedFilename;
 @property (nonatomic, readonly) CGPDFDocumentRef pdfDocument;
index 7c7ea6ffc58dbbf1b902b57f793d5eb806d73c57..746fbccda0a95188381ec8506ca0b976cf95c949 100644 (file)
@@ -33,6 +33,7 @@
 #import <CorePDF/UIPDFDocument.h>
 #import <CorePDF/UIPDFPage.h>
 #import <CorePDF/UIPDFPageView.h>
+#import <UIKit/UIScrollView_Private.h>
 #import <WebCore/FloatRect.h>
 #import <wtf/RetainPtr.h>
 #import <wtf/Vector.h>
@@ -45,6 +46,8 @@ const CGFloat pdfMaximumZoomScale = 5;
 
 const float overdrawHeightMultiplier = 1.5;
 
+static const CGFloat smartMagnificationElementPadding = 0.05;
+
 typedef struct {
     CGRect frame;
     RetainPtr<UIPDFPageView> view;
@@ -64,6 +67,8 @@ typedef struct {
     WKWebView *_webView;
     UIScrollView *_scrollView;
     UIView *_fixedOverlayView;
+
+    BOOL _isStartingZoom;
 }
 
 - (instancetype)web_initWithFrame:(CGRect)frame webView:(WKWebView *)webView
@@ -85,6 +90,7 @@ typedef struct {
 
 - (void)dealloc
 {
+    [self _clearPages];
     [_pageNumberIndicator removeFromSuperview];
     [super dealloc];
 }
@@ -99,14 +105,21 @@ typedef struct {
     return [_pdfDocument CGDocument];
 }
 
+- (void)_clearPages
+{
+    for (auto& page : _pages) {
+        [page.view removeFromSuperview];
+        [page.view setDelegate:nil];
+    }
+    
+    _pages.clear();
+}
+
 - (void)web_setContentProviderData:(NSData *)data suggestedFilename:(NSString *)filename
 {
     _suggestedFilename = adoptNS([filename copy]);
 
-    for (auto& page : _pages)
-        [page.view removeFromSuperview];
-
-    _pages.clear();
+    [self _clearPages];
 
     RetainPtr<CGDataProvider> dataProvider = adoptCF(CGDataProviderCreateWithCFData((CFDataRef)data));
     RetainPtr<CGPDFDocumentRef> cgPDFDocument = adoptCF(CGPDFDocumentCreateWithProvider(dataProvider.get()));
@@ -126,7 +139,7 @@ typedef struct {
 
 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
 {
-    if (scrollView.isZoomBouncing)
+    if (scrollView.isZoomBouncing || scrollView._isAnimatingZoom)
         return;
 
     [self _revalidateViews];
@@ -135,6 +148,9 @@ typedef struct {
 
 - (void)_revalidateViews
 {
+    if (_isStartingZoom)
+        return;
+
     CGRect targetRect = [_scrollView convertRect:_scrollView.bounds toView:self];
 
     // We apply overdraw after applying scale in order to avoid excessive
@@ -162,6 +178,7 @@ typedef struct {
 
         pageInfo.view = adoptNS([[UIPDFPageView alloc] initWithPage:pageInfo.page.get() tiledContent:YES]);
         [pageInfo.view setUseBackingLayer:YES];
+        [pageInfo.view setDelegate:self];
         [self addSubview:pageInfo.view.get()];
 
         [pageInfo.view setFrame:pageInfo.frame];
@@ -211,11 +228,9 @@ typedef struct {
 {
     NSUInteger pageCount = [_pdfDocument numberOfPages];
     [_pageNumberIndicator setPageCount:pageCount];
+    
+    [self _clearPages];
 
-    for (auto& pageInfo : _pages)
-        [pageInfo.view removeFromSuperview];
-
-    _pages.clear();
     _pages.reserveCapacity(pageCount);
 
     CGRect pageFrame = CGRectMake(0, 0, _minimumSize.width, _minimumSize.height);
@@ -244,6 +259,35 @@ typedef struct {
     [_scrollView setContentSize:newFrame.size];
 }
 
+
+- (void)zoom:(UIPDFPageView *)pageView to:(CGRect)targetRect atPoint:(CGPoint)origin kind:(UIPDFObjectKind)kind
+{
+    _isStartingZoom = YES;
+
+    BOOL isImage = kind == kUIPDFObjectKindGraphic;
+
+    if (!isImage)
+        targetRect = CGRectInset(targetRect, smartMagnificationElementPadding * targetRect.size.width, smartMagnificationElementPadding * targetRect.size.height);
+
+    CGRect rectInDocumentCoordinates = [pageView convertRect:targetRect toView:self];
+    CGPoint originInDocumentCoordinates = [pageView convertPoint:origin toView:self];
+
+    [_webView _zoomToRect:rectInDocumentCoordinates withOrigin:originInDocumentCoordinates fitEntireRect:isImage minimumScale:pdfMinimumZoomScale maximumScale:pdfMaximumZoomScale minimumScrollDistance:0];
+
+    _isStartingZoom = NO;
+}
+
+- (void)resetZoom:(UIPDFPageView *)pageView
+{
+    _isStartingZoom = YES;
+    
+    CGRect scrollViewBounds = _scrollView.bounds;
+    CGPoint centerOfPageInDocumentCoordinates = [_scrollView convertPoint:CGPointMake(CGRectGetMidX(scrollViewBounds), CGRectGetMidY(scrollViewBounds)) toView:self];
+    [_webView _zoomOutWithOrigin:centerOfPageInDocumentCoordinates];
+
+    _isStartingZoom = NO;
+}
+
 @end
 
 #endif /* PLATFORM(IOS) */