UnitsConverter.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using System.Drawing;
  2. using System.Globalization;
  3. using System.Windows.Forms;
  4. namespace FastReport.Import.RDL
  5. {
  6. /// <summary>
  7. /// The RDL units converter.
  8. /// </summary>
  9. public static partial class UnitsConverter
  10. {
  11. #region Public Methods
  12. /// <summary>
  13. /// Converts the RDL Boolean to bool value.
  14. /// </summary>
  15. /// <param name="boolean">The RDL Boolean value.</param>
  16. /// <returns>The bool value.</returns>
  17. public static bool BooleanToBool(string boolean)
  18. {
  19. if (boolean.ToLower() == "true")
  20. {
  21. return true;
  22. }
  23. return false;
  24. }
  25. /// <summary>
  26. /// Converts the RDL Color to Color.
  27. /// </summary>
  28. /// <param name="colorName">The RDL Color value.</param>
  29. /// <returns>The Color value.</returns>
  30. public static Color ConvertColor(string colorName)
  31. {
  32. return Color.FromName(colorName);
  33. }
  34. /// <summary>
  35. /// Converts the RDL Size to float value.
  36. /// </summary>
  37. /// <param name="size">The RDL Size value.</param>
  38. /// <param name="unit">The RDL Size units measure.</param>
  39. /// <returns>The float value of RDL Size.</returns>
  40. public static float SizeToFloat(string size, string unit)
  41. {
  42. return float.Parse(size.Replace(unit, ""), new CultureInfo("en-US", false).NumberFormat);
  43. }
  44. /// <summary>
  45. /// Converts the RDL Size to int value.
  46. /// </summary>
  47. /// <param name="size">The RDL Size value.</param>
  48. /// <param name="unit">The RDL Size units measure.</param>
  49. /// <returns>The int value of RDL Size.</returns>
  50. public static int SizeToInt(string size, string unit)
  51. {
  52. int resSize = 0;
  53. int.TryParse(size.Replace(unit, ""), NumberStyles.None, new CultureInfo("en-US", false).NumberFormat, out resSize);
  54. return resSize;
  55. }
  56. /// <summary>
  57. /// Converts the RDL Size to millimeters.
  58. /// </summary>
  59. /// <param name="size">The RDL Size value.</param>
  60. /// <returns>The float value of RDL Size in millimeters.</returns>
  61. public static float SizeToMillimeters(string size)
  62. {
  63. if (size.Contains(SizeUnits.Millimeter))
  64. {
  65. return SizeToFloat(size, SizeUnits.Millimeter);
  66. }
  67. else if (size.Contains(SizeUnits.Centimeter))
  68. {
  69. return SizeToFloat(size, SizeUnits.Centimeter) * SizeUnitsM.Centimeter;
  70. }
  71. else if (size.Contains(SizeUnits.Inch))
  72. {
  73. return SizeToFloat(size, SizeUnits.Inch) * SizeUnitsM.Inch;
  74. }
  75. else if (size.Contains(SizeUnits.Point))
  76. {
  77. return SizeToFloat(size, SizeUnits.Point) * SizeUnitsM.Point;
  78. }
  79. else if (size.Contains(SizeUnits.Pica))
  80. {
  81. return SizeToFloat(size, SizeUnits.Pica) * SizeUnitsM.Pica;
  82. }
  83. return 0.0f;
  84. }
  85. /// <summary>
  86. /// Converts the RDL Size to pixels.
  87. /// </summary>
  88. /// <param name="size">The RDL Size value.</param>
  89. /// <returns>The float value of RDL Size in pixels.</returns>
  90. public static float SizeToPixels(string size)
  91. {
  92. if (size.Contains(SizeUnits.Millimeter))
  93. {
  94. return SizeToFloat(size, SizeUnits.Millimeter) * SizeUnitsP.Millimeter;
  95. }
  96. else if (size.Contains(SizeUnits.Centimeter))
  97. {
  98. return SizeToFloat(size, SizeUnits.Centimeter) * SizeUnitsP.Centimeter;
  99. }
  100. else if (size.Contains(SizeUnits.Inch))
  101. {
  102. return SizeToFloat(size, SizeUnits.Inch) * SizeUnitsP.Inch;
  103. }
  104. else if (size.Contains(SizeUnits.Point))
  105. {
  106. return SizeToFloat(size, SizeUnits.Point) * SizeUnitsP.Point;
  107. }
  108. else if (size.Contains(SizeUnits.Pica))
  109. {
  110. return SizeToFloat(size, SizeUnits.Pica) * SizeUnitsP.Pica;
  111. }
  112. return 0.0f;
  113. }
  114. /// <summary>
  115. /// Converts the RDL FontStyle to FontStyle.
  116. /// </summary>
  117. /// <param name="fontStyle">The RDL FontStyle value.</param>
  118. /// <returns>The FontStyle value.</returns>
  119. public static FontStyle ConvertFontStyle(string fontStyle)
  120. {
  121. if (fontStyle == "Italic")
  122. {
  123. return FontStyle.Italic;
  124. }
  125. return FontStyle.Regular;
  126. }
  127. /// <summary>
  128. /// Converts the RDL FontSize to float.
  129. /// </summary>
  130. /// <param name="fontSize">The RDL FontSize value.</param>
  131. /// <returns>The float value of RDL FontSize in points.</returns>
  132. public static float ConvertFontSize(string fontSize)
  133. {
  134. return float.Parse(fontSize.Replace(SizeUnits.Point, ""), new CultureInfo("en-US", false).NumberFormat);
  135. }
  136. /// <summary>
  137. /// Converts the RDL TextAlign to HorzAlign.
  138. /// </summary>
  139. /// <param name="textAlign">The RDL TextAlign value.</param>
  140. /// <returns>The HorzAlign value.</returns>
  141. public static HorzAlign ConvertTextAlign(string textAlign)
  142. {
  143. if (textAlign == "Center")
  144. {
  145. return HorzAlign.Center;
  146. }
  147. else if (textAlign == "Right")
  148. {
  149. return HorzAlign.Right;
  150. }
  151. return HorzAlign.Left;
  152. }
  153. /// <summary>
  154. /// Converts the RDL TextAlign to VerticalAlign.
  155. /// </summary>
  156. /// <param name="verticalAlign">The RDL VerticalAlign value.</param>
  157. /// <returns>The VertAlign value.</returns>
  158. public static VertAlign ConvertVerticalAlign(string verticalAlign)
  159. {
  160. if (verticalAlign == "Middle")
  161. {
  162. return VertAlign.Center;
  163. }
  164. else if (verticalAlign == "Bottom")
  165. {
  166. return VertAlign.Bottom;
  167. }
  168. return VertAlign.Top;
  169. }
  170. /// <summary>
  171. /// Converts the RDL WritingMode to Angle.
  172. /// </summary>
  173. /// <param name="writingMode">The RDL WritingMode value.</param>
  174. /// <returns>The int value of RDL WritingMode in degree.</returns>
  175. public static int ConvertWritingMode(string writingMode)
  176. {
  177. if (writingMode == "tb-rl")
  178. {
  179. return 90;
  180. }
  181. return 0;
  182. }
  183. /// <summary>
  184. /// Converts the RDL TextAlign to StringAlignment.
  185. /// </summary>
  186. /// <param name="textAlign">The RDL TextAling value.</param>
  187. /// <returns>The StringAlignment value.</returns>
  188. public static StringAlignment ConvertTextAlignToStringAlignment(string textAlign)
  189. {
  190. if (textAlign == "Left")
  191. {
  192. return StringAlignment.Near;
  193. }
  194. else if (textAlign == "Right")
  195. {
  196. return StringAlignment.Far;
  197. }
  198. return StringAlignment.Center;
  199. }
  200. /// <summary>
  201. /// Converts the RDL TextAlign and VerticalAlign to ContentAlignment.
  202. /// </summary>
  203. /// <param name="textAlign">The RDL TextAlign value.</param>
  204. /// <param name="vertAlign">The RDL VerticalAlign value.</param>
  205. /// <returns>The ContentAlignment value.</returns>
  206. public static ContentAlignment ConvertTextAndVerticalAlign(string textAlign, string vertAlign)
  207. {
  208. if (textAlign == "General" || textAlign == "Center")
  209. {
  210. if (vertAlign == "Top")
  211. {
  212. return ContentAlignment.TopCenter;
  213. }
  214. else if (vertAlign == "Middle")
  215. {
  216. return ContentAlignment.MiddleCenter;
  217. }
  218. else if (vertAlign == "Bottom")
  219. {
  220. return ContentAlignment.BottomCenter;
  221. }
  222. return ContentAlignment.TopCenter;
  223. }
  224. else if (textAlign == "Left")
  225. {
  226. if (vertAlign == "Top")
  227. {
  228. return ContentAlignment.TopLeft;
  229. }
  230. else if (vertAlign == "Middle")
  231. {
  232. return ContentAlignment.MiddleLeft;
  233. }
  234. else if (vertAlign == "Bottom")
  235. {
  236. return ContentAlignment.BottomLeft;
  237. }
  238. return ContentAlignment.TopLeft;
  239. }
  240. else if (textAlign == "Right")
  241. {
  242. if (vertAlign == "Top")
  243. {
  244. return ContentAlignment.TopRight;
  245. }
  246. else if (vertAlign == "Middle")
  247. {
  248. return ContentAlignment.MiddleRight;
  249. }
  250. else if (vertAlign == "Bottom")
  251. {
  252. return ContentAlignment.BottomRight;
  253. }
  254. return ContentAlignment.TopRight;
  255. }
  256. return ContentAlignment.TopLeft;
  257. }
  258. /// <summary>
  259. /// Converts the RDL BorderStyle to LineStyle.
  260. /// </summary>
  261. /// <param name="borderStyle">The RDL BorderStyle value.</param>
  262. /// <returns>The LineStyle value.</returns>
  263. public static LineStyle ConvertBorderStyle(string borderStyle)
  264. {
  265. if (borderStyle == "Dotted")
  266. {
  267. return LineStyle.Dot;
  268. }
  269. else if (borderStyle == "Dashed")
  270. {
  271. return LineStyle.Dash;
  272. }
  273. else if (borderStyle == "Double")
  274. {
  275. return LineStyle.Double;
  276. }
  277. return LineStyle.Solid;
  278. }
  279. /// <summary>
  280. /// Converts the RDL Sizing to PictureBoxSizeMode.
  281. /// </summary>
  282. /// <param name="sizing">The RDL Sizing value.</param>
  283. /// <returns>The PictureBoxSizeMode value.</returns>
  284. public static PictureBoxSizeMode ConvertSizing(string sizing)
  285. {
  286. if (sizing == "AutoSize")
  287. {
  288. return PictureBoxSizeMode.AutoSize;
  289. }
  290. else if (sizing == "Fit")
  291. {
  292. return PictureBoxSizeMode.StretchImage;
  293. }
  294. else if (sizing == "Clip")
  295. {
  296. return PictureBoxSizeMode.Normal;
  297. }
  298. return PictureBoxSizeMode.Zoom;
  299. }
  300. /// <summary>
  301. /// Converts the RDL GradientType to GradientStyle.
  302. /// </summary>
  303. /// <param name="gradientType">The RDL GradientType value.</param>
  304. /// <returns>The GradientStyle value.</returns>
  305. /*public static GradientStyle ConvertGradientType(string gradientType)
  306. {
  307. if (gradientType == "LeftRight")
  308. {
  309. return GradientStyle.LeftRight;
  310. }
  311. else if (gradientType == "TopBottom")
  312. {
  313. return GradientStyle.TopBottom;
  314. }
  315. else if (gradientType == "Center")
  316. {
  317. return GradientStyle.Center;
  318. }
  319. else if (gradientType == "DiagonalLeft")
  320. {
  321. return GradientStyle.DiagonalLeft;
  322. }
  323. else if (gradientType == "DiagonalRight")
  324. {
  325. return GradientStyle.DiagonalRight;
  326. }
  327. else if (gradientType == "HorizontalCenter")
  328. {
  329. return GradientStyle.HorizontalCenter;
  330. }
  331. else if (gradientType == "VerticalCenter")
  332. {
  333. return GradientStyle.VerticalCenter;
  334. }
  335. return GradientStyle.None;
  336. }*/
  337. /// <summary>
  338. /// Converts the RDL Chart.Type to SeriesChartType.
  339. /// </summary>
  340. /// <param name="chartType">The RDL Chart.Type value.</param>
  341. /// <returns>The SeriesChartType value.</returns>
  342. /*public static SeriesChartType ConvertChartType(string chartType)
  343. {
  344. if (chartType == "Area")
  345. {
  346. return SeriesChartType.Area;
  347. }
  348. else if (chartType == "Bar")
  349. {
  350. return SeriesChartType.Bar;
  351. }
  352. else if (chartType == "Doughnut")
  353. {
  354. return SeriesChartType.Doughnut;
  355. }
  356. else if (chartType == "Line")
  357. {
  358. return SeriesChartType.Line;
  359. }
  360. else if (chartType == "Pie")
  361. {
  362. return SeriesChartType.Pie;
  363. }
  364. else if (chartType == "Bubble")
  365. {
  366. return SeriesChartType.Bubble;
  367. }
  368. return SeriesChartType.Column;
  369. }*/
  370. /// <summary>
  371. /// Converts the RDL Chart.Palette to ChartColorPalette.
  372. /// </summary>
  373. /// <param name="chartPalette">The RDL Chart.Palette value.</param>
  374. /// <returns>The RDL ChartColorPalette value.</returns>
  375. /*public static ChartColorPalette ConvertChartPalette(string chartPalette)
  376. {
  377. if (chartPalette == "EarthTones")
  378. {
  379. return ChartColorPalette.EarthTones;
  380. }
  381. else if (chartPalette == "Excel")
  382. {
  383. return ChartColorPalette.Excel;
  384. }
  385. else if (chartPalette == "GrayScale")
  386. {
  387. return ChartColorPalette.Grayscale;
  388. }
  389. else if (chartPalette == "Light")
  390. {
  391. return ChartColorPalette.Light;
  392. }
  393. else if (chartPalette == "Pastel")
  394. {
  395. return ChartColorPalette.Pastel;
  396. }
  397. else if (chartPalette == "SemiTransparent")
  398. {
  399. return ChartColorPalette.SemiTransparent;
  400. }
  401. return ChartColorPalette.None;
  402. }*/
  403. /// <summary>
  404. /// Converts the RDL Chart.Legend.Position to Legend.Docking and Legend.Alignment.
  405. /// </summary>
  406. /// <param name="chartLegendPosition">The RDL Chart.Legend.Position value.</param>
  407. /// <param name="legend">The Legend instance to convert to.</param>
  408. /*public static void ConvertChartLegendPosition(string chartLegendPosition, Legend legend)
  409. {
  410. if (chartLegendPosition == "TopLeft")
  411. {
  412. legend.Docking = Docking.Top;
  413. legend.Alignment = StringAlignment.Near;
  414. }
  415. else if (chartLegendPosition == "TopCenter")
  416. {
  417. legend.Docking = Docking.Top;
  418. legend.Alignment = StringAlignment.Center;
  419. }
  420. else if (chartLegendPosition == "TopRight")
  421. {
  422. legend.Docking = Docking.Top;
  423. legend.Alignment = StringAlignment.Far;
  424. }
  425. else if (chartLegendPosition == "LeftTop")
  426. {
  427. legend.Docking = Docking.Left;
  428. legend.Alignment = StringAlignment.Near;
  429. }
  430. else if (chartLegendPosition == "LeftCenter")
  431. {
  432. legend.Docking = Docking.Left;
  433. legend.Alignment = StringAlignment.Center;
  434. }
  435. else if (chartLegendPosition == "LeftBottom")
  436. {
  437. legend.Docking = Docking.Left;
  438. legend.Alignment = StringAlignment.Far;
  439. }
  440. else if (chartLegendPosition == "RightTop")
  441. {
  442. legend.Docking = Docking.Right;
  443. legend.Alignment = StringAlignment.Near;
  444. }
  445. else if (chartLegendPosition == "RightCenter")
  446. {
  447. legend.Docking = Docking.Right;
  448. legend.Alignment = StringAlignment.Center;
  449. }
  450. else if (chartLegendPosition == "RightBottom")
  451. {
  452. legend.Docking = Docking.Right;
  453. legend.Alignment = StringAlignment.Far;
  454. }
  455. else if (chartLegendPosition == "BottomLeft")
  456. {
  457. legend.Docking = Docking.Bottom;
  458. legend.Alignment = StringAlignment.Near;
  459. }
  460. else if (chartLegendPosition == "BottomCenter")
  461. {
  462. legend.Docking = Docking.Bottom;
  463. legend.Alignment = StringAlignment.Center;
  464. }
  465. else if (chartLegendPosition == "BottomRight")
  466. {
  467. legend.Docking = Docking.Bottom;
  468. legend.Alignment = StringAlignment.Far;
  469. }
  470. }*/
  471. /// <summary>
  472. /// Converts the RDL Chart.Legend.Layout to LegendStyle.
  473. /// </summary>
  474. /// <param name="chartLegendLayout">The RDL Chart.Legend.Layout value.</param>
  475. /// <returns>The LegendStyle value.</returns>
  476. /*public static LegendStyle ConvertChartLegendLayout(string chartLegendLayout)
  477. {
  478. if (chartLegendLayout == "Table")
  479. {
  480. return LegendStyle.Table;
  481. }
  482. else if (chartLegendLayout == "Row")
  483. {
  484. return LegendStyle.Row;
  485. }
  486. return LegendStyle.Column;
  487. }*/
  488. /// <summary>
  489. /// Converts the RDL BorderStyle to ChartDashStyle.
  490. /// </summary>
  491. /// <param name="borderStyle">The RDL BorderStyle value.</param>
  492. /// <returns>The ChartDashStyle value.</returns>
  493. /*public static ChartDashStyle ConvertBorderStyleToChartDashStyle(string borderStyle)
  494. {
  495. if (borderStyle == "Dotted")
  496. {
  497. return ChartDashStyle.Dot;
  498. }
  499. else if (borderStyle == "Dashed")
  500. {
  501. return ChartDashStyle.Dash;
  502. }
  503. return ChartDashStyle.Solid;
  504. }*/
  505. /// <summary>
  506. /// Converts the RDL Axis.Visible to AxisEnabled.
  507. /// </summary>
  508. /// <param name="axisVisible">The RDL Axis.Visible value.</param>
  509. /// <returns>The AxisEnabled value.</returns>
  510. /*public static AxisEnabled ConvertAxisVisibleToAxisEnabled(string axisVisible)
  511. {
  512. if (axisVisible.ToLower() == "true")
  513. {
  514. return AxisEnabled.True;
  515. }
  516. else if (axisVisible.ToLower() == "false")
  517. {
  518. return AxisEnabled.False;
  519. }
  520. return AxisEnabled.Auto;
  521. }*/
  522. /// <summary>
  523. /// Converts the RDL TickMarkStyle to TickMarkStyle.
  524. /// </summary>
  525. /// <param name="tickMarkStyle">The RDL TickMarkStyle value.</param>
  526. /// <returns>The TickMarkStyle value.</returns>
  527. /*public static TickMarkStyle ConvertTickMarkStyle(string tickMarkStyle)
  528. {
  529. if (tickMarkStyle == "Inside")
  530. {
  531. return TickMarkStyle.InsideArea;
  532. }
  533. else if (tickMarkStyle == "Outside")
  534. {
  535. return TickMarkStyle.OutsideArea;
  536. }
  537. else if (tickMarkStyle == "Cross")
  538. {
  539. return TickMarkStyle.AcrossAxis;
  540. }
  541. return TickMarkStyle.None;
  542. }*/
  543. /// <summary>
  544. /// Converts the RDL Shading to LightStyle.
  545. /// </summary>
  546. /// <param name="shading">The RDL Shading value.</param>
  547. /// <returns>The LightStyle value.</returns>
  548. /*public static LightStyle ConvertShading(string shading)
  549. {
  550. if (shading == "Simple")
  551. {
  552. return LightStyle.Simplistic;
  553. }
  554. else if (shading == "Real")
  555. {
  556. return LightStyle.Realistic;
  557. }
  558. return LightStyle.None;
  559. }*/
  560. #endregion // Public Methods
  561. }
  562. }