您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Formatting.cs 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*************************************************************************************
  2. DocX – DocX is the community edition of Xceed Words for .NET
  3. Copyright (C) 2009-2016 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features and fast professional support,
  7. pick up Xceed Words for .NET at https://xceed.com/xceed-words-for-net/
  8. ***********************************************************************************/
  9. using System;
  10. using System.Linq;
  11. using System.Xml.Linq;
  12. using System.Drawing;
  13. using System.Globalization;
  14. namespace Xceed.Words.NET
  15. {
  16. /// <summary>
  17. /// A text formatting.
  18. /// </summary>
  19. public class Formatting : IComparable
  20. {
  21. #region Private Members
  22. private XElement _rPr;
  23. private bool? _hidden;
  24. private bool? _bold;
  25. private bool? _italic;
  26. private StrikeThrough? _strikethrough;
  27. private Script? _script;
  28. private Highlight? _highlight;
  29. private Color? _shading;
  30. private double? _size;
  31. private Color? _fontColor;
  32. private Color? _underlineColor;
  33. private UnderlineStyle? _underlineStyle;
  34. private Misc? _misc;
  35. private CapsStyle? _capsStyle;
  36. private Font _fontFamily;
  37. private int? _percentageScale;
  38. private int? _kerning;
  39. private int? _position;
  40. private double? _spacing;
  41. private string _styleName;
  42. private CultureInfo _language;
  43. #endregion
  44. #region Constructors
  45. /// <summary>
  46. /// A text formatting.
  47. /// </summary>
  48. public Formatting()
  49. {
  50. _capsStyle = Xceed.Words.NET.CapsStyle.none;
  51. _strikethrough = Xceed.Words.NET.StrikeThrough.none;
  52. _script = Xceed.Words.NET.Script.none;
  53. _highlight = Xceed.Words.NET.Highlight.none;
  54. _underlineStyle = Xceed.Words.NET.UnderlineStyle.none;
  55. _misc = Xceed.Words.NET.Misc.none;
  56. // Use current culture by default
  57. _language = CultureInfo.CurrentCulture;
  58. _rPr = new XElement( XName.Get( "rPr", DocX.w.NamespaceName ) );
  59. }
  60. #endregion
  61. #region Public Properties
  62. /// <summary>
  63. /// Text language
  64. /// </summary>
  65. public CultureInfo Language
  66. {
  67. get
  68. {
  69. return _language;
  70. }
  71. set
  72. {
  73. _language = value;
  74. }
  75. }
  76. /// <summary>
  77. /// This formatting will apply Bold.
  78. /// </summary>
  79. public bool? Bold
  80. {
  81. get
  82. {
  83. return _bold;
  84. }
  85. set
  86. {
  87. _bold = value;
  88. }
  89. }
  90. /// <summary>
  91. /// This formatting will apply Italic.
  92. /// </summary>
  93. public bool? Italic
  94. {
  95. get
  96. {
  97. return _italic;
  98. }
  99. set
  100. {
  101. _italic = value;
  102. }
  103. }
  104. /// <summary>
  105. /// This formatting will apply StrickThrough.
  106. /// </summary>
  107. public StrikeThrough? StrikeThrough
  108. {
  109. get
  110. {
  111. return _strikethrough;
  112. }
  113. set
  114. {
  115. _strikethrough = value;
  116. }
  117. }
  118. /// <summary>
  119. /// The script that this formatting should be, normal, superscript or subscript.
  120. /// </summary>
  121. public Script? Script
  122. {
  123. get
  124. {
  125. return _script;
  126. }
  127. set
  128. {
  129. _script = value;
  130. }
  131. }
  132. /// <summary>
  133. /// The Size of this text, must be between 0 and 1638.
  134. /// </summary>
  135. public double? Size
  136. {
  137. get
  138. {
  139. return _size;
  140. }
  141. set
  142. {
  143. double? temp = value * 2;
  144. if( temp - ( int )temp == 0 )
  145. {
  146. if( value > 0 && value < 1639 )
  147. {
  148. _size = value;
  149. }
  150. else
  151. throw new ArgumentException( "Size", "Value must be in the range 0 - 1638" );
  152. }
  153. else
  154. throw new ArgumentException( "Size", "Value must be either a whole or half number, examples: 32, 32.5" );
  155. }
  156. }
  157. /// <summary>
  158. /// Percentage scale must be one of the following values 200, 150, 100, 90, 80, 66, 50 or 33.
  159. /// </summary>
  160. public int? PercentageScale
  161. {
  162. get
  163. {
  164. return _percentageScale;
  165. }
  166. set
  167. {
  168. if( ( new int?[] { 200, 150, 100, 90, 80, 66, 50, 33 } ).Contains( value ) )
  169. {
  170. _percentageScale = value;
  171. }
  172. else
  173. throw new ArgumentOutOfRangeException( "PercentageScale", "Value must be one of the following: 200, 150, 100, 90, 80, 66, 50 or 33" );
  174. }
  175. }
  176. /// <summary>
  177. /// 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.
  178. /// </summary>
  179. public int? Kerning
  180. {
  181. get
  182. {
  183. return _kerning;
  184. }
  185. set
  186. {
  187. if( new int?[] { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 }.Contains( value ) )
  188. _kerning = value;
  189. else
  190. 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" );
  191. }
  192. }
  193. /// <summary>
  194. /// Text position must be in the range (-1585 - 1585).
  195. /// </summary>
  196. public int? Position
  197. {
  198. get
  199. {
  200. return _position;
  201. }
  202. set
  203. {
  204. if( value > -1585 && value < 1585 )
  205. _position = value;
  206. else
  207. throw new ArgumentOutOfRangeException( "Position", "Value must be in the range -1585 - 1585" );
  208. }
  209. }
  210. /// <summary>
  211. /// Text spacing must be in the range (-1585 - 1585).
  212. /// </summary>
  213. public double? Spacing
  214. {
  215. get
  216. {
  217. return _spacing;
  218. }
  219. set
  220. {
  221. double? temp = value * 20;
  222. if( temp - ( int )temp == 0 )
  223. {
  224. if( value > -1585 && value < 1585 )
  225. _spacing = value;
  226. else
  227. throw new ArgumentException( "Spacing", "Value must be in the range: -1584 - 1584" );
  228. }
  229. else
  230. throw new ArgumentException( "Spacing", "Value must be either a whole or acurate to one decimal, examples: 32, 32.1, 32.2, 32.9" );
  231. }
  232. }
  233. /// <summary>
  234. /// The colour of the text.
  235. /// </summary>
  236. public Color? FontColor
  237. {
  238. get
  239. {
  240. return _fontColor;
  241. }
  242. set
  243. {
  244. _fontColor = value;
  245. }
  246. }
  247. /// <summary>
  248. /// Highlight colour.
  249. /// </summary>
  250. public Highlight? Highlight
  251. {
  252. get
  253. {
  254. return _highlight;
  255. }
  256. set
  257. {
  258. _highlight = value;
  259. }
  260. }
  261. /// <summary>
  262. /// Shading color.
  263. /// </summary>
  264. public Color? Shading
  265. {
  266. get
  267. {
  268. return _shading;
  269. }
  270. set
  271. {
  272. _shading = value;
  273. }
  274. }
  275. public string StyleName
  276. {
  277. get
  278. {
  279. return _styleName;
  280. }
  281. set
  282. {
  283. _styleName = value;
  284. }
  285. }
  286. /// <summary>
  287. /// The Underline style that this formatting applies.
  288. /// </summary>
  289. public UnderlineStyle? UnderlineStyle
  290. {
  291. get
  292. {
  293. return _underlineStyle;
  294. }
  295. set
  296. {
  297. _underlineStyle = value;
  298. }
  299. }
  300. /// <summary>
  301. /// The underline colour.
  302. /// </summary>
  303. public Color? UnderlineColor
  304. {
  305. get
  306. {
  307. return _underlineColor;
  308. }
  309. set
  310. {
  311. _underlineColor = value;
  312. }
  313. }
  314. /// <summary>
  315. /// Misc settings.
  316. /// </summary>
  317. public Misc? Misc
  318. {
  319. get
  320. {
  321. return _misc;
  322. }
  323. set
  324. {
  325. _misc = value;
  326. }
  327. }
  328. /// <summary>
  329. /// Is this text hidden or visible.
  330. /// </summary>
  331. public bool? Hidden
  332. {
  333. get
  334. {
  335. return _hidden;
  336. }
  337. set
  338. {
  339. _hidden = value;
  340. }
  341. }
  342. /// <summary>
  343. /// Capitalization style.
  344. /// </summary>
  345. public CapsStyle? CapsStyle
  346. {
  347. get
  348. {
  349. return _capsStyle;
  350. }
  351. set
  352. {
  353. _capsStyle = value;
  354. }
  355. }
  356. /// <summary>
  357. /// The font Family of this formatting.
  358. /// </summary>
  359. /// <!--
  360. /// Bug found and fixed by krugs525 on August 12 2009.
  361. /// Use TFS compare to see exact code change.
  362. /// -->
  363. public Font FontFamily
  364. {
  365. get
  366. {
  367. return _fontFamily;
  368. }
  369. set
  370. {
  371. _fontFamily = value;
  372. }
  373. }
  374. #endregion
  375. #region Internal Properties
  376. internal XElement Xml
  377. {
  378. get
  379. {
  380. _rPr = new XElement( XName.Get( "rPr", DocX.w.NamespaceName ) );
  381. if( _language != null )
  382. {
  383. _rPr.Add( new XElement( XName.Get( "lang", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _language.Name ) ) );
  384. }
  385. if( _spacing.HasValue )
  386. {
  387. _rPr.Add( new XElement( XName.Get( "spacing", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _spacing.Value * 20 ) ) );
  388. }
  389. if( !string.IsNullOrEmpty( _styleName ) )
  390. {
  391. _rPr.Add( new XElement( XName.Get( "rStyle", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _styleName ) ) );
  392. }
  393. if( _position.HasValue )
  394. {
  395. _rPr.Add( new XElement( XName.Get( "position", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _position.Value * 2 ) ) );
  396. }
  397. if( _kerning.HasValue )
  398. {
  399. _rPr.Add( new XElement( XName.Get( "kern", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _kerning.Value * 2 ) ) );
  400. }
  401. if( _percentageScale.HasValue )
  402. {
  403. _rPr.Add( new XElement( XName.Get( "w", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _percentageScale ) ) );
  404. }
  405. if( _fontFamily != null )
  406. {
  407. _rPr.Add
  408. (
  409. new XElement( XName.Get( "rFonts", DocX.w.NamespaceName ), new XAttribute( XName.Get( "ascii", DocX.w.NamespaceName ), _fontFamily.Name ),
  410. new XAttribute( XName.Get( "hAnsi", DocX.w.NamespaceName ), _fontFamily.Name ),
  411. new XAttribute( XName.Get( "cs", DocX.w.NamespaceName ), _fontFamily.Name ),
  412. new XAttribute( XName.Get( "eastAsia", DocX.w.NamespaceName ), _fontFamily.Name ) )
  413. );
  414. }
  415. if( _hidden.HasValue && _hidden.Value )
  416. {
  417. _rPr.Add( new XElement( XName.Get( "vanish", DocX.w.NamespaceName ) ) );
  418. }
  419. if( _bold.HasValue && _bold.Value )
  420. {
  421. _rPr.Add( new XElement( XName.Get( "b", DocX.w.NamespaceName ) ) );
  422. }
  423. if( _italic.HasValue && _italic.Value )
  424. {
  425. _rPr.Add( new XElement( XName.Get( "i", DocX.w.NamespaceName ) ) );
  426. }
  427. if( _underlineStyle.HasValue )
  428. {
  429. switch( _underlineStyle )
  430. {
  431. case Xceed.Words.NET.UnderlineStyle.none:
  432. break;
  433. case Xceed.Words.NET.UnderlineStyle.singleLine:
  434. _rPr.Add( new XElement( XName.Get( "u", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), "single" ) ) );
  435. break;
  436. case Xceed.Words.NET.UnderlineStyle.doubleLine:
  437. _rPr.Add( new XElement( XName.Get( "u", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), "double" ) ) );
  438. break;
  439. default:
  440. _rPr.Add( new XElement( XName.Get( "u", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _underlineStyle.ToString() ) ) );
  441. break;
  442. }
  443. }
  444. if( _underlineColor.HasValue )
  445. {
  446. // If an underlineColor has been set but no underlineStyle has been set
  447. if( _underlineStyle == Xceed.Words.NET.UnderlineStyle.none )
  448. {
  449. // Set the underlineStyle to the default
  450. _underlineStyle = Xceed.Words.NET.UnderlineStyle.singleLine;
  451. _rPr.Add( new XElement( XName.Get( "u", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), "single" ) ) );
  452. }
  453. _rPr.Element( XName.Get( "u", DocX.w.NamespaceName ) ).Add( new XAttribute( XName.Get( "color", DocX.w.NamespaceName ), _underlineColor.Value.ToHex() ) );
  454. }
  455. if( _strikethrough.HasValue )
  456. {
  457. switch( _strikethrough )
  458. {
  459. case Xceed.Words.NET.StrikeThrough.none:
  460. break;
  461. case Xceed.Words.NET.StrikeThrough.strike:
  462. _rPr.Add( new XElement( XName.Get( "strike", DocX.w.NamespaceName ) ) );
  463. break;
  464. case Xceed.Words.NET.StrikeThrough.doubleStrike:
  465. _rPr.Add( new XElement( XName.Get( "dstrike", DocX.w.NamespaceName ) ) );
  466. break;
  467. default:
  468. break;
  469. }
  470. }
  471. if( _script.HasValue )
  472. {
  473. switch( _script )
  474. {
  475. case Xceed.Words.NET.Script.none:
  476. break;
  477. default:
  478. _rPr.Add( new XElement( XName.Get( "vertAlign", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _script.ToString() ) ) );
  479. break;
  480. }
  481. }
  482. if( _size.HasValue )
  483. {
  484. _rPr.Add( new XElement( XName.Get( "sz", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), ( _size * 2 ).ToString() ) ) );
  485. _rPr.Add( new XElement( XName.Get( "szCs", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), ( _size * 2 ).ToString() ) ) );
  486. }
  487. if( _fontColor.HasValue )
  488. {
  489. _rPr.Add( new XElement( XName.Get( "color", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _fontColor.Value.ToHex() ) ) );
  490. }
  491. if( _highlight.HasValue )
  492. {
  493. switch( _highlight )
  494. {
  495. case Xceed.Words.NET.Highlight.none:
  496. break;
  497. default:
  498. _rPr.Add( new XElement( XName.Get( "highlight", DocX.w.NamespaceName ), new XAttribute( XName.Get( "val", DocX.w.NamespaceName ), _highlight.ToString() ) ) );
  499. break;
  500. }
  501. }
  502. if( _shading.HasValue )
  503. {
  504. _rPr.Add( new XElement( XName.Get( "shd", DocX.w.NamespaceName ), new XAttribute( XName.Get( "fill", DocX.w.NamespaceName ), _shading.Value.ToHex() ) ) );
  505. }
  506. if( _capsStyle.HasValue )
  507. {
  508. switch( _capsStyle )
  509. {
  510. case Xceed.Words.NET.CapsStyle.none:
  511. break;
  512. default:
  513. _rPr.Add( new XElement( XName.Get( _capsStyle.ToString(), DocX.w.NamespaceName ) ) );
  514. break;
  515. }
  516. }
  517. if( _misc.HasValue )
  518. {
  519. switch( _misc )
  520. {
  521. case Xceed.Words.NET.Misc.none:
  522. break;
  523. case Xceed.Words.NET.Misc.outlineShadow:
  524. _rPr.Add( new XElement( XName.Get( "outline", DocX.w.NamespaceName ) ) );
  525. _rPr.Add( new XElement( XName.Get( "shadow", DocX.w.NamespaceName ) ) );
  526. break;
  527. case Xceed.Words.NET.Misc.engrave:
  528. _rPr.Add( new XElement( XName.Get( "imprint", DocX.w.NamespaceName ) ) );
  529. break;
  530. default:
  531. _rPr.Add( new XElement( XName.Get( _misc.ToString(), DocX.w.NamespaceName ) ) );
  532. break;
  533. }
  534. }
  535. return _rPr;
  536. }
  537. }
  538. #endregion
  539. #region Public Methods
  540. /// <summary>
  541. /// Returns a cloned instance of Formatting.
  542. /// </summary>
  543. /// <returns></returns>
  544. public Formatting Clone()
  545. {
  546. var clone = new Formatting();
  547. clone.Bold = _bold;
  548. clone.CapsStyle = _capsStyle;
  549. clone.FontColor = _fontColor;
  550. clone.FontFamily = _fontFamily;
  551. clone.Hidden = _hidden;
  552. clone.Highlight = _highlight;
  553. clone.Shading = _shading;
  554. clone.Italic = _italic;
  555. if( _kerning.HasValue )
  556. {
  557. clone.Kerning = _kerning;
  558. }
  559. clone.Language = _language;
  560. clone.Misc = _misc;
  561. if( _percentageScale.HasValue )
  562. {
  563. clone.PercentageScale = _percentageScale;
  564. }
  565. if( _position.HasValue )
  566. {
  567. clone.Position = _position;
  568. }
  569. clone.Script = _script;
  570. if( _size.HasValue )
  571. {
  572. clone.Size = _size;
  573. }
  574. if( _spacing.HasValue )
  575. {
  576. clone.Spacing = _spacing;
  577. }
  578. if( !string.IsNullOrEmpty( _styleName ) )
  579. {
  580. clone.StyleName = _styleName;
  581. }
  582. clone.StrikeThrough = _strikethrough;
  583. clone.UnderlineColor = _underlineColor;
  584. clone.UnderlineStyle = _underlineStyle;
  585. return clone;
  586. }
  587. public static Formatting Parse( XElement rPr, Formatting formatting = null )
  588. {
  589. if( formatting == null )
  590. {
  591. formatting = new Formatting();
  592. }
  593. if( rPr == null )
  594. return formatting;
  595. // Build up the Formatting object.
  596. foreach( XElement option in rPr.Elements() )
  597. {
  598. switch( option.Name.LocalName )
  599. {
  600. case "lang":
  601. formatting.Language = new CultureInfo(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName), null) ?? option.GetAttribute(XName.Get("eastAsia", DocX.w.NamespaceName), null) ?? option.GetAttribute(XName.Get("bidi", DocX.w.NamespaceName)));
  602. break;
  603. case "spacing":
  604. formatting.Spacing = Double.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 20.0;
  605. break;
  606. case "position":
  607. formatting.Position = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2;
  608. break;
  609. case "kern":
  610. formatting.Kerning = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2;
  611. break;
  612. case "w":
  613. formatting.PercentageScale = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName)));
  614. break;
  615. case "sz":
  616. formatting.Size = Int32.Parse(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName))) / 2;
  617. break;
  618. case "rFonts":
  619. var fontName = option.GetAttribute( XName.Get( "cs", DocX.w.NamespaceName ), null )
  620. ?? option.GetAttribute( XName.Get( "ascii", DocX.w.NamespaceName ), null )
  621. ?? option.GetAttribute( XName.Get( "hAnsi", DocX.w.NamespaceName ), null )
  622. ?? option.GetAttribute( XName.Get( "hint", DocX.w.NamespaceName ), null )
  623. ?? option.GetAttribute( XName.Get( "eastAsia", DocX.w.NamespaceName ), null );
  624. formatting.FontFamily = new Font( fontName ?? "Calibri" );
  625. break;
  626. case "color":
  627. try
  628. {
  629. var color = option.GetAttribute(XName.Get("val", DocX.w.NamespaceName));
  630. formatting.FontColor = ( color == "auto") ? Color.Black : ColorTranslator.FromHtml(string.Format("#{0}", color));
  631. }
  632. catch (Exception)
  633. {
  634. // ignore
  635. }
  636. break;
  637. case "vanish":
  638. formatting._hidden = true;
  639. break;
  640. case "b":
  641. formatting.Bold = option.GetAttribute( XName.Get( "val", DocX.w.NamespaceName ) ) != "0";
  642. break;
  643. case "i":
  644. formatting.Italic = true;
  645. break;
  646. case "highlight":
  647. switch( option.GetAttribute( XName.Get( "val", DocX.w.NamespaceName ) ) )
  648. {
  649. case "yellow":
  650. formatting.Highlight = NET.Highlight.yellow;
  651. break;
  652. case "green":
  653. formatting.Highlight = NET.Highlight.green;
  654. break;
  655. case "cyan":
  656. formatting.Highlight = NET.Highlight.cyan;
  657. break;
  658. case "magenta":
  659. formatting.Highlight = NET.Highlight.magenta;
  660. break;
  661. case "blue":
  662. formatting.Highlight = NET.Highlight.blue;
  663. break;
  664. case "red":
  665. formatting.Highlight = NET.Highlight.red;
  666. break;
  667. case "darkBlue":
  668. formatting.Highlight = NET.Highlight.darkBlue;
  669. break;
  670. case "darkCyan":
  671. formatting.Highlight = NET.Highlight.darkCyan;
  672. break;
  673. case "darkGreen":
  674. formatting.Highlight = NET.Highlight.darkGreen;
  675. break;
  676. case "darkMagenta":
  677. formatting.Highlight = NET.Highlight.darkMagenta;
  678. break;
  679. case "darkRed":
  680. formatting.Highlight = NET.Highlight.darkRed;
  681. break;
  682. case "darkYellow":
  683. formatting.Highlight = NET.Highlight.darkYellow;
  684. break;
  685. case "darkGray":
  686. formatting.Highlight = NET.Highlight.darkGray;
  687. break;
  688. case "lightGray":
  689. formatting.Highlight = NET.Highlight.lightGray;
  690. break;
  691. case "black":
  692. formatting.Highlight = NET.Highlight.black;
  693. break;
  694. }
  695. break;
  696. case "strike":
  697. formatting.StrikeThrough = NET.StrikeThrough.strike;
  698. break;
  699. case "dstrike":
  700. formatting.StrikeThrough = NET.StrikeThrough.doubleStrike;
  701. break;
  702. case "u":
  703. formatting.UnderlineStyle = HelperFunctions.GetUnderlineStyle(option.GetAttribute(XName.Get("val", DocX.w.NamespaceName)));
  704. try
  705. {
  706. var color = option.GetAttribute( XName.Get( "color", DocX.w.NamespaceName ) );
  707. if( !string.IsNullOrEmpty( color ) )
  708. {
  709. formatting.UnderlineColor = System.Drawing.ColorTranslator.FromHtml( string.Format( "#{0}", color ) );
  710. }
  711. }
  712. catch( Exception )
  713. {
  714. // ignore
  715. }
  716. break;
  717. case "vertAlign": //script
  718. var script = option.GetAttribute(XName.Get("val", DocX.w.NamespaceName), null);
  719. formatting.Script = (Script)Enum.Parse(typeof(Script), script);
  720. break;
  721. case "caps":
  722. formatting.CapsStyle = NET.CapsStyle.caps;
  723. break;
  724. case "smallCaps":
  725. formatting.CapsStyle = NET.CapsStyle.smallCaps;
  726. break;
  727. case "shd":
  728. var fill = option.GetAttribute( XName.Get( "fill", DocX.w.NamespaceName ) );
  729. if( !string.IsNullOrEmpty( fill ) )
  730. {
  731. formatting.Shading = System.Drawing.ColorTranslator.FromHtml( string.Format( "#{0}", fill ) );
  732. }
  733. break;
  734. case "rStyle":
  735. var style = option.GetAttribute( XName.Get( "val", DocX.w.NamespaceName ), null );
  736. formatting.StyleName = style;
  737. break;
  738. default:
  739. break;
  740. }
  741. }
  742. return formatting;
  743. }
  744. public int CompareTo( object obj )
  745. {
  746. Formatting other = ( Formatting )obj;
  747. if( other._hidden != _hidden )
  748. return -1;
  749. if( other._bold != _bold )
  750. return -1;
  751. if( other._italic != _italic )
  752. return -1;
  753. if( other._strikethrough != _strikethrough )
  754. return -1;
  755. if( other._script != _script )
  756. return -1;
  757. if( other._highlight != _highlight )
  758. return -1;
  759. if( other._shading != _shading )
  760. return -1;
  761. if( other._size != _size )
  762. return -1;
  763. if( other._fontColor != _fontColor )
  764. return -1;
  765. if( other._underlineColor != _underlineColor )
  766. return -1;
  767. if( other._underlineStyle != _underlineStyle )
  768. return -1;
  769. if( other._misc != _misc )
  770. return -1;
  771. if( other._capsStyle != _capsStyle )
  772. return -1;
  773. if( other._fontFamily != _fontFamily )
  774. return -1;
  775. if( other._percentageScale != _percentageScale )
  776. return -1;
  777. if( other._kerning != _kerning )
  778. return -1;
  779. if( other._position != _position )
  780. return -1;
  781. if( other._spacing != _spacing )
  782. return -1;
  783. if( other._styleName != _styleName )
  784. return -1;
  785. if( !other._language.Equals(_language) )
  786. return -1;
  787. return 0;
  788. }
  789. #endregion
  790. }
  791. }