From ffae6c7f71e9da9f887002994d270b9dbf6d0673 Mon Sep 17 00:00:00 2001 From: Seigo Nonaka Date: Fri, 26 May 2017 17:50:10 -0700 Subject: [PATCH] Fix NPE and wrong behavior of Typeface.isSupportedAxes The Typeface kept by Paint object can be null if nobody calls setTypeface. The null typeface is equivalent to the Typeface.DEFAULT. To check whether the passed axis is usable for Typeface.DEFAULT, we need to pass Typeface.DEFAULT to isSupportedAxes if no typeface was set on the Paint. At the same time this CL fixes an issue in isSupportedAxes. If the requested axis is listed in the first element of axes list, isSupportedAxes returned false due to wrong handling of binarySearch result. Bug: 62146672 Bug: 62147012 Test: am instrument -w -e class android.graphics.cts.PaintTest android.graphics.cts/android.support.test.runner.AndroidJUnitRunner Change-Id: I7c154adfe8a19d6ed24ad645df7c10cee7880461 --- graphics/java/android/graphics/Paint.java | 7 +++++-- graphics/java/android/graphics/Typeface.java | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java index e62df8ff47429..472dd03f63f0c 100644 --- a/graphics/java/android/graphics/Paint.java +++ b/graphics/java/android/graphics/Paint.java @@ -1594,10 +1594,13 @@ public class Paint { return true; } + // The null typeface is valid and it is equivalent to Typeface.DEFAULT. + // To call isSupportedAxes method, use Typeface.DEFAULT instance. + Typeface targetTypeface = mTypeface == null ? Typeface.DEFAULT : mTypeface; FontVariationAxis[] axes = FontVariationAxis.fromFontVariationSettings(settings); final ArrayList filteredAxes = new ArrayList(); for (final FontVariationAxis axis : axes) { - if (mTypeface.isSupportedAxes(axis.getOpenTypeTagValue())) { + if (targetTypeface.isSupportedAxes(axis.getOpenTypeTagValue())) { filteredAxes.add(axis); } } @@ -1605,7 +1608,7 @@ public class Paint { return false; } mFontVariationSettings = settings; - setTypeface(Typeface.createFromTypefaceWithVariation(mTypeface, filteredAxes)); + setTypeface(Typeface.createFromTypefaceWithVariation(targetTypeface, filteredAxes)); return true; } diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java index 5a56f53353733..c4b56c333c646 100644 --- a/graphics/java/android/graphics/Typeface.java +++ b/graphics/java/android/graphics/Typeface.java @@ -725,8 +725,8 @@ public class Typeface { } /** @hide */ - public static Typeface createFromTypefaceWithVariation(Typeface family, - List axes) { + public static Typeface createFromTypefaceWithVariation(@Nullable Typeface family, + @NonNull List axes) { final long ni = family == null ? 0 : family.native_instance; return new Typeface(nativeCreateFromTypefaceWithVariation(ni, axes)); } @@ -1056,7 +1056,7 @@ public class Typeface { } } } - return Arrays.binarySearch(mSupportedAxes, axis) > 0; + return Arrays.binarySearch(mSupportedAxes, axis) >= 0; } private static native long nativeCreateFromTypeface(long native_instance, int style);