Java Code Examples for java.awt.font.FontRenderContext#equals()

The following examples show how to use java.awt.font.FontRenderContext#equals() . 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: FontDesignMetrics.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 2
Source File: FontDesignMetrics.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 3
Source File: FontDesignMetrics.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 4
Source File: FontDesignMetrics.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 5
Source File: FontDesignMetrics.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 6
Source File: FontDesignMetrics.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.usingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 7
Source File: FontDesignMetrics.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 8
Source File: FontDesignMetrics.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 9
Source File: FontDesignMetrics.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 10
Source File: FontDesignMetrics.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 11
Source File: FontDesignMetrics.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 12
Source File: FontDesignMetrics.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 13
Source File: FontDesignMetrics.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}
 
Example 14
Source File: FontDesignMetrics.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static FontDesignMetrics getMetrics(Font font,
                                           FontRenderContext frc) {


    /* When using alternate composites, can't cache based just on
     * the java.awt.Font. Since this is rarely used and we can still
     * cache the physical fonts, its not a problem to just return a
     * new instance in this case.
     * Note that currently Swing native L&F composites are not handled
     * by this code as they use the metrics of the physical anyway.
     */
    SunFontManager fm = SunFontManager.getInstance();
    if (fm.maybeUsingAlternateCompositeFonts() &&
        FontUtilities.getFont2D(font) instanceof CompositeFont) {
        return new FontDesignMetrics(font, frc);
    }

    FontDesignMetrics m = null;
    KeyReference r;

    /* There are 2 possible keys used to perform lookups in metricsCache.
     * If the FRC is set to all defaults, we just use the font as the key.
     * If the FRC is non-default in any way, we construct a hybrid key
     * that combines the font and FRC.
     */
    boolean usefontkey = frc.equals(getDefaultFrc());

    if (usefontkey) {
        r = metricsCache.get(font);
    } else /* use hybrid key */ {
        // NB synchronization is not needed here because of updates to
        // the metrics cache but is needed for the shared key.
        synchronized (MetricsKey.class) {
            MetricsKey.key.init(font, frc);
            r = metricsCache.get(MetricsKey.key);
        }
    }

    if (r != null) {
        m = (FontDesignMetrics)r.get();
    }

    if (m == null) {
        /* either there was no reference, or it was cleared. Need a new
         * metrics instance. The key to use in the map is a new
         * MetricsKey instance when we've determined the FRC is
         * non-default. Its constructed from local vars so we are
         * thread-safe - no need to worry about the shared key changing.
         */
        m = new FontDesignMetrics(font, frc);
        if (usefontkey) {
            metricsCache.put(font, new KeyReference(font, m));
        } else /* use hybrid key */ {
            MetricsKey newKey = new MetricsKey(font, frc);
            metricsCache.put(newKey, new KeyReference(newKey, m));
        }
    }

    /* Here's where we keep the recent metrics */
    for (int i=0; i<recentMetrics.length; i++) {
        if (recentMetrics[i]==m) {
            return m;
        }
    }

    synchronized (recentMetrics) {
        recentMetrics[recentIndex++] = m;
        if (recentIndex == MAXRECENT) {
            recentIndex = 0;
        }
    }
    return m;
}