Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Formatting.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.Drawing;
  7. namespace Novacode
  8. {
  9. /// <summary>
  10. /// A text formatting.
  11. /// </summary>
  12. public class Formatting
  13. {
  14. private XElement rPr;
  15. private bool hidden;
  16. private bool bold;
  17. private bool italic;
  18. private StrickThrough strikethrough;
  19. private Script script;
  20. private Highlight highlight;
  21. private double? size;
  22. private Color? fontColor;
  23. private Color? underlineColor;
  24. private UnderlineStyle underlineStyle;
  25. private Misc misc;
  26. private CapsStyle capsStyle;
  27. private FontFamily fontFamily;
  28. private int? percentageScale;
  29. private int? kerning;
  30. private int? position;
  31. private double? spacing;
  32. /// <summary>
  33. /// A text formatting.
  34. /// </summary>
  35. public Formatting()
  36. {
  37. capsStyle = CapsStyle.none;
  38. strikethrough = StrickThrough.none;
  39. script = Script.none;
  40. highlight = Highlight.none;
  41. underlineStyle = UnderlineStyle.none;
  42. misc = Misc.none;
  43. rPr = new XElement(XName.Get("rPr", DocX.w.NamespaceName));
  44. }
  45. internal XElement Xml
  46. {
  47. get
  48. {
  49. if(spacing.HasValue)
  50. rPr.Add(new XElement(XName.Get("spacing", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), spacing.Value * 20)));
  51. if(position.HasValue)
  52. rPr.Add(new XElement(XName.Get("position", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), position.Value * 2)));
  53. if (kerning.HasValue)
  54. rPr.Add(new XElement(XName.Get("kern", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), kerning.Value * 2)));
  55. if (percentageScale.HasValue)
  56. rPr.Add(new XElement(XName.Get("w", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), percentageScale)));
  57. if(fontFamily != null)
  58. rPr.Add(new XElement(XName.Get("rFonts", DocX.w.NamespaceName), new XAttribute(XName.Get("ascii", DocX.w.NamespaceName), fontFamily.Name)));
  59. if(hidden)
  60. rPr.Add(new XElement(XName.Get("vanish", DocX.w.NamespaceName)));
  61. if (bold)
  62. rPr.Add(new XElement(XName.Get("b", DocX.w.NamespaceName)));
  63. if (italic)
  64. rPr.Add(new XElement(XName.Get("i", DocX.w.NamespaceName)));
  65. switch (underlineStyle)
  66. {
  67. case UnderlineStyle.none:
  68. break;
  69. case UnderlineStyle.singleLine:
  70. rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "single")));
  71. break;
  72. case UnderlineStyle.doubleLine:
  73. rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "double")));
  74. break;
  75. default:
  76. rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), underlineStyle.ToString())));
  77. break;
  78. }
  79. if(underlineColor.HasValue)
  80. {
  81. // If an underlineColor has been set but no underlineStyle has been set
  82. if (underlineStyle == UnderlineStyle.none)
  83. {
  84. // Set the underlineStyle to the default
  85. underlineStyle = UnderlineStyle.singleLine;
  86. rPr.Add(new XElement(XName.Get("u", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "single")));
  87. }
  88. rPr.Element(XName.Get("u", DocX.w.NamespaceName)).Add(new XAttribute(XName.Get("color", DocX.w.NamespaceName), underlineColor.Value.ToHex()));
  89. }
  90. switch (strikethrough)
  91. {
  92. case StrickThrough.none:
  93. break;
  94. case StrickThrough.strike:
  95. rPr.Add(new XElement(XName.Get("strike", DocX.w.NamespaceName)));
  96. break;
  97. case StrickThrough.doubleStrike:
  98. rPr.Add(new XElement(XName.Get("dstrike", DocX.w.NamespaceName)));
  99. break;
  100. default:
  101. break;
  102. }
  103. switch (script)
  104. {
  105. case Script.none:
  106. break;
  107. default:
  108. rPr.Add(new XElement(XName.Get("vertAlign", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), script.ToString())));
  109. break;
  110. }
  111. if (size.HasValue)
  112. {
  113. rPr.Add(new XElement(XName.Get("sz", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), (size * 2).ToString())));
  114. rPr.Add(new XElement(XName.Get("szCs", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), (size * 2).ToString())));
  115. }
  116. if(fontColor.HasValue)
  117. rPr.Add(new XElement(XName.Get("color", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), fontColor.Value.ToHex())));
  118. switch (highlight)
  119. {
  120. case Highlight.none:
  121. break;
  122. default:
  123. rPr.Add(new XElement(XName.Get("highlight", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), highlight.ToString())));
  124. break;
  125. }
  126. switch (capsStyle)
  127. {
  128. case CapsStyle.none:
  129. break;
  130. default:
  131. rPr.Add(new XElement(XName.Get(capsStyle.ToString(), DocX.w.NamespaceName)));
  132. break;
  133. }
  134. switch (misc)
  135. {
  136. case Misc.none:
  137. break;
  138. case Misc.outlineShadow:
  139. rPr.Add(new XElement(XName.Get("outline", DocX.w.NamespaceName)));
  140. rPr.Add(new XElement(XName.Get("shadow", DocX.w.NamespaceName)));
  141. break;
  142. case Misc.engrave:
  143. rPr.Add(new XElement(XName.Get("imprint", DocX.w.NamespaceName)));
  144. break;
  145. default:
  146. rPr.Add(new XElement(XName.Get(misc.ToString(), DocX.w.NamespaceName)));
  147. break;
  148. }
  149. return rPr;
  150. }
  151. }
  152. /// <summary>
  153. /// This formatting will apply Bold.
  154. /// </summary>
  155. public bool Bold { get { return bold; } set { bold = value;} }
  156. /// <summary>
  157. /// This formatting will apply Italic.
  158. /// </summary>
  159. public bool Italic { get { return Italic; } set { italic = value; } }
  160. /// <summary>
  161. /// This formatting will apply StrickThrough.
  162. /// </summary>
  163. public StrickThrough StrikeThrough { get { return strikethrough; } set { strikethrough = value; } }
  164. /// <summary>
  165. /// The script that this formatting should be, normal, superscript or subscript.
  166. /// </summary>
  167. public Script Script { get { return script; } set { script = value; } }
  168. /// <summary>
  169. /// The Size of this text, must be between 0 and 1638.
  170. /// </summary>
  171. public double? Size
  172. {
  173. get { return size; }
  174. set
  175. {
  176. double? temp = value * 2;
  177. if (temp - (int)temp == 0)
  178. {
  179. if(value > 0 && value < 1639)
  180. size = value;
  181. else
  182. throw new ArgumentException("Size", "Value must be in the range 0 - 1638");
  183. }
  184. else
  185. throw new ArgumentException("Size", "Value must be either a whole or half number, examples: 32, 32.5");
  186. }
  187. }
  188. /// <summary>
  189. /// Percentage scale must be one of the following values 200, 150, 100, 90, 80, 66, 50 or 33.
  190. /// </summary>
  191. public int? PercentageScale
  192. {
  193. get { return percentageScale; }
  194. set
  195. {
  196. if ((new int?[] { 200, 150, 100, 90, 80, 66, 50, 33 }).Contains(value))
  197. percentageScale = value;
  198. else
  199. throw new ArgumentOutOfRangeException("PercentageScale", "Value must be one of the following: 200, 150, 100, 90, 80, 66, 50 or 33");
  200. }
  201. }
  202. /// <summary>
  203. /// The Kerning to apply to this text must be one of the following values 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72.
  204. /// </summary>
  205. public int? Kerning
  206. {
  207. get { return kerning; }
  208. set
  209. {
  210. if(new int?[] {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72}.Contains(value))
  211. kerning = value;
  212. else
  213. throw new ArgumentOutOfRangeException("Kerning", "Value must be one of the following: 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48 or 72");
  214. }
  215. }
  216. /// <summary>
  217. /// Text position must be in the range (-1585 - 1585).
  218. /// </summary>
  219. public int? Position
  220. {
  221. get { return position; }
  222. set
  223. {
  224. if (value > -1585 && value < 1585)
  225. position = value;
  226. else
  227. throw new ArgumentOutOfRangeException("Position", "Value must be in the range -1585 - 1585");
  228. }
  229. }
  230. /// <summary>
  231. /// Text spacing must be in the range (-1585 - 1585).
  232. /// </summary>
  233. public double? Spacing
  234. {
  235. get { return spacing; }
  236. set
  237. {
  238. double? temp = value * 20;
  239. if (temp - (int)temp == 0)
  240. {
  241. if (value > -1585 && value < 1585)
  242. spacing = value;
  243. else
  244. throw new ArgumentException("Spacing", "Value must be in the range: -1584 - 1584");
  245. }
  246. else
  247. throw new ArgumentException("Spacing", "Value must be either a whole or acurate to one decimal, examples: 32, 32.1, 32.2, 32.9");
  248. }
  249. }
  250. /// <summary>
  251. /// The colour of the text.
  252. /// </summary>
  253. public Color? FontColor { get { return fontColor; } set { fontColor = value; } }
  254. /// <summary>
  255. /// Highlight colour.
  256. /// </summary>
  257. public Highlight Highlight { get { return highlight; } set { highlight = value; } }
  258. /// <summary>
  259. /// The Underline style that this formatting applies.
  260. /// </summary>
  261. public UnderlineStyle UnderlineStyle { get { return underlineStyle; } set { underlineStyle = value; } }
  262. /// <summary>
  263. /// The underline colour.
  264. /// </summary>
  265. public Color? UnderlineColor { get { return underlineColor; } set { underlineColor = value; } }
  266. /// <summary>
  267. /// Misc settings.
  268. /// </summary>
  269. public Misc Misc { get { return misc; } set { misc = value; } }
  270. /// <summary>
  271. /// Is this text hidden or visible.
  272. /// </summary>
  273. public bool Hidden { get { return hidden; } set { hidden = value; } }
  274. /// <summary>
  275. /// Capitalization style.
  276. /// </summary>
  277. public CapsStyle CapsStyle { get { return capsStyle; } set { capsStyle = value; } }
  278. /// <summary>
  279. /// The font familt of this formatting.
  280. /// </summary>
  281. public FontFamily FontFamily { get { return FontFamily; } set { fontFamily = value; } }
  282. }
  283. }