Java Code Examples for java.awt.Font#hasLayoutAttributes()

The following examples show how to use java.awt.Font#hasLayoutAttributes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextSourceLabel.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public AffineTransform getBaselineTransform() {
    Font font = source.getFont();
    if (font.hasLayoutAttributes()) {
        return AttributeValues.getBaselineTransform(font.getAttributes());
    }
    return null;
}
 
Example 2
Source File: TextLayout.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 3
Source File: TextLayout.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 4
Source File: TextSourceLabel.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public AffineTransform getBaselineTransform() {
    Font font = source.getFont();
    if (font.hasLayoutAttributes()) {
        return AttributeValues.getBaselineTransform(font.getAttributes());
    }
    return null;
}
 
Example 5
Source File: StandardGlyphVector.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private float getTracking(Font font) {
    if (font.hasLayoutAttributes()) {
        AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
        return values.getTracking();
    }
    return 0;
}
 
Example 6
Source File: TextSourceLabel.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public AffineTransform getBaselineTransform() {
    Font font = source.getFont();
    if (font.hasLayoutAttributes()) {
        return AttributeValues.getBaselineTransform(font.getAttributes());
    }
    return null;
}
 
Example 7
Source File: TextLayout.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 8
Source File: TextLayout.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 9
Source File: StandardGlyphVector.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private float getTracking(Font font) {
    if (font.hasLayoutAttributes()) {
        AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
        return values.getTracking();
    }
    return 0;
}
 
Example 10
Source File: StandardGlyphVector.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private float getTracking(Font font) {
    if (font.hasLayoutAttributes()) {
        AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
        return values.getTracking();
    }
    return 0;
}
 
Example 11
Source File: TextLayout.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 12
Source File: TextSourceLabel.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public AffineTransform getBaselineTransform() {
    Font font = source.getFont();
    if (font.hasLayoutAttributes()) {
        return AttributeValues.getBaselineTransform(font.getAttributes());
    }
    return null;
}
 
Example 13
Source File: StandardGlyphVector.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private float getTracking(Font font) {
    if (font.hasLayoutAttributes()) {
        AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
        return values.getTracking();
    }
    return 0;
}
 
Example 14
Source File: TextSourceLabel.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public AffineTransform getBaselineTransform() {
    Font font = source.getFont();
    if (font.hasLayoutAttributes()) {
        return AttributeValues.getBaselineTransform(font.getAttributes());
    }
    return null;
}
 
Example 15
Source File: TextLayout.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 16
Source File: StandardGlyphVector.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private float getTracking(Font font) {
    if (font.hasLayoutAttributes()) {
        AttributeValues values = ((AttributeMap)font.getAttributes()).getValues();
        return values.getTracking();
    }
    return 0;
}
 
Example 17
Source File: TextLayout.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 18
Source File: TextLayout.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a <code>TextLayout</code> from a <code>String</code>
 * and a {@link Font}.  All the text is styled using the specified
 * <code>Font</code>.
 * <p>
 * The <code>String</code> must specify a single paragraph of text,
 * because an entire paragraph is required for the bidirectional
 * algorithm.
 * @param string the text to display
 * @param font a <code>Font</code> used to style the text
 * @param frc contains information about a graphics device which is needed
 *       to measure the text correctly.
 *       Text measurements can vary slightly depending on the
 *       device resolution, and attributes such as antialiasing.  This
 *       parameter does not specify a translation between the
 *       <code>TextLayout</code> and user space.
 */
public TextLayout(String string, Font font, FontRenderContext frc) {

    if (font == null) {
        throw new IllegalArgumentException("Null font passed to TextLayout constructor.");
    }

    if (string == null) {
        throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
    }

    if (string.length() == 0) {
        throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
    }

    Map<? extends Attribute, ?> attributes = null;
    if (font.hasLayoutAttributes()) {
        attributes = font.getAttributes();
    }

    char[] text = string.toCharArray();
    if (sameBaselineUpTo(font, text, 0, text.length) == text.length) {
        fastInit(text, font, attributes, frc);
    } else {
        AttributedString as = attributes == null
            ? new AttributedString(string)
            : new AttributedString(string, attributes);
        as.addAttribute(TextAttribute.FONT, font);
        standardInit(as.getIterator(), text, frc);
    }
}
 
Example 19
Source File: PSPathGraphics.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
protected void drawString(String str, float x, float y,
                          Font font, FontRenderContext frc, float w) {
    if (str.length() == 0) {
        return;
    }

    /* If the Font has layout attributes we need to delegate to TextLayout.
     * TextLayout renders text as GlyphVectors. We try to print those
     * using printer fonts - ie using Postscript text operators so
     * we may be reinvoked. In that case the "!printingGlyphVector" test
     * prevents us recursing and instead sends us into the body of the
     * method where we can safely ignore layout attributes as those
     * are already handled by TextLayout.
     */
    if (font.hasLayoutAttributes() && !printingGlyphVector) {
        TextLayout layout = new TextLayout(str, font, frc);
        layout.draw(this, x, y);
        return;
    }

    Font oldFont = getFont();
    if (!oldFont.equals(font)) {
        setFont(font);
    } else {
        oldFont = null;
    }

    boolean drawnWithPS = false;

    float translateX = 0f, translateY = 0f;
    boolean fontisTransformed = getFont().isTransformed();

    if (fontisTransformed) {
        AffineTransform fontTx = getFont().getTransform();
        int transformType = fontTx.getType();
        /* TYPE_TRANSLATION is a flag bit but we can do "==" here
         * because we want to detect when its just that bit set and
         *
         */
        if (transformType == AffineTransform.TYPE_TRANSLATION) {
            translateX = (float)(fontTx.getTranslateX());
            translateY = (float)(fontTx.getTranslateY());
            if (Math.abs(translateX) < 0.00001) translateX = 0f;
            if (Math.abs(translateY) < 0.00001) translateY = 0f;
            fontisTransformed = false;
        }
    }

    boolean directToPS = !fontisTransformed;

    if (!PSPrinterJob.shapeTextProp && directToPS) {

        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
        if (psPrinterJob.setFont(getFont())) {

            /* Set the text color.
             * We should not be in this shape printing path
             * if the application is drawing with non-solid
             * colors. We should be in the raster path. Because
             * we are here in the shape path, the cast of the
             * paint to a Color should be fine.
             */
            try {
                psPrinterJob.setColor((Color)getPaint());
            } catch (ClassCastException e) {
                if (oldFont != null) {
                    setFont(oldFont);
                }
                throw new IllegalArgumentException(
                                            "Expected a Color instance");
            }

            psPrinterJob.setTransform(getTransform());
            psPrinterJob.setClip(getClip());

            drawnWithPS = psPrinterJob.textOut(this, str,
                                               x+translateX, y+translateY,
                                               font, frc, w);
        }
    }

    /* The text could not be converted directly to PS text
     * calls so decompose the text into a shape.
     */
    if (drawnWithPS == false) {
        if (oldFont != null) {
            setFont(oldFont);
            oldFont = null;
        }
        super.drawString(str, x, y, font, frc, w);
    }

    if (oldFont != null) {
        setFont(oldFont);
    }
}
 
Example 20
Source File: PSPathGraphics.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
protected void drawString(String str, float x, float y,
                          Font font, FontRenderContext frc, float w) {
    if (str.length() == 0) {
        return;
    }

    /* If the Font has layout attributes we need to delegate to TextLayout.
     * TextLayout renders text as GlyphVectors. We try to print those
     * using printer fonts - ie using Postscript text operators so
     * we may be reinvoked. In that case the "!printingGlyphVector" test
     * prevents us recursing and instead sends us into the body of the
     * method where we can safely ignore layout attributes as those
     * are already handled by TextLayout.
     */
    if (font.hasLayoutAttributes() && !printingGlyphVector) {
        TextLayout layout = new TextLayout(str, font, frc);
        layout.draw(this, x, y);
        return;
    }

    Font oldFont = getFont();
    if (!oldFont.equals(font)) {
        setFont(font);
    } else {
        oldFont = null;
    }

    boolean drawnWithPS = false;

    float translateX = 0f, translateY = 0f;
    boolean fontisTransformed = getFont().isTransformed();

    if (fontisTransformed) {
        AffineTransform fontTx = getFont().getTransform();
        int transformType = fontTx.getType();
        /* TYPE_TRANSLATION is a flag bit but we can do "==" here
         * because we want to detect when its just that bit set and
         *
         */
        if (transformType == AffineTransform.TYPE_TRANSLATION) {
            translateX = (float)(fontTx.getTranslateX());
            translateY = (float)(fontTx.getTranslateY());
            if (Math.abs(translateX) < 0.00001) translateX = 0f;
            if (Math.abs(translateY) < 0.00001) translateY = 0f;
            fontisTransformed = false;
        }
    }

    boolean directToPS = !fontisTransformed;

    if (!PSPrinterJob.shapeTextProp && directToPS) {

        PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
        if (psPrinterJob.setFont(getFont())) {

            /* Set the text color.
             * We should not be in this shape printing path
             * if the application is drawing with non-solid
             * colors. We should be in the raster path. Because
             * we are here in the shape path, the cast of the
             * paint to a Color should be fine.
             */
            try {
                psPrinterJob.setColor((Color)getPaint());
            } catch (ClassCastException e) {
                if (oldFont != null) {
                    setFont(oldFont);
                }
                throw new IllegalArgumentException(
                                            "Expected a Color instance");
            }

            psPrinterJob.setTransform(getTransform());
            psPrinterJob.setClip(getClip());

            drawnWithPS = psPrinterJob.textOut(this, str,
                                               x+translateX, y+translateY,
                                               font, frc, w);
        }
    }

    /* The text could not be converted directly to PS text
     * calls so decompose the text into a shape.
     */
    if (drawnWithPS == false) {
        if (oldFont != null) {
            setFont(oldFont);
            oldFont = null;
        }
        super.drawString(str, x, y, font, frc, w);
    }

    if (oldFont != null) {
        setFont(oldFont);
    }
}