static void LoadBundleFont(Typography.FontManagement.InstalledTypefaceCollection fontCollection, string fontFilename) { if (File.Exists(fontFilename)) { using (Stream s = new FileStream(fontFilename, FileMode.Open, FileAccess.Read)) using (var ms = new MemoryStream())// This is a simple hack because on Xamarin.Android, a `Stream` created by `AssetManager.Open` is not seekable. { s.CopyTo(ms); fontCollection.AddFontStreamSource(new BundleResourceFontStreamSource(new MemoryStream(ms.ToArray()), fontFilename)); } } }
static void LoadBundleFont(Typography.FontManagement.InstalledTypefaceCollection fontCollection, string fontfile) { // This is a simple hack because on Xamarin.Android, a `Stream` created by `AssetManager.Open` is not seekable. using (Stream s = MainActivity.AssetManager.Open(fontfile)) { var ms = new MemoryStream(); s.CopyTo(ms); ms.Position = 0; fontCollection.AddFontStreamSource(new BundleResourceFontStreamSource(ms, fontfile)); } }
} // End Function TypefaceFromFile public static Typography.FontManagement.TypefaceStore GetTypeFaceStore(string fontDirectory) { //1. create font collection Typography.FontManagement.InstalledTypefaceCollection installedFontCollection = new Typography.FontManagement.InstalledTypefaceCollection(); //2. set some essential handler installedFontCollection.SetFontNameDuplicatedHandler((f1, f2) => Typography.FontManagement.FontNameDuplicatedDecision.Skip); // installedFontCollection.LoadFontsFromFolder(fontDirectory); Typography.FontManagement.InstalledTypefaceCollectionExtensions.LoadFontsFromFolder(installedFontCollection, fontDirectory); // // installedFontCollection.LoadSystemFonts(); // Typography.FontManagement.InstalledTypefaceCollectionExtensions.LoadSystemFonts(installedFontCollection); installedFontCollection.UpdateUnicodeRanges(); Typography.FontManagement.TypefaceStore typefaceStore = new Typography.FontManagement.TypefaceStore(); typefaceStore.FontCollection = installedFontCollection; return(typefaceStore); } // End Function GetTypeFaceStore
void LoadFonts(Typography.FontManagement.InstalledTypefaceCollection fontCollection) { LoadBundleFont(fontCollection, "DroidSans.ttf"); LoadBundleFont(fontCollection, "tahoma.ttf"); LoadBundleFont(fontCollection, "SOV_Thanamas.ttf"); }