UnitsConverter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7. using FastReport.Utils;
  8. using FastReport.Barcode;
  9. namespace FastReport.Import.DevExpress
  10. {
  11. /// <summary>
  12. /// The DevExpress units converter.
  13. /// </summary>
  14. public static class UnitsConverter
  15. {
  16. public static float Ratio = 1;
  17. #region Public Methods
  18. /// <summary>
  19. /// Converts SizeF to pixels.
  20. /// </summary>
  21. /// <param name="str">SizeF value as string.</param>
  22. /// <returns>The value in pixels.</returns>
  23. public static float SizeFToPixels(string str)
  24. {
  25. float value = 0.0f;
  26. if (!String.IsNullOrEmpty(str))
  27. {
  28. int end = str.IndexOf("F");
  29. if (end > -1)
  30. {
  31. value = Convert.ToSingle(str.Substring(0, end), CultureInfo.InvariantCulture);
  32. }
  33. else
  34. {
  35. value = Convert.ToSingle(str, CultureInfo.InvariantCulture);
  36. }
  37. }
  38. return value / Ratio;
  39. }
  40. /// <summary>
  41. /// Converts SizeF to pixels.
  42. /// </summary>
  43. /// <param name="str">SizeF value as string.</param>
  44. /// <returns>The value in pixels.</returns>
  45. /// <remarks>
  46. /// Use this method for fonts, because font size is not stored as multiplied by dpi
  47. /// </remarks>
  48. public static float SizeFToPixelsFont(string str)
  49. {
  50. return SizeFToPixels(str) * Ratio;
  51. }
  52. /// <summary>
  53. /// Converts value to Boolean.
  54. /// </summary>
  55. /// <param name="str">Boolen value as string.</param>
  56. public static bool ConvertBool(string str)
  57. {
  58. return str.ToLower() == "true";
  59. }
  60. /// <summary>
  61. /// Converts DevExpress Color.
  62. /// </summary>
  63. /// <param name="str">The DevExpress Color value as string.</param>
  64. /// <returns>The Color value.</returns>
  65. public static Color ConvertColor(string str)
  66. {
  67. if (!String.IsNullOrEmpty(str))
  68. {
  69. if (str.IndexOf("FromArgb") > -1)
  70. {
  71. int start = str.IndexOf("byte") + 6;
  72. int red = Convert.ToInt32(str.Substring(start, str.IndexOf(")", start) - start));
  73. start = str.IndexOf("byte", start) + 6;
  74. int green = Convert.ToInt32(str.Substring(start, str.IndexOf(")", start) - start));
  75. start = str.IndexOf("byte", start) + 6;
  76. int blue = Convert.ToInt32(str.Substring(start, str.IndexOf(")", start) - start));
  77. return Color.FromArgb(red, green, blue);
  78. }
  79. else if(str.Split(',').Length == 4)
  80. {
  81. string[] colors = str.Split(',');
  82. int alpha = Convert.ToInt32(colors[0]);
  83. int red = Convert.ToInt32(colors[1]);
  84. int green = Convert.ToInt32(colors[2]);
  85. int blue = Convert.ToInt32(colors[3]);
  86. return Color.FromArgb(alpha, red, green, blue);
  87. }
  88. else
  89. {
  90. return Color.FromName(str.Replace("System.Drawing.Color.", ""));
  91. }
  92. }
  93. return Color.Black;
  94. }
  95. /// <summary>
  96. /// Converts DevExpress BackColor.
  97. /// </summary>
  98. /// <param name="str">The DevExpress BackColor value as string.</param>
  99. /// <returns>The Color value.</returns>
  100. public static Color ConvertBackColor(string str)
  101. {
  102. if (!String.IsNullOrEmpty(str))
  103. {
  104. if (str.IndexOf("FromArgb") > -1)
  105. {
  106. int start = str.IndexOf("byte") + 6;
  107. int red = Convert.ToInt32(str.Substring(start, str.IndexOf(")", start) - start));
  108. start = str.IndexOf("byte", start) + 6;
  109. int green = Convert.ToInt32(str.Substring(start, str.IndexOf(")", start) - start));
  110. start = str.IndexOf("byte", start) + 6;
  111. int blue = Convert.ToInt32(str.Substring(start, str.IndexOf(")", start) - start));
  112. return Color.FromArgb(red, green, blue);
  113. }
  114. else if (str.Split(',').Length == 4)
  115. {
  116. string[] colors = str.Split(',');
  117. int alpha = Convert.ToInt32(colors[0]);
  118. int red = Convert.ToInt32(colors[1]);
  119. int green = Convert.ToInt32(colors[2]);
  120. int blue = Convert.ToInt32(colors[3]);
  121. return Color.FromArgb(alpha, red, green, blue);
  122. }
  123. else
  124. {
  125. return Color.FromName(str.Replace("System.Drawing.Color.", ""));
  126. }
  127. }
  128. return Color.Transparent;
  129. }
  130. /// <summary>
  131. /// Converts the DevExpress BorderDashStyle to LineStyle.
  132. /// </summary>
  133. /// <param name="borderDashStyle">The DevExpress BorderDashStyle value.</param>
  134. /// <returns>The LineStyle value.</returns>
  135. public static LineStyle ConvertBorderDashStyle(string borderDashStyle)
  136. {
  137. if (borderDashStyle == "DevExpress.XtraPrinting.BorderDashStyle.Dot" || borderDashStyle.Equals("Dot"))
  138. {
  139. return LineStyle.Dot;
  140. }
  141. else if (borderDashStyle == "DevExpress.XtraPrinting.BorderDashStyle.Dash" || borderDashStyle.Equals("Dash"))
  142. {
  143. return LineStyle.Dash;
  144. }
  145. else if (borderDashStyle == "DevExpress.XtraPrinting.BorderDashStyle.DashDot" || borderDashStyle.Equals("DashDot"))
  146. {
  147. return LineStyle.DashDot;
  148. }
  149. else if (borderDashStyle == "DevExpress.XtraPrinting.BorderDashStyle.DashDotDot" || borderDashStyle.Equals("DashDotDot"))
  150. {
  151. return LineStyle.DashDotDot;
  152. }
  153. else if (borderDashStyle == "DevExpress.XtraPrinting.BorderDashStyle.Double" || borderDashStyle.Equals("Double"))
  154. {
  155. return LineStyle.Double;
  156. }
  157. return LineStyle.Solid;
  158. }
  159. /// <summary>
  160. /// Converts the DevExpress LineStyle to LineStyle.
  161. /// </summary>
  162. /// <param name="lineStyle">The DevExpress LineStyle value.</param>
  163. /// <returns>The LineStyle value.</returns>
  164. public static LineStyle ConvertLineStyle(string lineStyle)
  165. {
  166. if (lineStyle == "System.Drawing.Drawing2D.DashStyle.Dot")
  167. {
  168. return LineStyle.Dot;
  169. }
  170. else if (lineStyle == "System.Drawing.Drawing2D.DashStyle.Dash")
  171. {
  172. return LineStyle.Dash;
  173. }
  174. else if (lineStyle == "System.Drawing.Drawing2D.DashStyle.DashDot")
  175. {
  176. return LineStyle.DashDot;
  177. }
  178. else if (lineStyle == "System.Drawing.Drawing2D.DashStyle.DashDotDot")
  179. {
  180. return LineStyle.DashDotDot;
  181. }
  182. else if (lineStyle == "System.Drawing.Drawing2D.DashStyle.Double")
  183. {
  184. return LineStyle.Double;
  185. }
  186. return LineStyle.Solid;
  187. }
  188. /// <summary>
  189. /// Converts the DevExpress TextAlignment to HorzAlignment.
  190. /// </summary>
  191. /// <param name="textAlignment">The DevExpress TextAlignment value.</param>
  192. /// <returns>The HorzAlign value.</returns>
  193. public static HorzAlign ConvertTextAlignmentToHorzAlign(string textAlignment)
  194. {
  195. if (textAlignment.Contains("Center"))
  196. {
  197. return HorzAlign.Center;
  198. }
  199. if (textAlignment.Contains("Justify"))
  200. {
  201. return HorzAlign.Justify;
  202. }
  203. if (textAlignment.Contains("Right"))
  204. {
  205. return HorzAlign.Right;
  206. }
  207. return HorzAlign.Left;
  208. }
  209. /// <summary>
  210. /// Converts the DevExpress TextAlignment to VertAlignment.
  211. /// </summary>
  212. /// <param name="textAlignment">The DevExpress TextAlignment value.</param>
  213. /// <returns>The VertAlign value.</returns>
  214. public static VertAlign ConvertTextAlignmentToVertAlign(string textAlignment)
  215. {
  216. if (textAlignment.Contains("Middle"))
  217. {
  218. return VertAlign.Center;
  219. }
  220. if (textAlignment.Contains("Bottom"))
  221. {
  222. return VertAlign.Bottom;
  223. }
  224. return VertAlign.Top;
  225. }
  226. /// <summary>
  227. /// Converts the DevExpress ImageSizeMode to PictureBoxSizeMode.
  228. /// </summary>
  229. /// <param name="sizeMode">The ImageSizeMode value as string.</param>
  230. /// <returns>The PictureBoxSizeMode value.</returns>
  231. public static PictureBoxSizeMode ConvertImageSizeMode(string sizeMode)
  232. {
  233. if (sizeMode == "DevExpress.XtraPrinting.ImageSizeMode.StretchImage" || sizeMode == "StretchImage")
  234. {
  235. return PictureBoxSizeMode.StretchImage;
  236. }
  237. else if (sizeMode == "DevExpress.XtraPrinting.ImageSizeMode.AutoSize" || sizeMode == "AutoSize")
  238. {
  239. return PictureBoxSizeMode.AutoSize;
  240. }
  241. else if (sizeMode == "DevExpress.XtraPrinting.ImageSizeMode.CenterImage" || sizeMode == "CenterImage")
  242. {
  243. return PictureBoxSizeMode.CenterImage;
  244. }
  245. else if (sizeMode == "DevExpress.XtraPrinting.ImageSizeMode.ZoomImage" || sizeMode == "ZoomImage")
  246. {
  247. return PictureBoxSizeMode.Zoom;
  248. }
  249. else if (sizeMode == "DevExpress.XtraPrinting.ImageSizeMode.Squeeze" || sizeMode == "Squeeze")
  250. {
  251. return PictureBoxSizeMode.Zoom;
  252. }
  253. return PictureBoxSizeMode.Normal;
  254. }
  255. internal static ImageAlign ConvertImageAlignment(string alignment)
  256. {
  257. ImageAlign align = ImageAlign.None;
  258. switch(alignment)
  259. {
  260. case "TopCenter":
  261. return ImageAlign.Top_Center;
  262. case "TopLeft":
  263. return ImageAlign.Top_Left;
  264. case "TopRight":
  265. return ImageAlign.Top_Right;
  266. case "CenterLeft":
  267. return ImageAlign.Center_Left;
  268. case "CenterCenter":
  269. return ImageAlign.Center_Center;
  270. case "CenterRight":
  271. return ImageAlign.Center_Right;
  272. case "BottomLeft":
  273. return ImageAlign.Bottom_Left;
  274. case "BottomCenter":
  275. return ImageAlign.Bottom_Center;
  276. case "BottomRight":
  277. return ImageAlign.Bottom_Right;
  278. }
  279. return align;
  280. }
  281. /// <summary>
  282. /// Converts the DevExpress Shape to ShapeKind.
  283. /// </summary>
  284. /// <param name="shape">The DevExpress Shape value as string.</param>
  285. /// <returns>The ShapeKind value.</returns>
  286. public static ShapeKind ConvertShape(string shape)
  287. {
  288. if (shape.Contains("Rectangle"))
  289. {
  290. return ShapeKind.Rectangle;
  291. }
  292. else if (shape.Contains("Polygon"))
  293. {
  294. return ShapeKind.Triangle;
  295. }
  296. return ShapeKind.Ellipse;
  297. }
  298. /// <summary>
  299. /// Converts the DevExpress Barcode.Symbology to Barcode.Barcode.
  300. /// </summary>
  301. /// <param name="symbology">The DevExpress Barcode.Symbology value as string.</param>
  302. /// <param name="barcode">The BarcodeObject instance.</param>
  303. public static void ConvertBarcodeSymbology(string symbology, BarcodeObject barcode)
  304. {
  305. symbology = symbology.ToLower();
  306. if (symbology.Contains("codabar"))
  307. {
  308. barcode.Barcode = new BarcodeCodabar();
  309. }
  310. else if (symbology.Contains("code128"))
  311. {
  312. barcode.Barcode = new Barcode128();
  313. }
  314. else if (symbology.Contains("code39"))
  315. {
  316. barcode.Barcode = new Barcode39();
  317. }
  318. else if (symbology.Contains("code39extended"))
  319. {
  320. barcode.Barcode = new Barcode39Extended();
  321. }
  322. else if (symbology.Contains("code93"))
  323. {
  324. barcode.Barcode = new Barcode93();
  325. }
  326. else if (symbology.Contains("code9eextended"))
  327. {
  328. barcode.Barcode = new Barcode93Extended();
  329. }
  330. else if (symbology.Contains("codemsi"))
  331. {
  332. barcode.Barcode = new BarcodeMSI();
  333. }
  334. else if (symbology.Contains("datamatrix"))
  335. {
  336. barcode.Barcode = new BarcodeDatamatrix();
  337. }
  338. else if (symbology.Contains("ean128"))
  339. {
  340. barcode.Barcode = new BarcodeEAN128();
  341. }
  342. else if (symbology.Contains("ean13"))
  343. {
  344. barcode.Barcode = new BarcodeEAN13();
  345. }
  346. else if (symbology.Contains("ean8"))
  347. {
  348. barcode.Barcode = new BarcodeEAN8();
  349. }
  350. else if (symbology.Contains("industrial2of5"))
  351. {
  352. barcode.Barcode = new Barcode2of5Industrial();
  353. }
  354. else if (symbology.Contains("interleaved2of5"))
  355. {
  356. barcode.Barcode = new Barcode2of5Interleaved();
  357. }
  358. else if (symbology.Contains("matrix2of5"))
  359. {
  360. barcode.Barcode = new Barcode2of5Matrix();
  361. }
  362. else if (symbology.Contains("pdf417"))
  363. {
  364. barcode.Barcode = new BarcodePDF417();
  365. }
  366. else if (symbology.Contains("postnet"))
  367. {
  368. barcode.Barcode = new BarcodePostNet();
  369. }
  370. else if (symbology.Contains("qrcode"))
  371. {
  372. barcode.Barcode = new BarcodeQR();
  373. }
  374. else if (symbology.Contains("upca"))
  375. {
  376. barcode.Barcode = new BarcodeUPC_A();
  377. }
  378. else if (symbology.Contains("upce0"))
  379. {
  380. barcode.Barcode = new BarcodeUPC_E0();
  381. }
  382. else if (symbology.Contains("upce1"))
  383. {
  384. barcode.Barcode = new BarcodeUPC_E1();
  385. }
  386. else if (symbology.Contains("upcsupplemental2"))
  387. {
  388. barcode.Barcode = new BarcodeSupplement2();
  389. }
  390. else if (symbology.Contains("upcsupplemental5"))
  391. {
  392. barcode.Barcode = new BarcodeSupplement5();
  393. }
  394. }
  395. /// <summary>
  396. /// Converts the DevExpress border sides to FastReport border sides
  397. /// </summary>
  398. /// <param name="sides">The DevExpress Barcode.Symbology value as string.</param>
  399. /// <param name="border">The BarcodeObject instance.</param>
  400. public static BorderLines ConvertBorderSides(string sides, Border border)
  401. {
  402. BorderLines borderLines = BorderLines.None;
  403. if (!String.IsNullOrEmpty(sides))
  404. {
  405. if (sides.IndexOf("Left") > -1)
  406. {
  407. borderLines |= BorderLines.Left;
  408. }
  409. if (sides.IndexOf("Top") > -1)
  410. {
  411. borderLines |= BorderLines.Top;
  412. }
  413. if (sides.IndexOf("Right") > -1)
  414. {
  415. borderLines |= BorderLines.Right;
  416. }
  417. if (sides.IndexOf("Bottom") > -1)
  418. {
  419. borderLines |= BorderLines.Bottom;
  420. }
  421. if (sides.IndexOf("All") > -1)
  422. {
  423. borderLines = BorderLines.All;
  424. }
  425. }
  426. return borderLines;
  427. }
  428. #endregion // Public Methods
  429. }
  430. }