How to get characters using their glyph indices
I'm working on a GUI application that renders rich edit text to an image. The rendering itself is done by means of drawGlyphRun method of QPainter. I also have to collect some statistics over the rendered characters (using character themselves and the bounding rectangles of their glyphs).
The problem is that I cannot find a way to calculate character(s) using its(their) glyph index. GlyphRuns for rendering are got using QTextLayout::glyphRuns method. I can get the text from richedit widget to build a map, but the problem is that the glyph indices calculated for that text (Text Glyph Indices) differ from what QTextLayout::glyphRuns gives (Richedit Glyph Indices).
Text Glyph Indices are calculated using QRawFont::glyphIndexesForString method. The instance of QRawFont is a property of QGlyphRun object (returned by QTextLayout::glyphRuns). So it appears to me that the indices have to be the same (the order might differ). But in reality they differ for the languages when one glyph might correspont to multiple characters (Kannada, for example).
Does anybody know how to get characters from glyph indices? I could use Richedit Glyph Indices for the calculation in this case.
Any help is appreciated.
haiku last edited by
For anyone who comes across this post from Google, I spent some time looking into this and I don't believe it is possible. The root of the problem is that there is a many to one mapping between glyph indices and character codes. Since multiple characters can map to the same glyph it is not possible to go from glyphs to characters as there would be multiple solutions. Even if you selected the first match it would likely not give you what you need.
One partial solution if you are obtaining your
QSGGlyphNodeis to use the
QSGGlyphNode::m_ownerElementprotected member to get the parent
QQuickTextitem that owns the glyph node and then extract the text from there. However since there can be multiple glyph nodes for a single
QQuickTextitem it might be difficult to work out exactly which part of the text you are currently working with. Still its very if you just need some debug info to orient yourself.
The correct solution would be to use the fonts cmap table to extract the mapping characters but as far as I see Qt provides no logic for doing this so it would be up to you to parse the cmap table and deal with the complexities there.