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

Commit c53c2858 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add Font#isSameSource API for faster checking underlying buffer equality."

parents 9761e30c af7629e7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -16430,6 +16430,7 @@ package android.graphics.fonts {
    method public void getMetrics(@NonNull android.graphics.Paint, @Nullable android.graphics.Paint.FontMetrics);
    method @NonNull public android.graphics.fonts.FontStyle getStyle();
    method @IntRange(from=0) public int getTtcIndex();
    method public boolean isSameSource(@NonNull android.graphics.fonts.Font);
  }
  public static final class Font.Builder {
+1 −0
Original line number Diff line number Diff line
@@ -16412,6 +16412,7 @@ package android.graphics.fonts {
    method public void getMetrics(@NonNull android.graphics.Paint, @Nullable android.graphics.Paint.FontMetrics);
    method @NonNull public android.graphics.fonts.FontStyle getStyle();
    method @IntRange(from=0) public int getTtcIndex();
    method public boolean isSameSource(@NonNull android.graphics.fonts.Font);
  }
  public static final class Font.Builder {
+46 −18
Original line number Diff line number Diff line
@@ -626,25 +626,33 @@ public final class Font {
        return mNativePtr;
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Font)) {
            return false;
        }
        Font f = (Font) o;
        boolean paramEqual = mFontStyle.equals(f.mFontStyle) && f.mTtcIndex == mTtcIndex
                && Arrays.equals(f.mAxes, mAxes) && Objects.equals(f.mLocaleList, mLocaleList)
                && Objects.equals(mFile, f.mFile);
    /**
     * Returns true if the given font is created from the same source data from this font.
     *
     * This method essentially compares {@link ByteBuffer} inside Font, but has some optimization
     * for faster comparing. This method compares the internal object before going to one-by-one
     * byte compare with {@link ByteBuffer}. This typically works efficiently if you compares the
     * font that is created from {@link Builder#Builder(Font)}.
     *
     * This API is typically useful for checking if two fonts can be interpolated by font variation
     * axes. For example, when you call {@link android.text.TextShaper} for the same
     * string but different style, you may get two font objects which is created from the same
     * source but have different parameters. You may want to animate between them by interpolating
     * font variation settings if these fonts are created from the same source.
     *
     * @param other a font object to be compared.
     * @return true if given font is created from the same source from this font. Otherwise false.
     */
    public boolean isSameSource(@NonNull Font other) {
        Objects.requireNonNull(other);

        if (!paramEqual) {
            return false;
        // Shortcut for the same instance.
        if (mBuffer == other.mBuffer) {
            return true;
        }

        // Shortcut for different font buffer check by comparing size.
        if (mBuffer.capacity() != f.mBuffer.capacity()) {
        if (mBuffer.capacity() != other.mBuffer.capacity()) {
            return false;
        }

@@ -652,15 +660,35 @@ public final class Font {
        // underlying native font object holds buffer address, check if this buffer points exactly
        // the same address as a shortcut of equality. For being compatible with of API30 or before,
        // check buffer position even if the buffer points the same address.
        if (nIsSameBufferAddress(mNativePtr, f.mNativePtr)
                && mBuffer.position() == f.mBuffer.position()) {
        if (nIsSameBufferAddress(mNativePtr, other.mNativePtr)
                && mBuffer.position() == other.mBuffer.position()) {
            return true;
        }

        // Unfortunately, need to compare bytes one-by-one since the buffer may be different font
        // file but has the same file size, or two font has same content but they are allocated
        // differently. For being compatible with API30 ore before, compare with ByteBuffer#equals.
        return mBuffer.equals(f.mBuffer);
        return mBuffer.equals(other.mBuffer);
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Font)) {
            return false;
        }
        Font f = (Font) o;
        boolean paramEqual = mFontStyle.equals(f.mFontStyle) && f.mTtcIndex == mTtcIndex
                && Arrays.equals(f.mAxes, mAxes) && Objects.equals(f.mLocaleList, mLocaleList)
                && Objects.equals(mFile, f.mFile);

        if (!paramEqual) {
            return false;
        }

        return isSameSource(f);
    }

    @Override