public static Typeface getTypeface(Context ctx, String path) { try { if (path.startsWith(fAssetPathPrefix)) { return Typeface.createFromAsset(ctx.getAssets(), path.replaceFirst("/android_asset/", "")); } else { return Typeface.createFromFile(path); } } catch (RuntimeException e) { Timber.w(e, "Runtime error in getTypeface for File: %s", path); if (!corruptFonts.contains(path)) { // Show warning toast String name = new File(path).getName(); Resources res = AnkiDroidApp.getAppResources(); Toast toast = Toast.makeText(ctx, res.getString(R.string.corrupt_font, name), Toast.LENGTH_LONG); toast.show(); // Don't warn again in this session corruptFonts.add(path); } return null; } }
@Override public void onCreate(Bundle state) { super.onCreate(state); setContentView(R.layout.main); TextView tv=(TextView)findViewById(R.id.custom); Typeface face= Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); tv.setTypeface(face); File font= new File(Environment.getExternalStorageDirectory(), "MgOpenCosmeticaBold.ttf"); if (font.exists()) { tv=(TextView)findViewById(R.id.file); face=Typeface.createFromFile(font); tv.setTypeface(face); } else { findViewById(R.id.filerow).setVisibility(View.GONE); } } }
typeface = Typeface.createFromFile(fontFilePath); new File(fontFilePath).delete(); } catch (IOException e) {
void checkForFontAndColors() { try { @SuppressLint("SdCardPath") File fontFile = new File("/data/data/com.termux/files/home/.termux/font.ttf"); @SuppressLint("SdCardPath") File colorsFile = new File("/data/data/com.termux/files/home/.termux/colors.properties"); final Properties props = new Properties(); if (colorsFile.isFile()) { try (InputStream in = new FileInputStream(colorsFile)) { props.load(in); } } TerminalColors.COLOR_SCHEME.updateWith(props); TerminalSession session = getCurrentTermSession(); if (session != null && session.getEmulator() != null) { session.getEmulator().mColors.reset(); } updateBackgroundColor(); final Typeface newTypeface = (fontFile.exists() && fontFile.length() > 0) ? Typeface.createFromFile(fontFile) : Typeface.MONOSPACE; mTerminalView.setTypeface(newTypeface); } catch (Exception e) { Log.e(EmulatorDebug.LOG_TAG, "Error in checkForFontAndColors()", e); } }
@Test public void createFromFile_withFile_shouldCreateTypeface() { Typeface typeface = Typeface.createFromFile(fontFile); assertThat(typeface.getStyle()).isEqualTo(Typeface.NORMAL); assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("myFont.ttf"); }
@Test public void createFromFile_withPath_shouldCreateTypeface() { Typeface typeface = Typeface.createFromFile(fontFile.getPath()); assertThat(typeface.getStyle()).isEqualTo(Typeface.NORMAL); assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("myFont.ttf"); assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.NORMAL); }
private static boolean loadLocalFontFile(String path, String fontFamily) { if (TextUtils.isEmpty(path) || TextUtils.isEmpty(fontFamily)) { return false; } try { File file = new File(path); if (!file.exists()) { return false; } Typeface typeface = Typeface.createFromFile(path); if (typeface != null) { FontDO fontDo = sCacheMap.get(fontFamily); if (fontDo != null) { fontDo.setState(FontDO.STATE_SUCCESS); fontDo.setTypeface(typeface); if(WXEnvironment.isApkDebugable()) { WXLogUtils.d(TAG, "load local font file success"); } return true; } } else { WXLogUtils.e(TAG, "load local font file failed, can't create font."); } } catch (Exception e) { WXLogUtils.e(TAG, e.toString()); } return false; }
public void setBaseFont(int fontNo, String path) { basefont[fontNo] = Typeface.createFromFile(path); }
public static Typeface getSCTypeface(Context context, String path, boolean assets) { if (assets) { return Typeface.createFromAsset(context.getAssets(), path); } return Typeface.createFromFile(path); }
public static void setTypefaceFromFile (Context context, String path) { File fileFont = new File(path); if (fileFont.exists()) mTypeface = Typeface.createFromFile(fileFont); }
public static Typeface get(File name) { Typeface tf = fontCache.get(name); if(tf == null) { try { tf = Typeface.createFromFile(name); } catch (Exception e) { return null; } fontCache.put(name, tf); } return tf; } }
private Typeface createTypefaceFromFile(String fontPath) { SoftReference<Typeface> typefaceRef = mCache.get(fontPath); Typeface typeface = null; if (typefaceRef == null || (typeface = typefaceRef.get()) == null) { typeface = Typeface.createFromFile(fontPath); typefaceRef = new SoftReference<>(typeface); mCache.put(fontPath, typefaceRef); } return typeface; }
@Override protected Typeface loadTypeface(final FontFamilyType type, final FontInfo fi) { final File f = getFontFile(fi); try { return f.exists() ? Typeface.createFromFile(f) : null; } catch (final Throwable th) { th.printStackTrace(); } return null; } }
void checkForFont() { try { @SuppressLint("SdCardPath") File fontFile = new File("/data/data/com.termux/files/home/.termux/font.ttf"); final Typeface newTypeface = (fontFile.exists() && fontFile.length() > 0) ? Typeface.createFromFile(fontFile) : Typeface.MONOSPACE; mTerminalView.setTypeface(newTypeface); } catch (Exception e) { Log.e(EmulatorDebug.LOG_TAG, "Error in checkForFont()", e); } }
private static Typeface createTypefaceFromFile(Typeface[] typefaces, String family, int style) { final File[] files = getFontFileMap(false).get(family); if (files == null) { return null; } try { if (files[style] != null) { return Typeface.createFromFile(files[style]); } for (int i = 0; i < 4; ++i) { if (files[i] != null) { if (typefaces[i] == null) { typefaces[i] = Typeface.createFromFile(files[i]); } return typefaces[i]; } } } catch (Throwable e) { } return null; }
public Typeface getFont(String key) { String name = getString(key); if (name != null) { File f = new File(getResDataDir("fonts"), name); if (f.exists()) return Typeface.createFromFile(f); } return Typeface.DEFAULT; }
public Typeface getChatFont() { if (mCachedFont != null) return mCachedFont; String font = mPreferences.getString(PREF_CHAT_FONT, "default"); if (ListWithCustomSetting.isPrefCustomValue(font)) { File file = ListWithCustomSetting.getCustomFile(mContext, PREF_CHAT_FONT, font); try { mCachedFont = Typeface.createFromFile(file); return mCachedFont; } catch (Exception ignored) { } } if (font.equals("monospace")) return Typeface.MONOSPACE; else if (font.equals("serif")) return Typeface.SERIF; else return Typeface.DEFAULT; }
public static Typeface getTypeface(String name){ if(contain(fileFonts,name)){ return Typeface.createFromFile(FileUtil.getFontPath(name)); }else if(contain(assetFonts,name)){ return Typeface.createFromAsset(context.getAssets(),"ttf/"+name); }else { return null; } }
private static Typeface getOrCreateEmbeddedTypeface(SystemInfo systemInfo, FontEntry entry, boolean bold, boolean italic) { final Spec spec = new Spec(entry, bold, italic); Object cached = ourCachedEmbeddedTypefaces.get(spec); if (cached == null) { final FileInfo fileInfo = entry.fileInfo(bold, italic); if (fileInfo != null) { final String realFileName = alias(systemInfo, entry.Family, bold, italic); if (copy(fileInfo, realFileName)) { try { cached = Typeface.createFromFile(realFileName); } catch (Throwable t) { // ignore } } new File(realFileName).delete(); } ourCachedEmbeddedTypefaces.put(spec, cached != null ? cached : NULL_OBJECT); } return cached instanceof Typeface ? (Typeface)cached : null; }
private static Typeface getOrCreateEmbeddedTypeface(SystemInfo systemInfo, FontEntry entry, boolean bold, boolean italic) { final Spec spec = new Spec(entry, bold, italic); Object cached = ourCachedEmbeddedTypefaces.get(spec); if (cached == null) { final FileInfo fileInfo = entry.fileInfo(bold, italic); if (fileInfo != null) { final String realFileName = alias(systemInfo, entry.Family, bold, italic); if (copy(fileInfo, realFileName)) { try { cached = Typeface.createFromFile(realFileName); } catch (Throwable t) { // ignore } } new File(realFileName).delete(); } ourCachedEmbeddedTypefaces.put(spec, cached != null ? cached : NULL_OBJECT); } return cached instanceof Typeface ? (Typeface)cached : null; }