1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics.fonts; 18 19 import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.text.FontConfig; 24 import android.util.Log; 25 26 import java.io.File; 27 import java.io.FileInputStream; 28 import java.io.IOException; 29 import java.nio.ByteBuffer; 30 import java.nio.channels.FileChannel; 31 import java.util.Locale; 32 import java.util.Map; 33 34 /** 35 * Delegate implementing the native methods of android.graphics.fonts.SystemFonts 36 * <p> 37 * Through the layoutlib_create tool, the original native methods of SystemFonts have been 38 * replaced by calls to methods of the same name in this delegate class. 39 * <p> 40 * This class behaves like the original native implementation, but in Java, keeping previously 41 * native data into its own objects and mapping them to int that are sent back and forth between it 42 * and the original SystemFonts class. 43 * 44 */ 45 public class SystemFonts_Delegate { 46 47 private static final String TAG = "SystemFonts_Delegate"; 48 private static String sFontLocation; 49 public static boolean sIsTypefaceInitialized = false; 50 setFontLocation(String fontLocation)51 public static void setFontLocation(String fontLocation) { 52 sFontLocation = fontLocation; 53 } 54 55 @LayoutlibDelegate getSystemFontConfigInternal( String fontsXml, String systemFontDir, String oemXml, String productFontDir, Map<String, File> updatableFontMap, long lastModifiedDate, int configVersion)56 /*package*/ static FontConfig getSystemFontConfigInternal( 57 String fontsXml, 58 String systemFontDir, 59 String oemXml, 60 String productFontDir, 61 Map<String, File> updatableFontMap, 62 long lastModifiedDate, 63 int configVersion) { 64 sIsTypefaceInitialized = true; 65 return SystemFonts.getSystemFontConfigInternal_Original( 66 sFontLocation + "fonts.xml", sFontLocation, null, null, updatableFontMap, 67 lastModifiedDate, configVersion); 68 } 69 70 @LayoutlibDelegate mmap(@onNull String fullPath)71 /*package*/ static ByteBuffer mmap(@NonNull String fullPath) { 72 // Android does memory mapping for font files. But Windows keeps files open 73 // until the byte buffer from the memory mapping is garbage collected. 74 // To avoid that, on Windows, read the file into a byte buffer instead. 75 // See JDK-4715154. 76 String osName = System.getProperty("os.name").toLowerCase(Locale.US); 77 if (osName.startsWith("windows")) { 78 try (FileInputStream file = new FileInputStream(fullPath)) { 79 final FileChannel fileChannel = file.getChannel(); 80 final int size = (int) fileChannel.size(); 81 // Native code requires the ByteBuffer to be direct 82 // (see android/graphics/fonts/Font.cpp) 83 ByteBuffer buffer = ByteBuffer.allocateDirect(size); 84 fileChannel.read(buffer); 85 return buffer; 86 } catch (IOException e) { 87 Log.e(TAG, "Error mapping font file " + fullPath); 88 return null; 89 } 90 } else { 91 return SystemFonts.mmap_Original(fullPath); 92 } 93 } 94 }