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 #include "config.h"
22 #include "PrintContext.h"
23
24 #include "GraphicsContext.h"
25 #include "Frame.h"
26 #include "FrameView.h"
27 #include "RenderView.h"
28
29 using namespace WebCore;
30
31 namespace WebCore {
32
PrintContext(Frame * frame)33 PrintContext::PrintContext(Frame* frame)
34 : m_frame(frame)
35 {
36 }
37
~PrintContext()38 PrintContext::~PrintContext()
39 {
40 m_pageRects.clear();
41 }
42
pageCount() const43 int PrintContext::pageCount() const
44 {
45 return m_pageRects.size();
46 }
47
computePageRects(const FloatRect & printRect,float headerHeight,float footerHeight,float userScaleFactor,float & outPageHeight)48 void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight)
49 {
50 m_pageRects.clear();
51 outPageHeight = 0;
52
53 if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
54 return;
55
56 RenderView* root = toRenderView(m_frame->document()->renderer());
57
58 if (!root) {
59 LOG_ERROR("document to be printed has no renderer");
60 return;
61 }
62
63 if (userScaleFactor <= 0) {
64 LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
65 return;
66 }
67
68 float ratio = printRect.height() / printRect.width();
69
70 float pageWidth = (float)root->overflowWidth();
71 float pageHeight = pageWidth * ratio;
72 outPageHeight = pageHeight; // this is the height of the page adjusted by margins
73 pageHeight -= headerHeight + footerHeight;
74
75 if (pageHeight <= 0) {
76 LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
77 return;
78 }
79
80 float currPageHeight = pageHeight / userScaleFactor;
81 float docHeight = root->layer()->height();
82 float currPageWidth = pageWidth / userScaleFactor;
83
84 // always return at least one page, since empty files should print a blank page
85 float printedPagesHeight = 0.0;
86 do {
87 float proposedBottom = std::min(docHeight, printedPagesHeight + pageHeight);
88 m_frame->view()->adjustPageHeight(&proposedBottom, printedPagesHeight, proposedBottom, printedPagesHeight);
89 currPageHeight = max(1.0f, proposedBottom - printedPagesHeight);
90
91 m_pageRects.append(IntRect(0, (int)printedPagesHeight, (int)currPageWidth, (int)currPageHeight));
92 printedPagesHeight += currPageHeight;
93 } while (printedPagesHeight < docHeight);
94 }
95
begin(float width)96 void PrintContext::begin(float width)
97 {
98 // By imaging to a width a little wider than the available pixels,
99 // thin pages will be scaled down a little, matching the way they
100 // print in IE and Camino. This lets them use fewer sheets than they
101 // would otherwise, which is presumably why other browsers do this.
102 // Wide pages will be scaled down more than this.
103 const float PrintingMinimumShrinkFactor = 1.25f;
104
105 // This number determines how small we are willing to reduce the page content
106 // in order to accommodate the widest line. If the page would have to be
107 // reduced smaller to make the widest line fit, we just clip instead (this
108 // behavior matches MacIE and Mozilla, at least)
109 const float PrintingMaximumShrinkFactor = 2.0f;
110
111 float minLayoutWidth = width * PrintingMinimumShrinkFactor;
112 float maxLayoutWidth = width * PrintingMaximumShrinkFactor;
113
114 // FIXME: This will modify the rendering of the on-screen frame.
115 // Could lead to flicker during printing.
116 m_frame->setPrinting(true, minLayoutWidth, maxLayoutWidth, true);
117 }
118
spoolPage(GraphicsContext & ctx,int pageNumber,float width)119 void PrintContext::spoolPage(GraphicsContext& ctx, int pageNumber, float width)
120 {
121 IntRect pageRect = m_pageRects[pageNumber];
122 float scale = width / pageRect.width();
123
124 ctx.save();
125 ctx.scale(FloatSize(scale, scale));
126 ctx.translate(-pageRect.x(), -pageRect.y());
127 ctx.clip(pageRect);
128 m_frame->view()->paintContents(&ctx, pageRect);
129 ctx.restore();
130 }
131
end()132 void PrintContext::end()
133 {
134 m_frame->setPrinting(false, 0, 0, true);
135 }
136
137 }
138