Can you use font ligatures with JavaFX - java

I'm implementing a text editor based on JavaFX. Is it possible to display font ligatures in the same way they work for example in Intellij?
I've found a reference to ligature support in the JavaFX API, but I don't know if this "proves" that there's support.

You can use Tomas Mikula RichTextFX library.
https://github.com/FXMisc/RichTextFX/
You many options here :
1) InlineCssTextArea uses the 
Node#setStyle(String cssStyle)
 method to style Text objects of
InlineCssTextArea
and for each word or line you can set a different style using :
InlineCssTextArea.setStyle(from,to,style);
I have extensively used it before to have different fonts and styles inside the same line or lines.
2) StyleClassedTextArea uses the Node#setStyleClass(String styleClass) method to style Text objects. You can define the style classes in your stylesheet.
So let's say that inside your application.css you have defined 5 different style classes with different fonts. One of them might be...
.red { -fx-fill: red; }
so you can use :
styleClassedTextArea.setStyleClass(from, to, "red");
This renders the text in the range [from, to) in red.
3) For more please check https://github.com/FXMisc/RichTextFX it has detailed description.
Also you can search for more examples on the web.

JavaFX will render required ligatures, but it has no ability to opt in to optional ligatures. The issue tracking adding an API to enable this behaviour is https://bugs.openjdk.org/browse/JDK-8091616.
See also this message from the OpenJFX dev mailing list: https://mail.openjdk.org/pipermail/openjfx-dev/2022-October/036309.html

Related

How to insert into NatTable cell two texts (each with a different font) and one image? and set the margins?

Two questions:
1) How to insert into NatTable cell two text (each with a different font) and a image?
2) how to set the margins as the below image?
In the following cases:
a) Using java 1.6 (no RichTexCellEditor, no css).
b) Using java 1.7 (with RichTexCellEditor, no css).
c) Using java 1.8 (with RichTexCellEditor, with css).
Thank you very much in advance.
NatTable styling is based on styles and painters as explained in [1]. For further information on NatTable configuration and getting started with NatTable, have a look at our Getting Started Tutorial [2]
To create a combination like the asked one, a combination of ICellPainter needs to be registered.
To add padding (not margin) between the cell border and the content, use the PaddingDecorator
To add an image additional to text, use a CellPainterDecorator that has a painter for the text as base and a ImagePainter as decorator.
Rendering text with different fonts is only supported by the RichTextCellPainter of the NatTable Nebula Extension. That one requires Java 1.7.
CSS styling in NatTable is used to create a styling configuration via CSS. There is no additional support despite that. The CSS support is contained in the NatTable E4 Extension that requires Java 1.8. The explanation is available in the 1.4 New & Noteworthy page [3]
So the answer to a) is: Rendering of text with two different fonts is not supported by NatTable Core, therefore you need to implement a custom ICellPainter that would support that.
The answer to b) is to create a complex painter and register it like this:
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_PAINTER,
new BackgroundPainter(
new PaddingDecorator(
new CellPainterDecorator(
new PaddingDecorator(new RichTextCellPainter(), 10, 0, 0, 0),
CellEdgeEnum.LEFT,
new ImagePainter()),
2, 5, 2, 5)),
DisplayMode.NORMAL,
ColumnLabelAccumulator.COLUMN_LABEL_PREFIX + 0);
Where the above example implies that there are column labels applied by the ColumnLabelAccumulator and the painter should only be configured for the first column. Also note that the style for that concrete cell would need the attribute CellStyleAttributes.IMAGE set, which is then be rendered by the ImagePainter. If the image should be fixed, it needs to be set as constructor parameter.
Of course the content needs to contain the necessary HTML markup for rendering different fonts. Something like:
<p><strong><span style="font-size:14px">This is a test</span></strong></p>
<p><span style="color:rgb(128, 128, 128)"><span style="font-size:12px">This is a test</span></span></p>
The answer to c) is not that trivial. First the complex painter structure shown above can't be accomplished with CSS. The reason is that multiple paddings need to be configured, which is not supported by the NatTable CSS.
Second the RichTextPainter is not known to the E4 Extension. Therefore it needs to be registered manually to the CellPainterFactory, e.g.
CellPainterFactory.getInstance().registerContentPainter(
"richtext",
(painterProperties, underlying) -> {
return new RichTextCellPainter();
});
where the handling of additional attributes need to be considered somehow.
In the CSS it would then look somehow like this:
painter: background padding decorator richtext;
decoration: left url('./nebula_logo_16.png') 5 true;
padding: 2 5;
[1] https://www.eclipse.org/nattable/documentation.php?page=styling
[2] http://www.vogella.com/tutorials/NatTable/article.html
[3] https://www.eclipse.org/nattable/nandn/nandn_140.php

Selenium Webdriver: Checking all fonts on a page

For a Sprint-story, I need to assert that a certain font is not available anymore on different pages. Right now, I can verify that a certain element doesn't contain a font and is indeed replaced by another, but is there a way to check entire pages for the presence of a certain font?
I use Selenium Webdriver cucumber/Java within IntelliJ.
It depends on whether the font is specified through a FONT tag or CSS. FONT tags are obsolete now and discouraged from use but you could use the code below to find them and write the entire tag out to the console.
// find FONT tags
List<WebElement> foundFontTags = driver.findElements(By.cssSelector("font[face*='fontname']"));
for (WebElement foundFontTag : foundFontTags)
{
System.out.println(foundFontTag.getAttribute("outerHTML"));
}
To find it in the CSS is going to be a bit more tricky. Your best bet is to talk to dev or your design team and ask how the font might be specified (if you don't already know). The problem is that without that knowledge, you are going to be looking all over the place for the font. It could be specified inline, a STYLE block, in a separate .css file, and so on each of which is going to require its own search. I provided one example for searching for a font in an inline CSS style.
// find font-face in inline CSS
foundFontTags = driver.findElements(By.cssSelector("[style*='font-family:fontname']"));
for (WebElement foundFontTag : foundFontTags)
{
System.out.println(foundFontTag.getAttribute("outerHTML"));
}
If the font is not specified in an external .css file, you might be able to get away with a text search of the entire HTML source for "font-face:fontname".
One other caveat... just because you find a definition of a css class that contains the wrong font doesn't mean it's actually applied to a visible element. That's a whole other issue. You probably should get a good definition of what success looks like from the Sprint team.
You could use Selenium like so:
WebElement.getCssValue("text-align")
But this will be a bit tricky for all the CSS you'll have to validate. Here is a good article about that . Maybe a CSS unit testing like Quixote libs/frameworks will be a better fit. Other possible way (that I've used succesfuly) - visual regressions with huxley, it can support your entire layout presentation's validation.

Make all tooltips multiline in Java (Swing)

Tooltips in my application can be quite long, therefore I'd like them to have line breaks.
I don't want to use html as I'd prefer to set a (max) width of my tooltips instead and have the line breaks dynamically.
In the accepted answer the this similar question Multi-line tooltips in Java? I read about JMultiLineToolTip. Unfortunately the provided link doesn't work anymore and there are many different JMultiLineToolTip out there. Therefore my two questions:
Which JMultiLineToolTip is a good one to use?
How can I use such a class to represent all of the tooltips in my application?
EDIT: as everyone seems to recommend the use of html, is there a way to define the width of my tooltip in pixels (or some other unit than number of characters) using html?
If you are not afraid of extending swing tool tip, you can create your own JMultiLineToolTip:
Extend JTooltip In the extended Tool tip component implementation,
set a custom tool tip UI In customUI implementation
Implementpaint() method to write given string in multi line
Here is an example - it shows how to use it as well
However, to answer your questions:
Which JMultiLineToolTip is a good one to use?
Use <html>
How can I use such a class to represent all of the tooltips in my application?
Per compopnent, it is easy but tedious to achieve as you will have to override creatreToolTip() API. But if you want to change it globally, you may:
(i)Simple way - Register your custom tooltip UI with the UIManager at the beginning of your execution.
UIManager.put( "ToolTipUI", "SeNormToolTipUI" );
UIManager.put( "SeNormToolTipUI",Class.forName( multiLineToolTipUIClassName ) );
(ii) complex way
You will have to start implementing your own look and feel. In the look and feel implementation, you would provide defaults for ToolTipUI as your UI implementation and then set that look and feel to the application you are running. For instance take a look at the MetalLookAndFeel implementation. You may just extend that part and implement your on lnf.
So, it is better to use <html>
1) Html is easiest of ways for plain JToolTip
2) use JWindow(un_decorated JDialog) with JTextArea, better would be JTextPane (supporting stylled text),
the disadvantage is you have to manually set window to the Point, you have to manually set for setInitialDelay and setDismissDelay (Swing Timer), setVisible(true/false)
the advantage is that you using full manageable top level container with definitions for own parent
3) I use JLabel with Html formatted and stylled text added to the GlassPane, notice easiest alternative is use non_opaque JLayeredPane (Java6) or JLayer (Java7)

How do i format a specific word/sentence inside jeditorpane?

I am not a java expert. Just learning as i go.. This is my way! I am now designing a IDE for C++ (just fun, not professional) . I have the project almost ready, now i want to add some text highlighting function to the IDE. For example i want the IDE to recognize a predefined set of words and color them green,red. How do i do it?
You could use HTML, but you probably would be better off using the TextAction methods for a JEditorPane.
Another alternative is the StyledDocument interface.
Added because of the comment: You can use the StyledEditorKit class to see implementations of TextAction.
Here's Oracle's tutorial on How to Use Editor Panes and Text Panes.

Swing JEditorPane CSS capabilities

I am displaying HTML content inside a Swing JEditorPane. To change the default look of the HTML i am using a CSS style sheet. This works great. My problem is only that the JEditorPane does not support the full CSS specification. Is there a list of CSS features the JEditorPane supports?
Looking at the CSS.java sourcecode freom the OpenJava JDK, I found this:
Defines a set of CSS attributes as a typesafe enumeration. The HTML View implementations use CSS attributes to determine how they will render. This also defines methods to map between CSS/HTML/StyleConstants. Any shorthand properties, such as font, are mapped to the intrinsic properties.
The following describes the CSS properties that are suppored by the rendering engine:
font-family
font-style
font-size (supports relative units)
font-weight
font
color
background-color (with the exception of transparent)
background-image
background-repeat
background-position
background
background-repeat
text-decoration (with the exception of blink and overline)
vertical-align (only sup and super)
text-align (justify is treated as center)
margin-top
margin-right
margin-bottom
margin-left
margin
padding-top
padding-right
padding-bottom
padding-left
border-style (only supports inset, outset and none)
list-style-type
list-style-position
The following are modeled, but currently not rendered.
font-variant
background-attachment (background always treated as scroll)
word-spacing
letter-spacing
text-indent
text-transform
line-height
border-top-width (this is used to indicate if a border should be used)
border-right-width
border-bottom-width
border-left-width
border-width
border-top
border-right
border-bottom
border-left
border
width
height
float
clear
display
white-space
list-style
Java has had a relatively poor record with regard to HTML/CSS support. The comment in the docs highlighted by trashgod have been promising improvements for years. Around about the time when JavaFX was being released there was talk of an official JWebPane which would allow Java developers access to the webkit engine, as used in Safari and Chrome. However, it never materialised.
The only advice I can offer is to look at alternative HTML/CSS renders for Java. One I'm often recommending is the xhtmlrenderer project. Development has slowed down as it generally maintains the existing version with the occasional bugfix. It targets CSS2.1, which is often more than adequate; although perhaps it'll move into CSS when the standard is actually finalised.
JEditorPane is very limited. You are better off integrating a native web browser if you want proper HTML display.
Check projects like DJ Native Swing project: http://djproject.sourceforge.net/ns

Categories

Resources