bobbylight / RSyntaxTextArea

A syntax highlighting, code folding text editor for Java Swing applications.
BSD 3-Clause "New" or "Revised" License
1.12k stars 259 forks source link

Incorrect text rendering with Font at 125% on Windows #457

Open olivbrau opened 2 years ago

olivbrau commented 2 years ago

Description The problem can arise in 2 situations : 1) If a Java program window with RSyntaxTextArea showing code is moved from a screen with Font set at 100%, to a screen with Font set at 125 % (cf. Windows screen parameter) 2) if this program is started on a screen with font at 100% and then this screen is changed to 125%

Then, the text can be rendered incorrectly in the RSyntaxTextArea component :

If the same program has other component like JTextField, the render is OK in these components : the problem seems to affect only RSyntaxTextArea.

Java version Java 16, tested with different look and feel (system, FlatLaf, ...)

ptorngren commented 1 year ago

The problem is also visible if the application is started with 125% scaling.

After changing from 100% to 125%, the cursor is mislocated and rendered with double lines: image

After restarting with 125%, the cursor seems to be aligned better but is still distorted: image

ptorngren commented 1 year ago

There are at least two problems that can be observed in the demo app using plain text (SYNTAX_STYLE_NONE) and the attached test file (testCaret.txt) when running with JDK11 on Windows 11 with display scaling set to 125%.

  1. The cursor is distorted (painted with two vertical lines) on every 4th character position
  2. The characters are offset on lines with tabs

image

Opening the same file in any regular editor or the standard JTextArea, or with display scaling set to 100% or 200% renders the file properly: image

Conclusion: There seems to be a general problem with fractional scaling on Windows displays.

bobbylight commented 1 year ago

My initial guess here is this is because RSTA is still using int-based rendering APIs, and starting with Java 9 when AWT/Swing started supporting HiDPI, they added equivalent APIs that used float, but we're just not using it because of our Java 8 baseline. I can try a Java 11 build and make these changes to see if it's as simple as that.

ptorngren commented 1 year ago

Yes and no. The problem appears if you run Java 11, and the reason is - as you say - that you're not leveraging the FPAPI available as of Java 9 (?).

I made a fork and fixed some of the problems. I compile and run in Java 11.

The fork (and branch) are public, you may want to have a look? https://github.com/dbvis/RSyntaxTextArea/tree/issue-457-fractional-scaling-Windows

I was planning to continue investigating next week and hopefully create a pull request, but if you are willing to have a look I will happily leave it in your hands. Let me know if your're gonna look into this? I am sure you will handle it faster than me :-)

Best Regards

Peer


Peer Törngren DbVis Software AB Stockholm, Sweden http://www.dbvis.com

Our products help over 24,500 customers in 144 countries

------ Original Message ------ From "bobbylight" @.> To "bobbylight/RSyntaxTextArea" @.> Cc "Peer Törngren" @.>; "Comment" @.> Date 2023-01-13 15:03:30 Subject Re: [bobbylight/RSyntaxTextArea] Incorrect text rendering with Font at 125% on Windows (Issue #457)

My initial guess here is this is because RSTA is still using int-based rendering APIs, and starting with Java 9 when AWT/Swing started supporting HiDPI, they added equivalent APIs that used float, but we're just not using it because of our Java 8 baseline. I can try a Java 11 build and make these changes to see if it's as simple as that.

— Reply to this email directly, view it on GitHub https://github.com/bobbylight/RSyntaxTextArea/issues/457#issuecomment-1381905249, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDCYS4C3TIJTLMYR67XKJTWSFODFANCNFSM55SPNVJA. You are receiving this because you commented.Message ID: @.***>

ptorngren commented 1 year ago

I think I have fixed the issues on my fork, but need to do more thorough testing before creating a Pull Request.

There actually seem to be two issues:

  1. RSTA does not respond to changes in screen resolution or scale
  2. RSTA does not leverage the FPAPI available as of Java 9

The first can be fixed by attaching a PropertyChangeListener for the property "graphicsConfiguration" to the main window, and have it call RSyntaxTextArea.refreshFontMetrics() via a new wrapper method (since the refresh method is private), eg: addPropertyChangeListener("graphicsConfiguration", evt->textArea.onGraphicsConfigurationChange());

The second is in theory a simple fix, but since these methods are called in many places, the changes ripple thru the application causing small changes in many places. I think I got it working, but need to test more.

Also, these changes will make RSTA require Java 9 or later. It could be made to run on both Java 8 and 9+ by wrapping the updated calls in a multi-release jar. This would not add any value to me since we're running JDK 11 and are looking at moving to 17.

PAX523 commented 1 year ago

Hallo @ptorngren! Great that you investigated into that issue and that you are approaching a fix! Good job! Did you observe some anomalies after your fix so far?

@bobbylight, can you please collaborate with Peer in order to find a solution according to the screen scale change event and the internal support for FPAPI?

ptorngren commented 1 year ago

Hi

So far everything seems to work as expected, but since most of us run macOS, the fix isn’t as thoroughly verified in daily use as we would have liked. And since we have not yet released any version with this fix, we haven’t had it verified by users.

Best Regards

Peer


Peer Törngren DbVis Software AB Stockholm, Sweden http://www.dbvis.com

Our products help over 25,000 customers in 145 countries

------ Original Message ------ From "PAX523" @.> To "bobbylight/RSyntaxTextArea" @.> Cc "Peer Törngren" @.>; "Mention" @.> Date 2023-10-26, 08:42:28 Subject Re: [bobbylight/RSyntaxTextArea] Incorrect text rendering with Font at 125% on Windows (Issue #457)

Hallo @ptorngren https://github.com/ptorngren! Great that you investigated into that issue and that you are approaching a fix! Good job! Did you observe some anomalies after your fix so far?

@bobbylight https://github.com/bobbylight, can you please collaborate with Peer in order to find a solution according to the screen scale change event and the internal support for FPAPI?

— Reply to this email directly, view it on GitHub https://github.com/bobbylight/RSyntaxTextArea/issues/457#issuecomment-1780505706, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDCYSYH5EYCHK2E4E7CBF3YBIA5JAVCNFSM55SPNVJKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZYGA2TANJXGA3A. You are receiving this because you were mentioned.Message ID: @.***>

ptorngren commented 1 year ago

Hi

Just created a Pull Request. Tried to summarize the changes in the PR:

Add support for FPAPI: Add new class SwingUtils with methods for leveraging the FPAPI

Modify concerned classes and methods to call SwingUtils and use Rectangle2D and float/double variables instead of Rectangle and integers:

Layout/painter changes:

Adapt to Java 11: Replace calls to deprecated elements with corresponding elements for Java 9 or later Rectangles and bounds:

Events:

Also modified the Demo Root Pane:

Best Regards

Peer


Peer Törngren DbVis Software AB Stockholm, Sweden http://www.dbvis.com

Our products help over 25,000 customers in 145 countries

------ Original Message ------ From "Peer Törngren" @.> To "bobbylight/RSyntaxTextArea" @.> Date 2023-10-26, 08:54:31 Subject Re[2]: [bobbylight/RSyntaxTextArea] Incorrect text rendering with Font at 125% on Windows (Issue #457)

Hi

So far everything seems to work as expected, but since most of us run macOS, the fix isn’t as thoroughly verified in daily use as we would have liked. And since we have not yet released any version with this fix, we haven’t had it verified by users.

Best Regards

Peer


Peer Törngren DbVis Software AB Stockholm, Sweden http://www.dbvis.com

Our products help over 25,000 customers in 145 countries

------ Original Message ------ From "PAX523" @.> To "bobbylight/RSyntaxTextArea" @.> Cc "Peer Törngren" @.>; "Mention" @.> Date 2023-10-26, 08:42:28 Subject Re: [bobbylight/RSyntaxTextArea] Incorrect text rendering with Font at 125% on Windows (Issue #457)

Hallo @ptorngren https://github.com/ptorngren! Great that you investigated into that issue and that you are approaching a fix! Good job! Did you observe some anomalies after your fix so far?

@bobbylight https://github.com/bobbylight, can you please collaborate with Peer in order to find a solution according to the screen scale change event and the internal support for FPAPI?

— Reply to this email directly, view it on GitHub https://github.com/bobbylight/RSyntaxTextArea/issues/457#issuecomment-1780505706, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDCYSYH5EYCHK2E4E7CBF3YBIA5JAVCNFSM55SPNVJKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZYGA2TANJXGA3A. You are receiving this because you were mentioned.Message ID: @.***>

ptorngren commented 11 months ago

Hi

If you’re interested, we’ve just released a BETA version of DbVisualizer where we use RSTA 3.3.3 with the adjustments we made for fractional scaling on Windows (included in the PR I created). We’re still waiting for it to be exposed to more users in the official release, but as far as we can tell, it works as expected.

Best Regards

Peer


Peer Törngren DbVis Software AB Stockholm, Sweden http://www.dbvis.com

Our products help over 25,000 customers in 145 countries

------ Original Message ------ From "PAX523" @.> To "bobbylight/RSyntaxTextArea" @.> Cc "Peer Törngren" @.>; "Mention" @.> Date 2023-10-26, 08:42:28 Subject Re: [bobbylight/RSyntaxTextArea] Incorrect text rendering with Font at 125% on Windows (Issue #457)

Hallo @ptorngren https://github.com/ptorngren! Great that you investigated into that issue and that you are approaching a fix! Good job! Did you observe some anomalies after your fix so far?

@bobbylight https://github.com/bobbylight, can you please collaborate with Peer in order to find a solution according to the screen scale change event and the internal support for FPAPI?

— Reply to this email directly, view it on GitHub https://github.com/bobbylight/RSyntaxTextArea/issues/457#issuecomment-1780505706, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDCYSYH5EYCHK2E4E7CBF3YBIA5JAVCNFSM55SPNVJKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCNZYGA2TANJXGA3A. You are receiving this because you were mentioned.Message ID: @.***>

PAX523 commented 10 months ago

@ptorngren, may I ask you to deploy your fix as a pre-release fork to the Maven repository?

ptorngren commented 10 months ago

@ptorngren, may I ask you to deploy your fix as a pre-release fork to the Maven repository?

Will do as soon as possible, but time is a bit tight at the moment .

PAX523 commented 10 months ago

I see. Thank you so much for all your efforts.

ptorngren commented 10 months ago

@PAX523 we've found one more issue that I'd like to address; selecting text can in some cases cause a slight distortion.

The probable cause seems to be that org.fife.ui.rtextarea.ChangeableHighlightPainter.paintLayer() calls java.awt.Shape.getBounds() (returning an integer-based Rectangle) when it probably should call java.awt.Shape.getBounds2D() (returning a double-based Rectangle2D).

Will look into this as well as soon as time permits.

ptorngren commented 7 months ago

Any news on this? We're planning to update our fork to 3.4.0 and would like to avoid any merge conflicts with the fixes we did for the scaling issue. Are there any plans to accept the PR I issued on October 27? Or are there any other plans to handle the fractional scaling issue?

@PAX523 we have not yet addressed the minor issue we found since it doesn't affect our application. We could make an effort to upload the version we have if that means that the changes will find their way into the core product, but I assumed that the PR was the preferred way to do this - what is the purpose of deploying a pre-release fork to the Maven repository?

PAX523 commented 7 months ago

@ptorngren this fix isn't urgent for us anymore. We found a way to work around entirely. So, there's no need for a pre-release anymore.

ptorngren commented 7 months ago

@PAX523 confused ... are you saying that the problem is solved, or that you do not intend to fix it, or that you will accept the PR without the need for a pre-release?

I just pulled the most recent changes from master, and as far as I can tell, the problems are still visible in the demo app when running 125% scaling on Windows.

Here's a screenshot taken when running the demo app on master at revision 45dfcd707f15a56efb194ebebc1fbdf8768c9e38: 125% scaling: image 100% scaling: image

PAX523 commented 7 months ago

Hallo Peer.

I think, this is a misconception 🙂. I'm not the maintainer of RSyntaxTextArea. I'm just a user as you. My company, that I'm working for, builds on top of RSyntaxTextArea. We found a way to circumvent the visualization issues when the scaling on operating system level is changed. Finally, that means, we are in no troubles anymore when it requires more time to merge your pull request.

So, I'm looking forward when your PR will be merged and this issue is getting fixed in the future. But I don't need a snapshot pre-release anymore.

ptorngren commented 7 months ago

I think, this is a misconception 🙂. I'm not the maintainer of RSyntaxTextArea. I'm just a user as you.

@PAX523 ok, I see, I did indeed misunderstand :-) Thanks for clarifying!

ptorngren commented 7 months ago

@bobbylight any news on this? Are there any plans to accept the PR I submitted on October 27 last year, or did you find any issues? As far as I can tell, the fix works as expected. We've released several versions of the application and have had no issues related to fractional scaling on Windows.