0

I am displaying text using gc.setText() method on canvas. But if the gc object changes then width of the text also changes.

I am applying the same font to GC using gc.setFont(font).

Font font = new Font(gc.getDevice(), fontFamily, (int) fontSize,     swtFontStyle);
    if (color != null) {
        gc.setForeground(color);
    } else {
        gc.setForeground(ColorConstants.black);
    }
    gc.setFont(font);
System.out.println("char width in gc: "+gc.getCharWidth('a'));

It is returning different character width when object of gc changes.

Can anyone help me to resolve this issue.

Thanks Ishan Jain

ishan jain
  • 681
  • 3
  • 10
  • 27
  • What do you mean by 'gc object changes'? How is this GC being created? – greg-449 Feb 16 '16 at 11:01
  • GC is the object which we get from the paint method of the canvas. I have added paintlistener to the canvas. When we call redraw, this event is get called: paintControl(PaintEvent e). "e.gc" gives the gc object. It changes as both tha canvas view are different hence different component, different paint listener and different gc object. – ishan jain Feb 16 '16 at 11:09
  • Have you checked that the parameters to the Font are the same in all cases. Look at the gc.getFontMetrics to see what is being used. Finally if you create a Font like this you **must** arrange to dispose it when it is no longer needed. – greg-449 Feb 16 '16 at 11:14
  • I am disposing it whenever it is not getting used and all the parameters in the font object are same. gc.getFontMetrics is giving different char width both the times. – ishan jain Feb 16 '16 at 11:23
  • So is the Device always the same (is everything on the same screen)? – greg-449 Feb 16 '16 at 12:03
  • same screen and same project, but different files or editors. – ishan jain Feb 16 '16 at 12:10
  • One more thing is that when I start the plugin then it gives the different font size but whenever I open the second editor and then again switch to first editor(the one which showing larger text than expected) then text becomes same as in second editor. – ishan jain Feb 16 '16 at 12:12

0 Answers0