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

Commit 68d1d5ae authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Select closest font's style for backward compatibility

Bug: 119425212
Test: atest CtsGraphicsTestCases
Test: atest CtsContentTestCases:android.content.res.cts.ResourcesTest
Change-Id: I796baf342f752366040b657954b0f82d358a7790
parent 122c0879
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -660,7 +660,22 @@ public class FontsContract {
        if (familyBuilder == null) {
            return null;
        }
        return new Typeface.CustomFallbackBuilder(familyBuilder.build()).build();

        final FontFamily family = familyBuilder.build();

        final FontStyle normal = new FontStyle(FontStyle.FONT_WEIGHT_NORMAL,
                FontStyle.FONT_SLANT_UPRIGHT);
        Font bestFont = family.getFont(0);
        int bestScore = normal.getMatchScore(bestFont.getStyle());
        for (int i = 1; i < family.getSize(); ++i) {
            final Font candidate = family.getFont(i);
            final int score = normal.getMatchScore(candidate.getStyle());
            if (score < bestScore) {
                bestFont = candidate;
                bestScore = score;
            }
        }
        return new Typeface.CustomFallbackBuilder(family).setStyle(bestFont.getStyle()).build();
    }

    /**
+16 −1
Original line number Diff line number Diff line
@@ -249,7 +249,22 @@ public class Typeface {
            if (familyBuilder == null) {
                return Typeface.DEFAULT;
            }
            typeface = new Typeface.CustomFallbackBuilder(familyBuilder.build()).build();
            final FontFamily family = familyBuilder.build();
            final FontStyle normal = new FontStyle(FontStyle.FONT_WEIGHT_NORMAL,
                    FontStyle.FONT_SLANT_UPRIGHT);
            Font bestFont = family.getFont(0);
            int bestScore = normal.getMatchScore(bestFont.getStyle());
            for (int i = 1; i < family.getSize(); ++i) {
                final Font candidate = family.getFont(i);
                final int score = normal.getMatchScore(candidate.getStyle());
                if (score < bestScore) {
                    bestFont = candidate;
                    bestScore = score;
                }
            }
            typeface = new Typeface.CustomFallbackBuilder(family)
                    .setStyle(bestFont.getStyle())
                    .build();
        } catch (IOException e) {
            typeface = Typeface.DEFAULT;
        }
+11 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.graphics.fonts;

import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;

import com.android.internal.util.Preconditions;
@@ -232,6 +233,16 @@ public final class FontStyle {
        return mSlant;
    }

    /**
     * Compute the matching score for another style.
     *
     * The smaller is better.
     * @hide
     */
    public int getMatchScore(@NonNull FontStyle o) {
        return Math.abs((getWeight() - o.getWeight())) / 100 + (getSlant() == o.getSlant() ? 0 : 2);
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (o == this) {