bfour
bfour

Reputation: 160

Unexpected padding to the left of first letter/unexpected typographic behaviour in JTextPane

I've implemented a multiline label by extending a JTextPane. The constructor sets various properties to make it look like a label, including disabling any border/setting margins to 0 which works well.

Environment:

When I increase the font size, the first letter sometimes has "blank space"/a margin of ~1px at 19pt (probably increasing with font size) to its left. This happens at least for letters B, F and L, but certainly not for A. Here's an example:

enter image description here enter image description here

On the left you can clearly see that the layout looks broken with the title having this weird margin on the left. Please note that the first line with the number (1861) is a regular JLabel.

Zooming in confirms this (the pink line is for illustration):

enter image description here enter image description here

So from what I can see the typesetting is improper.

Can this be considered a bug in swing? Is there a way to solve this? Eg. is there an easy and clean (ie. not paint()-ing) way to have fine-grained control over typographic features in swing in this context?

EDIT:

This is similar to what I would expect:

enter image description here enter image description here

vs before:

enter image description here enter image description here

Upvotes: 4

Views: 105

Answers (1)

Tim B
Tim B

Reputation: 41188

If you look at your screenshot here:

The image

And in particular look at the 1861...you can see that there is a larger space on both sides of the 1. In particular the gap between 1 and 8 and between 6 and 1 is larger than that between 8 and 6.

That is just how the layout has been arranged on that particular font. They clearly thought that a 1 was getting pushed too close to the characters around it and so they added more space on both sides.

Your options to "fix" this are limited.

  1. Use a different font.
  2. Render the line to an image, scan for empty columns, shuffle it left
  3. Build in a few manual hacks for common characters (i.e. if string starts with 1 then shuffle the line left 1 pixel
  4. Indent or outdent the title deliberately so it's not lined up and then the offset is no longer visible.

i.e.

1861
    Baked Beans
    dkjfdf skdfjsdlf

Upvotes: 1

Related Questions