Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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