SWT,styledText,获取特定行的 x 坐标

SWT, styledText, get x coordinates for certain line

我正在为 eclipse CDT 编辑器开发插件,它是一个可以突出显示某些代码部分的覆盖层。我可以访问打开的 CEditor(及其 IDocument),也可以从中访问 StyledText。我还有一个 字符偏移量 要突出显示的部分。 目前我在编辑器上有 PaintListener 并且我能够绘制(我制作透明图像并在其上绘制突出显示,然后将其设置为文本背景,即 styledText.setBackgroundImage(newImage); )。从 styledText 我还可以获得有关文本高度和文本垂直位置(包括滚动)的信息,即我能够突出显示代码行,但我只想突出显示该行的一部分。

gc.fillRectangle(OFFSETX1, styledText.getLinePixel(LINE), OFFSETX2, tyledText.getLineHeight());

我如何从字符偏移中获取以像素为单位的 X(OFFSETX1、OFFSETX2)?(是否可以从 StyledText 中获取?)。我已经搜索 StyledText API 数小时,但找不到。谢谢。

使用StyledText.getLocationAtOffset

Returns the x, y location of the upper left corner of the character bounding box at the specified offset in the text. The point is relative to the upper left corner of the widget client area.

Point loc = styledText.getLocationAtOffset(character offset);