Method from sun.font.FontDesignMetrics Detail: |
public int charWidth(char ch) {
// default metrics for compatibility with legacy code
float w;
if (ch < 0x100) {
w = getLatinCharWidth(ch);
}
else {
w = handleCharWidth(ch);
}
return (int)(0.5 + w);
}
|
public int charWidth(int ch) {
if (!Character.isValidCodePoint(ch)) {
ch = 0xffff;
}
float w = handleCharWidth(ch);
return (int)(0.5 + w);
}
|
public int charsWidth(char[] data,
int off,
int len) {
float width = 0;
if (font.hasLayoutAttributes()) {
if (len == 0) {
return 0;
}
String str = new String(data, off, len);
width = new TextLayout(str, font, frc).getAdvance();
} else {
/* Explicit test needed to satisfy superclass spec */
if (len < 0) {
throw new IndexOutOfBoundsException("len="+len);
}
int limit = off + len;
for (int i=off; i < limit; i++) {
char ch = data[i];
if (ch < 0x100) {
width += getLatinCharWidth(ch);
} else if (FontUtilities.isNonSimpleChar(ch)) {
String str = new String(data, off, len);
width = new TextLayout(str, font, frc).getAdvance();
break;
} else {
width += handleCharWidth(ch);
}
}
}
return (int) (0.5 + width);
}
|
public int getAscent() {
return (int)(roundingUpValue + this.ascent);
}
|
public int getDescent() {
return (int)(roundingUpValue + this.descent);
}
|
public FontRenderContext getFontRenderContext() {
return frc;
}
|
public int getHeight() {
if (height < 0) {
height = getAscent() + (int)(roundingUpValue + descent + leading);
}
return height;
}
|
public int getLeading() {
// nb this ensures the sum of the results of the public methods
// for leading, ascent & descent sum to height.
// if the calculations in any other methods change this needs
// to be changed too.
// the 0.95 value used here and in the other methods allows some
// tiny fraction of leeway before rouding up. A higher value (0.99)
// caused some excessive rounding up.
return
(int)(roundingUpValue + descent + leading) -
(int)(roundingUpValue + descent);
}
|
public int getMaxAdvance() {
return (int)(0.99f + this.maxAdvance);
}
|
public static FontDesignMetrics getMetrics(Font font) {
return getMetrics(font, getDefaultFrc());
}
|
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;
}
|
public int[] getWidths() {
int[] widths = new int[256];
for (char ch = 0 ; ch < 256 ; ch++) {
float w = advCache[ch];
if (w == UNKNOWN_WIDTH) {
w = advCache[ch] = handleCharWidth(ch);
}
widths[ch] = (int) (0.5 + w);
}
return widths;
}
Gets the advance widths of the first 256 characters in the
Font . The advance is the
distance from the leftmost point to the rightmost point on the
character's baseline. Note that the advance of a
String is not necessarily the sum of the advances
of its characters. |
public int stringWidth(String str) {
float width = 0;
if (font.hasLayoutAttributes()) {
/* TextLayout throws IAE for null, so throw NPE explicitly */
if (str == null) {
throw new NullPointerException("str is null");
}
if (str.length() == 0) {
return 0;
}
width = new TextLayout(str, font, frc).getAdvance();
} else {
int length = str.length();
for (int i=0; i < length; i++) {
char ch = str.charAt(i);
if (ch < 0x100) {
width += getLatinCharWidth(ch);
} else if (FontUtilities.isNonSimpleChar(ch)) {
width = new TextLayout(str, font, frc).getAdvance();
break;
} else {
width += handleCharWidth(ch);
}
}
}
return (int) (0.5 + width);
}
|