UnitsConverter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using FastReport.DataVisualization.Charting;
  7. namespace FastReport.Design.ExportPlugins.RDL
  8. {
  9. /// <summary>
  10. /// The FR units converter.
  11. /// </summary>
  12. internal static class UnitsConverter
  13. {
  14. #region Public Methods
  15. /// <summary>
  16. /// Converts the float size in pixels to string value in millimeters.
  17. /// </summary>
  18. /// <param name="pixels">The float value in pixels.</param>
  19. /// <returns>The string value in millimeters.</returns>
  20. public static string PixelsToMillimeters(float pixels)
  21. {
  22. return (pixels / Import.RDL.SizeUnitsP.Millimeter).ToString() + "mm";
  23. }
  24. /// <summary>
  25. /// Converts the float size in millimeters to string value in millimeters.
  26. /// </summary>
  27. /// <param name="millimeters">The float value in millimeters.</param>
  28. /// <returns>The string value in millimeters.</returns>
  29. public static string MillimetersToString(float millimeters)
  30. {
  31. return millimeters.ToString() + "mm";
  32. }
  33. /// <summary>
  34. /// Converts the bool value to string.
  35. /// </summary>
  36. /// <param name="value">The bool value.</param>
  37. /// <returns>The string value.</returns>
  38. public static string ConvertBool(bool value)
  39. {
  40. return value.ToString().ToLower();
  41. }
  42. /// <summary>
  43. /// Converts the int size in pixels to string value in pt.
  44. /// </summary>
  45. /// <param name="pixels">The int value in pixels.</param>
  46. /// <returns>The string value in pt.</returns>
  47. public static string ConvertPixels(int pixels)
  48. {
  49. return pixels.ToString() + "pt";
  50. }
  51. /// <summary>
  52. /// Converts the Color value to string.
  53. /// </summary>
  54. /// <param name="color">The Color value.</param>
  55. /// <returns>The string representation of Color value.</returns>
  56. public static string ConvertColor(Color color)
  57. {
  58. if (color.ToKnownColor() == 0)
  59. {
  60. return Color.Red.Name;
  61. }
  62. return color.Name;
  63. }
  64. /// <summary>
  65. /// Converts the LineStyle value to RDL BorderStyle value.
  66. /// </summary>
  67. /// <param name="ls">The LineStyle value.</param>
  68. /// <returns>The string with RDL BorderStyle value.</returns>
  69. public static string ConvertLineStyle(LineStyle ls)
  70. {
  71. if (ls == LineStyle.Dot)
  72. {
  73. return "Dotted";
  74. }
  75. else if (ls == LineStyle.Dash)
  76. {
  77. return "Dashed";
  78. }
  79. else if (ls == LineStyle.Double)
  80. {
  81. return "Double";
  82. }
  83. return "Solid";
  84. }
  85. /// <summary>
  86. /// Converts the GradientStyle value to RDL GradientType value.
  87. /// </summary>
  88. /// <param name="gs">The GradientStyle value.</param>
  89. /// <returns>The string with RDL GradientType value.</returns>
  90. public static string ConvertGradientStyle(GradientStyle gs)
  91. {
  92. if (gs == GradientStyle.Center)
  93. {
  94. return "Center";
  95. }
  96. else if (gs == GradientStyle.DiagonalLeft)
  97. {
  98. return "DiagonalLeft";
  99. }
  100. else if (gs == GradientStyle.DiagonalRight)
  101. {
  102. return "DiagonalRight";
  103. }
  104. else if (gs == GradientStyle.HorizontalCenter)
  105. {
  106. return "HorizontalCenter";
  107. }
  108. else if (gs == GradientStyle.LeftRight)
  109. {
  110. return "LeftRight";
  111. }
  112. else if (gs == GradientStyle.TopBottom)
  113. {
  114. return "TopBottom";
  115. }
  116. else if (gs == GradientStyle.VerticalCenter)
  117. {
  118. return "VerticalCenter";
  119. }
  120. return "None";
  121. }
  122. /// <summary>
  123. /// Converts the FontStyle value to RDL FontStyle value.
  124. /// </summary>
  125. /// <param name="fs">The FontStyle value.</param>
  126. /// <returns>The string with RDL FontStyle value.</returns>
  127. public static string ConvertFontStyle(FontStyle fs)
  128. {
  129. if (fs == FontStyle.Italic)
  130. {
  131. return "Italic";
  132. }
  133. return "Normal";
  134. }
  135. /// <summary>
  136. /// Converts the FontFamily value to RDL FontFamily value.
  137. /// </summary>
  138. /// <param name="ff">The FontFamily value.</param>
  139. /// <returns>The string with RDL FontFamily value.</returns>
  140. public static string ConvertFontFamily(FontFamily ff)
  141. {
  142. return ff.Name;
  143. }
  144. /// <summary>
  145. /// Converts the HorzAlign value to RDL TextAlign value.
  146. /// </summary>
  147. /// <param name="ha">The HorzAlign value.</param>
  148. /// <returns>The string with RDL TextAling value.</returns>
  149. public static string ConvertHorzAlign(HorzAlign ha)
  150. {
  151. if (ha == HorzAlign.Center)
  152. {
  153. return "Center";
  154. }
  155. else if (ha == HorzAlign.Right)
  156. {
  157. return "Right";
  158. }
  159. return "Left";
  160. }
  161. /// <summary>
  162. /// Converts the VertAling value to RDL VerticalAling value.
  163. /// </summary>
  164. /// <param name="va">The VertAling value.</param>
  165. /// <returns>The string with RDL VerticalAlign value.</returns>
  166. public static string ConvertVertAlign(VertAlign va)
  167. {
  168. if (va == VertAlign.Center)
  169. {
  170. return "Middle";
  171. }
  172. else if (va == VertAlign.Bottom)
  173. {
  174. return "Bottom";
  175. }
  176. return "Top";
  177. }
  178. /// <summary>
  179. /// Converts the Angle value to RDL WritingMode value.
  180. /// </summary>
  181. /// <param name="angle">The Angle value.</param>
  182. /// <returns>The string with RDL WritingMode value.</returns>
  183. public static string ConvertAngleToWritingMode(int angle)
  184. {
  185. if (angle == 90)
  186. {
  187. return "tb-rl";
  188. }
  189. return "lr-tb";
  190. }
  191. /// <summary>
  192. /// Converts the FontSize value to RDL FontSize value.
  193. /// </summary>
  194. /// <param name="fs">The FontSize value.</param>
  195. /// <returns>The string with RDL FontSize value.</returns>
  196. public static string ConvertFontSize(float fs)
  197. {
  198. return fs.ToString() + "pt";
  199. }
  200. /// <summary>
  201. /// Converts the PictureBoxSizeMode value to RDL Sizing value.
  202. /// </summary>
  203. /// <param name="sm">The PictureBoxSizeMode value.</param>
  204. /// <returns>The string with RDL Sizing value.</returns>
  205. public static string ConvertSizeMode(PictureBoxSizeMode sm)
  206. {
  207. if (sm == PictureBoxSizeMode.AutoSize)
  208. {
  209. return "AutoSize";
  210. }
  211. else if (sm == PictureBoxSizeMode.StretchImage)
  212. {
  213. return "Fit";
  214. }
  215. else if (sm == PictureBoxSizeMode.Zoom)
  216. {
  217. return "FitProportional";
  218. }
  219. return "Clip";
  220. }
  221. /// <summary>
  222. /// Converts the SeriesChartType value to RDL Chart.Type value.
  223. /// </summary>
  224. /// <param name="type">The SeriesChartType value.</param>
  225. /// <returns>The string with RDL Chart.Type value.</returns>
  226. public static string ConvertChartType(SeriesChartType type)
  227. {
  228. if (type == SeriesChartType.Area)
  229. {
  230. return "Area";
  231. }
  232. else if (type == SeriesChartType.Bar)
  233. {
  234. return "Bar";
  235. }
  236. else if (type == SeriesChartType.Doughnut)
  237. {
  238. return "Doughnut";
  239. }
  240. else if (type == SeriesChartType.Line)
  241. {
  242. return "Line";
  243. }
  244. else if (type == SeriesChartType.Pie)
  245. {
  246. return "Pie";
  247. }
  248. else if (type == SeriesChartType.Bubble)
  249. {
  250. return "Bubble";
  251. }
  252. return "Column";
  253. }
  254. /// <summary>
  255. /// Converts the ChartColorPalette value to RDL Chart.Palette value.
  256. /// </summary>
  257. /// <param name="palette">The ChartColorPalette value.</param>
  258. /// <returns>The string with RDL Chart.Palette value.</returns>
  259. public static string ConvertChartPalette(ChartColorPalette palette)
  260. {
  261. if (palette == ChartColorPalette.EarthTones)
  262. {
  263. return "EarthTones";
  264. }
  265. else if (palette == ChartColorPalette.Excel)
  266. {
  267. return "Excel";
  268. }
  269. else if (palette == ChartColorPalette.Grayscale)
  270. {
  271. return "GrayScale";
  272. }
  273. else if (palette == ChartColorPalette.Light)
  274. {
  275. return "Light";
  276. }
  277. else if (palette == ChartColorPalette.Pastel)
  278. {
  279. return "Pastel";
  280. }
  281. else if (palette == ChartColorPalette.SemiTransparent)
  282. {
  283. return "SemiTransparent";
  284. }
  285. return "Default";
  286. }
  287. /// <summary>
  288. /// Converts the Legend.Docking and Legend.Alignment values to RDL Chart.Legend.Position value.
  289. /// </summary>
  290. /// <param name="docking">The Legend.Docking value.</param>
  291. /// <param name="alignment">The Legend.Alignment value.</param>
  292. /// <returns>The string with RDL Chart.Legend.Position value.</returns>
  293. public static string ConvertLegendDockingAndAlignment(Docking docking, StringAlignment alignment)
  294. {
  295. if (docking == Docking.Top && alignment == StringAlignment.Near)
  296. {
  297. return "TopLeft";
  298. }
  299. else if (docking == Docking.Top && alignment == StringAlignment.Center)
  300. {
  301. return "TopCenter";
  302. }
  303. else if (docking == Docking.Top && alignment == StringAlignment.Far)
  304. {
  305. return "TopRight";
  306. }
  307. else if (docking == Docking.Left && alignment == StringAlignment.Near)
  308. {
  309. return "LeftTop";
  310. }
  311. else if (docking == Docking.Left && alignment == StringAlignment.Center)
  312. {
  313. return "LeftCenter";
  314. }
  315. else if (docking == Docking.Left && alignment == StringAlignment.Far)
  316. {
  317. return "LeftBottom";
  318. }
  319. else if (docking == Docking.Right && alignment == StringAlignment.Near)
  320. {
  321. return "RightTop";
  322. }
  323. else if (docking == Docking.Right && alignment == StringAlignment.Center)
  324. {
  325. return "RightCenter";
  326. }
  327. else if (docking == Docking.Right && alignment == StringAlignment.Far)
  328. {
  329. return "RightBottom";
  330. }
  331. else if (docking == Docking.Bottom && alignment == StringAlignment.Near)
  332. {
  333. return "BottomLeft";
  334. }
  335. else if (docking == Docking.Bottom && alignment == StringAlignment.Center)
  336. {
  337. return "BottomCenter";
  338. }
  339. else if (docking == Docking.Bottom && alignment == StringAlignment.Far)
  340. {
  341. return "BottomRight";
  342. }
  343. return "TopLeft";
  344. }
  345. /// <summary>
  346. /// Converts the LegendStyle value to Chart.Legend.Layout value.
  347. /// </summary>
  348. /// <param name="ls">The LegendStyle value.</param>
  349. /// <returns>The string with RDL Chart.Legend.Layout value.</returns>
  350. public static string ConvertLegendStyle(LegendStyle ls)
  351. {
  352. if (ls == LegendStyle.Table)
  353. {
  354. return "Table";
  355. }
  356. else if (ls == LegendStyle.Row)
  357. {
  358. return "Row";
  359. }
  360. return "Column";
  361. }
  362. /// <summary>
  363. /// Converts the LightStyle value to RDL Shading value.
  364. /// </summary>
  365. /// <param name="ls">The LightStyle value.</param>
  366. /// <returns>The string with RDL Shading value.</returns>
  367. public static string ConvertLightStyle(LightStyle ls)
  368. {
  369. if (ls == LightStyle.Simplistic)
  370. {
  371. return "Simple";
  372. }
  373. else if (ls == LightStyle.Realistic)
  374. {
  375. return "Real";
  376. }
  377. return "Simple";
  378. }
  379. /// <summary>
  380. /// Converts the ChartDashStyle value to RDL BorderStyle value.
  381. /// </summary>
  382. /// <param name="cds">The ChartDashStyle value.</param>
  383. /// <returns>The string with RDL ChartDahsStyle value.</returns>
  384. public static string ConvertChartDashStyle(ChartDashStyle cds)
  385. {
  386. if (cds == ChartDashStyle.Dot)
  387. {
  388. return "Dotted";
  389. }
  390. else if (cds == ChartDashStyle.Dash)
  391. {
  392. return "Dashed";
  393. }
  394. return "Solid";
  395. }
  396. /// <summary>
  397. /// Converts the ContentAlignment value to RDL TextAlign value.
  398. /// </summary>
  399. /// <param name="ca">The ContentAlignment value.</param>
  400. /// <returns>The string with RDL TextAlign value.</returns>
  401. public static string ContentAlignmentToTextAlign(ContentAlignment ca)
  402. {
  403. if (ca == ContentAlignment.BottomCenter || ca == ContentAlignment.MiddleCenter || ca == ContentAlignment.TopCenter)
  404. {
  405. return "Center";
  406. }
  407. else if (ca == ContentAlignment.BottomLeft || ca == ContentAlignment.MiddleLeft || ca == ContentAlignment.TopLeft)
  408. {
  409. return "Left";
  410. }
  411. else if (ca == ContentAlignment.BottomRight || ca == ContentAlignment.MiddleRight || ca == ContentAlignment.TopRight)
  412. {
  413. return "Right";
  414. }
  415. return "Left";
  416. }
  417. /// <summary>
  418. /// Converts the ContentAlignment value to RDL VerticalAlign value.
  419. /// </summary>
  420. /// <param name="ca">The ContentAlignment value.</param>
  421. /// <returns>The string with RDL VerticalAlign value.</returns>
  422. public static string ContentAlignmentToVerticalAlign(ContentAlignment ca)
  423. {
  424. if (ca == ContentAlignment.TopCenter || ca == ContentAlignment.TopLeft || ca == ContentAlignment.TopRight)
  425. {
  426. return "Top";
  427. }
  428. else if (ca == ContentAlignment.MiddleCenter || ca == ContentAlignment.MiddleLeft || ca == ContentAlignment.MiddleRight)
  429. {
  430. return "Middle";
  431. }
  432. else if (ca == ContentAlignment.BottomCenter || ca == ContentAlignment.BottomLeft || ca == ContentAlignment.BottomRight)
  433. {
  434. return "Bottom";
  435. }
  436. return "Top";
  437. }
  438. /// <summary>
  439. /// Converts the AxisEnabled value to RDL Axis.Visible value.
  440. /// </summary>
  441. /// <param name="ae">The AxisEnabled value.</param>
  442. /// <returns>The string with RDL Axis.Visible value.</returns>
  443. public static string ConvertAxisEnabled(AxisEnabled ae)
  444. {
  445. if (ae == AxisEnabled.False)
  446. {
  447. return "false";
  448. }
  449. return "true";
  450. }
  451. /// <summary>
  452. /// Converts the TickMarkStyle value to RDL TickMarkStyle value.
  453. /// </summary>
  454. /// <param name="style">The TickMarkStyle value.</param>
  455. /// <returns>The string with RDL TickMarkStyle value.</returns>
  456. public static string ConvertTickMarkStyle(TickMarkStyle style)
  457. {
  458. if (style == TickMarkStyle.InsideArea)
  459. {
  460. return "Inside";
  461. }
  462. else if (style == TickMarkStyle.OutsideArea)
  463. {
  464. return "Outside";
  465. }
  466. else if (style == TickMarkStyle.AcrossAxis)
  467. {
  468. return "Cross";
  469. }
  470. return "None";
  471. }
  472. /// <summary>
  473. /// Converts the StringAlignment value to RDL TextAlign value.
  474. /// </summary>
  475. /// <param name="sa">The StringAlignment value.</param>
  476. /// <returns>The string with RDL TextAlign value.</returns>
  477. public static string ConvertStringAlignment(StringAlignment sa)
  478. {
  479. if (sa == StringAlignment.Near)
  480. {
  481. return "Left";
  482. }
  483. else if (sa == StringAlignment.Far)
  484. {
  485. return "Right";
  486. }
  487. return "Center";
  488. }
  489. #endregion // Public Methods
  490. }
  491. }