Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit db86a6b0 authored by Siyamed Sinir's avatar Siyamed Sinir
Browse files

Fix rounding error related to autoSize

setupAutoSize fills in the possible text sizes that can be generated
between a min and max value. In order to do that, it counts the number
of steps starting from minSize until maxSize. However, while counting
it rounds the initial value, which causes rounding error at the final
step.

Test: Change system font scale to 1.1 via
      adb shell settings put system font_scale 1.1
Test: atest android.widget.cts.TextViewTest

Bug: 73917559
Bug: 75266270
Change-Id: I61811db28ef01262bd48f5042d783d75c71c3614
parent 11701602
Loading
Loading
Loading
Loading
+5 −12
Original line number Diff line number Diff line
@@ -1917,19 +1917,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            // Calculate the sizes set based on minimum size, maximum size and step size if we do
            // not have a predefined set of sizes or if the current sizes array is empty.
            if (!mHasPresetAutoSizeValues || mAutoSizeTextSizesInPx.length == 0) {
                int autoSizeValuesLength = 1;
                float currentSize = Math.round(mAutoSizeMinTextSizeInPx);
                while (Math.round(currentSize + mAutoSizeStepGranularityInPx)
                        <= Math.round(mAutoSizeMaxTextSizeInPx)) {
                    autoSizeValuesLength++;
                    currentSize += mAutoSizeStepGranularityInPx;
                }

                int[] autoSizeTextSizesInPx = new int[autoSizeValuesLength];
                float sizeToAdd = mAutoSizeMinTextSizeInPx;
                final int autoSizeValuesLength = ((int) Math.floor((mAutoSizeMaxTextSizeInPx
                        - mAutoSizeMinTextSizeInPx) / mAutoSizeStepGranularityInPx)) + 1;
                final int[] autoSizeTextSizesInPx = new int[autoSizeValuesLength];
                for (int i = 0; i < autoSizeValuesLength; i++) {
                    autoSizeTextSizesInPx[i] = Math.round(sizeToAdd);
                    sizeToAdd += mAutoSizeStepGranularityInPx;
                    autoSizeTextSizesInPx[i] = Math.round(
                            mAutoSizeMinTextSizeInPx + (i * mAutoSizeStepGranularityInPx));
                }
                mAutoSizeTextSizesInPx = cleanupAutoSizePresetSizes(autoSizeTextSizesInPx);
            }