Loading core/java/android/text/Layout.java +7 −2 Original line number Diff line number Diff line Loading @@ -75,9 +75,12 @@ public abstract class Layout { // These should match the constants in framework/base/libs/hwui/hwui/DrawTextFunctor.h private static final float HIGH_CONTRAST_TEXT_BORDER_WIDTH_MIN_PX = 0f; private static final float HIGH_CONTRAST_TEXT_BORDER_WIDTH_FACTOR = 0f; private static final float HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_DP = 5f; // since we're not using soft light yet, this needs to be much lower than the spec'd 0.8 private static final float HIGH_CONTRAST_TEXT_BACKGROUND_ALPHA_PERCENTAGE = 0.7f; @VisibleForTesting static final float HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP = 5f; @VisibleForTesting static final float HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR = 0.5f; /** @hide */ @IntDef(prefix = { "BREAK_STRATEGY_" }, value = { Loading Loading @@ -1030,7 +1033,9 @@ public abstract class Layout { var padding = Math.max(HIGH_CONTRAST_TEXT_BORDER_WIDTH_MIN_PX, mPaint.getTextSize() * HIGH_CONTRAST_TEXT_BORDER_WIDTH_FACTOR); var cornerRadius = mPaint.density * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_DP; var cornerRadius = Math.max( mPaint.density * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP, mPaint.getTextSize() * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR); // We set the alpha on the color itself instead of Paint.setAlpha(), because that function // actually mutates the color in... *ehem* very strange ways. Also the color might get reset Loading core/tests/coretests/src/android/text/LayoutTest.java +76 −3 Original line number Diff line number Diff line Loading @@ -16,6 +16,9 @@ package android.text; import static android.text.Layout.HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR; import static android.text.Layout.HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP; import static com.android.graphics.hwui.flags.Flags.FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT; import static org.junit.Assert.assertArrayEquals; Loading Loading @@ -1073,6 +1076,68 @@ public class LayoutTest { } } @Test @RequiresFlagsEnabled(FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT) public void highContrastTextEnabled_testRoundedRectSize_belowMinimum_usesMinimumValue() { mTextPaint.setColor(Color.BLACK); mTextPaint.setTextSize(8); // Value chosen so that N * RADIUS_FACTOR < RADIUS_MIN_DP Layout layout = new StaticLayout("Test text", mTextPaint, mWidth, mAlign, mSpacingMult, mSpacingAdd, /* includePad= */ false); MockCanvas c = new MockCanvas(/* width= */ 256, /* height= */ 256); c.setHighContrastTextEnabled(true); layout.draw( c, /* highlightPaths= */ null, /* highlightPaints= */ null, /* selectionPath= */ null, /* selectionPaint= */ null, /* cursorOffsetVertical= */ 0 ); final float expectedRoundedRectSize = mTextPaint.density * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP; List<MockCanvas.DrawCommand> drawCommands = c.getDrawCommands(); for (int i = 0; i < drawCommands.size(); i++) { MockCanvas.DrawCommand drawCommand = drawCommands.get(i); if (drawCommand.rect != null) { expect.that(drawCommand.rX).isEqualTo(expectedRoundedRectSize); expect.that(drawCommand.rY).isEqualTo(expectedRoundedRectSize); } } } @Test @RequiresFlagsEnabled(FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT) public void highContrastTextEnabled_testRoundedRectSize_aboveMinimum_usesScaledValue() { mTextPaint.setColor(Color.BLACK); mTextPaint.setTextSize(50); // Value chosen so that N * RADIUS_FACTOR > RADIUS_MIN_DP Layout layout = new StaticLayout("Test text", mTextPaint, mWidth, mAlign, mSpacingMult, mSpacingAdd, /* includePad= */ false); MockCanvas c = new MockCanvas(/* width= */ 256, /* height= */ 256); c.setHighContrastTextEnabled(true); layout.draw( c, /* highlightPaths= */ null, /* highlightPaints= */ null, /* selectionPath= */ null, /* selectionPaint= */ null, /* cursorOffsetVertical= */ 0 ); final float expectedRoundedRectSize = mTextPaint.getTextSize() * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR; List<MockCanvas.DrawCommand> drawCommands = c.getDrawCommands(); for (int i = 0; i < drawCommands.size(); i++) { MockCanvas.DrawCommand drawCommand = drawCommands.get(i); if (drawCommand.rect != null) { expect.that(drawCommand.rX).isEqualTo(expectedRoundedRectSize); expect.that(drawCommand.rY).isEqualTo(expectedRoundedRectSize); } } } private int removeAlpha(int color) { return Color.rgb( Color.red(color), Loading @@ -1087,6 +1152,8 @@ public class LayoutTest { public final String text; public final float x; public final float y; public final float rX; public final float rY; public final Path path; public final RectF rect; public final Paint paint; Loading @@ -1098,6 +1165,8 @@ public class LayoutTest { this.paint = new Paint(paint); path = null; rect = null; this.rX = 0; this.rY = 0; } DrawCommand(Path path, Paint paint) { Loading @@ -1107,15 +1176,19 @@ public class LayoutTest { x = 0; text = null; rect = null; this.rX = 0; this.rY = 0; } DrawCommand(RectF rect, Paint paint) { DrawCommand(RectF rect, Paint paint, float rX, float rY) { this.rect = new RectF(rect); this.paint = new Paint(paint); path = null; y = 0; x = 0; text = null; this.rX = rX; this.rY = rY; } @Override Loading Loading @@ -1189,12 +1262,12 @@ public class LayoutTest { @Override public void drawRect(RectF rect, Paint p) { mDrawCommands.add(new DrawCommand(rect, p)); mDrawCommands.add(new DrawCommand(rect, p, 0, 0)); } @Override public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) { mDrawCommands.add(new DrawCommand(rect, paint)); mDrawCommands.add(new DrawCommand(rect, paint, rx, ry)); } List<DrawCommand> getDrawCommands() { Loading Loading
core/java/android/text/Layout.java +7 −2 Original line number Diff line number Diff line Loading @@ -75,9 +75,12 @@ public abstract class Layout { // These should match the constants in framework/base/libs/hwui/hwui/DrawTextFunctor.h private static final float HIGH_CONTRAST_TEXT_BORDER_WIDTH_MIN_PX = 0f; private static final float HIGH_CONTRAST_TEXT_BORDER_WIDTH_FACTOR = 0f; private static final float HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_DP = 5f; // since we're not using soft light yet, this needs to be much lower than the spec'd 0.8 private static final float HIGH_CONTRAST_TEXT_BACKGROUND_ALPHA_PERCENTAGE = 0.7f; @VisibleForTesting static final float HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP = 5f; @VisibleForTesting static final float HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR = 0.5f; /** @hide */ @IntDef(prefix = { "BREAK_STRATEGY_" }, value = { Loading Loading @@ -1030,7 +1033,9 @@ public abstract class Layout { var padding = Math.max(HIGH_CONTRAST_TEXT_BORDER_WIDTH_MIN_PX, mPaint.getTextSize() * HIGH_CONTRAST_TEXT_BORDER_WIDTH_FACTOR); var cornerRadius = mPaint.density * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_DP; var cornerRadius = Math.max( mPaint.density * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP, mPaint.getTextSize() * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR); // We set the alpha on the color itself instead of Paint.setAlpha(), because that function // actually mutates the color in... *ehem* very strange ways. Also the color might get reset Loading
core/tests/coretests/src/android/text/LayoutTest.java +76 −3 Original line number Diff line number Diff line Loading @@ -16,6 +16,9 @@ package android.text; import static android.text.Layout.HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR; import static android.text.Layout.HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP; import static com.android.graphics.hwui.flags.Flags.FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT; import static org.junit.Assert.assertArrayEquals; Loading Loading @@ -1073,6 +1076,68 @@ public class LayoutTest { } } @Test @RequiresFlagsEnabled(FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT) public void highContrastTextEnabled_testRoundedRectSize_belowMinimum_usesMinimumValue() { mTextPaint.setColor(Color.BLACK); mTextPaint.setTextSize(8); // Value chosen so that N * RADIUS_FACTOR < RADIUS_MIN_DP Layout layout = new StaticLayout("Test text", mTextPaint, mWidth, mAlign, mSpacingMult, mSpacingAdd, /* includePad= */ false); MockCanvas c = new MockCanvas(/* width= */ 256, /* height= */ 256); c.setHighContrastTextEnabled(true); layout.draw( c, /* highlightPaths= */ null, /* highlightPaints= */ null, /* selectionPath= */ null, /* selectionPaint= */ null, /* cursorOffsetVertical= */ 0 ); final float expectedRoundedRectSize = mTextPaint.density * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_MIN_DP; List<MockCanvas.DrawCommand> drawCommands = c.getDrawCommands(); for (int i = 0; i < drawCommands.size(); i++) { MockCanvas.DrawCommand drawCommand = drawCommands.get(i); if (drawCommand.rect != null) { expect.that(drawCommand.rX).isEqualTo(expectedRoundedRectSize); expect.that(drawCommand.rY).isEqualTo(expectedRoundedRectSize); } } } @Test @RequiresFlagsEnabled(FLAG_HIGH_CONTRAST_TEXT_SMALL_TEXT_RECT) public void highContrastTextEnabled_testRoundedRectSize_aboveMinimum_usesScaledValue() { mTextPaint.setColor(Color.BLACK); mTextPaint.setTextSize(50); // Value chosen so that N * RADIUS_FACTOR > RADIUS_MIN_DP Layout layout = new StaticLayout("Test text", mTextPaint, mWidth, mAlign, mSpacingMult, mSpacingAdd, /* includePad= */ false); MockCanvas c = new MockCanvas(/* width= */ 256, /* height= */ 256); c.setHighContrastTextEnabled(true); layout.draw( c, /* highlightPaths= */ null, /* highlightPaints= */ null, /* selectionPath= */ null, /* selectionPaint= */ null, /* cursorOffsetVertical= */ 0 ); final float expectedRoundedRectSize = mTextPaint.getTextSize() * HIGH_CONTRAST_TEXT_BACKGROUND_CORNER_RADIUS_FACTOR; List<MockCanvas.DrawCommand> drawCommands = c.getDrawCommands(); for (int i = 0; i < drawCommands.size(); i++) { MockCanvas.DrawCommand drawCommand = drawCommands.get(i); if (drawCommand.rect != null) { expect.that(drawCommand.rX).isEqualTo(expectedRoundedRectSize); expect.that(drawCommand.rY).isEqualTo(expectedRoundedRectSize); } } } private int removeAlpha(int color) { return Color.rgb( Color.red(color), Loading @@ -1087,6 +1152,8 @@ public class LayoutTest { public final String text; public final float x; public final float y; public final float rX; public final float rY; public final Path path; public final RectF rect; public final Paint paint; Loading @@ -1098,6 +1165,8 @@ public class LayoutTest { this.paint = new Paint(paint); path = null; rect = null; this.rX = 0; this.rY = 0; } DrawCommand(Path path, Paint paint) { Loading @@ -1107,15 +1176,19 @@ public class LayoutTest { x = 0; text = null; rect = null; this.rX = 0; this.rY = 0; } DrawCommand(RectF rect, Paint paint) { DrawCommand(RectF rect, Paint paint, float rX, float rY) { this.rect = new RectF(rect); this.paint = new Paint(paint); path = null; y = 0; x = 0; text = null; this.rX = rX; this.rY = rY; } @Override Loading Loading @@ -1189,12 +1262,12 @@ public class LayoutTest { @Override public void drawRect(RectF rect, Paint p) { mDrawCommands.add(new DrawCommand(rect, p)); mDrawCommands.add(new DrawCommand(rect, p, 0, 0)); } @Override public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint) { mDrawCommands.add(new DrawCommand(rect, paint)); mDrawCommands.add(new DrawCommand(rect, paint, rx, ry)); } List<DrawCommand> getDrawCommands() { Loading