Thanks for the guidance! I am not trying to calculate my own line heights -- I am trying to determine what the rendered heights are. To simplify things, here is what I want to do:
#include <QTGui>
int main(int argc, char *argv[])
{
editor->setDocument(text_document);
editor->setHtml(file.readAll());
while (text_block.isValid() )
{
qDebug() << text_block.text() << CalculateLineHeight(text_block);
text_block = text_block.next();
}
window->setCentralWidget(editor);
window->show();
return app.exec();
}
{
QList<QTextLayout::FormatRange> text_block_format_ranges;
// Gather the format ranges for each fragment of the text block.
for (QTextBlock::Iterator fragment_it
= text_block.
begin();
!(fragment_it.
atEnd());
++fragment_it
) {
text_block_format_range.format = fragment_format;
text_block_format_range.start = fragment.position();
text_block_format_range.length = fragment.length();
text_block_format_ranges << text_block_format_range;
}
// Create text layout
text_layout.setAdditionalFormats( text_block_format_ranges );
text_layout.beginLayout();
text_layout.endLayout();
return text_layout.boundingRect().height();
}
#include <QTGui>
int CalculateLineHeight(QTextBlock text_block);
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow* window = new QMainWindow;
QTextEdit* editor = new QTextEdit(window);
QTextDocument* text_document = new QTextDocument(window);
editor->setDocument(text_document);
QFile file("test.html");
if (file.open(QFile::ReadOnly | QFile::Text))
editor->setHtml(file.readAll());
QTextBlock text_block = text_document->begin();
while (text_block.isValid() )
{
qDebug() << text_block.text() << CalculateLineHeight(text_block);
text_block = text_block.next();
}
window->setCentralWidget(editor);
window->show();
return app.exec();
}
int CalculateLineHeight(QTextBlock text_block)
{
QList<QTextLayout::FormatRange> text_block_format_ranges;
// Gather the format ranges for each fragment of the text block.
for (QTextBlock::Iterator fragment_it = text_block.begin(); !(fragment_it.atEnd()); ++fragment_it)
{
QTextFragment fragment = fragment_it.fragment();
QTextCharFormat fragment_format = fragment.charFormat();
QTextLayout::FormatRange text_block_format_range;
text_block_format_range.format = fragment_format;
text_block_format_range.start = fragment.position();
text_block_format_range.length = fragment.length();
text_block_format_ranges << text_block_format_range;
}
// Create text layout
QTextLayout text_layout(text_block.text());
text_layout.setAdditionalFormats( text_block_format_ranges );
text_layout.beginLayout();
QTextLine line = text_layout.createLine();
text_layout.endLayout();
return text_layout.boundingRect().height();
}
To copy to clipboard, switch view to plain text mode
This is test.html:
<!DOCTYPE html>
<html>
<head>
<style>
.consolas{
font-family: "Consolas";
font-size:14pt;
background-color: cyan;
}
.arial{
font-family: "Arial";
font-size:14pt;
background-color: cyan;
}
</style>
</head>
<body>
<p><span class="consolas">E</span></p>
<p><span class="arial">E</span></p>
<p><span class="consolas">E</span><span class="arial">E</span></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.consolas{
font-family: "Consolas";
font-size:14pt;
background-color: cyan;
}
.arial{
font-family: "Arial";
font-size:14pt;
background-color: cyan;
}
</style>
</head>
<body>
<p><span class="consolas">E</span></p>
<p><span class="arial">E</span></p>
<p><span class="consolas">E</span><span class="arial">E</span></p>
</body>
</html>
To copy to clipboard, switch view to plain text mode
The window displays (with my annotations indicating the LineHeights):
editor window.JPG
But I get the following console output:
"E" 22
"E" 13
"EE" 13
So, it apparently calculates it properly on the first line, but subsequent lines it does not calculate it properly (2nd line s/b 23, 3rd s/b 24). So, I believe I am closer but I suspect my problem lies in how I am handling the text layouts.
Any thoughts?
Bookmarks