Changeset 77220 in webkit


Ignore:
Timestamp:
Jan 31, 2011 9:53:05 PM (13 years ago)
Author:
ap@apple.com
Message:

Reviewed by Maciej Stachowiak.

https://bugs.webkit.org/show_bug.cgi?id=53466
Move WebKit2 to printing via API methods

Also fixes <rdar://problem/8933724> REGRESSION: Wrong pages are printed when not printing from page 1

Also fixes horizontal tiling (no bug filed).

The main idea here is that we don't force AppKit to use a different scaling factor when
a Web view can't be resized to exact page size, and scale when drawing instead.

  • UIProcess/API/mac/WKPrintingView.mm: (-[WKPrintingView _firstPrintedPageNumber]): Factored out into a separate method. (-[WKPrintingView _lastPrintedPageNumber]): Ditto. (pageDidDrawToPDF): Avoid crashing if data is null. (-[WKPrintingView _preparePDFDataForPrintingOnSecondaryThread]): Use -_firstPrintedPageNumber and -_lastPrintedPageNumber. (pageDidComputePageRects): Set frame size to what it would have been for a normal NSView that's been asked to resize. (-[WKPrintingView _pageForRect:]): Check x(), because several pages can be at the same y() when tiling horizontally. (-[WKPrintingView _drawPDFDocument:page:atPoint:]): Point to draw at and frame size are now using different units, translate as appropriate. (-[WKPrintingView _drawPreview:]): Scale the rect to WebCore units. (-[WKPrintingView drawRect:]): Correctly calculate page number when the first page to print isn't page 1. (-[WKPrintingView rectForPage:]): Scale the rect to AppKit units.
Location:
trunk/Source
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r77216 r77220  
     12011-01-31  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Reviewed by Maciej Stachowiak.
     4
     5        https://bugs.webkit.org/show_bug.cgi?id=53466
     6        Move WebKit2 to printing via API methods
     7
     8        * WebCore.exp.in: Export IntRect::scale().
     9
    1102011-01-31  Patrick Gansterer  <paroga@webkit.org>
    211
  • trunk/Source/WebCore/WebCore.exp.in

    r77153 r77220  
    751751__ZN7WebCore7Console21shouldPrintExceptionsEv
    752752__ZN7WebCore7Console24setShouldPrintExceptionsEb
     753__ZN7WebCore7IntRect5scaleEf
    753754__ZN7WebCore7IntRect5uniteERKS0_
    754755__ZN7WebCore7IntRect9intersectERKS0_
  • trunk/Source/WebKit2/ChangeLog

    r77182 r77220  
     12011-01-31  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Reviewed by Maciej Stachowiak.
     4
     5        https://bugs.webkit.org/show_bug.cgi?id=53466
     6        Move WebKit2 to printing via API methods
     7
     8        Also fixes <rdar://problem/8933724> REGRESSION: Wrong pages are printed when not printing from page 1
     9
     10        Also fixes horizontal tiling (no bug filed).
     11
     12        The main idea here is that we don't force AppKit to use a different scaling factor when
     13        a Web view can't be resized to exact page size, and scale when drawing instead.
     14
     15        * UIProcess/API/mac/WKPrintingView.mm:
     16        (-[WKPrintingView _firstPrintedPageNumber]): Factored out into a separate method.
     17        (-[WKPrintingView _lastPrintedPageNumber]): Ditto.
     18        (pageDidDrawToPDF): Avoid crashing if data is null.
     19        (-[WKPrintingView _preparePDFDataForPrintingOnSecondaryThread]): Use -_firstPrintedPageNumber
     20        and -_lastPrintedPageNumber.
     21        (pageDidComputePageRects): Set frame size to what it would have been for a normal NSView
     22        that's been asked to resize.
     23        (-[WKPrintingView _pageForRect:]): Check x(), because several pages can be at the same y()
     24        when tiling horizontally.
     25        (-[WKPrintingView _drawPDFDocument:page:atPoint:]): Point to draw at and frame size are
     26        now using different units, translate as appropriate.
     27        (-[WKPrintingView _drawPreview:]): Scale the rect to WebCore units.
     28        (-[WKPrintingView drawRect:]): Correctly calculate page number when the first page to print
     29        isn't page 1.
     30        (-[WKPrintingView rectForPage:]): Scale the rect to AppKit units.
     31
    1322011-01-31  Darin Adler  <darin@apple.com>
    233
  • trunk/Source/WebKit2/UIProcess/API/mac/WKPrintingView.mm

    r77051 r77220  
    3232#import "WebPageProxy.h"
    3333
    34 @interface NSView (WebNSViewDetails)
    35 - (void)_recursiveDisplayRectIfNeededIgnoringOpacity:(NSRect)rect isVisibleRect:(BOOL)isVisibleRect rectIsVisibleRectForView:(NSView *)visibleView topView:(BOOL)topView;
    36 @end
    37 
    3834using namespace WebKit;
    3935using namespace WebCore;
     
    113109}
    114110
     111- (NSUInteger)_firstPrintedPageNumber
     112{
     113    // Need to directly access the dictionary because -[NSPrintOperation pageRange] verifies pagination, potentially causing recursion.
     114    return [[[[_printOperation printInfo] dictionary] objectForKey:NSPrintFirstPage] unsignedIntegerValue];
     115}
     116
     117- (NSUInteger)_lastPrintedPageNumber
     118{
     119    ASSERT([self _hasPageRects]);
     120
     121    // Need to directly access the dictionary because -[NSPrintOperation pageRange] verifies pagination, potentially causing recursion.
     122    NSUInteger firstPage = [[[[_printOperation printInfo] dictionary] objectForKey:NSPrintFirstPage] unsignedIntegerValue];
     123    NSUInteger lastPage = [[[[_printOperation printInfo] dictionary] objectForKey:NSPrintLastPage] unsignedIntegerValue];
     124    if (lastPage - firstPage >= _printingPageRects.size())
     125        return _printingPageRects.size();
     126    return lastPage;
     127}
     128
    115129- (uint64_t)_expectedPreviewCallbackForRect:(const IntRect&)rect
    116130{
     
    150164            ASSERT([view _isPrintingPreview]);
    151165
    152             pair<HashMap<WebCore::IntRect, Vector<uint8_t> >::iterator, bool> entry = view->_pagePreviews.add(iter->second, Vector<uint8_t>());
    153             entry.first->second.append(data->bytes(), data->size());
    154 
     166            if (data) {
     167                pair<HashMap<WebCore::IntRect, Vector<uint8_t> >::iterator, bool> entry = view->_pagePreviews.add(iter->second, Vector<uint8_t>());
     168                entry.first->second.append(data->bytes(), data->size());
     169            }
    155170            bool receivedResponseToLatestRequest = view->_latestExpectedPreviewCallback == context->callbackID;
    156171            view->_latestExpectedPreviewCallback = 0;
     
    178193    ASSERT(!_expectedPrintCallback);
    179194
    180     unsigned firstPage = [[[[_printOperation printInfo] dictionary] objectForKey:NSPrintFirstPage] unsignedIntValue];
    181     unsigned lastPage = [[[[_printOperation printInfo] dictionary] objectForKey:NSPrintLastPage] unsignedIntValue];
    182     if (lastPage > _printingPageRects.size()) // Last page is NSIntegerMax if not set by the user.
    183         lastPage = _printingPageRects.size();
     195    NSUInteger firstPage = [self _firstPrintedPageNumber];
     196    NSUInteger lastPage = [self _lastPrintedPageNumber];
    184197
    185198    ASSERT(firstPage > 0);
     
    218231        view->_printingPageRects = pageRects;
    219232        view->_totalScaleFactorForPrinting = totalScaleFactorForPrinting;
     233
     234        const IntRect& lastPrintingPageRect = view->_printingPageRects[view->_printingPageRects.size() - 1];
     235        NSRect newFrameSize = NSMakeRect(0, 0,
     236            ceil(lastPrintingPageRect.right() * view->_totalScaleFactorForPrinting),
     237            ceil(lastPrintingPageRect.bottom() * view->_totalScaleFactorForPrinting));
     238        LOG(View, "WKPrintingView setting frame size to x:%g y:%g width:%g height:%g", newFrameSize.origin.x, newFrameSize.origin.y, newFrameSize.size.width, newFrameSize.size.height);
     239        [view setFrame:newFrameSize];
    220240
    221241        if ([view _isPrintingPreview]) {
     
    302322    // Assuming that rect exactly matches one of the pages.
    303323    for (size_t i = 0; i < _printingPageRects.size(); ++i) {
    304         if (rect.origin.y == _printingPageRects[i].y())
     324        IntRect currentRect(_printingPageRects[i]);
     325        currentRect.scale(_totalScaleFactorForPrinting);
     326        if (rect.origin.y == currentRect.y() && rect.origin.x == currentRect.x())
    305327            return i + 1;
    306328    }
     329    ASSERT_NOT_REACHED();
    307330    return 0; // Invalid page number.
    308331}
     
    325348
    326349    CGContextSaveGState(context);
    327     // Flip the destination.
    328     CGContextScaleCTM(context, 1, -1);
    329     CGContextTranslateCTM(context, point.x, -point.y -CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox).size.height);
     350    CGContextTranslateCTM(context, point.x, point.y);
     351    CGContextScaleCTM(context, _totalScaleFactorForPrinting, -_totalScaleFactorForPrinting);
     352    CGContextTranslateCTM(context, 0, -CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox).size.height);
    330353    CGContextDrawPDFPage(context, pdfPage);
    331354    CGContextRestoreGState(context);
     
    337360
    338361    IntRect rect(nsRect);
     362    rect.scale(1 / _totalScaleFactorForPrinting);
    339363    HashMap<WebCore::IntRect, Vector<uint8_t> >::iterator pagePreviewIterator = _pagePreviews.find(rect);
    340364    if (pagePreviewIterator == _pagePreviews.end())  {
     
    377401}
    378402
    379 // Take over printing. AppKit applies incorrect clipping, and doesn't print pages beyond the first one.
    380 - (void)_recursiveDisplayRectIfNeededIgnoringOpacity:(NSRect)nsRect isVisibleRect:(BOOL)isVisibleRect rectIsVisibleRectForView:(NSView *)visibleView topView:(BOOL)topView
    381 {
    382     LOG(View, "WKPrintingView printing rect x:%g, y:%g, width:%g, height:%g", nsRect.origin.x, nsRect.origin.y, nsRect.size.width, nsRect.size.height);
     403- (void)drawRect:(NSRect)nsRect
     404{
     405    LOG(View, "WKPrintingView printing rect x:%g, y:%g, width:%g, height:%g%s", nsRect.origin.x, nsRect.origin.y, nsRect.size.width, nsRect.size.height, [self _isPrintingPreview] ? " for preview" : "");
    383406
    384407    ASSERT(_printOperation == [NSPrintOperation currentOperation]);
    385     ASSERT(self == visibleView);
    386408
    387409    if (!_webFrame->page())
     
    401423    }
    402424
    403     [self _drawPDFDocument:_printedPagesPDFDocument.get() page:[self _pageForRect:nsRect] atPoint:NSMakePoint(nsRect.origin.x, nsRect.origin.y)];
     425    unsigned printedPageNumber = [self _pageForRect:nsRect] - [self _firstPrintedPageNumber] + 1;
     426    [self _drawPDFDocument:_printedPagesPDFDocument.get() page:printedPageNumber atPoint:NSMakePoint(nsRect.origin.x, nsRect.origin.y)];
    404427}
    405428
     
    460483}
    461484
    462 // FIXME 3491344: This is an AppKit-internal method that we need to override in order
    463 // to get our shrink-to-fit to work with a custom pagination scheme. We can do this better
    464 // if AppKit makes it SPI/API.
    465 - (CGFloat)_provideTotalScaleFactorForPrintOperation:(NSPrintOperation *)printOperation
    466 {
    467     ASSERT(_printOperation == printOperation);
    468     return _totalScaleFactorForPrinting;
    469 }
    470 
    471 // Return the drawing rectangle for a particular page number
    472485- (NSRect)rectForPage:(NSInteger)page
    473486{
     
    479492        return NSMakeRect(0, 0, 1, 1);
    480493    }
    481     LOG(View, "-[WKPrintingView rectForPage:%d] -> x %d, y %d, width %d, height %d", (int)page, _printingPageRects[page - 1].x(), _printingPageRects[page - 1].y(), _printingPageRects[page - 1].width(), _printingPageRects[page - 1].height());
    482     return _printingPageRects[page - 1];
     494
     495    IntRect rect = _printingPageRects[page - 1];
     496    rect.scale(_totalScaleFactorForPrinting);
     497    LOG(View, "-[WKPrintingView rectForPage:%d] -> x %d, y %d, width %d, height %d", (int)page, rect.x(), rect.y(), rect.width(), rect.height());
     498    return rect;
    483499}
    484500
Note: See TracChangeset for help on using the changeset viewer.