42 lines
1.0 KiB
C++
42 lines
1.0 KiB
C++
#include "QDocumentChunkMeasurer.h"
|
|
#include <QFont>
|
|
#include <QTextBlockFormat>
|
|
#include <QTextCursor>
|
|
#include <QTextDocument>
|
|
|
|
QDocumentChunkMeasurer::QDocumentChunkMeasurer(int pixelSize, qreal lineHeightPercent)
|
|
: m_pixelSize(pixelSize)
|
|
, m_lineHeightPercent(lineHeightPercent)
|
|
{
|
|
}
|
|
|
|
QVector<qreal> QDocumentChunkMeasurer::measure(const QStringList &chunks, qreal textWidth) const
|
|
{
|
|
if (textWidth <= 0)
|
|
textWidth = 1;
|
|
|
|
QFont font;
|
|
font.setPixelSize(m_pixelSize);
|
|
|
|
QTextDocument doc;
|
|
doc.setDefaultFont(font);
|
|
|
|
QVector<qreal> heights;
|
|
heights.reserve(chunks.size());
|
|
|
|
for (const QString &chunk : chunks) {
|
|
doc.setHtml(chunk);
|
|
doc.setTextWidth(textWidth);
|
|
|
|
QTextBlockFormat fmt;
|
|
fmt.setLineHeight(m_lineHeightPercent, QTextBlockFormat::ProportionalHeight);
|
|
QTextCursor cursor(&doc);
|
|
cursor.select(QTextCursor::Document);
|
|
cursor.mergeBlockFormat(fmt);
|
|
|
|
heights.append(doc.size().height());
|
|
}
|
|
|
|
return heights;
|
|
}
|