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

Commit f39661e4 authored by Seigo Nonaka's avatar Seigo Nonaka
Browse files

Try to draw with given font before updating

Bug: 187542525
Test: atest FontFamilyUpdateRequestTest
Test: atest FontListParserTest
Test: atest FontManagerTest
Test: atest NativeSystemFontTest
Test: atest PersistentSystemFontConfigTest
Test: atest SystemFontsTest
Test: atest SystemFontsUniqueNameTest
Test: atest UpdatableFontDirTest
Test: atest UpdatableSystemFontTest
Change-Id: Iadb717c1cdf9e3c9470001be5884c0419a26035e
parent e13111ba
Loading
Loading
Loading
Loading
+29 −2
Original line number Original line Diff line number Diff line
@@ -20,6 +20,8 @@ import android.Manifest;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.content.Context;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.Typeface;
import android.graphics.fonts.Font;
import android.graphics.fonts.Font;
import android.graphics.fonts.FontFamily;
import android.graphics.fonts.FontFamily;
@@ -32,6 +34,9 @@ import android.os.SharedMemory;
import android.os.ShellCallback;
import android.os.ShellCallback;
import android.system.ErrnoException;
import android.system.ErrnoException;
import android.text.FontConfig;
import android.text.FontConfig;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.AndroidException;
import android.util.AndroidException;
import android.util.IndentingPrintWriter;
import android.util.IndentingPrintWriter;
@@ -196,10 +201,32 @@ public final class FontManagerService extends IFontManager.Stub {
        }
        }


        @Override
        @Override
        public void tryToCreateTypeface(File file) throws IOException {
        public void tryToCreateTypeface(File file) throws Throwable {
            Font font = new Font.Builder(file).build();
            Font font = new Font.Builder(file).build();
            FontFamily family = new FontFamily.Builder(font).build();
            FontFamily family = new FontFamily.Builder(font).build();
            new Typeface.CustomFallbackBuilder(family).build();
            Typeface typeface = new Typeface.CustomFallbackBuilder(family).build();

            TextPaint p = new TextPaint();
            p.setTextSize(24f);
            p.setTypeface(typeface);

            // Test string to try with the passed font.
            // TODO: Good to extract from font file.
            String testTextToDraw = "abcXYZ@- "
                    + "\uD83E\uDED6" // Emoji E13.0
                    + "\uD83C\uDDFA\uD83C\uDDF8" // Emoji Flags
                    + "\uD83D\uDC8F\uD83C\uDFFB" // Emoji Skin tone Sequence
                    // ZWJ Sequence
                    + "\uD83D\uDC68\uD83C\uDFFC\u200D\u2764\uFE0F\u200D\uD83D\uDC8B\u200D"
                    + "\uD83D\uDC68\uD83C\uDFFF";

            int width = (int) Math.ceil(Layout.getDesiredWidth(testTextToDraw, p));
            StaticLayout layout = StaticLayout.Builder.obtain(
                    testTextToDraw, 0, testTextToDraw.length(), p, width).build();
            Bitmap bmp = Bitmap.createBitmap(
                    layout.getWidth(), layout.getHeight(), Bitmap.Config.ALPHA_8);
            Canvas canvas = new Canvas(bmp);
            layout.draw(canvas);
        }
        }


        private static ByteBuffer mmap(File file) throws IOException {
        private static ByteBuffer mmap(File file) throws IOException {
+3 −3
Original line number Original line Diff line number Diff line
@@ -69,7 +69,7 @@ final class UpdatableFontDir {


        long getRevision(File file) throws IOException;
        long getRevision(File file) throws IOException;


        void tryToCreateTypeface(File file) throws IOException;
        void tryToCreateTypeface(File file) throws Throwable;
    }
    }


    /** Interface to mock fs-verity in tests. */
    /** Interface to mock fs-verity in tests. */
@@ -378,10 +378,10 @@ final class UpdatableFontDir {
            // Try to create Typeface and treat as failure something goes wrong.
            // Try to create Typeface and treat as failure something goes wrong.
            try {
            try {
                mParser.tryToCreateTypeface(fontFileInfo.getFile());
                mParser.tryToCreateTypeface(fontFileInfo.getFile());
            } catch (IOException e) {
            } catch (Throwable t) {
                throw new SystemFontException(
                throw new SystemFontException(
                        FontManager.RESULT_ERROR_INVALID_FONT_FILE,
                        FontManager.RESULT_ERROR_INVALID_FONT_FILE,
                        "Failed to create Typeface from file", e);
                        "Failed to create Typeface from file", t);
            }
            }


            FontConfig fontConfig = getSystemFontConfig();
            FontConfig fontConfig = getSystemFontConfig();
+1 −1
Original line number Original line Diff line number Diff line
@@ -92,7 +92,7 @@ public final class UpdatableFontDirTest {
        }
        }


        @Override
        @Override
        public void tryToCreateTypeface(File file) throws IOException {
        public void tryToCreateTypeface(File file) throws Throwable {
        }
        }
    }
    }