FrameSnapshotting.cpp [plain text]
#include "config.h"
#include "FrameSnapshotting.h"
#include "Document.h"
#include "Frame.h"
#include "FrameSelection.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "Page.h"
#include "RenderObject.h"
#include "Settings.h"
namespace WebCore {
struct ScopedFramePaintingState {
ScopedFramePaintingState(Frame& frame, Node* node)
: frame(frame)
, node(node)
, paintBehavior(frame.view()->paintBehavior())
, backgroundColor(frame.view()->baseBackgroundColor())
{
ASSERT(!node || node->renderer());
}
~ScopedFramePaintingState()
{
frame.view()->setPaintBehavior(paintBehavior);
frame.view()->setBaseBackgroundColor(backgroundColor);
frame.view()->setNodeToDraw(nullptr);
}
const Frame& frame;
const Node* node;
const PaintBehavior paintBehavior;
const Color backgroundColor;
};
std::unique_ptr<ImageBuffer> snapshotFrameRect(Frame& frame, const IntRect& imageRect, SnapshotOptions options)
{
if (!frame.page())
return nullptr;
frame.document()->updateLayout();
FrameView::SelectionInSnapshot shouldIncludeSelection = FrameView::IncludeSelection;
if (options & SnapshotOptionsExcludeSelectionHighlighting)
shouldIncludeSelection = FrameView::ExcludeSelection;
FrameView::CoordinateSpaceForSnapshot coordinateSpace = FrameView::DocumentCoordinates;
if (options & SnapshotOptionsInViewCoordinates)
coordinateSpace = FrameView::ViewCoordinates;
ScopedFramePaintingState state(frame, nullptr);
PaintBehavior paintBehavior = state.paintBehavior;
if (options & SnapshotOptionsForceBlackText)
paintBehavior |= PaintBehaviorForceBlackText;
if (options & SnapshotOptionsPaintSelectionOnly)
paintBehavior |= PaintBehaviorSelectionOnly;
if (options & SnapshotOptionsPaintSelectionAndBackgroundsOnly)
paintBehavior |= PaintBehaviorSelectionAndBackgroundsOnly;
frame.view()->setPaintBehavior(paintBehavior);
float scaleFactor = frame.page()->deviceScaleFactor();
if (frame.settings().delegatesPageScaling())
scaleFactor *= frame.page()->pageScaleFactor();
std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(imageRect.size(), scaleFactor, ColorSpaceDeviceRGB);
if (!buffer)
return nullptr;
buffer->context()->translate(-imageRect.x(), -imageRect.y());
frame.view()->paintContentsForSnapshot(buffer->context(), imageRect, shouldIncludeSelection, coordinateSpace);
return buffer;
}
std::unique_ptr<ImageBuffer> snapshotSelection(Frame& frame, SnapshotOptions options)
{
auto& selection = frame.selection();
if (!selection.isRange())
return nullptr;
FloatRect selectionBounds = selection.selectionBounds();
if (selectionBounds.isEmpty())
return nullptr;
options |= SnapshotOptionsPaintSelectionOnly;
return snapshotFrameRect(frame, enclosingIntRect(selectionBounds), options);
}
std::unique_ptr<ImageBuffer> snapshotNode(Frame& frame, Node& node)
{
if (!node.renderer())
return nullptr;
ScopedFramePaintingState state(frame, &node);
frame.view()->setBaseBackgroundColor(Color::transparent);
frame.view()->setNodeToDraw(&node);
LayoutRect topLevelRect;
return snapshotFrameRect(frame, snappedIntRect(node.renderer()->paintingRootRect(topLevelRect)));
}
}