1 /* 2 * Copyright (C) 2017 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.text; 18 19 import android.annotation.NonNull; 20 import android.content.Context; 21 import android.content.res.AssetManager; 22 import android.graphics.FontFamily; 23 import android.graphics.Typeface; 24 import android.support.test.InstrumentationRegistry; 25 import android.util.ArrayMap; 26 27 import java.io.File; 28 import java.io.FileOutputStream; 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.nio.charset.Charset; 32 import java.nio.file.Files; 33 import java.nio.file.StandardCopyOption; 34 35 public class FontFallbackSetup implements AutoCloseable { 36 private final String[] mTestFontFiles; 37 private final String mXml; 38 private final String mTestFontsDir; 39 final ArrayMap<String, Typeface> mFontMap = new ArrayMap<>(); 40 FontFallbackSetup(@onNull String testSubDir, @NonNull String[] testFontFiles, @NonNull String xml)41 public FontFallbackSetup(@NonNull String testSubDir, @NonNull String[] testFontFiles, 42 @NonNull String xml) { 43 mTestFontFiles = testFontFiles; 44 mXml = xml; 45 46 final Context targetCtx = InstrumentationRegistry.getInstrumentation().getTargetContext(); 47 final File cacheDir = new File(targetCtx.getCacheDir(), testSubDir); 48 if (!cacheDir.isDirectory()) { 49 final boolean dirsCreated = cacheDir.mkdirs(); 50 if (!dirsCreated) { 51 throw new RuntimeException("Creating test directories for fonts failed."); 52 } 53 } 54 mTestFontsDir = cacheDir.getAbsolutePath() + "/"; 55 56 final String testFontsXml = new File(mTestFontsDir, "fonts.xml").getAbsolutePath(); 57 final AssetManager am = 58 InstrumentationRegistry.getInstrumentation().getContext().getAssets(); 59 for (String fontFile : mTestFontFiles) { 60 final String sourceInAsset = "fonts/" + fontFile; 61 final File outInCache = new File(mTestFontsDir, fontFile); 62 try (InputStream is = am.open(sourceInAsset)) { 63 Files.copy(is, outInCache.toPath(), StandardCopyOption.REPLACE_EXISTING); 64 } catch (IOException e) { 65 throw new RuntimeException(e); 66 } 67 } 68 69 try (FileOutputStream fos = new FileOutputStream(testFontsXml)) { 70 fos.write(mXml.getBytes(Charset.forName("UTF-8"))); 71 } catch (IOException e) { 72 throw new RuntimeException(e); 73 } 74 75 final ArrayMap<String, FontFamily[]> fallbackMap = new ArrayMap<>(); 76 Typeface.buildSystemFallback(testFontsXml, mTestFontsDir, mFontMap, fallbackMap); 77 } 78 79 @NonNull getTypefaceFor(@onNull String fontName)80 public Typeface getTypefaceFor(@NonNull String fontName) { 81 return mFontMap.get(fontName); 82 } 83 84 @NonNull getPaintFor(@onNull String fontName)85 public TextPaint getPaintFor(@NonNull String fontName) { 86 final TextPaint paint = new TextPaint(); 87 paint.setTypeface(getTypefaceFor(fontName)); 88 return paint; 89 } 90 91 @Override close()92 public void close() { 93 for (String fontFile : mTestFontFiles) { 94 final File outInCache = new File(mTestFontsDir, fontFile); 95 outInCache.delete(); 96 } 97 } 98 } 99