Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot enable ligatures (JetBrains Mono font) #503

Open
tferr opened this issue Apr 21, 2023 · 4 comments
Open

Cannot enable ligatures (JetBrains Mono font) #503

tferr opened this issue Apr 21, 2023 · 4 comments
Assignees
Labels

Comments

@tferr
Copy link

tferr commented Apr 21, 2023

JetBrains mono is a much-improved font for coding, featuring many readability features such as coding-specific ligatures. I am not able to use this font properly in RSyntaxTextArea.

Take this snippet that requires FlatLaf and the FlatLaf JetBrains Mono distribution (direct downloadable jars for both dependencies: flatlaf, flatlaf-fonts-jetbrains-mono):

import java.awt.Font;
import java.awt.font.TextAttribute;
import java.util.*;
import javax.swing.*;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

import com.formdev.flatlaf.fonts.jetbrains_mono.FlatJetBrainsMonoFont;
import com.formdev.flatlaf.util.FontUtils;

public class Test {

	public Test() {
		FlatJetBrainsMonoFont.install();
		run(new JTextArea());
		run(new RSyntaxTextArea());
	}

	void run(final JTextArea ta) {
		final Font f = FontUtils.getCompositeFont(FlatJetBrainsMonoFont.FAMILY, Font.PLAIN, 40);
		final Map<TextAttribute, Object> textAttributes = new HashMap<>();
		textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
		textAttributes.put(TextAttribute.LIGATURES, TextAttribute.LIGATURES_ON);
		ta.setFont(f.deriveFont(textAttributes));
		ta.setText("Current font: " + ta.getFont().getFontName() + "\nligatures: ## <> => ++ ~~ <= >= <=> ->>");
		JFrame frame = new JFrame(ta.getClass().getSimpleName());
		frame.getContentPane().add(ta);
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(final String[] args) {
		new Test();
	}

The ligatures do render in JTextArea:
image

..but not in a RSyntaxTextArea:

image

Why is that? I could not find any obvious signs that TextAttributes are being overridden in the code base.

@tferr tferr added the bug label Apr 21, 2023
@tferr
Copy link
Author

tferr commented Apr 21, 2023

pinging @DevCharly in case they have experienced this elsewhere

@DevCharly
Copy link

I'm actually surprised that ligatures work in Swing 😮
Didn't know TextAttribute.LIGATURES...

So I tried to enable ligatures in FlatLaf Theme Editor, which uses RSyntaxTextArea, and it works 😮

image

I think the reason is that FlatLaf Theme Editor uses a subclass of org.fife.ui.rsyntaxtextarea.SyntaxScheme, which always uses the same font that has the ligatures attribute.

There are several places in SyntaxScheme where a new font is created using family, style and size, but without attributes. I think the ligature attribute is lost there. E.g.

StyleContext sc = StyleContext.getDefaultStyleContext();
Font boldFont = sc.getFont(baseFont.getFamily(), Font.BOLD,
baseFont.getSize());
Font italicFont = sc.getFont(baseFont.getFamily(), Font.ITALIC,
baseFont.getSize());

or

StyleContext sc = StyleContext.getDefaultStyleContext();
style.font = sc.getFont(font.getFamily(), s, font.getSize());

To fix this, and keep the usage of StyleContext.getFont(), it is probably necessary to additionally invoke font.deriveFont(Map attributes) to reapply some font attributes of the base font.

@tferr
Copy link
Author

tferr commented Jun 5, 2023

Thanks a lot for this. I'm closing it, as this is a reasonable workaround.

@tferr tferr closed this as completed Jun 5, 2023
@bobbylight
Copy link
Owner

I'll reopen this to make the necessary changes in SyntaxScheme pointed out above. Thanks folks for this!

@bobbylight bobbylight reopened this Jul 9, 2023
@bobbylight bobbylight added this to the 3.3.4 milestone Jul 9, 2023
@bobbylight bobbylight self-assigned this Jul 9, 2023
@bobbylight bobbylight removed this from the 3.3.4 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants