diff --git a/src/utils/CachedFontMetrics.h b/src/utils/CachedFontMetrics.h index 8e6f1558..79e59b99 100644 --- a/src/utils/CachedFontMetrics.h +++ b/src/utils/CachedFontMetrics.h @@ -52,6 +52,29 @@ public: return mHeight; } + int position(const QString &text, int offset) + { + int curWidth = 0; + QChar temp; + + for (int i = 0; i < text.length(); i++) { + QChar ch = text[i]; + + if (ch.isHighSurrogate()) + temp = ch; + else if (ch.isLowSurrogate()) + curWidth += mFontMetrics.width(QString(temp) + ch); + else + curWidth += width(ch); + + if (curWidth >= offset) { + return i; + } + } + + return -1; + } + private: QFontMetrics mFontMetrics; uchar mWidths[0x10000 - 0xE000 + 0xD800]; diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index e9f3ed5e..dafc907b 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -441,6 +441,7 @@ void DisassemblerGraphView::drawBlock(QPainter &p, GraphView::GraphBlock &block) // highlight selected tokens if (highlight_token != nullptr) { int y = block.y + (2 * charWidth) + (db.header_text.lines.size() * charHeight); + int tokenWidth = mFontMetrics->width(highlight_token->content); for (Instr &instr : db.instrs) { int pos = -1; @@ -453,7 +454,8 @@ void DisassemblerGraphView::drawBlock(QPainter &p, GraphView::GraphBlock &block) continue; } - p.fillRect(QRect(block.x + charWidth * 3 + pos * charWidth, y, highlight_token->length * charWidth, + int widthBefore = mFontMetrics->width(instr.plainText.left(pos)); + p.fillRect(QRect(block.x + charWidth * 3 + widthBefore, y, tokenWidth, charHeight), disassemblySelectionColor.lighter(250)); } @@ -747,8 +749,12 @@ void DisassemblerGraphView::seekPrev() DisassemblerGraphView::Token *DisassemblerGraphView::getToken(Instr *instr, int x) { - int clickedCharPos = x / charWidth - 3; + x -= (3 * charWidth); // Ignore left margin + if (x < 0) { + return nullptr; + } + int clickedCharPos = mFontMetrics->position(instr->plainText, x); if (clickedCharPos > instr->plainText.length()) { return nullptr; }