JBR-3215 'deriveFont(float)' can return a different font (not just change the size)

(cherry picked from commit 8eafcaab24)
This commit is contained in:
Dmitry Batrak
2021-03-22 15:56:25 +03:00
committed by alexey.ushakov@jetbrains.com
parent 3915012e2e
commit d010cd8d26

View File

@@ -52,6 +52,7 @@ import java.util.EventListener;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import sun.awt.ComponentFactory;
import sun.font.AttributeMap;
@@ -608,7 +609,7 @@ public class Font implements java.io.Serializable
/* This constructor is used by deriveFont when attributes is null */
private Font(String name, int style, float sizePts,
boolean created, Font2DHandle handle) {
boolean created, Font2DHandle handle, boolean useOldHandle) {
this(name, style, sizePts);
this.createdFont = created;
/* Fonts created from a stream will use the same font2D instance
@@ -628,6 +629,8 @@ public class Font implements java.io.Serializable
} else {
this.font2DHandle = handle;
}
} else if (useOldHandle) {
this.font2DHandle = handle;
}
}
@@ -673,7 +676,7 @@ public class Font implements java.io.Serializable
* In these cases there is no need to interrogate "values".
*/
private Font(AttributeValues values, String oldName, int oldStyle,
boolean created, Font2DHandle handle) {
boolean created, Font2DHandle handle, boolean useOldHandle) {
this.createdFont = created;
if (created) {
@@ -702,6 +705,8 @@ public class Font implements java.io.Serializable
this.createdFont = false;
this.font2DHandle = null;
}
} else if (useOldHandle) {
this.font2DHandle = handle;
}
initFromValues(values);
}
@@ -871,7 +876,7 @@ public class Font implements java.io.Serializable
values = font.getAttributeValues().clone();
values.merge(attributes, SECONDARY_MASK);
return new Font(values, font.name, font.style,
font.createdFont, font.font2DHandle);
font.createdFont, font.font2DHandle, false);
}
return new Font(attributes);
}
@@ -882,7 +887,7 @@ public class Font implements java.io.Serializable
AttributeValues values = font.getAttributeValues().clone();
values.merge(attributes, SECONDARY_MASK);
return new Font(values, font.name, font.style,
font.createdFont, font.font2DHandle);
font.createdFont, font.font2DHandle, false);
}
return font;
@@ -2083,13 +2088,13 @@ public class Font implements java.io.Serializable
*/
public Font deriveFont(int style, float size){
if (values == null) {
return new Font(name, style, size, createdFont, font2DHandle);
return new Font(name, style, size, createdFont, font2DHandle, false);
}
AttributeValues newValues = getAttributeValues().clone();
int oldStyle = (this.style != style) ? this.style : -1;
applyStyle(style, newValues);
newValues.setSize(size);
return new Font(newValues, null, oldStyle, createdFont, font2DHandle);
return new Font(newValues, null, oldStyle, createdFont, font2DHandle, false);
}
/**
@@ -2108,7 +2113,7 @@ public class Font implements java.io.Serializable
int oldStyle = (this.style != style) ? this.style : -1;
applyStyle(style, newValues);
applyTransform(trans, newValues);
return new Font(newValues, null, oldStyle, createdFont, font2DHandle);
return new Font(newValues, null, oldStyle, createdFont, font2DHandle, false);
}
/**
@@ -2120,11 +2125,11 @@ public class Font implements java.io.Serializable
*/
public Font deriveFont(float size){
if (values == null) {
return new Font(name, style, size, createdFont, font2DHandle);
return new Font(name, style, size, createdFont, font2DHandle, true);
}
AttributeValues newValues = getAttributeValues().clone();
newValues.setSize(size);
return new Font(newValues, null, -1, createdFont, font2DHandle);
return new Font(newValues, null, -1, createdFont, font2DHandle, true);
}
/**
@@ -2140,7 +2145,7 @@ public class Font implements java.io.Serializable
public Font deriveFont(AffineTransform trans){
AttributeValues newValues = getAttributeValues().clone();
applyTransform(trans, newValues);
return new Font(newValues, null, -1, createdFont, font2DHandle);
return new Font(newValues, null, -1, createdFont, font2DHandle, true);
}
/**
@@ -2152,14 +2157,24 @@ public class Font implements java.io.Serializable
*/
public Font deriveFont(int style){
if (values == null) {
return new Font(name, style, size, createdFont, font2DHandle);
return new Font(name, style, size, createdFont, font2DHandle, false);
}
AttributeValues newValues = getAttributeValues().clone();
int oldStyle = (this.style != style) ? this.style : -1;
applyStyle(style, newValues);
return new Font(newValues, null, oldStyle, createdFont, font2DHandle);
return new Font(newValues, null, oldStyle, createdFont, font2DHandle, false);
}
/*
* Set of attributes, updating which on a Font instance cannot possibly
* change the underlying Font2D instance it uses.
*/
private static final Set<? extends Attribute> FONT_2D_UNRELATED_ATTRS
= Set.of(TextAttribute.KERNING,
TextAttribute.LIGATURES,
TextAttribute.SIZE,
TextAttribute.TRANSFORM);
/**
* Creates a new {@code Font} object by replicating the current
* {@code Font} object and applying a new set of font attributes
@@ -2177,7 +2192,15 @@ public class Font implements java.io.Serializable
AttributeValues newValues = getAttributeValues().clone();
newValues.merge(attributes, RECOGNIZED_MASK);
return new Font(newValues, name, style, createdFont, font2DHandle);
boolean keepFont2DHandle = true;
for (Attribute attribute : attributes.keySet()) {
if (!FONT_2D_UNRELATED_ATTRS.contains(attribute)) {
keepFont2DHandle = false;
break;
}
}
return new Font(newValues, name, style, createdFont, font2DHandle,
keepFont2DHandle);
}
/**