2 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
3 * Copyright (C) 2007 Apple Inc.
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.
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.
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.
22 #include "PrintContext.h"
24 #include "GraphicsContext.h"
26 #include "FrameView.h"
27 #include "RenderLayer.h"
28 #include "RenderView.h"
29 #include <wtf/text/WTFString.h>
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;
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;
46 PrintContext::PrintContext(Frame* frame)
52 PrintContext::~PrintContext()
58 void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling)
63 if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
66 if (userScaleFactor <= 0) {
67 LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
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();
77 outPageHeight = pageHeight; // this is the height of the page adjusted by margins
78 pageHeight -= headerHeight + footerHeight;
80 if (pageHeight <= 0) {
81 LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
85 computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), allowHorizontalTiling);
88 void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling)
91 computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalTiling);
94 void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowInlineDirectionTiling)
96 if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
99 RenderView* view = toRenderView(m_frame->document()->renderer());
101 IntRect docRect = view->documentRect();
103 int pageWidth = pageSizeInPixels.width();
104 int pageHeight = pageSizeInPixels.height();
106 bool isHorizontal = view->style()->isHorizontalWritingMode();
108 int docLogicalHeight = isHorizontal ? docRect.height() : docRect.width();
109 int pageLogicalHeight = isHorizontal ? pageHeight : pageWidth;
110 int pageLogicalWidth = isHorizontal ? pageWidth : pageHeight;
112 int inlineDirectionStart;
113 int inlineDirectionEnd;
114 int blockDirectionStart;
115 int blockDirectionEnd;
117 if (view->style()->isFlippedBlocksWritingMode()) {
118 blockDirectionStart = docRect.maxY();
119 blockDirectionEnd = docRect.y();
121 blockDirectionStart = docRect.y();
122 blockDirectionEnd = docRect.maxY();
124 inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.x() : docRect.maxX();
125 inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxX() : docRect.x();
127 if (view->style()->isFlippedBlocksWritingMode()) {
128 blockDirectionStart = docRect.maxX();
129 blockDirectionEnd = docRect.x();
131 blockDirectionStart = docRect.x();
132 blockDirectionEnd = docRect.maxX();
134 inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.y() : docRect.maxY();
135 inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxY() : docRect.y();
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);
150 pageRect = pageRect.transposedRect();
151 m_pageRects.append(pageRect);
154 int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? inlineDirectionStart : inlineDirectionStart - pageLogicalWidth;
155 IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight);
157 pageRect = pageRect.transposedRect();
158 m_pageRects.append(pageRect);
163 void PrintContext::begin(float width, float height)
165 // This function can be called multiple times to adjust printing parameters without going back to screen mode.
168 FloatSize minLayoutSize = m_frame->resizePageRectsKeepingRatio(FloatSize(width, height), FloatSize(width * printingMinimumShrinkFactor, height * printingMinimumShrinkFactor));
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);
174 float PrintContext::computeAutomaticScaleFactor(const FloatSize& availablePaperSize)
176 if (!m_frame->view())
179 bool useViewWidth = true;
180 if (m_frame->document() && m_frame->document()->renderView())
181 useViewWidth = m_frame->document()->renderView()->style()->isHorizontalWritingMode();
183 float viewLogicalWidth = useViewWidth ? m_frame->view()->contentsWidth() : m_frame->view()->contentsHeight();
184 if (viewLogicalWidth < 1)
187 float maxShrinkToFitScaleFactor = 1 / printingMaximumShrinkFactor;
188 float shrinkToFitScaleFactor = (useViewWidth ? availablePaperSize.width() : availablePaperSize.height()) / viewLogicalWidth;
189 return max(maxShrinkToFitScaleFactor, shrinkToFitScaleFactor);
192 void PrintContext::spoolPage(GraphicsContext& ctx, int pageNumber, float width)
194 // FIXME: Not correct for vertical text.
195 IntRect pageRect = m_pageRects[pageNumber];
196 float scale = width / pageRect.width();
199 ctx.scale(FloatSize(scale, scale));
200 ctx.translate(-pageRect.x(), -pageRect.y());
202 m_frame->view()->paintContents(&ctx, pageRect);
206 void PrintContext::spoolRect(GraphicsContext& ctx, const IntRect& rect)
208 // FIXME: Not correct for vertical text.
210 ctx.translate(-rect.x(), -rect.y());
212 m_frame->view()->paintContents(&ctx, rect);
216 void PrintContext::end()
218 ASSERT(m_isPrinting);
219 m_isPrinting = false;
220 m_frame->setPrinting(false, FloatSize(), 0, AdjustViewSize);
223 static RenderBoxModelObject* enclosingBoxModelObject(RenderObject* object)
226 while (object && !object->isBoxModelObject())
227 object = object->parent();
230 return toRenderBoxModelObject(object);
233 int PrintContext::pageNumberForElement(Element* element, const FloatSize& pageSizeInPixels)
235 // Make sure the element is not freed during the layout.
236 RefPtr<Element> elementRef(element);
237 element->document()->updateLayout();
239 RenderBoxModelObject* box = enclosingBoxModelObject(element->renderer());
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);
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())
262 String PrintContext::pageProperty(Frame* frame, const char* propertyName, int pageNumber)
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);
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());
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());
285 return String("pageProperty() unimplemented for: ") + propertyName;
288 bool PrintContext::isPageBoxVisible(Frame* frame, int pageNumber)
290 return frame->document()->isPageBoxVisible(pageNumber);
293 String PrintContext::pageSizeAndMarginsInPixels(Frame* frame, int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
295 IntSize pageSize(width, height);
296 frame->document()->pageSizeAndMarginsInPixels(pageNumber, pageSize, marginTop, marginRight, marginBottom, marginLeft);
298 return "(" + String::number(pageSize.width()) + ", " + String::number(pageSize.height()) + ") " +
299 String::number(marginTop) + ' ' + String::number(marginRight) + ' ' + String::number(marginBottom) + ' ' + String::number(marginLeft);
302 int PrintContext::numberOfPages(Frame* frame, const FloatSize& pageSizeInPixels)
304 frame->document()->updateLayout();
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();
316 void PrintContext::spoolAllPagesWithBoundaries(Frame* frame, GraphicsContext& graphicsContext, const FloatSize& pageSizeInPixels)
318 if (!frame->document() || !frame->view() || !frame->document()->renderer())
321 frame->document()->updateLayout();
323 PrintContext printContext(frame);
324 printContext.begin(pageSizeInPixels.width(), pageSizeInPixels.height());
327 printContext.computePageRects(FloatRect(FloatPoint(0, 0), pageSizeInPixels), 0, 0, 1, pageHeight);
329 const float pageWidth = pageSizeInPixels.width();
330 const Vector<IntRect>& pageRects = printContext.pageRects();
331 int totalHeight = pageRects.size() * (pageSizeInPixels.height() + 1) - 1;
333 // Fill the whole background by white.
334 graphicsContext.setFillColor(Color(255, 255, 255), ColorSpaceDeviceRGB);
335 graphicsContext.fillRect(FloatRect(0, 0, pageWidth, totalHeight));
337 graphicsContext.save();
338 graphicsContext.translate(0, totalHeight);
339 graphicsContext.scale(FloatSize(1, -1));
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.
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();
353 graphicsContext.save();
354 graphicsContext.translate(0, currentHeight);
355 printContext.spoolPage(graphicsContext, pageIndex, pageWidth);
356 graphicsContext.restore();
358 currentHeight += pageSizeInPixels.height() + 1;
361 graphicsContext.restore();