1/*
2 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
3 * Copyright (C) 2007 Apple Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef PrintContext_h
22#define PrintContext_h
23
24#include "wtf/Forward.h"
25#include "wtf/HashMap.h"
26#include "wtf/Vector.h"
27#include "wtf/text/WTFString.h"
28
29namespace WebCore {
30
31class Element;
32class Frame;
33class FloatRect;
34class FloatSize;
35class GraphicsContext;
36class IntRect;
37class Node;
38
39class PrintContext {
40public:
41    explicit PrintContext(Frame*);
42    ~PrintContext();
43
44    Frame* frame() const { return m_frame; }
45
46    // Break up a page into rects without relayout.
47    // 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.
48    // FIXME: Header and footer height should be applied before layout, not after.
49    // FIXME: The printRect argument is only used to determine page aspect ratio, it would be better to pass a FloatSize with page dimensions instead.
50    void computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling = false);
51
52    // Deprecated. Page size computation is already in this class, clients shouldn't be copying it.
53    void computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling);
54
55    // These are only valid after page rects are computed.
56    size_t pageCount() const { return m_pageRects.size(); }
57    const IntRect& pageRect(size_t pageNumber) const { return m_pageRects[pageNumber]; }
58    const Vector<IntRect>& pageRects() const { return m_pageRects; }
59
60    float computeAutomaticScaleFactor(const FloatSize& availablePaperSize);
61
62    // Enter print mode, updating layout for new page size.
63    // This function can be called multiple times to apply new print options without going back to screen mode.
64    void begin(float width, float height = 0);
65
66    // FIXME: eliminate width argument.
67    void spoolPage(GraphicsContext& ctx, int pageNumber, float width);
68
69    void spoolRect(GraphicsContext& ctx, const IntRect&);
70
71    // Return to screen mode.
72    void end();
73
74    // Used by layout tests.
75    static int pageNumberForElement(Element*, const FloatSize& pageSizeInPixels); // Returns -1 if page isn't found.
76    static String pageProperty(Frame* frame, const char* propertyName, int pageNumber);
77    static bool isPageBoxVisible(Frame* frame, int pageNumber);
78    static String pageSizeAndMarginsInPixels(Frame* frame, int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft);
79    static int numberOfPages(Frame*, const FloatSize& pageSizeInPixels);
80    // Draw all pages into a graphics context with lines which mean page boundaries.
81    // The height of the graphics context should be
82    // (pageSizeInPixels.height() + 1) * number-of-pages - 1
83    static void spoolAllPagesWithBoundaries(Frame*, GraphicsContext&, const FloatSize& pageSizeInPixels);
84
85protected:
86    void outputLinkedDestinations(GraphicsContext&, Node*, const IntRect& pageRect);
87
88    Frame* m_frame;
89    Vector<IntRect> m_pageRects;
90
91private:
92    void computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling);
93    void collectLinkedDestinations(Node*);
94
95    // Used to prevent misuses of begin() and end() (e.g., call end without begin).
96    bool m_isPrinting;
97
98    HashMap<String, Element*> m_linkedDestinations;
99    bool m_linkedDestinationsValid;
100};
101
102}
103
104#endif
105