How to get Graphics2D instance for Text Editor Content Component?

Hi,

I was trying to create IDEA plugin, something akin TextSharp for Visual Studio, because I did not like any of text rendering hints IDEA has for its Antialiasing settings. My intention is to apply VALUE_TEXT_ANTIALIAS_LCD_HRGB hint to editor contents.

But, unfortunately, GraphicsUtil.safelyGetGraphics(editorFactoryEvent.getEditor().getContentComponent()) returns instance of Graphics class, not Graphics2D.

Is there any other way to get Graphics2D instance that corresponds to text editor content area? I can not believe that there is none.

Here is the code snippet of my failed attempt:

https://gist.github.com/0xfeeddeadbeef/e655a279d94cc472994123061cbfea31 

0
4 comments
Avatar
Permanently deleted user

These people claim, that Graphics can almost always be cast to Graphics2D: https://stackoverflow.com/questions/179415/java2d-is-it-always-safe-to-cast-graphics-into-graphics2d 

It seems, IDEA text component is an exception too.

0

For your code snippet, Graphics instance you get from safelyGetGraphics is just null, so naturally it's not an instance of Graphics2D. When it's not null, it's usually can be cast to Graphics2D. And you're getting null, because at the time editor creation event is generated, editor component is not yet displayable (i.e. it hasn't been installed in component hierarchy). If you try to do the same when editor is visible, you'll get a not-null Graphics (Graphics2D) instance.

Supposing you get the graphics for a displayed editor, and set rendering hints for it. There's no guarantee those hints will be the same at the time editor will be painting - a new instance of Graphics will most probably be used for it. But that doesn't matter anyway - editor sets anti-aliasing hint itself on painting (overriding any default values Graphics has). When Subpixel anti-aliasing is selected for editor in Settings->Appearance&Behaviour->Appearance->Antialiasing, that hint would be VALUE_TEXT_ANTIALIAS_LCD_HRGB, exactly the value you wanted to set via plugin. If resulting rendering is not what you'd like to see in editor, there's not much you can do from Java code (there's one additional parameter to play with though - RenderingHints.KEY_TEXT_LCD_CONTRAST, you can change its value in IDEA by setting lcd.contrast.value registry value), to change it you'll need to make changes to JDK text rendering code.

If you still want to try changing something about text rendering in IDEA, I'd suggest you to try to get desired result in a simple Java application first, and if you succeed there, let us know. It's hardly possible to change the text rendering in IDEA via plugin, but we're considering patches for both IDEA and JetBrains Runtime.

 

0
Avatar
Permanently deleted user

Thanks for your reply.

This was my wishful thinking, — because it was so easy in Visual Studio, I assumed it could be done similarly in the IDEA. But, I forgot, that it is different technology stack (WPF vs GDI+, .NET vs Java, etc) and of course, creators of IDEA may have done things differently.

As for VALUE_TEXT_ANTIALIAS_LCD_HRGB hint, — my bad, did not notice that it was same. I guess ClearType rendering hint is missing from AWT. It would be great to have ClearType in IDEA appearance options...

0

JDK already uses ClearType rendering when LCD anti-aliasing is requested on Windows. Right now its implemented via GDI, there's a ticket in our tracker to add support for DirectWrite (https://youtrack.jetbrains.com/issue/JRE-11).

0

Please sign in to leave a comment.