Describe
Implement a function that can seperate a Qt text from one document(entity) into multiple documents(entities), with the key focus being on determining the distance between adjacent characters (the intervals in below graph).
Effect
Before Seperating, ‘Hello World’ is a entity.
After Seperating, every characters are entities.
Structure
Each entity holds a QTextDocument class composed of more than one QTextBlock class, where each ‘Block’ represents a single line.
Key variables are the blockHeight and every intervals.
Code
void seperate(){
// count the total block height
float blockHeight = 0;
// get format from qt document
auto format = QTextCursor(docoument).charFormat();
// block loop
for (int j = 0; j < docoument->blockCount(); j++)
{
QTextBlock block = docoument->findBlockByNumber(j); // get current block
QTextCursor textCursor(block); // get current cursor
QString context = block.text(); // get current context
QTextLine line = block.layout()->lineAt(0); //get current line
QList<QGlyphRun> glyphs;
QVector<QVector<QPointF>> pos; // save each word position
// loop of evey word in the block
for (int i = 0; i < context.length(); i++)
{
glyphs.append(line.glyphRuns(i, 1));
// get the interval from fisrt word to current word (img 4)
auto interval = glyphs[i].positions();
pos.push_back(interval);
auto new_item = new TextDocumentItem();
QTextCursor new_cursor(new_item->get_document());
// set new format
new_cursor.setBlockCharFormat(new_cursor.blockCharFormat());
new_cursor.setCharFormat(textCursor.charFormat());
// set text context
new_cursor.insertText(QString{ context[i] });
// interval(pos[i][0].x(), blockHeight)
// new document position(orignal.x + pos[i][0].x(), orignal.y - blockHeight)
}
blockHeight += line.height();
}
}