Changeset 75955 in webkit


Ignore:
Timestamp:
Jan 17, 2011 10:31:12 AM (13 years ago)
Author:
ap@apple.com
Message:

Reviewed by Dan Bernstein.

https://bugs.webkit.org/show_bug.cgi?id=52495

No change in behavior, so no tests.

  • WebCore.exp.in: Export additional methods, as I'm going to use more code from PrintContext.
  • page/PrintContext.cpp: (WebCore::PrintContext::~PrintContext): No need to clear m_pageRects, the object is being destroyed already. (WebCore::PrintContext::pageCount): Changed page count from int to size_t. (WebCore::PrintContext::pageRect): Ditto. (WebCore::PrintContext::computePageRects): Pass allowHorizontalTiling as an argument. PrintContext already has code to calculate scale factor, so it makes sense to make methods that contain it universal (allowHorizontalTiling is always true for Safari). Round page height to an integer, because Mac code does that, and because page height is treated as integer almost everywhere else in code. (WebCore::PrintContext::begin): Allow calling this function multiple times. There is no need to return to screen mode if e.g. "print backgounds" option changes. (WebCore::PrintContext::computeAutomaticScaleFactor): Expose scale factor computation, so that clients don't have to copy it. (WebCore::PrintContext::spoolRect): Add a way to spool a precomputed rect - handy if a request comes from code that doesn't have page number. (WebCore::PrintContext::pageNumberForElement): Page number int -> size_t.
  • page/PrintContext.h: Added comments and FIXMEs. PrintContext needs cleanup, but that depends on deciding how it really needs to work (e.g. whether computePageRects() should cause relayout).
Location:
trunk/Source
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r75952 r75955  
     12011-01-17  Alexey Proskuryakov  <ap@apple.com>
     2
     3        Reviewed by Dan Bernstein.
     4
     5        https://bugs.webkit.org/show_bug.cgi?id=52495
     6
     7        No change in behavior, so no tests.
     8
     9        * WebCore.exp.in: Export additional methods, as I'm going to use more code from PrintContext.
     10
     11        * page/PrintContext.cpp:
     12        (WebCore::PrintContext::~PrintContext): No need to clear m_pageRects, the object is being
     13        destroyed already.
     14        (WebCore::PrintContext::pageCount): Changed page count from int to size_t.
     15        (WebCore::PrintContext::pageRect): Ditto.
     16        (WebCore::PrintContext::computePageRects): Pass allowHorizontalTiling as an argument.
     17        PrintContext already has code to calculate scale factor, so it makes sense to make methods
     18        that contain it universal (allowHorizontalTiling is always true for Safari).
     19        Round page height to an integer, because Mac code does that, and because page height is
     20        treated as integer almost everywhere else in code.
     21        (WebCore::PrintContext::begin): Allow calling this function multiple times. There is no need
     22        to return to screen mode if e.g. "print backgounds" option changes.
     23        (WebCore::PrintContext::computeAutomaticScaleFactor): Expose scale factor computation, so
     24        that clients don't have to copy it.
     25        (WebCore::PrintContext::spoolRect): Add a way to spool a precomputed rect - handy if a request
     26        comes from code that doesn't have page number.
     27        (WebCore::PrintContext::pageNumberForElement): Page number int -> size_t.
     28
     29        * page/PrintContext.h: Added comments and FIXMEs. PrintContext needs cleanup, but that
     30        depends on deciding how it really needs to work (e.g. whether computePageRects() should
     31        cause relayout).
     32
    1332011-01-17  Pavel Feldman  <pfeldman@chromium.org>
    234
  • trunk/Source/WebCore/WebCore.exp.in

    r75939 r75955  
    263263__ZN7WebCore12PrintContext12pagePropertyEPNS_5FrameEPKci
    264264__ZN7WebCore12PrintContext13numberOfPagesEPNS_5FrameERKNS_9FloatSizeE
     265__ZN7WebCore12PrintContext16computePageRectsERKNS_9FloatRectEfffRfb
    265266__ZN7WebCore12PrintContext16isPageBoxVisibleEPNS_5FrameEi
    266267__ZN7WebCore12PrintContext20pageNumberForElementEPNS_7ElementERKNS_9FloatSizeE
    267268__ZN7WebCore12PrintContext26pageSizeAndMarginsInPixelsEPNS_5FrameEiiiiiii
     269__ZN7WebCore12PrintContext27computeAutomaticScaleFactorEf
    268270__ZN7WebCore12PrintContext27spoolAllPagesWithBoundariesEPNS_5FrameERNS_15GraphicsContextERKNS_9FloatSizeE
    269271__ZN7WebCore12PrintContext28computePageRectsWithPageSizeERKNS_9FloatSizeEb
     272__ZN7WebCore12PrintContext3endEv
     273__ZN7WebCore12PrintContext5beginEff
     274__ZN7WebCore12PrintContext9spoolPageERNS_15GraphicsContextEif
     275__ZN7WebCore12PrintContext9spoolRectERNS_15GraphicsContextERKNS_7IntRectE
    270276__ZN7WebCore12PrintContextC1EPNS_5FrameE
    271277__ZN7WebCore12PrintContextD1Ev
  • trunk/Source/WebCore/page/PrintContext.cpp

    r74908 r75955  
    2929#include <wtf/text/StringConcatenate.h>
    3030
    31 using namespace WebCore;
    32 
    3331namespace WebCore {
     32
     33// By imaging to a width a little wider than the available pixels,
     34// thin pages will be scaled down a little, matching the way they
     35// print in IE and Camino. This lets them use fewer sheets than they
     36// would otherwise, which is presumably why other browsers do this.
     37// Wide pages will be scaled down more than this.
     38const float printingMinimumShrinkFactor = 1.25;
     39
     40// This number determines how small we are willing to reduce the page content
     41// in order to accommodate the widest line. If the page would have to be
     42// reduced smaller to make the widest line fit, we just clip instead (this
     43// behavior matches MacIE and Mozilla, at least)
     44const float printingMaximumShrinkFactor = 2;
    3445
    3546PrintContext::PrintContext(Frame* frame)
     
    4354    if (m_isPrinting)
    4455        end();
    45     m_pageRects.clear();
    46 }
    47 
    48 int PrintContext::pageCount() const
     56}
     57
     58size_t PrintContext::pageCount() const
    4959{
    5060    return m_pageRects.size();
    5161}
    5262
    53 const IntRect& PrintContext::pageRect(int pageNumber) const
     63const IntRect& PrintContext::pageRect(size_t pageNumber) const
    5464{
    5565    return m_pageRects[pageNumber];
    5666}
    5767
    58 void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight)
     68void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling)
    5969{
    6070    m_pageRects.clear();
     
    7484
    7585    float pageWidth  = view->docWidth();
    76     float pageHeight = pageWidth * ratio;
     86    float pageHeight = floorf(pageWidth * ratio);
    7787    outPageHeight = pageHeight; // this is the height of the page adjusted by margins
    7888    pageHeight -= headerHeight + footerHeight;
     
    8393    }
    8494
    85     computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), false);
    86 }
    87 
    88 void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalMultiPages)
     95    computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), allowHorizontalTiling);
     96}
     97
     98void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling)
    8999{
    90100    m_pageRects.clear();
    91     computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalMultiPages);
    92 }
    93 
    94 void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowHorizontalMultiPages)
     101    computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalTiling);
     102}
     103
     104void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling)
    95105{
    96106    if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
     
    106116    unsigned pageCount = ceilf((float)docRect.height() / pageHeight);
    107117    for (unsigned i = 0; i < pageCount; ++i) {
    108         if (allowHorizontalMultiPages) {
     118        if (allowHorizontalTiling) {
    109119            for (int currentX = docRect.x(); currentX < docRect.right(); currentX += pageWidth)
    110120                m_pageRects.append(IntRect(currentX, docRect.y() + i * pageHeight, pageWidth, pageHeight));
     
    116126void PrintContext::begin(float width, float height)
    117127{
    118     ASSERT(!m_isPrinting);
     128    // This function can be called multiple times to adjust printing parameters without going back to screen mode.
    119129    m_isPrinting = true;
    120130
    121     // By imaging to a width a little wider than the available pixels,
    122     // thin pages will be scaled down a little, matching the way they
    123     // print in IE and Camino. This lets them use fewer sheets than they
    124     // would otherwise, which is presumably why other browsers do this.
    125     // Wide pages will be scaled down more than this.
    126     const float PrintingMinimumShrinkFactor = 1.25f;
    127 
    128     // This number determines how small we are willing to reduce the page content
    129     // in order to accommodate the widest line. If the page would have to be
    130     // reduced smaller to make the widest line fit, we just clip instead (this
    131     // behavior matches MacIE and Mozilla, at least)
    132     const float PrintingMaximumShrinkFactor = 2.0f;
    133 
    134     float minLayoutWidth = width * PrintingMinimumShrinkFactor;
    135     float minLayoutHeight = height * PrintingMinimumShrinkFactor;
    136 
    137     // FIXME: This will modify the rendering of the on-screen frame.
    138     // Could lead to flicker during printing.
    139     m_frame->setPrinting(true, FloatSize(minLayoutWidth, minLayoutHeight), PrintingMaximumShrinkFactor / PrintingMinimumShrinkFactor, Frame::AdjustViewSize);
     131    float minLayoutWidth = width * printingMinimumShrinkFactor;
     132    float minLayoutHeight = height * printingMinimumShrinkFactor;
     133
     134    // This changes layout, so callers need to make sure that they don't paint to screen while in printing mode.
     135    m_frame->setPrinting(true, FloatSize(minLayoutWidth, minLayoutHeight), printingMaximumShrinkFactor / printingMinimumShrinkFactor, Frame::AdjustViewSize);
     136}
     137
     138float PrintContext::computeAutomaticScaleFactor(float availablePaperWidth)
     139{
     140    if (!m_frame->view())
     141        return 1;
     142
     143    float viewWidth = m_frame->view()->contentsWidth();
     144    if (viewWidth < 1)
     145        return 1;
     146
     147    float maxShrinkToFitScaleFactor = 1 / printingMaximumShrinkFactor;
     148    float shrinkToFitScaleFactor = availablePaperWidth / viewWidth;
     149    return max(maxShrinkToFitScaleFactor, shrinkToFitScaleFactor);
    140150}
    141151
     
    150160    ctx.clip(pageRect);
    151161    m_frame->view()->paintContents(&ctx, pageRect);
     162    ctx.restore();
     163}
     164
     165void PrintContext::spoolRect(GraphicsContext& ctx, const IntRect& rect)
     166{
     167    ctx.save();
     168    ctx.scale(FloatSize(1, -1));
     169    ctx.translate(0, -rect.height());
     170    ctx.clip(rect);
     171    m_frame->view()->paintContents(&ctx, rect);
    152172    ctx.restore();
    153173}
     
    190210    int top = box->offsetTop();
    191211    int left = box->offsetLeft();
    192     int pageNumber = 0;
     212    size_t pageNumber = 0;
    193213    for (; pageNumber < printContext.pageCount(); pageNumber++) {
    194214        const IntRect& page = printContext.pageRect(pageNumber);
  • trunk/Source/WebCore/page/PrintContext.h

    r65021 r75955  
    3939    ~PrintContext();
    4040
    41     int pageCount() const;
    42     const IntRect& pageRect(int pageNumber) const;
     41    Frame* frame() const { return m_frame; }
     42
     43    // Break up a page into rects without relayout.
     44    // FIXME: This means that CSS page breaks won't be on page boundary if the size is different than what was passed to begin(). That's probably not always desirable.
     45    // FIXME: Header and footer height should be applied before layout, not after.
     46    // FIXME: The printRect argument is only used to determine page aspect ratio, it would be better to pass a FloatSize with page dimensions instead.
     47    void computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling = false);
     48
     49    // Deprecated. Page size computation is already in this class, clients shouldn't be copying it.
     50    void computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling);
     51
     52    // These are only valid after page rects are computed.
     53    size_t pageCount() const;
     54    const IntRect& pageRect(size_t pageNumber) const;
    4355    const Vector<IntRect>& pageRects() const { return m_pageRects; }
    4456
    45     void computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight);
    46     void computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalMultiPages);
     57    float computeAutomaticScaleFactor(float availablePaperWidth);
    4758
    48     // TODO: eliminate width param
     59    // Enter print mode, updating layout for new page size.
     60    // This function can be called multiple times to apply new print options without going back to screen mode.
    4961    void begin(float width, float height = 0);
    5062
    51     // TODO: eliminate width param
     63    // FIXME: eliminate width argument.
    5264    void spoolPage(GraphicsContext& ctx, int pageNumber, float width);
    5365
     66    void spoolRect(GraphicsContext& ctx, const IntRect&);
     67
     68    // Return to screen mode.
    5469    void end();
    5570
    5671    // Used by layout tests.
    57     static int pageNumberForElement(Element*, const FloatSize& pageSizeInPixels);
     72    static int pageNumberForElement(Element*, const FloatSize& pageSizeInPixels); // Returns -1 if page isn't found.
    5873    static String pageProperty(Frame* frame, const char* propertyName, int pageNumber);
    5974    static bool isPageBoxVisible(Frame* frame, int pageNumber);
     
    7085
    7186private:
    72     void computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowHorizontalMultiPages);
     87    void computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling);
    7388
    7489    // Used to prevent misuses of begin() and end() (e.g., call end without begin).
  • trunk/Source/WebKit/qt/Api/qwebframe.cpp

    r73865 r75955  
    14551455    // paranoia check
    14561456    fromPage = qMax(1, fromPage);
    1457     toPage = qMin(printContext.pageCount(), toPage);
     1457    toPage = qMin(static_cast<int>(printContext.pageCount()), toPage);
    14581458    if (toPage < fromPage) {
    14591459        // if the user entered a page range outside the actual number
  • trunk/Source/WebKit/qt/ChangeLog

    r75944 r75955  
     12011-01-17  Alexey Proskuryakov  <ap@apple.com>
     2
     3        https://bugs.webkit.org/show_bug.cgi?id=52495
     4
     5        * Api/qwebframe.cpp: (QWebFrame::print): Avoid breaking the build due to type mismatch.
     6        Longer term, this code should be changed to use an unsigned type for page count.
     7
    182011-01-17  Yi Shen  <yi.4.shen@nokia.com>
    29
Note: See TracChangeset for help on using the changeset viewer.