IDEA-183638 [fps] moving caret up/down leaves artifacts in the editor

This commit is contained in:
Anton Tarasov
2017-12-12 16:18:00 +03:00
parent b1f77afccb
commit 4f91e7a65f
2 changed files with 20 additions and 8 deletions
@@ -230,7 +230,7 @@ public class EditorComponentImpl extends JTextComponent implements Scrollable, D
UISettings.setupAntialiasing(gg);
}
gg.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, myEditor.myFractionalMetricsHintValue);
AffineTransform origTx = JBUI.alignToIntGrid(gg);
AffineTransform origTx = JBUI.alignToIntGrid(gg, true, false);
myEditor.paint(gg);
if (origTx != null) gg.setTransform(origTx);
}
@@ -556,21 +556,24 @@ public class JBUI {
}
/**
* If the graphics has floating point scale transform, aligns the graphics to the integer coordinate grid,
* Aligns the x or/and y translate of the graphics to the integer coordinate grid if the graphics has fractional scale transform,
* otherwise does nothing. This is used to avoid the rounding problem, see JRE-502.
*
* @param g the graphics to align
* @param alignX should the x-translate be aligned
* @param alignY should the y-translate be aligned
* @return the original graphics transform when aligned, otherwise null
*/
public static AffineTransform alignToIntGrid(@NotNull Graphics2D g) {
public static AffineTransform alignToIntGrid(@NotNull Graphics2D g, boolean alignX, boolean alignY) {
try {
AffineTransform tx = g.getTransform();
double scaleX = tx.getScaleX();
double scaleY = tx.getScaleY();
boolean fpsTx = scaleX != (int)scaleX || scaleY != (int)scaleY;
if (fpsTx) {
if (isFractionalScale(tx)) {
double scaleX = tx.getScaleX();
double scaleY = tx.getScaleY();
AffineTransform alignedTx = new AffineTransform();
alignedTx.translate((int)Math.ceil(tx.getTranslateX() - 0.5), (int)Math.ceil(tx.getTranslateY() - 0.5));
double trX = alignX ? (int)Math.ceil(tx.getTranslateX() - 0.5) : tx.getTranslateX();
double trY = alignY ? (int)Math.ceil(tx.getTranslateY() - 0.5) : tx.getTranslateY();
alignedTx.translate(trX, trY);
alignedTx.scale(scaleX, scaleY);
assert tx.getShearX() == 0 && tx.getShearY() == 0; // the shear is ignored
g.setTransform(alignedTx);
@@ -583,6 +586,15 @@ public class JBUI {
return null;
}
/**
* Returns true if the transform matrix contains fractional scale element.
*/
public static boolean isFractionalScale(AffineTransform tx) {
double scaleX = tx.getScaleX();
double scaleY = tx.getScaleY();
return scaleX != (int)scaleX || scaleY != (int)scaleY;
}
public static class Fonts {
@NotNull
public static JBFont label() {