-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFontXml.cs
131 lines (103 loc) · 2.68 KB
/
FontXml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
using System.Xml;
namespace UnitaleFontMaker
{
public class FontXml
{
private XmlDocument doc;
private XmlElement font;
private XmlElement voice;
private XmlElement lineSpacing;
private XmlElement spriteSheet;
private Character[] chars;
public string Voice
{
get { return voice.InnerText; }
set { voice.InnerText = value; }
}
public string LineSpacing
{
get { return lineSpacing.InnerText; }
set { lineSpacing.InnerText = value; }
}
public Character[] Characters
{
get { return chars; }
set { chars = value; }
}
public FontXml()
{
doc = new XmlDocument();//创建XML文档
XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);//XML文档声明
doc.AppendChild(decl);//插入文档声明
font = doc.CreateElement("font");//创建 font 元素
doc.AppendChild(font);
voice = doc.CreateElement("voice");
font.AppendChild(voice);
lineSpacing = doc.CreateElement("linespacing");
font.AppendChild(lineSpacing);
spriteSheet = doc.CreateElement("spritesheet");
font.AppendChild(spriteSheet);
}
/// <summary>
/// 保存 XML 文件
/// </summary>
/// <param name="path">保存路径</param>
public void Save(string path)
{
for (int i = 0; i < Characters.Length; i++)
{
XmlElement spr = doc.CreateElement("sprite");
string name = CheckSpecialChar(Characters[i].Char.ToString());
spr.SetAttribute("name", name);
spriteSheet.AppendChild(spr);
XmlElement rect = doc.CreateElement("rect");
rect.SetAttribute("x", ((int)Characters[i].X).ToString());
rect.SetAttribute("y", ((int)Characters[i].Y).ToString());
rect.SetAttribute("w", ((int)Characters[i].Width).ToString());
rect.SetAttribute("h", ((int)Characters[i].Height).ToString());
spr.AppendChild(rect);
}
doc.Save(path);
}
/// <summary>
/// 检查特殊字符并替换
/// </summary>
/// <param name="str">待检查的字符</param>
/// <returns>替换后的结果</returns>
private string CheckSpecialChar(string str)
{
switch (str)
{
case "/":
return "slash";
case ".":
return "dot";
case "|":
return "pipe";
case "\\":
return "backslash";
case ":":
return "colon";
case "?":
return "questionmark";
case "\"":
return "doublequote";
case "*":
return "asterisk";
case " ":
return "space";
case "<":
return "lt";
case ">":
return "rt";
case "&":
return "ampersand";
}
return str;
}
}
}