Changeset 57246 in webkit


Ignore:
Timestamp:
Apr 7, 2010 9:11:48 PM (14 years ago)
Author:
mitz@apple.com
Message:

Refactor WebHTMLView printing code and add private methods to enter and exit printing mode.
https://bugs.webkit.org/show_bug.cgi?id=37246

Reviewed by Adele Peterson.

  • Misc/WebNSPrintOperationExtras.h: Declared -_web_availablePaperWidth and -_web_availablePaperHeight.
  • Misc/WebNSPrintOperationExtras.m:

(-[NSPrintOperation _web_availablePaperWidth]): Turned -[WebHTMLView _availablePaperWidthForPrintOperation:]
into this method.
(-[NSPrintOperation _web_availablePaperHeight]): Turned -[WebHTMLView _calculatePrintHeight] into this
method.

  • WebView/WebHTMLView.mm:

(-[WebHTMLView _isInPrintMode]): Added this accessor.
(-[WebHTMLView _beginPrintModeWithPageWidth:shrinkToFit:]): Added. Moved the code from -knowsPageRange: that
computes the layout widths and enters printing mode into this private method.
(-[WebHTMLView _endPrintMode]): New private method (the old -_endPrintMode has been renamed).
(-[WebHTMLView _scaleFactorForPrintOperation:]): Use -[NSPrintOperation _web_availablePaperWidth].
(-[WebHTMLView _endPrintModeAndRestoreWindowAutodisplay]): Renamed -_endPrintMode to this, changed it to call
_endPrintMode.
(-[WebHTMLView _delayedEndPrintMode:]): Updated for rename.
(-[WebHTMLView knowsPageRange:]): Use -_beginPrintModeWithPageWidth:shrintToFit:,
-[NSPrintOperation _web_availablePaperWidth], and -[NSPrintOperation _web_availablePaperHeight]. Updated for
rename.
(-[WebHTMLView beginDocument]): Updated for rename.
(-[WebHTMLView endDocument]): Ditto.

  • WebView/WebHTMLViewPrivate.h: Declared new private methods -_isInPrintMode,

-_beginPrintModeWithPageWidth:shrinkToFit: and -_endPrintMode.

Location:
trunk/WebKit/mac
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKit/mac/ChangeLog

    r57210 r57246  
     12010-04-07  Dan Bernstein  <mitz@apple.com>
     2
     3        Reviewed by Adele Peterson.
     4
     5        Refactor WebHTMLView printing code and add private methods to enter and exit printing mode.
     6        https://bugs.webkit.org/show_bug.cgi?id=37246
     7
     8        * Misc/WebNSPrintOperationExtras.h: Declared -_web_availablePaperWidth and -_web_availablePaperHeight.
     9        * Misc/WebNSPrintOperationExtras.m:
     10        (-[NSPrintOperation _web_availablePaperWidth]): Turned -[WebHTMLView _availablePaperWidthForPrintOperation:]
     11        into this method.
     12        (-[NSPrintOperation _web_availablePaperHeight]): Turned -[WebHTMLView _calculatePrintHeight] into this
     13        method.
     14        * WebView/WebHTMLView.mm:
     15        (-[WebHTMLView _isInPrintMode]): Added this accessor.
     16        (-[WebHTMLView _beginPrintModeWithPageWidth:shrinkToFit:]): Added. Moved the code from -knowsPageRange: that
     17        computes the layout widths and enters printing mode into this private method.
     18        (-[WebHTMLView _endPrintMode]): New private method (the old -_endPrintMode has been renamed).
     19        (-[WebHTMLView _scaleFactorForPrintOperation:]): Use -[NSPrintOperation _web_availablePaperWidth].
     20        (-[WebHTMLView _endPrintModeAndRestoreWindowAutodisplay]): Renamed -_endPrintMode to this, changed it to call
     21        _endPrintMode.
     22        (-[WebHTMLView _delayedEndPrintMode:]): Updated for rename.
     23        (-[WebHTMLView knowsPageRange:]): Use -_beginPrintModeWithPageWidth:shrintToFit:,
     24        -[NSPrintOperation _web_availablePaperWidth], and -[NSPrintOperation _web_availablePaperHeight]. Updated for
     25        rename.
     26        (-[WebHTMLView beginDocument]): Updated for rename.
     27        (-[WebHTMLView endDocument]): Ditto.
     28        * WebView/WebHTMLViewPrivate.h: Declared new private methods -_isInPrintMode,
     29        -_beginPrintModeWithPageWidth:shrinkToFit: and -_endPrintMode.
     30
    1312010-04-07  Andrey Kosyakov  <caseq@chromium.org>
    232
  • trunk/WebKit/mac/Misc/WebNSPrintOperationExtras.h

    r11962 r57246  
    3232
    3333- (float)_web_pageSetupScaleFactor;
     34- (float)_web_availablePaperWidth;
     35- (float)_web_availablePaperHeight;
    3436
    3537@end
  • trunk/WebKit/mac/Misc/WebNSPrintOperationExtras.m

    r11962 r57246  
    3636}
    3737
     38- (float)_web_availablePaperWidth
     39{
     40    NSPrintInfo *printInfo = [self printInfo];
     41    return [printInfo paperSize].width - [printInfo leftMargin] - [printInfo rightMargin];
     42}
     43
     44- (float)_web_availablePaperHeight
     45{
     46    NSPrintInfo *printInfo = [self printInfo];
     47    return [printInfo paperSize].height - [printInfo topMargin] - [printInfo bottomMargin];
     48}
     49
    3850@end
  • trunk/WebKit/mac/WebView/WebHTMLView.mm

    r57184 r57246  
    323323- (BOOL)_shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action;
    324324- (BOOL)_shouldReplaceSelectionWithText:(NSString *)text givenAction:(WebViewInsertAction)action;
    325 - (float)_calculatePrintHeight;
    326325- (DOMRange *)_selectedRange;
    327326- (BOOL)_shouldDeleteRange:(DOMRange *)range;
     
    900899{
    901900    return [self _shouldInsertText:text replacingDOMRange:[self _selectedRange] givenAction:action];
    902 }
    903 
    904 // Calculate the vertical size of the view that fits on a single page
    905 - (float)_calculatePrintHeight
    906 {
    907     // Obtain the print info object for the current operation
    908     NSPrintInfo *pi = [[NSPrintOperation currentOperation] printInfo];
    909    
    910     // Calculate the page height in points
    911     NSSize paperSize = [pi paperSize];
    912     return paperSize.height - [pi topMargin] - [pi bottomMargin];
    913901}
    914902
     
    22012189    return 0;
    22022190#endif
     2191}
     2192
     2193- (BOOL)_isInPrintMode
     2194{
     2195    return _private->printing;
     2196}
     2197
     2198- (BOOL)_beginPrintModeWithPageWidth:(float)pageWidth shrinkToFit:(BOOL)shrinkToFit
     2199{
     2200    Frame* frame = core([self _frame]);
     2201    if (!frame)
     2202        return NO;
     2203
     2204    float minLayoutWidth = 0;
     2205    float maxLayoutWidth = 0;
     2206
     2207    // If we are a frameset just print with the layout we have onscreen, otherwise relayout
     2208    // according to the page width.
     2209    if (!frame->document() || !frame->document()->isFrameSet()) {
     2210        minLayoutWidth = shrinkToFit ? pageWidth * PrintingMinimumShrinkFactor : pageWidth;
     2211        maxLayoutWidth = shrinkToFit ? pageWidth * PrintingMaximumShrinkFactor : pageWidth;
     2212    }
     2213    [self _setPrinting:YES minimumPageWidth:minLayoutWidth maximumPageWidth:maxLayoutWidth adjustViewSize:YES];
     2214
     2215    return YES;
     2216}
     2217
     2218- (void)_endPrintMode
     2219{
     2220    [self _setPrinting:NO minimumPageWidth:0 maximumPageWidth:0 adjustViewSize:YES];
    22032221}
    22042222
     
    38293847}
    38303848
    3831 - (float)_availablePaperWidthForPrintOperation:(NSPrintOperation *)printOperation
    3832 {
    3833     NSPrintInfo *printInfo = [printOperation printInfo];
    3834     return [printInfo paperSize].width - [printInfo leftMargin] - [printInfo rightMargin];
    3835 }
    3836 
    38373849- (float)_scaleFactorForPrintOperation:(NSPrintOperation *)printOperation
    38383850{
     
    38453857    float userScaleFactor = [printOperation _web_pageSetupScaleFactor];
    38463858    float maxShrinkToFitScaleFactor = 1.0f / PrintingMaximumShrinkFactor;
    3847     float shrinkToFitScaleFactor = [self _availablePaperWidthForPrintOperation:printOperation]/viewWidth;
     3859    float shrinkToFitScaleFactor = [printOperation _web_availablePaperWidth] / viewWidth;
    38483860    float shrinkToAvoidOrphan = _private->avoidingPrintOrphan ? (1.0f / PrintingOrphanShrinkAdjustment) : 1.0f;
    38493861    return userScaleFactor * max(maxShrinkToFitScaleFactor, shrinkToFitScaleFactor) * shrinkToAvoidOrphan;
     
    38653877}
    38663878
    3867 - (void)_endPrintMode
    3868 {
    3869     [self _setPrinting:NO minimumPageWidth:0.0f maximumPageWidth:0.0f adjustViewSize:YES];
     3879- (void)_endPrintModeAndRestoreWindowAutodisplay
     3880{
     3881    [self _endPrintMode];
    38703882    [[self window] setAutodisplay:YES];
    38713883}
     
    38933905        // the print mode here.
    38943906        ASSERT(currentOperation == nil);
    3895         [self _endPrintMode];
     3907        [self _endPrintModeAndRestoreWindowAutodisplay];
    38963908    }
    38973909}
     
    39033915    // sheet was up, using printer fonts (and looking different).
    39043916    [self displayIfNeeded];
    3905     [[self window] setAutodisplay:NO];
    3906    
    3907     // If we are a frameset just print with the layout we have onscreen, otherwise relayout
    3908     // according to the paper size
    3909     float minLayoutWidth = 0.0f;
    3910     float maxLayoutWidth = 0.0f;
    3911     Frame* frame = core([self _frame]);
    3912     if (!frame)
     3917    [[self window] setAutodisplay:NO];   
     3918
     3919    NSPrintOperation *printOperation = [NSPrintOperation currentOperation];
     3920    if (![self _beginPrintModeWithPageWidth:[printOperation _web_availablePaperWidth] shrinkToFit:YES])
    39133921        return NO;
    3914     if (!frame->document() || !frame->document()->isFrameSet()) {
    3915         float paperWidth = [self _availablePaperWidthForPrintOperation:[NSPrintOperation currentOperation]];
    3916         minLayoutWidth = paperWidth * PrintingMinimumShrinkFactor;
    3917         maxLayoutWidth = paperWidth * PrintingMaximumShrinkFactor;
    3918     }
    3919     [self _setPrinting:YES minimumPageWidth:minLayoutWidth maximumPageWidth:maxLayoutWidth adjustViewSize:YES]; // will relayout
    3920     NSPrintOperation *printOperation = [NSPrintOperation currentOperation];
     3922
    39213923    // Certain types of errors, including invalid page ranges, can cause beginDocument and
    39223924    // endDocument to be skipped after we've put ourselves in print mode (see 4145905). In those cases
     
    39363938    float userScaleFactor = [printOperation _web_pageSetupScaleFactor];
    39373939    [_private->pageRects release];
    3938     float fullPageHeight = floorf([self _calculatePrintHeight]/totalScaleFactor);
    3939     NSArray *newPageRects = [[self _frame] _computePageRectsWithPrintWidthScaleFactor:userScaleFactor
    3940                                                                           printHeight:fullPageHeight];
     3940    float fullPageHeight = floorf([printOperation _web_availablePaperHeight] / totalScaleFactor);
     3941    WebFrame *frame = [self _frame];
     3942    NSArray *newPageRects = [frame _computePageRectsWithPrintWidthScaleFactor:userScaleFactor printHeight:fullPageHeight];
    39413943   
    39423944    // AppKit gets all messed up if you give it a zero-length page count (see 3576334), so if we
     
    39513953        float lastPageHeight = NSHeight([[newPageRects lastObject] rectValue]);
    39523954        if (lastPageHeight/fullPageHeight < LastPrintedPageOrphanRatio) {
    3953             NSArray *adjustedPageRects = [[self _frame] _computePageRectsWithPrintWidthScaleFactor:userScaleFactor
    3954                                                                                        printHeight:fullPageHeight*PrintingOrphanShrinkAdjustment];
     3955            NSArray *adjustedPageRects = [frame _computePageRectsWithPrintWidthScaleFactor:userScaleFactor printHeight:fullPageHeight * PrintingOrphanShrinkAdjustment];
    39553956            // Use the adjusted rects only if the page count went down
    39563957            if ([adjustedPageRects count] < [newPageRects count]) {
     
    39923993        // Exception during [super beginDocument] means that endDocument will not get called,
    39933994        // so we need to clean up our "print mode" here.
    3994         [self _endPrintMode];
     3995        [self _endPrintModeAndRestoreWindowAutodisplay];
    39953996    }
    39963997}
     
    40004001    [super endDocument];
    40014002    // Note sadly at this point [NSGraphicsContext currentContextDrawingToScreen] is still NO
    4002     [self _endPrintMode];
     4003    [self _endPrintModeAndRestoreWindowAutodisplay];
    40034004}
    40044005
  • trunk/WebKit/mac/WebView/WebHTMLViewPrivate.h

    r57190 r57246  
    132132- (void)_layoutForPrinting;
    133133- (WebCGFloat)_adjustedBottomOfPageWithTop:(WebCGFloat)top bottom:(WebCGFloat)bottom limit:(WebCGFloat)bottomLimit;
     134- (BOOL)_isInPrintMode;
     135- (BOOL)_beginPrintModeWithPageWidth:(float)pageWidth shrinkToFit:(BOOL)shrinkToFit;
     136- (void)_endPrintMode;
    134137
    135138- (BOOL)_canSmartReplaceWithPasteboard:(NSPasteboard *)pasteboard;
Note: See TracChangeset for help on using the changeset viewer.