1/*
2 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
3 * Copyright (C) 2007-2019 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 "ElementTraversal.h"
25#include "GraphicsContext.h"
26#include "Frame.h"
27#include "FrameView.h"
28#include "LengthBox.h"
29#include "RenderView.h"
30#include "RuntimeEnabledFeatures.h"
31#include "StyleInheritedData.h"
32#include "StyleResolver.h"
33#include "StyleScope.h"
34#include <wtf/text/StringConcatenateNumbers.h>
35
36namespace WebCore {
37
38PrintContext::PrintContext(Frame* frame)
39 : FrameDestructionObserver(frame)
40{
41}
42
43PrintContext::~PrintContext()
44{
45 if (m_isPrinting)
46 end();
47}
48
49void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling)
50{
51 if (!frame())
52 return;
53
54 auto& frame = *this->frame();
55 m_pageRects.clear();
56 outPageHeight = 0;
57
58 if (!frame.document() || !frame.view() || !frame.document()->renderView())
59 return;
60
61 if (userScaleFactor <= 0) {
62 LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
63 return;
64 }
65
66 RenderView* view = frame.document()->renderView();
67 const IntRect& documentRect = view->documentRect();
68 FloatSize pageSize = frame.resizePageRectsKeepingRatio(FloatSize(printRect.width(), printRect.height()), FloatSize(documentRect.width(), documentRect.height()));
69 float pageWidth = pageSize.width();
70 float pageHeight = pageSize.height();
71
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 computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), allowHorizontalTiling);
81}
82
83FloatBoxExtent PrintContext::computedPageMargin(FloatBoxExtent printMargin)
84{
85 if (!frame() || !frame()->document())
86 return printMargin;
87 if (!RuntimeEnabledFeatures::sharedFeatures().pageAtRuleSupportEnabled())
88 return printMargin;
89 // FIXME Currently no pseudo class is supported.
90 auto style = frame()->document()->styleScope().resolver().styleForPage(0);
91
92 float pixelToPointScaleFactor = 1 / CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(CSSPrimitiveValue::CSS_PT);
93 return { style->marginTop().isAuto() ? printMargin.top() : style->marginTop().value() * pixelToPointScaleFactor,
94 style->marginRight().isAuto() ? printMargin.right() : style->marginRight().value() * pixelToPointScaleFactor,
95 style->marginBottom().isAuto() ? printMargin.bottom() : style->marginBottom().value() * pixelToPointScaleFactor,
96 style->marginLeft().isAuto() ? printMargin.left() : style->marginLeft().value() * pixelToPointScaleFactor };
97}
98
99FloatSize PrintContext::computedPageSize(FloatSize pageSize, FloatBoxExtent printMargin)
100{
101 auto computedMargin = computedPageMargin(printMargin);
102 if (computedMargin == printMargin)
103 return pageSize;
104
105 auto horizontalMarginDelta = (printMargin.left() - computedMargin.left()) + (printMargin.right() - computedMargin.right());
106 auto verticalMarginDelta = (printMargin.top() - computedMargin.top()) + (printMargin.bottom() - computedMargin.bottom());
107 return { pageSize.width() + horizontalMarginDelta, pageSize.height() + verticalMarginDelta };
108}
109
110void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling)
111{
112 m_pageRects.clear();
113 computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalTiling);
114}
115
116void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowInlineDirectionTiling)
117{
118 if (!frame())
119 return;
120
121 auto& frame = *this->frame();
122 if (!frame.document() || !frame.view() || !frame.document()->renderView())
123 return;
124
125 RenderView* view = frame.document()->renderView();
126
127 IntRect docRect = view->documentRect();
128
129 int pageWidth = pageSizeInPixels.width();
130 int pageHeight = pageSizeInPixels.height();
131
132 bool isHorizontal = view->style().isHorizontalWritingMode();
133
134 int docLogicalHeight = isHorizontal ? docRect.height() : docRect.width();
135 int pageLogicalHeight = isHorizontal ? pageHeight : pageWidth;
136 int pageLogicalWidth = isHorizontal ? pageWidth : pageHeight;
137
138 int inlineDirectionStart;
139 int inlineDirectionEnd;
140 int blockDirectionStart;
141 int blockDirectionEnd;
142 if (isHorizontal) {
143 if (view->style().isFlippedBlocksWritingMode()) {
144 blockDirectionStart = docRect.maxY();
145 blockDirectionEnd = docRect.y();
146 } else {
147 blockDirectionStart = docRect.y();
148 blockDirectionEnd = docRect.maxY();
149 }
150 inlineDirectionStart = view->style().isLeftToRightDirection() ? docRect.x() : docRect.maxX();
151 inlineDirectionEnd = view->style().isLeftToRightDirection() ? docRect.maxX() : docRect.x();
152 } else {
153 if (view->style().isFlippedBlocksWritingMode()) {
154 blockDirectionStart = docRect.maxX();
155 blockDirectionEnd = docRect.x();
156 } else {
157 blockDirectionStart = docRect.x();
158 blockDirectionEnd = docRect.maxX();
159 }
160 inlineDirectionStart = view->style().isLeftToRightDirection() ? docRect.y() : docRect.maxY();
161 inlineDirectionEnd = view->style().isLeftToRightDirection() ? docRect.maxY() : docRect.y();
162 }
163
164 unsigned pageCount = ceilf((float)docLogicalHeight / pageLogicalHeight);
165 for (unsigned i = 0; i < pageCount; ++i) {
166 int pageLogicalTop = blockDirectionEnd > blockDirectionStart ?
167 blockDirectionStart + i * pageLogicalHeight :
168 blockDirectionStart - (i + 1) * pageLogicalHeight;
169 if (allowInlineDirectionTiling) {
170 for (int currentInlinePosition = inlineDirectionStart;
171 inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition < inlineDirectionEnd : currentInlinePosition > inlineDirectionEnd;
172 currentInlinePosition += (inlineDirectionEnd > inlineDirectionStart ? pageLogicalWidth : -pageLogicalWidth)) {
173 int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition : currentInlinePosition - pageLogicalWidth;
174 IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight);
175 if (!isHorizontal)
176 pageRect = pageRect.transposedRect();
177 m_pageRects.append(pageRect);
178 }
179 } else {
180 int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? inlineDirectionStart : inlineDirectionStart - pageLogicalWidth;
181 IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight);
182 if (!isHorizontal)
183 pageRect = pageRect.transposedRect();
184 m_pageRects.append(pageRect);
185 }
186 }
187}
188
189void PrintContext::begin(float width, float height)
190{
191 if (!frame())
192 return;
193
194 auto& frame = *this->frame();
195 // This function can be called multiple times to adjust printing parameters without going back to screen mode.
196 m_isPrinting = true;
197
198 FloatSize originalPageSize = FloatSize(width, height);
199 FloatSize minLayoutSize = frame.resizePageRectsKeepingRatio(originalPageSize, FloatSize(width * minimumShrinkFactor(), height * minimumShrinkFactor()));
200
201 // This changes layout, so callers need to make sure that they don't paint to screen while in printing mode.
202 frame.setPrinting(true, minLayoutSize, originalPageSize, maximumShrinkFactor() / minimumShrinkFactor(), AdjustViewSize);
203}
204
205float PrintContext::computeAutomaticScaleFactor(const FloatSize& availablePaperSize)
206{
207 if (!frame())
208 return 1;
209
210 auto& frame = *this->frame();
211 if (!frame.view())
212 return 1;
213
214 bool useViewWidth = true;
215 if (frame.document() && frame.document()->renderView())
216 useViewWidth = frame.document()->renderView()->style().isHorizontalWritingMode();
217
218 float viewLogicalWidth = useViewWidth ? frame.view()->contentsWidth() : frame.view()->contentsHeight();
219 if (viewLogicalWidth < 1)
220 return 1;
221
222 float maxShrinkToFitScaleFactor = 1 / maximumShrinkFactor();
223 float shrinkToFitScaleFactor = (useViewWidth ? availablePaperSize.width() : availablePaperSize.height()) / viewLogicalWidth;
224 return std::max(maxShrinkToFitScaleFactor, shrinkToFitScaleFactor);
225}
226
227void PrintContext::spoolPage(GraphicsContext& ctx, int pageNumber, float width)
228{
229 if (!frame())
230 return;
231
232 auto& frame = *this->frame();
233 // FIXME: Not correct for vertical text.
234 IntRect pageRect = m_pageRects[pageNumber];
235 float scale = width / pageRect.width();
236
237 ctx.save();
238 ctx.scale(scale);
239 ctx.translate(-pageRect.x(), -pageRect.y());
240 ctx.clip(pageRect);
241 frame.view()->paintContents(ctx, pageRect);
242 outputLinkedDestinations(ctx, *frame.document(), pageRect);
243 ctx.restore();
244}
245
246void PrintContext::spoolRect(GraphicsContext& ctx, const IntRect& rect)
247{
248 if (!frame())
249 return;
250
251 auto& frame = *this->frame();
252 // FIXME: Not correct for vertical text.
253 ctx.save();
254 ctx.translate(-rect.x(), -rect.y());
255 ctx.clip(rect);
256 frame.view()->paintContents(ctx, rect);
257 outputLinkedDestinations(ctx, *frame.document(), rect);
258 ctx.restore();
259}
260
261void PrintContext::end()
262{
263 if (!frame())
264 return;
265
266 auto& frame = *this->frame();
267 ASSERT(m_isPrinting);
268 m_isPrinting = false;
269 frame.setPrinting(false, FloatSize(), FloatSize(), 0, AdjustViewSize);
270 m_linkedDestinations = nullptr;
271}
272
273static inline RenderBoxModelObject* enclosingBoxModelObject(RenderElement* renderer)
274{
275 while (renderer && !is<RenderBoxModelObject>(*renderer))
276 renderer = renderer->parent();
277 return downcast<RenderBoxModelObject>(renderer);
278}
279
280int PrintContext::pageNumberForElement(Element* element, const FloatSize& pageSizeInPixels)
281{
282 // Make sure the element is not freed during the layout.
283 RefPtr<Element> elementRef(element);
284 element->document().updateLayout();
285
286 auto* box = enclosingBoxModelObject(element->renderer());
287 if (!box)
288 return -1;
289
290 Frame* frame = element->document().frame();
291 FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels);
292 PrintContext printContext(frame);
293 printContext.begin(pageRect.width(), pageRect.height());
294 FloatSize scaledPageSize = pageSizeInPixels;
295 scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width());
296 printContext.computePageRectsWithPageSize(scaledPageSize, false);
297
298 int top = roundToInt(box->offsetTop());
299 int left = roundToInt(box->offsetLeft());
300 size_t pageNumber = 0;
301 for (; pageNumber < printContext.pageCount(); pageNumber++) {
302 const IntRect& page = printContext.pageRect(pageNumber);
303 if (page.x() <= left && left < page.maxX() && page.y() <= top && top < page.maxY())
304 return pageNumber;
305 }
306 return -1;
307}
308
309void PrintContext::collectLinkedDestinations(Document& document)
310{
311 for (Element* child = document.documentElement(); child; child = ElementTraversal::next(*child)) {
312 String outAnchorName;
313 if (Element* element = child->findAnchorElementForLink(outAnchorName))
314 m_linkedDestinations->add(outAnchorName, *element);
315 }
316}
317
318void PrintContext::outputLinkedDestinations(GraphicsContext& graphicsContext, Document& document, const IntRect& pageRect)
319{
320 if (!graphicsContext.supportsInternalLinks())
321 return;
322
323 if (!m_linkedDestinations) {
324 m_linkedDestinations = std::make_unique<HashMap<String, Ref<Element>>>();
325 collectLinkedDestinations(document);
326 }
327
328 for (const auto& it : *m_linkedDestinations) {
329 RenderElement* renderer = it.value->renderer();
330 if (!renderer)
331 continue;
332
333 FloatPoint point = renderer->absoluteAnchorRect().minXMinYCorner();
334 point = point.expandedTo(FloatPoint());
335
336 if (!pageRect.contains(roundedIntPoint(point)))
337 continue;
338
339 graphicsContext.addDestinationAtPoint(it.key, point);
340 }
341}
342
343String PrintContext::pageProperty(Frame* frame, const char* propertyName, int pageNumber)
344{
345 ASSERT(frame);
346 ASSERT(frame->document());
347
348 Ref<Frame> protectedFrame(*frame);
349
350 auto& document = *frame->document();
351 PrintContext printContext(frame);
352 printContext.begin(800); // Any width is OK here.
353 document.updateLayout();
354 auto style = document.styleScope().resolver().styleForPage(pageNumber);
355
356 // Implement formatters for properties we care about.
357 if (!strcmp(propertyName, "margin-left")) {
358 if (style->marginLeft().isAuto())
359 return "auto"_s;
360 return String::number(style->marginLeft().value());
361 }
362 if (!strcmp(propertyName, "line-height"))
363 return String::number(style->lineHeight().value());
364 if (!strcmp(propertyName, "font-size"))
365 return String::number(style->fontDescription().computedPixelSize());
366 if (!strcmp(propertyName, "font-family"))
367 return style->fontDescription().firstFamily();
368 if (!strcmp(propertyName, "size"))
369 return makeString(style->pageSize().width.value(), ' ', style->pageSize().height.value());
370
371 return makeString("pageProperty() unimplemented for: ", propertyName);
372}
373
374bool PrintContext::isPageBoxVisible(Frame* frame, int pageNumber)
375{
376 return frame->document()->isPageBoxVisible(pageNumber);
377}
378
379String PrintContext::pageSizeAndMarginsInPixels(Frame* frame, int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
380{
381 IntSize pageSize(width, height);
382 frame->document()->pageSizeAndMarginsInPixels(pageNumber, pageSize, marginTop, marginRight, marginBottom, marginLeft);
383
384 return makeString('(', pageSize.width(), ", ", pageSize.height(), ") ", marginTop, ' ', marginRight, ' ', marginBottom, ' ', marginLeft);
385}
386
387bool PrintContext::beginAndComputePageRectsWithPageSize(Frame& frame, const FloatSize& pageSizeInPixels)
388{
389 if (!frame.document() || !frame.view() || !frame.document()->renderView())
390 return false;
391
392 frame.document()->updateLayout();
393
394 begin(pageSizeInPixels.width(), pageSizeInPixels.height());
395 // Account for shrink-to-fit.
396 FloatSize scaledPageSize = pageSizeInPixels;
397 scaledPageSize.scale(frame.view()->contentsSize().width() / pageSizeInPixels.width());
398 computePageRectsWithPageSize(scaledPageSize, false);
399
400 return true;
401}
402
403int PrintContext::numberOfPages(Frame& frame, const FloatSize& pageSizeInPixels)
404{
405 Ref<Frame> protectedFrame(frame);
406
407 PrintContext printContext(&frame);
408 if (!printContext.beginAndComputePageRectsWithPageSize(frame, pageSizeInPixels))
409 return -1;
410
411 return printContext.pageCount();
412}
413
414void PrintContext::spoolAllPagesWithBoundaries(Frame& frame, GraphicsContext& graphicsContext, const FloatSize& pageSizeInPixels)
415{
416 Ref<Frame> protectedFrame(frame);
417
418 PrintContext printContext(&frame);
419 if (!printContext.beginAndComputePageRectsWithPageSize(frame, pageSizeInPixels))
420 return;
421
422 const float pageWidth = pageSizeInPixels.width();
423 const Vector<IntRect>& pageRects = printContext.pageRects();
424 int totalHeight = pageRects.size() * (pageSizeInPixels.height() + 1) - 1;
425
426 // Fill the whole background by white.
427 graphicsContext.setFillColor(Color(255, 255, 255));
428 graphicsContext.fillRect(FloatRect(0, 0, pageWidth, totalHeight));
429
430 graphicsContext.save();
431
432 int currentHeight = 0;
433 for (size_t pageIndex = 0; pageIndex < pageRects.size(); pageIndex++) {
434 // Draw a line for a page boundary if this isn't the first page.
435 if (pageIndex > 0) {
436#if PLATFORM(COCOA)
437 int boundaryLineY = currentHeight;
438#else
439 int boundaryLineY = currentHeight - 1;
440#endif
441 graphicsContext.save();
442 graphicsContext.setStrokeColor(Color(0, 0, 255));
443 graphicsContext.setFillColor(Color(0, 0, 255));
444 graphicsContext.drawLine(IntPoint(0, boundaryLineY), IntPoint(pageWidth, boundaryLineY));
445 graphicsContext.restore();
446 }
447
448 graphicsContext.save();
449 graphicsContext.translate(0, currentHeight);
450 printContext.spoolPage(graphicsContext, pageIndex, pageWidth);
451 graphicsContext.restore();
452
453 currentHeight += pageSizeInPixels.height() + 1;
454 }
455
456 graphicsContext.restore();
457}
458
459}
460