#include "config.h"
#include "PrintContext.h"
#include "GraphicsContext.h"
#include "Frame.h"
#include "FrameView.h"
#include "RenderView.h"
using namespace WebCore;
namespace WebCore {
PrintContext::PrintContext(Frame* frame)
: m_frame(frame)
, m_isPrinting(false)
{
}
PrintContext::~PrintContext()
{
if (m_isPrinting)
end();
m_pageRects.clear();
}
int PrintContext::pageCount() const
{
return m_pageRects.size();
}
const IntRect& PrintContext::pageRect(int pageNumber) const
{
return m_pageRects[pageNumber];
}
void PrintContext::computePageRects(const FloatRect& printRect, float headerHeight, float footerHeight, float userScaleFactor, float& outPageHeight)
{
m_pageRects.clear();
outPageHeight = 0;
if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
return;
if (userScaleFactor <= 0) {
LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
return;
}
RenderView* root = toRenderView(m_frame->document()->renderer());
float ratio = printRect.height() / printRect.width();
float pageWidth = (float)root->rightLayoutOverflow();
float pageHeight = pageWidth * ratio;
outPageHeight = pageHeight; pageHeight -= headerHeight + footerHeight;
if (pageHeight <= 0) {
LOG_ERROR("pageHeight has bad value %.2f", pageHeight);
return;
}
computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), false);
}
void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalMultiPages)
{
m_pageRects.clear();
computePageRectsWithPageSizeInternal(pageSizeInPixels, allowHorizontalMultiPages);
}
void PrintContext::computePageRectsWithPageSizeInternal(const FloatSize& pageSizeInPixels, bool allowHorizontalMultiPages)
{
if (!m_frame->document() || !m_frame->view() || !m_frame->document()->renderer())
return;
RenderView* root = toRenderView(m_frame->document()->renderer());
const float pageWidth = pageSizeInPixels.width();
const float docWidth = root->layer()->width();
const float docHeight = root->layer()->height();
float currPageHeight = pageSizeInPixels.height();
float printedPagesHeight = 0;
do {
float proposedBottom = std::min(docHeight, printedPagesHeight + pageSizeInPixels.height());
m_frame->view()->adjustPageHeight(&proposedBottom, printedPagesHeight, proposedBottom, printedPagesHeight);
currPageHeight = max(1.0f, proposedBottom - printedPagesHeight);
if (allowHorizontalMultiPages) {
for (float curWidth = 0; curWidth < docWidth; curWidth += pageWidth)
m_pageRects.append(IntRect(curWidth, (int)printedPagesHeight, (int)pageWidth, (int)currPageHeight));
} else
m_pageRects.append(IntRect(0, (int)printedPagesHeight, (int)pageWidth, (int)currPageHeight));
printedPagesHeight += currPageHeight;
} while (printedPagesHeight < docHeight);
}
void PrintContext::begin(float width)
{
ASSERT(!m_isPrinting);
m_isPrinting = true;
const float PrintingMinimumShrinkFactor = 1.25f;
const float PrintingMaximumShrinkFactor = 2.0f;
float minLayoutWidth = width * PrintingMinimumShrinkFactor;
float maxLayoutWidth = width * PrintingMaximumShrinkFactor;
m_frame->setPrinting(true, minLayoutWidth, maxLayoutWidth, true);
}
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);
ctx.restore();
}
void PrintContext::end()
{
ASSERT(m_isPrinting);
m_isPrinting = false;
m_frame->setPrinting(false, 0, 0, true);
}
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;
Frame* frame = element->document()->frame();
FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels);
PrintContext printContext(frame);
printContext.begin(pageRect.width());
printContext.computePageRectsWithPageSize(pageSizeInPixels, false);
int top = box->offsetTop();
int left = box->offsetLeft();
int pageNumber = 0;
for (; pageNumber < printContext.pageCount(); pageNumber++) {
const IntRect& page = printContext.pageRect(pageNumber);
if (page.x() <= left && left < page.right() && page.y() <= top && top < page.bottom())
return pageNumber;
}
return -1;
}
int PrintContext::numberOfPages(Frame* frame, const FloatSize& pageSizeInPixels)
{
frame->document()->updateLayout();
FloatRect pageRect(FloatPoint(0, 0), pageSizeInPixels);
PrintContext printContext(frame);
printContext.begin(pageRect.width());
printContext.computePageRectsWithPageSize(pageSizeInPixels, false);
return printContext.pageCount();
}
}