BmFont XML Serializer for C#


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:01 p.m.

FORMAT: Text only

SIZE: 6.1 kB

HITS: 1315

  1. // ---- AngelCode BmFont XML serializer ----------------------
  2. // ---- By DeadlyDan @ deadlydan@gmail.com -------------------
  3. // ---- There's no license restrictions, use as you will. ----
  4. // ---- Credits to http://www.angelcode.com/ -----------------
  5. using System;
  6. using System.IO;
  7. using System.Xml.Serialization;
  8. namespace BmFont
  9. {
  10. [Serializable]
  11. [XmlRoot ( "font" )]
  12. public class FontFile
  13. {
  14. [XmlElement ( "info" )]
  15. public FontInfo Info
  16. {
  17. get;
  18. set;
  19. }
  20. [XmlElement ( "common" )]
  21. public FontCommon Common
  22. {
  23. get;
  24. set;
  25. }
  26. [XmlArray ( "pages" )]
  27. [XmlArrayItem ( "page" )]
  28. public List<FontPage> Pages
  29. {
  30. get;
  31. set;
  32. }
  33. [XmlArray ( "chars" )]
  34. [XmlArrayItem ( "char" )]
  35. public List<FontChar> Chars
  36. {
  37. get;
  38. set;
  39. }
  40. [XmlArray ( "kernings" )]
  41. [XmlArrayItem ( "kerning" )]
  42. public List<FontKerning> Kernings
  43. {
  44. get;
  45. set;
  46. }
  47. }
  48. [Serializable]
  49. public class FontInfo
  50. {
  51. [XmlAttribute ( "face" )]
  52. public String Face
  53. {
  54. get;
  55. set;
  56. }
  57. [XmlAttribute ( "size" )]
  58. public Int32 Size
  59. {
  60. get;
  61. set;
  62. }
  63. [XmlAttribute ( "bold" )]
  64. public Int32 Bold
  65. {
  66. get;
  67. set;
  68. }
  69. [XmlAttribute ( "italic" )]
  70. public Int32 Italic
  71. {
  72. get;
  73. set;
  74. }
  75. [XmlAttribute ( "charset" )]
  76. public String CharSet
  77. {
  78. get;
  79. set;
  80. }
  81. [XmlAttribute ( "unicode" )]
  82. public Int32 Unicode
  83. {
  84. get;
  85. set;
  86. }
  87. [XmlAttribute ( "stretchH" )]
  88. public Int32 StretchHeight
  89. {
  90. get;
  91. set;
  92. }
  93. [XmlAttribute ( "smooth" )]
  94. public Int32 Smooth
  95. {
  96. get;
  97. set;
  98. }
  99. [XmlAttribute ( "aa" )]
  100. public Int32 SuperSampling
  101. {
  102. get;
  103. set;
  104. }
  105. private Rectangle _Padding;
  106. [XmlAttribute ( "padding" )]
  107. public String Padding
  108. {
  109. get
  110. {
  111. return _Padding.X + "," + _Padding.Y + "," + _Padding.Width + "," + _Padding.Height;
  112. }
  113. set
  114. {
  115. String[] padding = value.Split ( ',' );
  116. _Padding = new Rectangle ( Convert.ToInt32 ( padding[0] ), Convert.ToInt32 ( padding[1] ), Convert.ToInt32 ( padding[2] ), Convert.ToInt32 ( padding[3] ) );
  117. }
  118. }
  119. private Point _Spacing;
  120. [XmlAttribute ( "spacing" )]
  121. public String Spacing
  122. {
  123. get
  124. {
  125. return _Spacing.X + "," + _Spacing.Y;
  126. }
  127. set
  128. {
  129. String[] spacing = value.Split ( ',' );
  130. _Spacing = new Point ( Convert.ToInt32 ( spacing[0] ), Convert.ToInt32 ( spacing[1] ) );
  131. }
  132. }
  133. [XmlAttribute ( "outline" )]
  134. public Int32 OutLine
  135. {
  136. get;
  137. set;
  138. }
  139. }
  140. [Serializable]
  141. public class FontCommon
  142. {
  143. [XmlAttribute ( "lineHeight" )]
  144. public Int32 LineHeight
  145. {
  146. get;
  147. set;
  148. }
  149. [XmlAttribute ( "base" )]
  150. public Int32 Base
  151. {
  152. get;
  153. set;
  154. }
  155. [XmlAttribute ( "scaleW" )]
  156. public Int32 ScaleW
  157. {
  158. get;
  159. set;
  160. }
  161. [XmlAttribute ( "scaleH" )]
  162. public Int32 ScaleH
  163. {
  164. get;
  165. set;
  166. }
  167. [XmlAttribute ( "pages" )]
  168. public Int32 Pages
  169. {
  170. get;
  171. set;
  172. }
  173. [XmlAttribute ( "packed" )]
  174. public Int32 Packed
  175. {
  176. get;
  177. set;
  178. }
  179. [XmlAttribute ( "alphaChnl" )]
  180. public Int32 AlphaChannel
  181. {
  182. get;
  183. set;
  184. }
  185. [XmlAttribute ( "redChnl" )]
  186. public Int32 RedChannel
  187. {
  188. get;
  189. set;
  190. }
  191. [XmlAttribute ( "greenChnl" )]
  192. public Int32 GreenChannel
  193. {
  194. get;
  195. set;
  196. }
  197. [XmlAttribute ( "blueChnl" )]
  198. public Int32 BlueChannel
  199. {
  200. get;
  201. set;
  202. }
  203. }
  204. [Serializable]
  205. public class FontPage
  206. {
  207. [XmlAttribute ( "id" )]
  208. public Int32 ID
  209. {
  210. get;
  211. set;
  212. }
  213. [XmlAttribute ( "file" )]
  214. public String File
  215. {
  216. get;
  217. set;
  218. }
  219. }
  220. [Serializable]
  221. public class FontChar
  222. {
  223. [XmlAttribute ( "id" )]
  224. public Int32 ID
  225. {
  226. get;
  227. set;
  228. }
  229. [XmlAttribute ( "x" )]
  230. public Int32 X
  231. {
  232. get;
  233. set;
  234. }
  235. [XmlAttribute ( "y" )]
  236. public Int32 Y
  237. {
  238. get;
  239. set;
  240. }
  241. [XmlAttribute ( "width" )]
  242. public Int32 Width
  243. {
  244. get;
  245. set;
  246. }
  247. [XmlAttribute ( "height" )]
  248. public Int32 Height
  249. {
  250. get;
  251. set;
  252. }
  253. [XmlAttribute ( "xoffset" )]
  254. public Int32 XOffset
  255. {
  256. get;
  257. set;
  258. }
  259. [XmlAttribute ( "yoffset" )]
  260. public Int32 YOffset
  261. {
  262. get;
  263. set;
  264. }
  265. [XmlAttribute ( "xadvance" )]
  266. public Int32 XAdvance
  267. {
  268. get;
  269. set;
  270. }
  271. [XmlAttribute ( "page" )]
  272. public Int32 Page
  273. {
  274. get;
  275. set;
  276. }
  277. [XmlAttribute ( "chnl" )]
  278. public Int32 Channel
  279. {
  280. get;
  281. set;
  282. }
  283. }
  284. [Serializable]
  285. public class FontKerning
  286. {
  287. [XmlAttribute ( "first" )]
  288. public Int32 First
  289. {
  290. get;
  291. set;
  292. }
  293. [XmlAttribute ( "second" )]
  294. public Int32 Second
  295. {
  296. get;
  297. set;
  298. }
  299. [XmlAttribute ( "amount" )]
  300. public Int32 Amount
  301. {
  302. get;
  303. set;
  304. }
  305. }
  306. public class FontLoader
  307. {
  308. public static FontFile Load ( String filename )
  309. {
  310. XmlSerializer deserializer = new XmlSerializer ( typeof ( FontFile ) );
  311. TextReader textReader = new StreamReader ( filename );
  312. FontFile file = ( FontFile ) deserializer.Deserialize ( textReader );
  313. textReader.Close ( );
  314. return file;
  315. }
  316. }
  317. }

comments powered by Disqus