You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Formatting.cs 28KB

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