mirror of https://github.com/vitalif/phantomjs
Merge branch 'master' of https://github.com/wouterdebie/phantomjs into wouterdebie-master
commit
87a337f6df
|
@ -131,6 +131,7 @@ class Phantom: public QObject
|
|||
Q_PROPERTY(QVariantMap version READ version)
|
||||
Q_PROPERTY(QVariantMap viewportSize READ viewportSize WRITE setViewportSize)
|
||||
Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize)
|
||||
Q_PROPERTY(QVariantMap clipRect READ clipRect WRITE setClipRect)
|
||||
|
||||
public:
|
||||
Phantom(QObject *parent = 0);
|
||||
|
@ -156,6 +157,9 @@ public:
|
|||
void setViewportSize(const QVariantMap &size);
|
||||
QVariantMap viewportSize() const;
|
||||
|
||||
void setClipRect(const QVariantMap &size);
|
||||
QVariantMap clipRect() const;
|
||||
|
||||
void setPaperSize(const QVariantMap &size);
|
||||
QVariantMap paperSize() const;
|
||||
|
||||
|
@ -183,6 +187,7 @@ private:
|
|||
QString m_state;
|
||||
CSConverter *m_converter;
|
||||
QVariantMap m_paperSize; // For PDF output via render()
|
||||
QRect m_clipRect;
|
||||
};
|
||||
|
||||
Phantom::Phantom(QObject *parent)
|
||||
|
@ -383,18 +388,36 @@ bool Phantom::render(const QString &fileName)
|
|||
return renderPdf(fileName);
|
||||
|
||||
QSize viewportSize = m_page.viewportSize();
|
||||
QSize pageSize = m_page.mainFrame()->contentsSize();
|
||||
|
||||
QSize pageSize = m_page.mainFrame()->contentsSize();
|
||||
|
||||
QSize bufferSize;
|
||||
if (!m_clipRect.isEmpty()) {
|
||||
bufferSize = m_clipRect.size();
|
||||
} else {
|
||||
bufferSize = m_page.mainFrame()->contentsSize();
|
||||
}
|
||||
|
||||
if (pageSize.isEmpty())
|
||||
return false;
|
||||
|
||||
QImage buffer(pageSize, QImage::Format_ARGB32);
|
||||
QImage buffer(bufferSize, QImage::Format_ARGB32);
|
||||
buffer.fill(qRgba(255, 255, 255, 0));
|
||||
QPainter p(&buffer);
|
||||
|
||||
p.setRenderHint(QPainter::Antialiasing, true);
|
||||
p.setRenderHint(QPainter::TextAntialiasing, true);
|
||||
p.setRenderHint(QPainter::SmoothPixmapTransform, true);
|
||||
|
||||
m_page.setViewportSize(pageSize);
|
||||
m_page.mainFrame()->render(&p);
|
||||
|
||||
if (!m_clipRect.isEmpty()) {
|
||||
p.translate(-m_clipRect.left(), -m_clipRect.top());
|
||||
m_page.mainFrame()->render(&p, QRegion(m_clipRect));
|
||||
} else {
|
||||
m_page.mainFrame()->render(&p);
|
||||
}
|
||||
|
||||
p.end();
|
||||
m_page.setViewportSize(viewportSize);
|
||||
|
||||
|
@ -478,6 +501,33 @@ QVariantMap Phantom::viewportSize() const
|
|||
return result;
|
||||
}
|
||||
|
||||
void Phantom::setClipRect(const QVariantMap &size)
|
||||
{
|
||||
int w = size.value("width").toInt();
|
||||
int h = size.value("height").toInt();
|
||||
int top = size.value("top").toInt();
|
||||
int left = size.value("left").toInt();
|
||||
|
||||
if (top < 0)
|
||||
top = 0;
|
||||
|
||||
if (left < 0)
|
||||
left = 0;
|
||||
|
||||
if (w > 0 && h > 0)
|
||||
m_clipRect = QRect(left, top, w, h);
|
||||
}
|
||||
|
||||
QVariantMap Phantom::clipRect() const
|
||||
{
|
||||
QVariantMap result;
|
||||
result["width"] = m_clipRect.width();
|
||||
result["height"] = m_clipRect.height();
|
||||
result["top"] = m_clipRect.top();
|
||||
result["left"] = m_clipRect.left();
|
||||
return result;
|
||||
}
|
||||
|
||||
void Phantom::setPaperSize(const QVariantMap &size)
|
||||
{
|
||||
m_paperSize = size;
|
||||
|
|
Loading…
Reference in New Issue