Skip to content

Commit 847a0d6

Browse files
committed
Font metrics upgrade
1 parent cf4480e commit 847a0d6

File tree

17 files changed

+775
-691
lines changed

17 files changed

+775
-691
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20240225103130
1+
20240301123437
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20240225103130
1+
20240301123437
391 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/java/awt/Font.java

Lines changed: 103 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
import sun.font.AttributeValues;
7171
import sun.font.CoreMetrics;
7272
import sun.font.Font2DHandle;
73+
import sun.font.FontDesignMetrics;
7374
import sun.font.FontLineMetrics;
74-
import swingjs.JSFontMetrics;
7575
import swingjs.JSToolkit;
7676

7777
/**
@@ -235,18 +235,10 @@
235235
public class Font
236236
{
237237

238-
FontMetrics fm;
239-
240-
public FontMetrics getFontMetrics() {
241-
if (fm == null)
242-
((JSFontMetrics) (fm = new JSFontMetrics())).setFont(this);
243-
return fm;
238+
public FontMetrics getFontMetrics() {
239+
return FontDesignMetrics.getMetrics(this);
244240
}
245241

246-
public void setFontMetrics(FontMetrics fm) {
247-
this.fm = fm;
248-
}
249-
250242
// /**
251243
// * This is now only used during serialization. Typically
252244
// * it is null.
@@ -422,16 +414,16 @@ public void setFontMetrics(FontMetrics fm) {
422414
*/
423415
private transient boolean nonIdentityTx;
424416

425-
/*
426-
* A cached value used when a transform is required for internal
427-
* use. This must not be exposed to callers since AffineTransform
428-
* is mutable.
429-
*/
430-
private static AffineTransform identityTx;
431-
432-
private static AffineTransform getIdentityTx() {
433-
return (identityTx == null ? (identityTx = new AffineTransform()) : identityTx);
434-
}
417+
// /*
418+
// * A cached value used when a transform is required for internal
419+
// * use. This must not be exposed to callers since AffineTransform
420+
// * is mutable.
421+
// */
422+
// private static AffineTransform identityTx;
423+
//
424+
// private static AffineTransform getIdentityTx() {
425+
// return (identityTx == null ? (identityTx = new AffineTransform()) : identityTx);
426+
// }
435427
/*
436428
* JDK 1.1 serialVersionUID
437429
*/
@@ -2212,11 +2204,7 @@ private FontLineMetrics defaultLineMetrics(FontRenderContext frc) {
22122204
* just pass identity here
22132205
*/
22142206
float [] metrics = new float[8];
2215-
((JSFontMetrics)getFontMetrics()).//getFont2D().
2216-
getMetrics(getIdentityTx(),
2217-
frc.getAntiAliasingHint(),
2218-
frc.getFractionalMetricsHint(),
2219-
metrics);
2207+
((FontDesignMetrics) getFontMetrics()).秘fillMetrics(metrics);
22202208
float ascent = metrics[0];
22212209
float descent = metrics[1];
22222210
float leading = metrics[2];
@@ -2367,7 +2355,7 @@ public LineMetrics getLineMetrics(CharacterIterator ci,
23672355
* @since 1.2
23682356
*/
23692357
public Rectangle2D getStringBounds( String str, FontRenderContext frc) {
2370-
return getStringBoundsStr(str, 0, -1);
2358+
return getStringBoundsStr(str, 0, str.length());
23712359
}
23722360

23732361
/**
@@ -2403,56 +2391,93 @@ public Rectangle2D getStringBounds( String str,
24032391
return getStringBoundsStr(str, beginIndex, limit);
24042392
}
24052393

2406-
/**
2407-
* Returns the logical bounds of the specified array of characters
2408-
* in the specified <code>FontRenderContext</code>. The logical
2409-
* bounds contains the origin, ascent, advance, and height, which
2410-
* includes the leading. The logical bounds does not always enclose
2411-
* all the text. For example, in some languages and in some fonts,
2412-
* accent marks can be positioned above the ascent or below the
2413-
* descent. To obtain a visual bounding box, which encloses all the
2414-
* text, use the {@link TextLayout#getBounds() getBounds} method of
2415-
* <code>TextLayout</code>.
2416-
* <p>Note: The returned bounds is in baseline-relative coordinates
2417-
* (see {@link java.awt.Font class notes}).
2418-
* @param chars an array of characters
2419-
* @param beginIndex the initial offset in the array of
2420-
* characters
2421-
* @param limit the end offset in the array of characters
2422-
* @param frc the specified <code>FontRenderContext</code>
2423-
* @return a <code>Rectangle2D</code> that is the bounding box of the
2424-
* specified array of characters in the specified
2425-
* <code>FontRenderContext</code>.
2426-
* @throws IndexOutOfBoundsException if <code>beginIndex</code> is
2427-
* less than zero, or <code>limit</code> is greater than the
2428-
* length of <code>chars</code>, or <code>beginIndex</code>
2429-
* is greater than <code>limit</code>.
2430-
* @see FontRenderContext
2431-
* @see Font#createGlyphVector
2432-
* @since 1.2
2433-
*/
2434-
public Rectangle2D getStringBounds(char [] chars,
2435-
int beginIndex, int limit,
2436-
FontRenderContext frc) {
2437-
SB sb = new SB();
2438-
sb.appendCB(chars, beginIndex, limit);
2439-
return getStringBoundsStr(sb.toString(), 0, -1);
2440-
}
2441-
2442-
public Rectangle2D getStringBoundsStr(String s, int i, int j) {
2443-
if (j >= i)
2444-
s = s.substring(i, j);
2445-
// if (beginIndex < 0) {
2446-
// throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
2447-
// }
2448-
// if (limit > chars.length) {
2449-
// throw new IndexOutOfBoundsException("limit: " + limit);
2450-
// }
2451-
// if (beginIndex > limit) {
2452-
// throw new IndexOutOfBoundsException("range length: " +
2453-
// (limit - beginIndex));
2454-
// }
2394+
/**
2395+
* Returns the logical bounds of the specified array of characters in the
2396+
* specified <code>FontRenderContext</code>. The logical bounds contains the
2397+
* origin, ascent, advance, and height, which includes the leading. The logical
2398+
* bounds does not always enclose all the text. For example, in some languages
2399+
* and in some fonts, accent marks can be positioned above the ascent or below
2400+
* the descent. To obtain a visual bounding box, which encloses all the text,
2401+
* use the {@link TextLayout#getBounds() getBounds} method of
2402+
* <code>TextLayout</code>.
2403+
* <p>
2404+
* Note: The returned bounds is in baseline-relative coordinates (see
2405+
* {@link java.awt.Font class notes}).
2406+
*
2407+
* @param chars an array of characters
2408+
* @param beginIndex the initial offset in the array of characters
2409+
* @param limit the end offset in the array of characters
2410+
* @param frc the specified <code>FontRenderContext</code>
2411+
* @return a <code>Rectangle2D</code> that is the bounding box of the specified
2412+
* array of characters in the specified <code>FontRenderContext</code>.
2413+
* @throws IndexOutOfBoundsException if <code>beginIndex</code> is less than
2414+
* zero, or <code>limit</code> is greater than
2415+
* the length of <code>chars</code>, or
2416+
* <code>beginIndex</code> is greater than
2417+
* <code>limit</code>.
2418+
* @see FontRenderContext
2419+
* @see Font#createGlyphVector
2420+
* @since 1.2
2421+
*/
2422+
@SuppressWarnings("unused")
2423+
public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc) {
24552424

2425+
if (beginIndex < 0) {
2426+
throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
2427+
}
2428+
if (limit > chars.length) {
2429+
throw new IndexOutOfBoundsException("limit: " + limit);
2430+
}
2431+
if (beginIndex > limit) {
2432+
throw new IndexOutOfBoundsException("range length: " + (limit - beginIndex));
2433+
}
2434+
2435+
// In JavaScript, we just let the browser figure this out.
2436+
SB sb = new SB();
2437+
sb.appendCB(chars, beginIndex, limit);
2438+
return ((FontDesignMetrics) getFontMetrics()).秘getStringBounds(sb.toString());
2439+
2440+
2441+
// boolean simple = values == null || (values.getKerning() == 0 && values.getLigatures() == 0
2442+
// && values.getBaselineTransform() == null);
2443+
// if (simple) {
2444+
// simple = !FontUtilities.isComplexText(chars, beginIndex, limit);
2445+
// }
2446+
// //
2447+
// if (simple) {
2448+
// GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex, limit - beginIndex, frc);
2449+
// return gv.getLogicalBounds();
2450+
// } else {
2451+
// // need char array constructor on textlayout
2452+
// String str = new String(chars, beginIndex, limit - beginIndex);
2453+
// TextLayout tl = new TextLayout(str, this, frc);
2454+
// return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(),
2455+
// tl.getAscent() + tl.getDescent() + tl.getLeading());
2456+
// }
2457+
2458+
}
2459+
2460+
/**
2461+
* Keep it simple in JavaScript.
2462+
*
2463+
* @param s
2464+
* @param beginIndex
2465+
* @param limit
2466+
* @return Rectangle2D.Float
2467+
*/
2468+
public Rectangle2D getStringBoundsStr(String s, int beginIndex, int limit) {
2469+
if (beginIndex < 0) {
2470+
throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
2471+
}
2472+
if (limit > s.length()) {
2473+
throw new IndexOutOfBoundsException("limit: " + limit);
2474+
}
2475+
if (beginIndex > limit) {
2476+
throw new IndexOutOfBoundsException("range length: " +
2477+
(limit - beginIndex));
2478+
}
2479+
2480+
s = s.substring(beginIndex, limit);
24562481
// this code should be in textlayout
24572482
// quick check for simple text, assume GV ok to use if simple
24582483

@@ -2466,11 +2491,7 @@ public Rectangle2D getStringBoundsStr(String s, int i, int j) {
24662491
// if (simple) {
24672492
// GlyphVector gv = new StandardGlyphVector(this, chars, beginIndex,
24682493
// limit - beginIndex, frc);
2469-
FontMetrics fm = getFontMetrics();
2470-
int dec = fm.getDescent();
2471-
int asc = fm.getAscent();
2472-
int width = fm.stringWidth(s);
2473-
return new Rectangle2D.Float(0, -dec, width, asc + dec);
2494+
return ((FontDesignMetrics) getFontMetrics()).秘getStringBounds(s);
24742495
// return null;//gv.getLogicalBounds();
24752496
// } else {
24762497
// // need char array constructor on textlayout
@@ -2551,9 +2572,7 @@ public Rectangle2D getStringBounds(CharacterIterator ci,
25512572
*/
25522573
public Rectangle2D getMaxCharBounds(FontRenderContext frc) {
25532574
float [] metrics = new float[4];
2554-
2555-
((JSFontMetrics) getFontMetrics()).getMetrics(frc, metrics);
2556-
2575+
((FontDesignMetrics) getFontMetrics()).秘fillMetrics(metrics);
25572576
return new Rectangle2D.Float(0, -metrics[0],
25582577
metrics[3],
25592578
metrics[0] + metrics[1] + metrics[2]);

0 commit comments

Comments
 (0)