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#pragma once
22
23#include "FrameDestructionObserver.h"
24#include "LengthBox.h"
25#include <wtf/Forward.h>
26#include <wtf/HashMap.h>
27#include <wtf/Vector.h>
28#include <wtf/text/WTFString.h>
29
30namespace WebCore {
31
32class Document;
33class Element;
34class Frame;
35class FloatRect;
36class FloatSize;
37class GraphicsContext;
38class IntRect;
39class Node;
40
41class PrintContext : public FrameDestructionObserver {
42 WTF_MAKE_FAST_ALLOCATED;
43public:
44 WEBCORE_EXPORT explicit PrintContext(Frame*);
45 WEBCORE_EXPORT ~PrintContext();
46
47 // Break up a page into rects without relayout.
48 // 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.
49 // FIXME: Header and footer height should be applied before layout, not after.
50 // FIXME: The printRect argument is only used to determine page aspect ratio, it would be better to pass a FloatSize with page dimensions instead.
51 WEBCORE_EXPORT void computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling = false);
52
53 // Deprecated. Page size computation is already in this class, clients shouldn't be copying it.
54 WEBCORE_EXPORT void computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling);
55
56 // These are only valid after page rects are computed.
57 size_t pageCount() const { return m_pageRects.size(); }
58 const IntRect& pageRect(size_t pageNumber) const { return m_pageRects[pageNumber]; }
59 const Vector<IntRect>& pageRects() const { return m_pageRects; }
60 WEBCORE_EXPORT FloatBoxExtent computedPageMargin(FloatBoxExtent printMargin);
61 WEBCORE_EXPORT FloatSize computedPageSize(FloatSize pageSize, FloatBoxExtent printMargin);
62
63 WEBCORE_EXPORT float computeAutomaticScaleFactor(const FloatSize& availablePaperSize);
64
65 // Enter print mode, updating layout for new page size.
66 // This function can be called multiple times to apply new print options without going back to screen mode.
67 WEBCORE_EXPORT void begin(float width, float height = 0);
68
69 // FIXME: eliminate width argument.
70 WEBCORE_EXPORT void spoolPage(GraphicsContext& ctx, int pageNumber, float width);
71
72 WEBCORE_EXPORT void spoolRect(GraphicsContext& ctx, const IntRect&);
73
74 // Return to screen mode.
75 WEBCORE_EXPORT void end();
76
77 // Used by layout tests.
78 WEBCORE_EXPORT static int pageNumberForElement(Element*, const FloatSize& pageSizeInPixels); // Returns -1 if page isn't found.
79 WEBCORE_EXPORT static String pageProperty(Frame*, const char* propertyName, int pageNumber);
80 WEBCORE_EXPORT static bool isPageBoxVisible(Frame*, int pageNumber);
81 WEBCORE_EXPORT static String pageSizeAndMarginsInPixels(Frame*, int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft);
82 WEBCORE_EXPORT static int numberOfPages(Frame&, const FloatSize& pageSizeInPixels);
83 // Draw all pages into a graphics context with lines which mean page boundaries.
84 // The height of the graphics context should be
85 // (pageSizeInPixels.height() + 1) * number-of-pages - 1
86 WEBCORE_EXPORT static void spoolAllPagesWithBoundaries(Frame&, GraphicsContext&, const FloatSize& pageSizeInPixels);
87
88 // By imaging to a width a little wider than the available pixels,
89 // thin pages will be scaled down a little, matching the way they
90 // print in IE and Camino. This lets them use fewer sheets than they
91 // would otherwise, which is presumably why other browsers do this.
92 // Wide pages will be scaled down more than this.
93 static constexpr float minimumShrinkFactor() { return 1.25; }
94
95 // This number determines how small we are willing to reduce the page content
96 // in order to accommodate the widest line. If the page would have to be
97 // reduced smaller to make the widest line fit, we just clip instead (this
98 // behavior matches MacIE and Mozilla, at least)
99 static constexpr float maximumShrinkFactor() { return 2; }
100
101protected:
102 Vector<IntRect> m_pageRects;
103
104private:
105 void computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling);
106 bool beginAndComputePageRectsWithPageSize(Frame&, const FloatSize& pageSizeInPixels);
107 void collectLinkedDestinations(Document&);
108 void outputLinkedDestinations(GraphicsContext&, Document&, const IntRect& pageRect);
109
110 // Used to prevent misuses of begin() and end() (e.g., call end without begin).
111 bool m_isPrinting { false };
112
113 std::unique_ptr<HashMap<String, Ref<Element>>> m_linkedDestinations;
114};
115
116} // namespace WebCore
117