initial import
[vuplus_webkit] / Source / WebCore / page / PrintContext.cpp
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 "RenderLayer.h"
28 #include "RenderView.h"
29 #include <wtf/text/WTFString.h>
30
31 namespace 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.
38 const float printingMinimumShrinkFactor = 1.25f;
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)
44 const float printingMaximumShrinkFactor = 2;
45
46 PrintContext::PrintContext(Frame* frame)
47     : m_frame(frame)
48     , m_isPrinting(false)
49 {
50 }
51
52 PrintContext::~PrintContext()
53 {
54     if (m_isPrinting)
55         end();
56 }
57
58 void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling)
59 {
60     m_pageRects.clear();
61     outPageHeight = 0;
62
63     if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
64         return;
65
66     if (userScaleFactor <= 0) {
67         LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
68         return;
69     }
70
71     RenderView* view = toRenderView(m_frame->document()->renderer());
72     const IntRect& documentRect = view->documentRect();
73     FloatSize pageSize = m_frame->resizePageRectsKeepingRatio(FloatSize(printRect.width(), printRect.height()), FloatSize(documentRect.width(), documentRect.height()));
74     float pageWidth = pageSize.width();
75     float pageHeight = pageSize.height();
76
77     outPageHeight = pageHeight; // this is the height of the page adjusted by margins
78     pageHeight -= headerHeight + footerHeight;
79
80     if (pageHeight <= 0) {
81         LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
82         return;
83     }
84
85     computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), allowHorizontalTiling);
86 }
87
88 void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling)
89 {
90     m_pageRects.clear();
91     computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalTiling);
92 }
93
94 void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowInlineDirectionTiling)
95 {
96     if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
97         return;
98
99     RenderView* view = toRenderView(m_frame->document()->renderer());
100
101     IntRect docRect = view->documentRect();
102
103     int pageWidth = pageSizeInPixels.width();
104     int pageHeight = pageSizeInPixels.height();
105
106     bool isHorizontal = view->style()->isHorizontalWritingMode();
107
108     int docLogicalHeight = isHorizontal ? docRect.height() : docRect.width();
109     int pageLogicalHeight = isHorizontal ? pageHeight : pageWidth;
110     int pageLogicalWidth = isHorizontal ? pageWidth : pageHeight;
111
112     int inlineDirectionStart;
113     int inlineDirectionEnd;
114     int blockDirectionStart;
115     int blockDirectionEnd;
116     if (isHorizontal) {
117         if (view->style()->isFlippedBlocksWritingMode()) {
118             blockDirectionStart = docRect.maxY();
119             blockDirectionEnd = docRect.y();
120         } else {
121             blockDirectionStart = docRect.y();
122             blockDirectionEnd = docRect.maxY();
123         }
124         inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.x() : docRect.maxX();
125         inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxX() : docRect.x();
126     } else {
127         if (view->style()->isFlippedBlocksWritingMode()) {
128             blockDirectionStart = docRect.maxX();
129             blockDirectionEnd = docRect.x();
130         } else {
131             blockDirectionStart = docRect.x();
132             blockDirectionEnd = docRect.maxX();
133         }
134         inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.y() : docRect.maxY();
135         inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxY() : docRect.y();
136     }
137
138     unsigned pageCount = ceilf((float)docLogicalHeight / pageLogicalHeight);
139     for (unsigned i = 0; i < pageCount; ++i) {
140         int pageLogicalTop = blockDirectionEnd > blockDirectionStart ?
141                                 blockDirectionStart + i * pageLogicalHeight : 
142                                 blockDirectionStart - (i + 1) * pageLogicalHeight;
143         if (allowInlineDirectionTiling) {
144             for (int currentInlinePosition = inlineDirectionStart;
145                  inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition < inlineDirectionEnd : currentInlinePosition > inlineDirectionEnd;
146                  currentInlinePosition += (inlineDirectionEnd > inlineDirectionStart ? pageLogicalWidth : -pageLogicalWidth)) {
147                 int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition : currentInlinePosition - pageLogicalWidth;
148                 IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight);
149                 if (!isHorizontal)
150                     pageRect = pageRect.transposedRect();
151                 m_pageRects.append(pageRect);
152             }
153         } else {
154             int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? inlineDirectionStart : inlineDirectionStart - pageLogicalWidth;
155             IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight);
156             if (!isHorizontal)
157                 pageRect = pageRect.transposedRect();
158             m_pageRects.append(pageRect);
159         }
160     }
161 }
162
163 void PrintContext::begin(float width, float height)
164 {
165     // This function can be called multiple times to adjust printing parameters without going back to screen mode.
166     m_isPrinting = true;
167
168     FloatSize minLayoutSize = m_frame->resizePageRectsKeepingRatio(FloatSize(width, height), FloatSize(width * printingMinimumShrinkFactor, height * printingMinimumShrinkFactor));
169
170     // This changes layout, so callers need to make sure that they don't paint to screen while in printing mode.
171     m_frame->setPrinting(true, minLayoutSize, printingMaximumShrinkFactor / printingMinimumShrinkFactor, AdjustViewSize);
172 }
173
174 float PrintContext::computeAutomaticScaleFactor(const FloatSize& availablePaperSize)
175 {
176     if (!m_frame->view())
177         return 1;
178
179     bool useViewWidth = true;
180     if (m_frame->document() && m_frame->document()->renderView())
181         useViewWidth = m_frame->document()->renderView()->style()->isHorizontalWritingMode();
182
183     float viewLogicalWidth = useViewWidth ? m_frame->view()->contentsWidth() : m_frame->view()->contentsHeight();
184     if (viewLogicalWidth < 1)
185         return 1;
186
187     float maxShrinkToFitScaleFactor = 1 / printingMaximumShrinkFactor;
188     float shrinkToFitScaleFactor = (useViewWidth ? availablePaperSize.width() : availablePaperSize.height()) / viewLogicalWidth;
189     return max(maxShrinkToFitScaleFactor, shrinkToFitScaleFactor);
190 }
191
192 void PrintContext::spoolPage(GraphicsContext& ctx, int pageNumber, float width)
193 {
194     // FIXME: Not correct for vertical text.
195     IntRect pageRect = m_pageRects[pageNumber];
196     float scale = width / pageRect.width();
197
198     ctx.save();
199     ctx.scale(FloatSize(scale, scale));
200     ctx.translate(-pageRect.x(), -pageRect.y());
201     ctx.clip(pageRect);
202     m_frame->view()->paintContents(&ctx, pageRect);
203     ctx.restore();
204 }
205
206 void PrintContext::spoolRect(GraphicsContext& ctx, const IntRect& rect)
207 {
208     // FIXME: Not correct for vertical text.
209     ctx.save();
210     ctx.translate(-rect.x(), -rect.y());
211     ctx.clip(rect);
212     m_frame->view()->paintContents(&ctx, rect);
213     ctx.restore();
214 }
215
216 void PrintContext::end()
217 {
218     ASSERT(m_isPrinting);
219     m_isPrinting = false;
220     m_frame->setPrinting(false, FloatSize(), 0, AdjustViewSize);
221 }
222
223 static RenderBoxModelObject* enclosingBoxModelObject(RenderObject* object)
224 {
225
226     while (object && !object->isBoxModelObject())
227         object = object->parent();
228     if (!object)
229         return 0;
230     return toRenderBoxModelObject(object);
231 }
232
233 int PrintContext::pageNumberForElement(Element* element, const FloatSize& pageSizeInPixels)
234 {
235     // Make sure the element is not freed during the layout.
236     RefPtr<Element> elementRef(element);
237     element->document()->updateLayout();
238
239     RenderBoxModelObject* box = enclosingBoxModelObject(element->renderer());
240     if (!box)
241         return -1;
242
243     Frame* frame = element->document()->frame();
244     FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels);
245     PrintContext printContext(frame);
246     printContext.begin(pageRect.width(), pageRect.height());
247     FloatSize scaledPageSize = pageSizeInPixels;
248     scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width());
249     printContext.computePageRectsWithPageSize(scaledPageSize, false);
250
251     int top = box->offsetTop();
252     int left = box->offsetLeft();
253     size_t pageNumber = 0;
254     for (; pageNumber < printContext.pageCount(); pageNumber++) {
255         const IntRect& page = printContext.pageRect(pageNumber);
256         if (page.x() <= left && left < page.maxX() && page.y() <= top && top < page.maxY())
257             return pageNumber;
258     }
259     return -1;
260 }
261
262 String PrintContext::pageProperty(Frame* frame, const char* propertyName, int pageNumber)
263 {
264     Document* document = frame->document();
265     PrintContext printContext(frame);
266     printContext.begin(800); // Any width is OK here.
267     document->updateLayout();
268     RefPtr<RenderStyle> style = document->styleForPage(pageNumber);
269
270     // Implement formatters for properties we care about.
271     if (!strcmp(propertyName, "margin-left")) {
272         if (style->marginLeft().isAuto())
273             return String("auto");
274         return String::number(style->marginLeft().value());
275     }
276     if (!strcmp(propertyName, "line-height"))
277         return String::number(style->lineHeight().value());
278     if (!strcmp(propertyName, "font-size"))
279         return String::number(style->fontDescription().computedPixelSize());
280     if (!strcmp(propertyName, "font-family"))
281         return style->fontDescription().family().family().string();
282     if (!strcmp(propertyName, "size"))
283         return String::number(style->pageSize().width().value()) + ' ' + String::number(style->pageSize().height().value());
284
285     return String("pageProperty() unimplemented for: ") + propertyName;
286 }
287
288 bool PrintContext::isPageBoxVisible(Frame* frame, int pageNumber)
289 {
290     return frame->document()->isPageBoxVisible(pageNumber);
291 }
292
293 String PrintContext::pageSizeAndMarginsInPixels(Frame* frame, int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
294 {
295     IntSize pageSize(width, height);
296     frame->document()->pageSizeAndMarginsInPixels(pageNumber, pageSize, marginTop, marginRight, marginBottom, marginLeft);
297
298     return "(" + String::number(pageSize.width()) + ", " + String::number(pageSize.height()) + ") " +
299            String::number(marginTop) + ' ' + String::number(marginRight) + ' ' + String::number(marginBottom) + ' ' + String::number(marginLeft);
300 }
301
302 int PrintContext::numberOfPages(Frame* frame, const FloatSize& pageSizeInPixels)
303 {
304     frame->document()->updateLayout();
305
306     FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels);
307     PrintContext printContext(frame);
308     printContext.begin(pageRect.width(), pageRect.height());
309     // Account for shrink-to-fit.
310     FloatSize scaledPageSize = pageSizeInPixels;
311     scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width());
312     printContext.computePageRectsWithPageSize(scaledPageSize, false);
313     return printContext.pageCount();
314 }
315
316 void PrintContext::spoolAllPagesWithBoundaries(Frame* frame, GraphicsContext& graphicsContext, const FloatSize& pageSizeInPixels)
317 {
318     if (!frame->document() || !frame->view() || !frame->document()->renderer())
319         return;
320
321     frame->document()->updateLayout();
322
323     PrintContext printContext(frame);
324     printContext.begin(pageSizeInPixels.width(), pageSizeInPixels.height());
325
326     float pageHeight;
327     printContext.computePageRects(FloatRect(FloatPoint(0, 0), pageSizeInPixels), 0, 0, 1, pageHeight);
328
329     const float pageWidth = pageSizeInPixels.width();
330     const Vector<IntRect>& pageRects = printContext.pageRects();
331     int totalHeight = pageRects.size() * (pageSizeInPixels.height() + 1) - 1;
332
333     // Fill the whole background by white.
334     graphicsContext.setFillColor(Color(255, 255, 255), ColorSpaceDeviceRGB);
335     graphicsContext.fillRect(FloatRect(0, 0, pageWidth, totalHeight));
336
337     graphicsContext.save();
338     graphicsContext.translate(0, totalHeight);
339     graphicsContext.scale(FloatSize(1, -1));
340
341     int currentHeight = 0;
342     for (size_t pageIndex = 0; pageIndex < pageRects.size(); pageIndex++) {
343         // Draw a line for a page boundary if this isn't the first page.
344         if (pageIndex > 0) {
345             graphicsContext.save();
346             graphicsContext.setStrokeColor(Color(0, 0, 255), ColorSpaceDeviceRGB);
347             graphicsContext.setFillColor(Color(0, 0, 255), ColorSpaceDeviceRGB);
348             graphicsContext.drawLine(IntPoint(0, currentHeight),
349                                      IntPoint(pageWidth, currentHeight));
350             graphicsContext.restore();
351         }
352
353         graphicsContext.save();
354         graphicsContext.translate(0, currentHeight);
355         printContext.spoolPage(graphicsContext, pageIndex, pageWidth);
356         graphicsContext.restore();
357
358         currentHeight += pageSizeInPixels.height() + 1;
359     }
360
361     graphicsContext.restore();
362 }
363
364 }