RSyntaxTextArea icon indicating copy to clipboard operation
RSyntaxTextArea copied to clipboard

Incorrect text rendering with Font at 125% on Windows

Open olivbrau opened this issue 3 years ago • 19 comments

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 :

  • the text caret can be badly positionned
  • some caracters can overlap on each other
  • etc.

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, ...)

olivbrau avatar Aug 04 '22 13:08 olivbrau

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 avatar Jan 09 '23 06:01 ptorngren

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.

ptorngren avatar Jan 11 '23 09:01 ptorngren

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.

bobbylight avatar Jan 13 '23 14:01 bobbylight

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 avatar Jan 13 '23 17:01 ptorngren

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.

ptorngren avatar Jan 19 '23 11:01 ptorngren

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?

PAX523 avatar Oct 26 '23 06:10 PAX523

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 avatar Oct 26 '23 06:10 ptorngren

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:

  • DefaultTokenPainter.paintBackground()
  • RSyntaxUtilities.getLineWidthUpTo()
  • SyntaxView: tabBase, tabSize, clipStart, clipEnd, .calculateLongestLine(), lineToRect(), damageLineRange(), modelToView(), nextTabStop(), paint()
  • Token.listOffsetToView()
  • TokenImpl: getListOffset(), getWidthUpTo(), listOffsetToView()
  • VisibleWhitespaceTokenPainter()
  • WrappedSyntaxView.modelToView()

Layout/painter changes:

  • SyntaxViiew.nextTabStop(): Add offset for tabs to avoid losing tabs that occur on the same pixel as the preceding character end
  • ConfigurableCaret: use Rectangle2D and floats/doubles, primarily to avoid double lines on thick vertical line style
  • Add RSyntaxTextArea.onGraphicsChange() to let owner refresh font metrics when graphic properties change
  • DefaultTokenPainter: Fix tab lines by offsetting the line location slightly
  • SwingUtils.drawChars(): Use TextLayout for very long lines or containing complex characters (inspired by SwingUtilities2)

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

  • add SwingUtils.getBounds() to wrap calls to modelToView2D(pos).getBounds() in a null-safe way
  • replace calls to JTextComponent.modelToView(int pos) with calls to SwingUtils.getBounds(JTextComponent c, int pos)
  • replace calls to JTextComponent.viewToModel(Point p) with calls to JTextComponent.viewToModel2D(Point2D p)

Events:

  • replace calls to InputEvent.getModifiers() with calls to MouseEvent.getModifiersEx()
  • replace calls to Toolkit.getMenuShortcutKeyMask() Toolkit.getMenuShortcutKeyMaskEx()
  • replace references to Event.META_MASK with InputEvent.META_DOWN_MASK
  • replace references to Event.CTRL_MASK with InputEvent.CTRL_DOWN_MASK
  • replace references to MouseEvent.BUTTON1_MASK with MouseEvent.BUTTON1_DOWN_MASK Not modified:
  • SyntaxScheme calls deprecated XMLReaderFactory.createXMLReader() which should be replaced with SaxParserFactory

Also modified the Demo Root Pane:

  • add a text example
  • show a larger screen centered on monitor
  • add a font selector
  • add menu option to make whitespace visible

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 avatar Oct 27 '23 08:10 ptorngren

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: @.***>

ptorngren avatar Dec 20 '23 07:12 ptorngren

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

PAX523 avatar Jan 18 '24 13:01 PAX523

@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 .

ptorngren avatar Jan 18 '24 15:01 ptorngren

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

PAX523 avatar Jan 18 '24 18:01 PAX523

@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 avatar Jan 23 '24 08:01 ptorngren

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?

ptorngren avatar Apr 15 '24 11:04 ptorngren

@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.

PAX523 avatar Apr 15 '24 12:04 PAX523

@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

ptorngren avatar Apr 15 '24 13:04 ptorngren

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.

PAX523 avatar Apr 15 '24 15:04 PAX523

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 avatar Apr 15 '24 15:04 ptorngren

@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.

ptorngren avatar Apr 15 '24 15:04 ptorngren