This source file includes following definitions.
- m_linkedDestinationsValid
- computePageRects
- computePageRectsWithPageSize
- computePageRectsWithPageSizeInternal
- begin
- computeAutomaticScaleFactor
- spoolPage
- spoolRect
- end
- enclosingBoxModelObject
- pageNumberForElement
- collectLinkedDestinations
- outputLinkedDestinations
- pageProperty
- isPageBoxVisible
- pageSizeAndMarginsInPixels
- numberOfPages
- spoolAllPagesWithBoundaries
#include "config.h"
#include "core/page/PrintContext.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/rendering/RenderView.h"
#include "platform/graphics/GraphicsContext.h"
namespace WebCore {
const float printingMinimumShrinkFactor = 1.25f;
const float printingMaximumShrinkFactor = 2;
PrintContext::PrintContext(LocalFrame* frame)
: m_frame(frame)
, m_isPrinting(false)
, m_linkedDestinationsValid(false)
{
}
PrintContext::~PrintContext()
{
if (m_isPrinting)
end();
}
void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight, bool allowHorizontalTiling)
{
m_pageRects.clear();
outPageHeight = 0;
if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderView())
return;
if (userScaleFactor <= 0) {
WTF_LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
return;
}
RenderView* view = m_frame->document()->renderView();
const IntRect& documentRect = view->documentRect();
FloatSize pageSize = m_frame->resizePageRectsKeepingRatio(FloatSize(printRect.width(), printRect.height()), FloatSize(documentRect.width(), documentRect.height()));
float pageWidth = pageSize.width();
float pageHeight = pageSize.height();
outPageHeight = pageHeight;
pageHeight -= headerHeight + footerHeight;
if (pageHeight <= 0) {
WTF_LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
return;
}
computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), allowHorizontalTiling);
}
void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling)
{
m_pageRects.clear();
computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalTiling);
}
void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowInlineDirectionTiling)
{
if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderView())
return;
RenderView* view = m_frame->document()->renderView();
IntRect docRect = view->documentRect();
int pageWidth = pageSizeInPixels.width();
int pageHeight = pageSizeInPixels.height();
bool isHorizontal = view->style()->isHorizontalWritingMode();
int docLogicalHeight = isHorizontal ? docRect.height() : docRect.width();
int pageLogicalHeight = isHorizontal ? pageHeight : pageWidth;
int pageLogicalWidth = isHorizontal ? pageWidth : pageHeight;
int inlineDirectionStart;
int inlineDirectionEnd;
int blockDirectionStart;
int blockDirectionEnd;
if (isHorizontal) {
if (view->style()->isFlippedBlocksWritingMode()) {
blockDirectionStart = docRect.maxY();
blockDirectionEnd = docRect.y();
} else {
blockDirectionStart = docRect.y();
blockDirectionEnd = docRect.maxY();
}
inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.x() : docRect.maxX();
inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxX() : docRect.x();
} else {
if (view->style()->isFlippedBlocksWritingMode()) {
blockDirectionStart = docRect.maxX();
blockDirectionEnd = docRect.x();
} else {
blockDirectionStart = docRect.x();
blockDirectionEnd = docRect.maxX();
}
inlineDirectionStart = view->style()->isLeftToRightDirection() ? docRect.y() : docRect.maxY();
inlineDirectionEnd = view->style()->isLeftToRightDirection() ? docRect.maxY() : docRect.y();
}
unsigned pageCount = ceilf((float)docLogicalHeight / pageLogicalHeight);
for (unsigned i = 0; i < pageCount; ++i) {
int pageLogicalTop = blockDirectionEnd > blockDirectionStart ?
blockDirectionStart + i * pageLogicalHeight :
blockDirectionStart - (i + 1) * pageLogicalHeight;
if (allowInlineDirectionTiling) {
for (int currentInlinePosition = inlineDirectionStart;
inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition < inlineDirectionEnd : currentInlinePosition > inlineDirectionEnd;
currentInlinePosition += (inlineDirectionEnd > inlineDirectionStart ? pageLogicalWidth : -pageLogicalWidth)) {
int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? currentInlinePosition : currentInlinePosition - pageLogicalWidth;
IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight);
if (!isHorizontal)
pageRect = pageRect.transposedRect();
m_pageRects.append(pageRect);
}
} else {
int pageLogicalLeft = inlineDirectionEnd > inlineDirectionStart ? inlineDirectionStart : inlineDirectionStart - pageLogicalWidth;
IntRect pageRect(pageLogicalLeft, pageLogicalTop, pageLogicalWidth, pageLogicalHeight);
if (!isHorizontal)
pageRect = pageRect.transposedRect();
m_pageRects.append(pageRect);
}
}
}
void PrintContext::begin(float width, float height)
{
m_isPrinting = true;
FloatSize originalPageSize = FloatSize(width, height);
FloatSize minLayoutSize = m_frame->resizePageRectsKeepingRatio(originalPageSize, FloatSize(width * printingMinimumShrinkFactor, height * printingMinimumShrinkFactor));
m_frame->setPrinting(true, minLayoutSize, originalPageSize, printingMaximumShrinkFactor / printingMinimumShrinkFactor);
}
float PrintContext::computeAutomaticScaleFactor(const FloatSize& availablePaperSize)
{
if (!m_frame->view())
return 1;
bool useViewWidth = true;
if (m_frame->document() && m_frame->document()->renderView())
useViewWidth = m_frame->document()->renderView()->style()->isHorizontalWritingMode();
float viewLogicalWidth = useViewWidth ? m_frame->view()->contentsWidth() : m_frame->view()->contentsHeight();
if (viewLogicalWidth < 1)
return 1;
float maxShrinkToFitScaleFactor = 1 / printingMaximumShrinkFactor;
float shrinkToFitScaleFactor = (useViewWidth ? availablePaperSize.width() : availablePaperSize.height()) / viewLogicalWidth;
return max(maxShrinkToFitScaleFactor, shrinkToFitScaleFactor);
}
void PrintContext::spoolPage(GraphicsContext& ctx, int pageNumber, float width)
{
IntRect pageRect = m_pageRects[pageNumber];
float scale = width / pageRect.width();
ctx.save();
ctx.scale(FloatSize(scale, scale));
ctx.translate(-pageRect.x(), -pageRect.y());
ctx.clip(pageRect);
m_frame->view()->paintContents(&ctx, pageRect);
if (ctx.supportsURLFragments())
outputLinkedDestinations(ctx, m_frame->document(), pageRect);
ctx.restore();
}
void PrintContext::spoolRect(GraphicsContext& ctx, const IntRect& rect)
{
ctx.save();
ctx.translate(-rect.x(), -rect.y());
ctx.clip(rect);
m_frame->view()->paintContents(&ctx, rect);
ctx.restore();
}
void PrintContext::end()
{
ASSERT(m_isPrinting);
m_isPrinting = false;
m_frame->setPrinting(false, FloatSize(), FloatSize(), 0);
m_linkedDestinations.clear();
m_linkedDestinationsValid = false;
}
static RenderBoxModelObject* enclosingBoxModelObject(RenderObject* object)
{
while (object && !object->isBoxModelObject())
object = object->parent();
if (!object)
return 0;
return toRenderBoxModelObject(object);
}
int PrintContext::pageNumberForElement(Element* element, const FloatSize& pageSizeInPixels)
{
RefPtr<Element> elementRef(element);
element->document().updateLayout();
RenderBoxModelObject* box = enclosingBoxModelObject(element->renderer());
if (!box)
return -1;
LocalFrame* frame = element->document().frame();
FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels);
PrintContext printContext(frame);
printContext.begin(pageRect.width(), pageRect.height());
FloatSize scaledPageSize = pageSizeInPixels;
scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width());
printContext.computePageRectsWithPageSize(scaledPageSize, false);
int top = box->pixelSnappedOffsetTop();
int left = box->pixelSnappedOffsetLeft();
size_t pageNumber = 0;
for (; pageNumber < printContext.pageCount(); pageNumber++) {
const IntRect& page = printContext.pageRect(pageNumber);
if (page.x() <= left && left < page.maxX() && page.y() <= top && top < page.maxY())
return pageNumber;
}
return -1;
}
void PrintContext::collectLinkedDestinations(Node* node)
{
for (Node* i = node->firstChild(); i; i = i->nextSibling())
collectLinkedDestinations(i);
if (!node->isLink() || !node->isElementNode())
return;
const AtomicString& href = toElement(node)->getAttribute(HTMLNames::hrefAttr);
if (href.isNull())
return;
KURL url = node->document().completeURL(href);
if (!url.isValid())
return;
if (url.hasFragmentIdentifier() && equalIgnoringFragmentIdentifier(url, node->document().baseURL())) {
String name = url.fragmentIdentifier();
Element* element = node->document().findAnchor(name);
if (element)
m_linkedDestinations.set(name, element);
}
}
void PrintContext::outputLinkedDestinations(GraphicsContext& graphicsContext, Node* node, const IntRect& pageRect)
{
if (!m_linkedDestinationsValid) {
collectLinkedDestinations(node);
m_linkedDestinationsValid = true;
}
HashMap<String, Element*>::const_iterator end = m_linkedDestinations.end();
for (HashMap<String, Element*>::const_iterator it = m_linkedDestinations.begin(); it != end; ++it) {
RenderObject* renderer = it->value->renderer();
if (renderer) {
IntRect boundingBox = renderer->absoluteBoundingBoxRect();
if (pageRect.intersects(boundingBox)) {
IntPoint point = boundingBox.minXMinYCorner();
point.clampNegativeToZero();
graphicsContext.addURLTargetAtPoint(it->key, point);
}
}
}
}
String PrintContext::pageProperty(LocalFrame* frame, const char* propertyName, int pageNumber)
{
Document* document = frame->document();
PrintContext printContext(frame);
printContext.begin(800);
document->updateLayout();
RefPtr<RenderStyle> style = document->styleForPage(pageNumber);
if (!strcmp(propertyName, "margin-left")) {
if (style->marginLeft().isAuto())
return String("auto");
return String::number(style->marginLeft().value());
}
if (!strcmp(propertyName, "line-height"))
return String::number(style->lineHeight().value());
if (!strcmp(propertyName, "font-size"))
return String::number(style->fontDescription().computedPixelSize());
if (!strcmp(propertyName, "font-family"))
return style->fontDescription().family().family().string();
if (!strcmp(propertyName, "size"))
return String::number(style->pageSize().width().value()) + ' ' + String::number(style->pageSize().height().value());
return String("pageProperty() unimplemented for: ") + propertyName;
}
bool PrintContext::isPageBoxVisible(LocalFrame* frame, int pageNumber)
{
return frame->document()->isPageBoxVisible(pageNumber);
}
String PrintContext::pageSizeAndMarginsInPixels(LocalFrame* frame, int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft)
{
IntSize pageSize(width, height);
frame->document()->pageSizeAndMarginsInPixels(pageNumber, pageSize, marginTop, marginRight, marginBottom, marginLeft);
return "(" + String::number(pageSize.width()) + ", " + String::number(pageSize.height()) + ") " +
String::number(marginTop) + ' ' + String::number(marginRight) + ' ' + String::number(marginBottom) + ' ' + String::number(marginLeft);
}
int PrintContext::numberOfPages(LocalFrame* frame, const FloatSize& pageSizeInPixels)
{
frame->document()->updateLayout();
FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels);
PrintContext printContext(frame);
printContext.begin(pageRect.width(), pageRect.height());
FloatSize scaledPageSize = pageSizeInPixels;
scaledPageSize.scale(frame->view()->contentsSize().width() / pageRect.width());
printContext.computePageRectsWithPageSize(scaledPageSize, false);
return printContext.pageCount();
}
void PrintContext::spoolAllPagesWithBoundaries(LocalFrame* frame, GraphicsContext& graphicsContext, const FloatSize& pageSizeInPixels)
{
if (!frame->document() || !frame->view() || !frame->document()->renderer())
return;
frame->document()->updateLayout();
PrintContext printContext(frame);
printContext.begin(pageSizeInPixels.width(), pageSizeInPixels.height());
float pageHeight;
printContext.computePageRects(FloatRect(FloatPoint(0, 0), pageSizeInPixels), 0, 0, 1, pageHeight);
const float pageWidth = pageSizeInPixels.width();
const Vector<IntRect>& pageRects = printContext.pageRects();
int totalHeight = pageRects.size() * (pageSizeInPixels.height() + 1) - 1;
graphicsContext.setFillColor(Color(255, 255, 255));
graphicsContext.fillRect(FloatRect(0, 0, pageWidth, totalHeight));
graphicsContext.save();
graphicsContext.translate(0, totalHeight);
graphicsContext.scale(FloatSize(1, -1));
int currentHeight = 0;
for (size_t pageIndex = 0; pageIndex < pageRects.size(); pageIndex++) {
if (pageIndex > 0) {
graphicsContext.save();
graphicsContext.setStrokeColor(Color(0, 0, 255));
graphicsContext.setFillColor(Color(0, 0, 255));
graphicsContext.drawLine(IntPoint(0, currentHeight),
IntPoint(pageWidth, currentHeight));
graphicsContext.restore();
}
graphicsContext.save();
graphicsContext.translate(0, currentHeight);
printContext.spoolPage(graphicsContext, pageIndex, pageWidth);
graphicsContext.restore();
currentHeight += pageSizeInPixels.height() + 1;
}
graphicsContext.restore();
}
}