a bit of refactor
This commit is contained in:
19 files changed
+546
-232
No files matched your search
@@ -0,0 +1,41 @@
|
||||
#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;
|
||||
}
|
||||
Reference in new issue
Block a user