Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. public enum Script { superscript, subscript, none }
  10. public enum Highlight { yellow, green, cyan, magenta, blue, red, darkBlue, darkCyan, darkGreen, darkMagenta, darkRed, darkYellow, darkGray, lightGray, black, none};
  11. public enum UnderlineStyle { none, singleLine, doubleLine, thick, dotted, dottedHeavy, dash, dashedHeavy, dashLong, dashLongHeavy, dotDash, dashDotHeavy, dotDotDash, dashDotDotHeavy, wave, wavyHeavy, wavyDouble, words};
  12. public enum StrickThrough { none, strike, doubleStrike };
  13. public enum Misc { none, shadow, outline, outlineShadow, emboss, engrave};
  14. public enum CapsStyle { none, caps, smallCaps };
  15. public class Formatting
  16. {
  17. private XElement rPr;
  18. private bool hidden;
  19. private bool bold;
  20. private bool italic;
  21. private StrickThrough strikethrough;
  22. private Script script;
  23. private Highlight highlight;
  24. private double? size;
  25. private Color? fontColor;
  26. private Color? underlineColor;
  27. private UnderlineStyle underlineStyle;
  28. private Misc misc;
  29. private CapsStyle capsStyle;
  30. private FontFamily fontFamily;
  31. private int? percentageScale;
  32. private int? kerning;
  33. private int? position;
  34. private double? spacing;
  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. public 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. public bool Bold { get { return bold; } set { bold = value;} }
  153. public bool Italic { get { return Italic; } set { italic = value; } }
  154. public StrickThrough StrikeThrough { get { return strikethrough; } set { strikethrough = value; } }
  155. public Script Script { get { return script; } set { script = value; } }
  156. public double? Size
  157. {
  158. get { return size; }
  159. set
  160. {
  161. double? temp = value * 2;
  162. if (temp - (int)temp == 0)
  163. {
  164. if(value > 0 && value < 1639)
  165. size = value;
  166. else
  167. throw new ArgumentException("Size", "Value must be in the range 0 - 1638");
  168. }
  169. else
  170. throw new ArgumentException("Size", "Value must be either a whole or half number, examples: 32, 32.5");
  171. }
  172. }
  173. public int? PercentageScale
  174. {
  175. get { return percentageScale; }
  176. set
  177. {
  178. if ((new int?[] { 200, 150, 100, 90, 80, 66, 50, 33 }).Contains(value))
  179. percentageScale = value;
  180. else
  181. throw new ArgumentOutOfRangeException("PercentageScale", "Value must be one of the following: 200, 150, 100, 90, 80, 66, 50 or 33");
  182. }
  183. }
  184. public int? Kerning
  185. {
  186. get { return kerning; }
  187. set
  188. {
  189. if(new int?[] {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72}.Contains(value))
  190. kerning = value;
  191. else
  192. 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");
  193. }
  194. }
  195. public int? Position
  196. {
  197. get { return position; }
  198. set
  199. {
  200. if (value > -1585 && value < 1585)
  201. position = value;
  202. else
  203. throw new ArgumentOutOfRangeException("Position", "Value must be in the range -1585 - 1585");
  204. }
  205. }
  206. public double? Spacing
  207. {
  208. get { return spacing; }
  209. set
  210. {
  211. double? temp = value * 20;
  212. if (temp - (int)temp == 0)
  213. {
  214. if (value > -1585 && value < 1585)
  215. spacing = value;
  216. else
  217. throw new ArgumentException("Spacing", "Value must be in the range: -1584 - 1584");
  218. }
  219. else
  220. throw new ArgumentException("Spacing", "Value must be either a whole or acurate to one decimal, examples: 32, 32.1, 32.2, 32.9");
  221. }
  222. }
  223. public Color? FontColor { get { return fontColor; } set { fontColor = value; } }
  224. public Highlight Highlight { get { return highlight; } set { highlight = value; } }
  225. public UnderlineStyle UnderlineStyle { get { return underlineStyle; } set { underlineStyle = value; } }
  226. public Color? UnderlineColor { get { return underlineColor; } set { underlineColor = value; } }
  227. public Misc Misc { get { return misc; } set { misc = value; } }
  228. public bool Hidden { get { return hidden; } set { hidden = value; } }
  229. public CapsStyle CapsStyle { get { return capsStyle; } set { capsStyle = value; } }
  230. public FontFamily FontFamily { get { return FontFamily; } set { fontFamily = value; } }
  231. }
  232. }