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

Draw Attributed text only supports Font, while Family/Weight/Posture is an alternate way. #51

Open
fransbouwmans opened this issue Jun 21, 2023 · 4 comments

Comments

@fransbouwmans
Copy link

Alternate code in PdfBoxGraphics2DFontTextDrawer.java, start line 314.
If FONT attribute is provided that overrides,
otherwise use FAMILY/WEIGHT/POSTURE to create a Font if provided.

        ```

boolean wasAttributeFont = true;
Font attributeFont = (Font) iterator.getAttribute(TextAttribute.FONT);
if (attributeFont == null) {
attributeFont = env.getFont();
String family = (String) iterator.getAttribute(TextAttribute.FAMILY);
if (family != null) {
int defSize = attributeFont.getSize();
int style = Font.PLAIN;

                Number fontSize = ((Number) iterator.getAttribute(TextAttribute.SIZE));
                if (fontSize != null) {
                    defSize = (int)fontSize.floatValue();
                }
                Float weight = (Float) iterator.getAttribute(TextAttribute.WEIGHT);
                if (weight != null && weight > 1.5) {
                	// weight can have many values, TextAttribute.WEIGHT_BOLD = 2.0
                	// There may be fonts where the font itself expresses weight
                	style |=Font.BOLD;
                }
                
                Float posture = (Float) iterator.getAttribute(TextAttribute.POSTURE);
                if (posture != null && TextAttribute.POSTURE_OBLIQUE.equals(posture)) {
                	style |= Font.ITALIC;
                }
                attributeFont = new Font(family, style, defSize);
        	} else {
        		wasAttributeFont = false;
        	}
        }
        PDFont font = applyFont(attributeFont, env);
@fransbouwmans
Copy link
Author

In my application, I use styling to add bold or italic style to the text.
With a support for marks like text, which is then parsed into an AttributeString that is drawn. This worked when drawing on a panel, but did not transfer to the generated pdf. With this update that works fine.

As seen in application on panel
image

As seen in the pdf when generated:
image

Note thatin addition superscript and subscript would also be desirable, but have not found how that can translate to the pdf (drawing attributed string with those elements works fine).

@rototor
Copy link
Owner

rototor commented Jun 21, 2023

Would you mind making a pull request with your changes?

Sub- and superscript has likely to be emulated by creating a derived font and then adding some text matrix transformation to it. That could be tricky. As you might need to exit the text mode and enter it again.

If you provide a pull request I'll can review this and try to get a release with this fix out soon. Otherwise it can take some time, as I'm rather busy at the moment.

@fransbouwmans
Copy link
Author

Looking at your test code, I would refer to the definition of TextAttribute and the use of Font:
https://docs.oracle.com/javase/8/docs/api/java/awt/font/TextAttribute.html#FONT
Here it specifically indicates that when the "FONT" is used primary attributes are taken from the Font and only secondary attributes can overwrite the rendering. As Superscript is a primary attribute that may explain why it is not working in combination with FONT (although the same would apply to weight and posture).

@fransbouwmans
Copy link
Author

Looking at the images, the culprit seems to be the new function applyTextWidth, which calculates the new position based on the added text. Here I assumed a horizontal text in order to keep the baseline of the text (i.e. the subscript/superscript have an translation to move the text vertically, which is temporary, i.e. does not extend past the superscript text). This of course works fine if there is no other translation or rotation applied.
With rotated text, the move along the baseline is also rotated and that is now not applied as the Y component is discarded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants