Search in sources :

Example 11 with FontInfo

use of com.xenoage.utils.font.FontInfo in project Zong by Xenoage.

the class TextLayoutTools method createAttributesMap.

/**
 * Creates a Map with the attributes representing
 * the given style for an AttibutedString.
 */
private static Map<TextAttribute, Object> createAttributesMap(FormattedTextStyle style) {
    Map<TextAttribute, Object> ret = new HashMap<>();
    FontInfo fontInfo = notNull(style.getFont(), FontInfo.defaultValue);
    // font name
    Font font = toAwtFont(fontInfo);
    ret.put(TextAttribute.FAMILY, font.getFamily());
    // font size
    ret.put(TextAttribute.SIZE, font.getSize2D());
    // color
    ret.put(TextAttribute.FOREGROUND, toAwtColor(style.getColor()));
    // bold
    FontStyle fontStyle = fontInfo.getStyle();
    ret.put(TextAttribute.WEIGHT, (fontStyle.isSet(FontStyle.Bold) ? TextAttribute.WEIGHT_BOLD : TextAttribute.WEIGHT_REGULAR));
    // italic
    ret.put(TextAttribute.POSTURE, (fontStyle.isSet(FontStyle.Italic) ? TextAttribute.POSTURE_OBLIQUE : TextAttribute.POSTURE_REGULAR));
    // underline
    ret.put(TextAttribute.UNDERLINE, (fontStyle.isSet(FontStyle.Underline) ? TextAttribute.UNDERLINE_ON : null));
    // superscript
    switch(style.getSuperscript()) {
        case Super:
            ret.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
            break;
        case Sub:
            ret.put(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
            break;
        default:
            ret.put(TextAttribute.SUPERSCRIPT, null);
    }
    return ret;
}
Also used : FontStyle(com.xenoage.utils.font.FontStyle) HashMap(java.util.HashMap) TextAttribute(java.awt.font.TextAttribute) FontInfo(com.xenoage.utils.font.FontInfo) AwtFontUtils.toAwtFont(com.xenoage.utils.jse.font.AwtFontUtils.toAwtFont)

Example 12 with FontInfo

use of com.xenoage.utils.font.FontInfo in project Zong by Xenoage.

the class FormattedTextConverter method getStyleFromAttributeSet.

private static FormattedTextStyle getStyleFromAttributeSet(AttributeSet attr) {
    if (attr == null) {
        return FormattedTextStyle.Companion.getDefaultStyle();
    }
    // font style
    FontStyle fontStyle = FontStyle.normal;
    if (StyleConstants.isBold(attr)) {
        fontStyle = fontStyle.with(FontStyle.Bold);
    }
    if (StyleConstants.isItalic(attr)) {
        fontStyle = fontStyle.with(FontStyle.Italic);
    }
    if (StyleConstants.isUnderline(attr)) {
        fontStyle = fontStyle.with(FontStyle.Underline);
    }
    if (StyleConstants.isStrikeThrough(attr)) {
        fontStyle = fontStyle.with(FontStyle.Strikethrough);
    }
    // superscript
    Superscript superscript = Superscript.Normal;
    if (StyleConstants.isSuperscript(attr)) {
        superscript = Superscript.Super;
    }
    if (StyleConstants.isSubscript(attr)) {
        superscript = Superscript.Sub;
    }
    // font
    FontInfo fontInfo = new FontInfo(StyleConstants.getFontFamily(attr), (float) StyleConstants.getFontSize(attr), fontStyle);
    return new FormattedTextStyle(fontInfo, fromAwtColor(StyleConstants.getForeground(attr)), superscript);
}
Also used : FontStyle(com.xenoage.utils.font.FontStyle) FormattedTextStyle(com.xenoage.zong.core.text.FormattedTextStyle) Superscript(com.xenoage.zong.core.text.Superscript) FontInfo(com.xenoage.utils.font.FontInfo)

Aggregations

FontInfo (com.xenoage.utils.font.FontInfo)12 FormattedTextStyle (com.xenoage.zong.core.text.FormattedTextStyle)9 FormattedTextString (com.xenoage.zong.core.text.FormattedTextString)5 FontStyle (com.xenoage.utils.font.FontStyle)4 FormattedTextParagraph (com.xenoage.zong.core.text.FormattedTextParagraph)4 FormattedTextElement (com.xenoage.zong.core.text.FormattedTextElement)3 Test (org.junit.Test)3 Color (com.xenoage.utils.color.Color)2 Score (com.xenoage.zong.core.Score)2 Chord (com.xenoage.zong.core.music.chord.Chord)2 FormattedText (com.xenoage.zong.core.text.FormattedText)2 MaybeNull (com.xenoage.utils.annotations.MaybeNull)1 NonNull (com.xenoage.utils.annotations.NonNull)1 TextMeasurer (com.xenoage.utils.font.TextMeasurer)1 AwtFontUtils.toAwtFont (com.xenoage.utils.jse.font.AwtFontUtils.toAwtFont)1 MusicContext (com.xenoage.zong.core.music.MusicContext)1 Pitch (com.xenoage.zong.core.music.Pitch)1 StemDirection (com.xenoage.zong.core.music.chord.StemDirection)1 Words (com.xenoage.zong.core.music.direction.Words)1 TraditionalKey (com.xenoage.zong.core.music.key.TraditionalKey)1