Skip to content

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

@fransbouwmans

Description

@fransbouwmans

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions