#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)
{
}
PrintContext::~PrintContext()
{
m_pageRects.clear();
}
int PrintContext::pageCount() const
{
return m_pageRects.size();
}
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;
RenderView* root = toRenderView(m_frame->document()->renderer());
if (!root) {
LOG_ERROR("document to be printed has no renderer");
return;
}
if (userScaleFactor <= 0) {
LOG_ERROR("userScaleFactor has bad value %.2f", userScaleFactor);
return;
}
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;
}
float currPageHeight = pageHeight / userScaleFactor;
float docHeight = root->layer()->height();
float currPageWidth = pageWidth / userScaleFactor;
float printedPagesHeight = 0.0;
do {
float proposedBottom = std::min(docHeight, printedPagesHeight + pageHeight);
m_frame->view()->adjustPageHeight(&proposedBottom, printedPagesHeight, proposedBottom, printedPagesHeight);
currPageHeight = max(1.0f, proposedBottom - printedPagesHeight);
m_pageRects.append(IntRect(0, (int)printedPagesHeight, (int)currPageWidth, (int)currPageHeight));
printedPagesHeight += currPageHeight;
} while (printedPagesHeight < docHeight);
}
void PrintContext::begin(float width)
{
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()
{
m_frame->setPrinting(false, 0, 0, true);
}
}