Merge "Move some display logic into hierarchy [2/2]"
diff --git a/bridge/src/android/graphics/fonts/Font_Builder_Delegate.java b/bridge/src/android/graphics/fonts/Font_Builder_Delegate.java
index 900e70c..afa7dca 100644
--- a/bridge/src/android/graphics/fonts/Font_Builder_Delegate.java
+++ b/bridge/src/android/graphics/fonts/Font_Builder_Delegate.java
@@ -24,7 +24,10 @@
 import android.annotation.NonNull;
 import android.content.res.AssetManager;
 
+import java.io.File;
+import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.file.Files;
 
 import libcore.util.NativeAllocationRegistry_Delegate;
 
@@ -43,7 +46,10 @@
 public class Font_Builder_Delegate {
     protected static final DelegateManager<Font_Builder_Delegate> sBuilderManager =
             new DelegateManager<>(Font_Builder_Delegate.class);
+    private static final DelegateManager<String> sAssetManager =
+            new DelegateManager<>(String.class);
     private static long sFontFinalizer = -1;
+    private static long sAssetFinalizer = -1;
 
     protected ByteBuffer mBuffer;
     protected int mWeight;
@@ -59,19 +65,34 @@
     @LayoutlibDelegate
     /*package*/ static long nGetNativeAsset(
             @NonNull AssetManager am, @NonNull String path, boolean isAsset, int cookie) {
-        // Not used in layoutlib so far
-        return 0;
+        return sAssetManager.addNewDelegate(path);
     }
 
     @LayoutlibDelegate
     /*package*/ static ByteBuffer nGetAssetBuffer(long nativeAsset) {
-        // Not used in layoutlib so far
-        return null;
+        String fullPath = sAssetManager.getDelegate(nativeAsset);
+        if (fullPath == null) {
+            return null;
+        }
+        try {
+            byte[] byteArray = Files.readAllBytes(new File(fullPath).toPath());
+            return ByteBuffer.wrap(byteArray);
+        } catch (IOException e) {
+            Bridge.getLog().error(LayoutLog.TAG_MISSING_ASSET,
+                    "Error mapping font file " + fullPath, null, null, null);
+            return null;
+        }
     }
 
     @LayoutlibDelegate
     /*package*/ static long nGetReleaseNativeAssetFunc() {
-        return 0;
+        synchronized (Font_Builder_Delegate.class) {
+            if (sAssetFinalizer == -1) {
+                sAssetFinalizer = NativeAllocationRegistry_Delegate.createFinalizer(
+                        sAssetManager::removeJavaReferenceFor);
+            }
+        }
+        return sAssetFinalizer;
     }
 
     @LayoutlibDelegate
diff --git a/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java b/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java
index 662fa3e..b1f417d 100644
--- a/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java
+++ b/bridge/tests/src/com/android/layoutlib/bridge/intensive/RenderTests.java
@@ -762,6 +762,8 @@
     public void testFonts() throws ClassNotFoundException, FileNotFoundException {
         // TODO: styles seem to be broken in TextView
         renderAndVerify("fonts_test.xml", "font_test.png");
+        sRenderMessages.removeIf(
+                message -> message.equals("Font$Builder.nAddAxis is not supported."));
     }
 
     @Test