/// <summary> /// 将字体的导入信息储存到文件中 /// </summary> /// <param name="filePath"></param> /// <param name="info"></param> static public void Save ( string filePath, FontLoadInfo info ) { try { XmlSerializer serializer = new XmlSerializer( typeof( FontLoadInfo ) ); serializer.Serialize( File.Open( filePath, FileMode.Create ), info ); } catch (Exception) { throw; } }
/// <summary> /// 将字体的导入信息储存到文件中 /// </summary> /// <param name="filePath"></param> /// <param name="info"></param> static public void Save(string filePath, FontLoadInfo info) { try { XmlSerializer serializer = new XmlSerializer(typeof(FontLoadInfo)); serializer.Serialize(File.Open(filePath, FileMode.Create), info); } catch (Exception) { throw; } }
/// <summary> /// 从文件中读取字体的导入信息 /// </summary> /// <param name="filePath"></param> /// <returns></returns> static public FontLoadInfo Load ( string filePath ) { try { XmlSerializer serializer = new XmlSerializer( typeof( FontLoadInfo ) ); FileStream stream = File.Open( filePath, FileMode.Open ); return (FontLoadInfo)(serializer.Deserialize( stream )); } catch (Exception e) { if (e is FileNotFoundException) { FontLoadInfo model = new FontLoadInfo(); model.ASCIIFontInfos.Add( new FontInfo( "fontName2", "fontAssetPath : 请填入XNA专用字体信息文件路径" ) ); model.UnitCodeFontInfos.Add( new FontInfo( "fontName1", "TrueTypeFontPath : 请填入TrueType字体文件路径" ) ); Save( filePath, model ); Log.Write( "没找到字体导入配置文件,自动生成配置文件模板" ); } throw; } }
/// <summary> /// 从文件中读取字体的导入信息 /// </summary> /// <param name="filePath"></param> /// <returns></returns> static public FontLoadInfo Load(string filePath) { try { XmlSerializer serializer = new XmlSerializer(typeof(FontLoadInfo)); FileStream stream = File.Open(filePath, FileMode.Open); return((FontLoadInfo)(serializer.Deserialize(stream))); } catch (Exception e) { if (e is FileNotFoundException) { FontLoadInfo model = new FontLoadInfo(); model.ASCIIFontInfos.Add(new FontInfo("fontName2", "fontAssetPath : 请填入XNA专用字体信息文件路径")); model.UnitCodeFontInfos.Add(new FontInfo("fontName1", "TrueTypeFontPath : 请填入TrueType字体文件路径")); Save(filePath, model); Log.Write("没找到字体导入配置文件,自动生成配置文件模板"); } throw; } }
private void LoadFonts(ContentManager contentMgr) { FontLoadInfo fontLoadInfo = FontLoadInfo.Load(configFilePath); chineseWriter.Intitial(fontLoadInfo); if (contentMgr == null) { return; } try { foreach (FontInfo info in fontLoadInfo.ASCIIFontInfos) { fonts.Add(info.name, contentMgr.Load <SpriteFont>(info.path)); } } catch (Exception) { throw; } }