PSDocument.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Drawing;
  6. using FastReport.Export.TTF;
  7. using FastReport.Utils;
  8. namespace FastReport.Export.PS
  9. {
  10. /// <summary>
  11. /// Contains Dashes enum
  12. /// </summary>
  13. public enum Dashes
  14. {
  15. /// <summary>
  16. /// Specifies the Dash.
  17. /// </summary>
  18. Dash,
  19. /// <summary>
  20. /// Specifies the Dot.
  21. /// </summary>
  22. Dot,
  23. /// <summary>
  24. /// Specifies the DashDot.
  25. /// </summary>
  26. DashDot,
  27. /// <summary>
  28. /// Specifies the DashDotDot.
  29. /// </summary>
  30. DashDotDot,
  31. /// <summary>
  32. /// Specifies the Double line.
  33. /// </summary>
  34. Double
  35. }
  36. class PSDocument
  37. {
  38. protected float windowHeight;
  39. protected float windowWidth;
  40. protected StringBuilder psData = new StringBuilder();
  41. protected bool textInCurves = false;
  42. internal bool TextInCurves
  43. {
  44. get { return textInCurves; }
  45. set { textInCurves = value; }
  46. }
  47. /// <summary>
  48. /// Create Window.
  49. /// </summary>
  50. public void CreateWindow()
  51. {
  52. if (Math.Round(windowWidth) == 595 && Math.Round(windowHeight) == 842) psData.Append("a4 ");
  53. }
  54. private bool RotatedTextIsVertical(float width, float height, float angle)
  55. {
  56. float diagonal = (float)Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2));
  57. float sinA = height / diagonal;
  58. // angle left or right angle of diagonal
  59. float andleLRDiagonal = (float)(Math.Asin(sinA) / Math.PI * 180 * 2);
  60. // angle top or bottom angle of diagonal
  61. float andleTBDiagonal = (360 - andleLRDiagonal * 2) / 2;
  62. // angle should be in range from 0 to 360
  63. angle %= 360;
  64. if (andleLRDiagonal / 2 < angle && angle < andleLRDiagonal / 2 + andleTBDiagonal || angle > andleLRDiagonal * 1.5 + andleTBDiagonal && angle < 360 - andleLRDiagonal / 2)
  65. return true;
  66. return false;
  67. }
  68. protected List<float> TextAlignments(float x, ref float y, string HorizontalAlignment, string VerticalAlignment, float Width, float Height, Font font, List<string> txt_lns,
  69. float PaddingLeft, float PaddingRight, float PaddingTop, float PaddingBottom, float BorderThikness)
  70. {
  71. Bitmap objBmpImage = new Bitmap(1, 1);
  72. Graphics objGraphics = Graphics.FromImage(objBmpImage);
  73. float Xold = x;
  74. float Yold = y;
  75. int N = txt_lns.Count;
  76. List<float> x_alignments = new List<float>();
  77. using (Font f = new Font(font.FontFamily, font.Size * DrawUtils.ScreenDpiFX, font.Style))
  78. {
  79. foreach (string line in txt_lns)
  80. {
  81. if (HorizontalAlignment == "Center")
  82. {
  83. x_alignments.Add(x + Width / 2 - objGraphics.MeasureString(line, f).Width / 2f * 0.75f);
  84. }
  85. else if (HorizontalAlignment == "Right")
  86. {
  87. x_alignments.Add(x + Width - objGraphics.MeasureString(line, f).Width * 0.75f);
  88. }
  89. else if (HorizontalAlignment == "Left") x_alignments.Add(x);
  90. else if (HorizontalAlignment == "Justify") x_alignments.Add(x);
  91. }
  92. }
  93. if (VerticalAlignment == "Center")
  94. {
  95. y = y - Height / 2f + ((float)font.Size) / 2;
  96. if (N > 1) y -= ((float)font.Size) / 2 * N;
  97. }
  98. if (VerticalAlignment == "Top")
  99. {
  100. y = y - Height + (float)font.Size;
  101. }
  102. if (VerticalAlignment == "Bottom")
  103. {
  104. if (N > 1) y = y - ((float)font.Size) * N;
  105. }
  106. //Paddings
  107. if (Yold - Height + (float)font.Size + PaddingTop > y)
  108. {
  109. y = Yold - Height + (float)font.Size + PaddingTop;
  110. }
  111. if (Yold - PaddingBottom < y)
  112. {
  113. y = Yold - PaddingBottom;
  114. }
  115. for (int i = 0; i < x_alignments.Count; i++)
  116. {
  117. if (Xold + PaddingLeft > x_alignments[i])
  118. {
  119. x_alignments[i] = Xold + PaddingLeft;
  120. }
  121. if (Xold + Width - PaddingRight < x_alignments[i])
  122. {
  123. x_alignments[i] = Xold + Width - PaddingRight;
  124. }
  125. }
  126. return x_alignments;
  127. }
  128. private bool gBorderLines(string BorderLines, out bool None, out bool Left, out bool Right,
  129. out bool Top, out bool Bottom)
  130. {
  131. string[] masLines = BorderLines.Split(',', ' ');//names lines borders
  132. bool All = false;
  133. None = false;
  134. Left = false;
  135. Right = false;
  136. Top = false;
  137. Bottom = false;
  138. for (int i = 0; i < masLines.Length; i++)
  139. {
  140. if (masLines[i] == "All")
  141. {
  142. All = true;
  143. }
  144. if (masLines[i] == "None")
  145. {
  146. None = true;
  147. }
  148. if (masLines[i] == "Left")
  149. {
  150. Left = true;
  151. }
  152. if (masLines[i] == "Right")
  153. {
  154. Right = true;
  155. }
  156. if (masLines[i] == "Top")
  157. {
  158. Top = true;
  159. }
  160. if (masLines[i] == "Bottom")
  161. {
  162. Bottom = true;
  163. }
  164. }
  165. return All;
  166. }
  167. private void createRotateText(float x, float y, string HorizontalAlignment, string VerticalAlignment, float Width, float Height, Font font, string textstr,
  168. float PaddingLeft, float PaddingRight, float PaddingTop, float PaddingBottom, float BorderThickness, string Foreground, string Background, float Angle)
  169. {
  170. List<float> t_x = new List<float>(); List<float> t_y = new List<float>();
  171. bool textIsVertical = RotatedTextIsVertical(Width, Height, Angle);
  172. Bitmap objBmpImage = new Bitmap(1, 1);
  173. Graphics objGraphics = Graphics.FromImage(objBmpImage);
  174. double angle = ((360 - Angle % 360) * Math.PI / 180);
  175. float sin = (float)Math.Sin(angle);
  176. double cos = Math.Cos(angle);
  177. List<string> txt_lns = new List<string>();
  178. string[] words = textstr.Split(' ');
  179. string line = "";
  180. for (int n = 0; n < words.Length; n++)
  181. {
  182. string testLine = line;
  183. using (Font f = new Font(font.FontFamily, font.Size * DrawUtils.ScreenDpiFX, font.Style))
  184. {
  185. if (!RotatedTextIsVertical(Width, Height, Angle) && (float)objGraphics.MeasureString(testLine + words[n], f).Width * 0.93f > Width ||
  186. RotatedTextIsVertical(Width, Height, Angle) && (float)objGraphics.MeasureString(testLine + words[n], f).Width * 0.93f > Height)
  187. {
  188. if (line != "")
  189. txt_lns.Add(line);
  190. line = words[n] + " ";
  191. }
  192. else
  193. {
  194. testLine += words[n];
  195. if (words[n].Contains("\n") || words[n].Contains("\r"))
  196. {
  197. int rn = testLine.IndexOf('\r') != -1 ? testLine.IndexOf('\r') : testLine.IndexOf('\n');
  198. line = testLine.Remove(rn);
  199. txt_lns.Add(line);
  200. line = testLine.Remove(0, rn).Replace("\r", "").Replace("\n", "") + " ";
  201. }
  202. else
  203. line = testLine + " ";
  204. }
  205. }
  206. }
  207. if (txt_lns.Count == 0)
  208. txt_lns.Add(line);
  209. else if (txt_lns[txt_lns.Count - 1] != line)
  210. txt_lns.Add(line);
  211. // perform coordinates
  212. float Xold = x;
  213. float Yold = y;
  214. int N = txt_lns.Count;
  215. SizeF textSize;
  216. using (Font f = new Font(font.FontFamily, font.Size * DrawUtils.ScreenDpiFX, font.Style))
  217. {
  218. foreach (string ln in txt_lns)
  219. {
  220. textSize = objGraphics.MeasureString(ln, f);
  221. y = Yold;
  222. if (!textIsVertical)
  223. {
  224. if (HorizontalAlignment == "Center")
  225. {
  226. int negativSin = 1;
  227. if (Angle > 180)
  228. negativSin = -1;
  229. t_x.Add(x + (Width - textSize.Width * (float)cos + textSize.Height * sin * negativSin) / 2);
  230. }
  231. else if (HorizontalAlignment == "Right" || VerticalAlignment == "Top" && textIsVertical)
  232. {
  233. t_x.Add(x + Width - objGraphics.MeasureString(ln, f).Width * 0.75f);
  234. }
  235. else if (HorizontalAlignment == "Left") t_x.Add(x);
  236. else if (HorizontalAlignment == "Justify") t_x.Add(x);
  237. }
  238. else
  239. {
  240. if (VerticalAlignment == "Center")
  241. {
  242. int negativSin = 1;
  243. if (Angle > 180)
  244. negativSin = -1;
  245. t_x.Add(x + (Width - textSize.Width * (float)cos + textSize.Height * sin * negativSin) / 2);
  246. }
  247. else if (VerticalAlignment == "Top" && textIsVertical)
  248. {
  249. t_x.Add(x + Width - objGraphics.MeasureString(ln, f).Height * 0.75f);
  250. }
  251. else if (VerticalAlignment == "Bottom" && textIsVertical) t_x.Add(x);
  252. }
  253. if (VerticalAlignment == "Center")
  254. {
  255. y = (y + ((float)font.Size / 2) + (Height - textSize.Height * 0.75f * (float)cos - textSize.Width * 0.75f * -sin) / 2);
  256. if (N > 1) y -= ((float)font.Size) / 2 * N;
  257. }
  258. if (VerticalAlignment == "Top")
  259. {
  260. y = y + (float)font.Size;
  261. if (textIsVertical && Angle % 360 >= 180)
  262. {
  263. y += Height;
  264. }
  265. }
  266. if (VerticalAlignment == "Bottom")
  267. {
  268. y = y - ((float)font.Size) / 2 * N;
  269. if (textIsVertical && Angle % 360 >= 180)
  270. {
  271. y += Height;
  272. }
  273. }
  274. t_y.Add(y);
  275. }
  276. }
  277. // create Glyphs
  278. ExportTTFFont pdffont = new ExportTTFFont(font);
  279. float cur_x;
  280. float cur_y;
  281. for (int i = 0; i < txt_lns.Count; i++)
  282. {
  283. ExportTTFFont.RunInfo[] runs = pdffont.GetFontRuns(txt_lns[i], false, font);
  284. foreach (var run in runs)
  285. {
  286. ExportTTFFont.GlyphTTF[] txt = pdffont.GetGlyphPath(run);
  287. cur_y = t_y[i];
  288. if (RotatedTextIsVertical(Width, Height, Angle))
  289. cur_y += i * ((float)cos * font.GetHeight() * DrawUtils.ScreenDpiFX * 0.75f);
  290. cur_x = t_x[i] + i * (sin * font.GetHeight() * DrawUtils.ScreenDpiFX * 0.75f) + run.OffsetX;
  291. foreach (ExportTTFFont.GlyphTTF g in txt)
  292. {
  293. if (g.Path.PointCount == 0)
  294. {
  295. cur_x += (float)cos * g.Width * 0.75f;
  296. cur_y += -sin * g.Width * 0.75f;
  297. continue;
  298. }
  299. System.Drawing.Drawing2D.Matrix matrix = new System.Drawing.Drawing2D.Matrix();
  300. matrix.Rotate(Angle);
  301. g.Path.Transform(matrix);
  302. RectangleF rct = g.Path.GetBounds(matrix);
  303. PointF[] ps = g.Path.PathPoints;
  304. byte[] pt = g.Path.PathTypes;
  305. for (int j = 0; j < g.Path.PointCount;)
  306. {
  307. switch (pt[j])
  308. {
  309. case 0://start
  310. if (j > 0) ClosePath();
  311. MoveTo((cur_x + ps[j].X * 0.75f), cur_y + ps[j].Y * 0.75f);
  312. j++;
  313. break;
  314. case 1://line
  315. AppendLine(cur_x + ps[j].X * 0.75f, cur_y + ps[j].Y * 0.75f);
  316. j++;
  317. break;
  318. case 3://interpolate bezier
  319. AppendBezier(cur_x, cur_y, ps[j], ps[j + 1], ps[j + 2]);
  320. j += 3;
  321. break;
  322. default:
  323. i++;
  324. break;
  325. }
  326. }
  327. ClosePath();
  328. EndFig(Foreground, Background);
  329. cur_x += (float)cos * g.Width * 0.75f;
  330. cur_y += -sin * g.Width * 0.75f;
  331. }
  332. }
  333. }
  334. psData.Append("\n");
  335. }
  336. private void createText(float x, float y, string HorizontalAlignment, string VerticalAlignment, float Width, float Height, Font font, string textstr,
  337. float PaddingLeft, float PaddingRight, float PaddingTop, float PaddingBottom, float BorderThickness, string Foreground, string Background, float Angle)
  338. {
  339. List<float> t_x; float t_y = y;
  340. Bitmap objBmpImage = new Bitmap(1, 1);
  341. Graphics objGraphics = Graphics.FromImage(objBmpImage);
  342. List<string> txt_lns = new List<string>();
  343. string[] words = textstr.Split(' ');
  344. string line = "";
  345. for (int n = 0; n < words.Length; n++)
  346. {
  347. string testLine = line + words[n];
  348. using (Font f = new Font(font.FontFamily, font.Size * DrawUtils.ScreenDpiFX, font.Style))
  349. {
  350. if ((float)objGraphics.MeasureString(testLine, f).Width * 0.75f > Width)
  351. {
  352. txt_lns.Add(line);
  353. line = words[n] + " ";
  354. }
  355. else
  356. {
  357. if (words[n].Contains("\n") || words[n].Contains("\r"))
  358. {
  359. int rn = testLine.IndexOf('\r') != -1 ? testLine.IndexOf('\r') : testLine.IndexOf('\n');
  360. line = testLine.Remove(rn);
  361. txt_lns.Add(line);
  362. line = testLine.Remove(0, rn).Replace("\r", "").Replace("\n", "") + " ";
  363. }
  364. else
  365. line = testLine + " ";
  366. }
  367. }
  368. }
  369. if (txt_lns.Count == 0)
  370. txt_lns.Add(line);
  371. else if (txt_lns[txt_lns.Count - 1] != line)
  372. txt_lns.Add(line);
  373. t_x = TextAlignments(x, ref t_y, HorizontalAlignment, VerticalAlignment, Width, Height, font, txt_lns, PaddingLeft, PaddingRight, PaddingTop, PaddingBottom, BorderThickness);
  374. if (textInCurves)
  375. AddCurveTextLine(t_x, t_y, Foreground, Background, font, Width, Height, txt_lns, Angle);
  376. else
  377. AddTextLine(t_x, t_y, Foreground, font, Width, Height, txt_lns, Angle);
  378. }
  379. private void AddTextLine(List<float> t_x, float t_y, string Foreground, Font font, float Width, float Height, List<string> txt_lns, float Angle)
  380. {
  381. bool fstart = true;
  382. string internal_data = "";
  383. string gsave;
  384. string coords;
  385. float cur_y = 0f;
  386. for (int i = 0; i < txt_lns.Count; i++)
  387. {
  388. string text_col = ColorToPsRgb(Foreground);
  389. if (Angle == 0)
  390. internal_data = "/" + PSFont(font.Name) + " findfont " + FloatToString(font.Size) + " scalefont setfont " + FloatToString(t_x[i]) + " "
  391. + FloatToString(windowHeight - t_y - Height) + " moveto " + text_col + " setrgbcolor (" + txt_lns[i] + ") show ";
  392. else
  393. {
  394. if (Angle <= 90)
  395. {
  396. t_x[i] += Width / 2;
  397. t_y -= Height / 2;
  398. }
  399. if (fstart)
  400. {
  401. gsave = " gsave "; fstart = false;
  402. coords = FloatToString(t_x[i]) + " " + FloatToString(windowHeight - t_y - Height) + " translate 0 0 moveto " + -Angle + " rotate ";
  403. }
  404. else
  405. {
  406. gsave = "";
  407. coords = FloatToString(t_x[i] - t_x[0]) + " " + FloatToString(cur_y) + " moveto ";
  408. }
  409. cur_y -= font.GetHeight() * DrawUtils.ScreenDpiFX * 0.75f;
  410. internal_data = gsave + "/" + font.Name + " findfont " + FloatToString(font.Size) + " scalefont setfont " +
  411. coords + text_col + " setrgbcolor (" + txt_lns[i] + ") show ";
  412. if (i == txt_lns.Count - 1) internal_data += "grestore ";
  413. }
  414. psData.Append(internal_data + "\n");
  415. t_y += font.GetHeight() * DrawUtils.ScreenDpiFX * 0.75f;
  416. }
  417. }
  418. /// Add TextLine in curves
  419. private void AddCurveTextLine(List<float> t_x, float t_y, string Foreground, string Background, Font font, float Width, float Height, List<string> txt_lns, float Angle)
  420. {
  421. ExportTTFFont pdffont = new ExportTTFFont(font);
  422. float cur_y = t_y + Height;
  423. for (int i = 0; i < txt_lns.Count; i++)
  424. {
  425. ExportTTFFont.RunInfo[] runs = pdffont.GetFontRuns(txt_lns[i], false, font);
  426. foreach (var run in runs)
  427. {
  428. ExportTTFFont.GlyphTTF[] txt = pdffont.GetGlyphPath(run);
  429. float cur_x = t_x[i] + run.OffsetX;
  430. if (Angle == 0)
  431. {
  432. foreach (ExportTTFFont.GlyphTTF g in txt)
  433. {
  434. if (g.Path.PointCount == 0)
  435. {
  436. cur_x += g.Width * 0.75f;
  437. continue;
  438. }
  439. PointF[] ps = g.Path.PathPoints;
  440. byte[] pt = g.Path.PathTypes;
  441. for (int j = 0; j < g.Path.PointCount;)
  442. {
  443. switch (pt[j])
  444. {
  445. case 0://start
  446. if (j > 0) ClosePath();
  447. MoveTo(cur_x + ps[j].X * 0.75f, cur_y + ps[j].Y * 0.75f);
  448. j++;
  449. break;
  450. case 1://line
  451. AppendLine(cur_x + ps[j].X * 0.75f, cur_y + ps[j].Y * 0.75f);
  452. j++;
  453. break;
  454. case 3://interpolate bezier
  455. AppendBezier(cur_x, cur_y, ps[j], ps[j + 1], ps[j + 2]);
  456. j += 3;
  457. break;
  458. default:
  459. i++;
  460. break;
  461. }
  462. }
  463. ClosePath();
  464. EndFig(Foreground, Background);
  465. cur_x += g.Width * 0.75f;
  466. }
  467. cur_y += font.GetHeight() * DrawUtils.ScreenDpiFX * 0.75f;
  468. }
  469. }
  470. }
  471. psData.Append("\n");
  472. }
  473. ///<summary>
  474. ///Method for add TextObject.
  475. /// </summary>
  476. public void AddTextObject(float x, float y, float Width, float Height,
  477. string HorizontalAlignment, string VerticalAlignment, string BorderBrush, float BorderThickness,
  478. float LeftLine, float TopLine, float RightLine, float BottomLine, string LeftLineDashStile,
  479. string TopLineDashStile, string RightLineDashStile, string BottomLineDashStile, string colorLeftLine,
  480. string colorTopLine, string colorRightLine, string colorBottomLine, bool Shadow, string ShadowColor, float ShadowWidth, string Background, string BorderLines,
  481. string Text, string Foreground, float PaddingLeft, float PaddingTop, float PaddingRight, float PaddingBottom, bool WordWrap, float Angle, bool Glass, string colorTop, Font font, bool isWatermark)
  482. {
  483. bool All = false;
  484. bool None = false;
  485. bool Left = false;
  486. bool Right = false;
  487. bool Top = false;
  488. bool Bottom = false;
  489. All = gBorderLines(BorderLines, out None, out Left, out Right, out Top, out Bottom);
  490. if (All && (LeftLine == TopLine && TopLine == RightLine && RightLine == BottomLine) &&
  491. (LeftLineDashStile == TopLineDashStile && TopLineDashStile == RightLineDashStile &&
  492. RightLineDashStile == BottomLineDashStile && BottomLineDashStile == "Solid") &&
  493. (colorLeftLine == colorTopLine && colorTopLine == colorRightLine && colorRightLine == colorBottomLine /*&& colorBottomLine == Background*/))
  494. {
  495. AddRectangle(x, y, Width, Height, BorderBrush, BorderThickness, Background, false);
  496. }
  497. else
  498. {
  499. if (Background != "none")
  500. {
  501. AddRectangle(x, y, Width, Height, BorderBrush, 0, Background, false);
  502. }
  503. if (Left || All)
  504. {
  505. if (LeftLineDashStile == "Solid")
  506. {
  507. AddLine(x, y, x, y + Height, colorLeftLine, LeftLine);
  508. }
  509. if (LeftLineDashStile == "Dash")
  510. {
  511. AddLine(x, y, x, y + Height, colorLeftLine, LeftLine, Dashes.Dash);
  512. }
  513. if (LeftLineDashStile == "Dot")
  514. {
  515. AddLine(x, y, x, y + Height, colorLeftLine, LeftLine, Dashes.Dot);
  516. }
  517. if (LeftLineDashStile == "DashDot")
  518. {
  519. AddLine(x, y, x, y + Height, colorLeftLine, LeftLine, Dashes.DashDot);
  520. }
  521. if (LeftLineDashStile == "DashDotDot")
  522. {
  523. AddLine(x, y, x, y + Height, colorLeftLine, LeftLine, Dashes.DashDotDot);
  524. }
  525. if (LeftLineDashStile == "Double")
  526. {
  527. AddLine(x, y, x, y + Height, colorLeftLine, LeftLine);
  528. AddLine(x - BorderThickness * 2, y - BorderThickness * 2, x - BorderThickness * 2, y + Height + BorderThickness * 2, colorLeftLine, LeftLine);
  529. }
  530. }
  531. if (Right || All)
  532. {
  533. if (RightLineDashStile == "Solid")
  534. {
  535. AddLine(x + Width, y, x + Width, y + Height, colorRightLine, RightLine);
  536. }
  537. if (RightLineDashStile == "Dash")
  538. {
  539. AddLine(x + Width, y, x + Width, y + Height, colorRightLine, RightLine, Dashes.Dash);
  540. }
  541. if (RightLineDashStile == "Dot")
  542. {
  543. AddLine(x + Width, y, x + Width, y + Height, colorRightLine, RightLine, Dashes.Dot);
  544. }
  545. if (RightLineDashStile == "DashDot")
  546. {
  547. AddLine(x + Width, y, x + Width, y + Height, colorRightLine, RightLine, Dashes.DashDot);
  548. }
  549. if (RightLineDashStile == "DashDotDot")
  550. {
  551. AddLine(x + Width, y, x + Width, y + Height, colorRightLine, RightLine, Dashes.DashDotDot);
  552. }
  553. if (RightLineDashStile == "Double")
  554. {
  555. AddLine(x + Width, y, x + Width, y + Height, colorRightLine, RightLine);
  556. AddLine(x + Width + BorderThickness * 2, y - BorderThickness * 2, x + Width + BorderThickness * 2, y + Height + BorderThickness * 2, colorRightLine, RightLine);
  557. }
  558. }
  559. if (Top || All)
  560. {
  561. if (TopLineDashStile == "Solid")
  562. {
  563. AddLine(x, y, x + Width, y, colorTopLine, TopLine);
  564. }
  565. if (TopLineDashStile == "Dash")
  566. {
  567. AddLine(x, y, x + Width, y, colorTopLine, TopLine, Dashes.Dash);
  568. }
  569. if (TopLineDashStile == "Dot")
  570. {
  571. AddLine(x, y, x + Width, y, colorTopLine, TopLine, Dashes.Dot);
  572. }
  573. if (TopLineDashStile == "DashDot")
  574. {
  575. AddLine(x, y, x + Width, y, colorTopLine, TopLine, Dashes.DashDot);
  576. }
  577. if (TopLineDashStile == "DashDotDot")
  578. {
  579. AddLine(x, y, x + Width, y, colorTopLine, TopLine, Dashes.DashDotDot);
  580. }
  581. if (TopLineDashStile == "Double")
  582. {
  583. AddLine(x, y, x + Width, y, colorTopLine, TopLine);
  584. AddLine(x - BorderThickness * 2, y - BorderThickness * 2, x + Width + BorderThickness * 2, y - BorderThickness * 2, colorTopLine, TopLine);
  585. }
  586. }
  587. if (Bottom || All)
  588. {
  589. if (BottomLineDashStile == "Solid")
  590. {
  591. AddLine(x, y + Height, x + Width, y + Height, colorBottomLine, BottomLine);
  592. }
  593. if (BottomLineDashStile == "Dash")
  594. {
  595. AddLine(x, y + Height, x + Width, y + Height, colorBottomLine, BottomLine, Dashes.Dash);
  596. }
  597. if (BottomLineDashStile == "Dot")
  598. {
  599. AddLine(x, y + Height, x + Width, y + Height, colorBottomLine, BottomLine, Dashes.Dot);
  600. }
  601. if (BottomLineDashStile == "DashDot")
  602. {
  603. AddLine(x, y + Height, x + Width, y + Height, colorBottomLine, BottomLine, Dashes.DashDot);
  604. }
  605. if (BottomLineDashStile == "DashDotDot")
  606. {
  607. AddLine(x, y + Height, x + Width, y + Height, colorBottomLine, BottomLine, Dashes.DashDotDot);
  608. }
  609. if (BottomLineDashStile == "Double")
  610. {
  611. AddLine(x, y + Height, x + Width, y + Height, colorBottomLine, BottomLine);
  612. AddLine(x - BorderThickness * 2, y + Height + BorderThickness * 2, x + Width + BorderThickness * 2, y + Height + BorderThickness * 2, colorBottomLine, BottomLine);
  613. }
  614. }
  615. }
  616. //Glass--------------------
  617. if (Glass)
  618. {
  619. AddRectangle(x, y, Width, Height / 2, BorderBrush, 0, colorTop, false);
  620. AddRectangle(x, y + Height / 2, Width, Height / 2, BorderBrush, 0, Background, false);
  621. }
  622. //Shadow-------------------
  623. if (Shadow)
  624. {
  625. AddLine(x + ShadowWidth, y + Height + ShadowWidth / 2, x + Width + ShadowWidth, y + Height + ShadowWidth / 2, ShadowColor, ShadowWidth);
  626. AddLine(x + Width + ShadowWidth / 2, y + ShadowWidth, x + Width + ShadowWidth / 2, y + Height + ShadowWidth, ShadowColor, ShadowWidth);
  627. }
  628. if (Text != null)
  629. {
  630. if (isWatermark && textInCurves)
  631. createRotateText(x, y, HorizontalAlignment, VerticalAlignment, Width, Height, font, Text, PaddingLeft, PaddingRight, PaddingTop, PaddingBottom, BorderThickness, Foreground, Background, Angle);
  632. else
  633. createText(x, y, HorizontalAlignment, VerticalAlignment, Width, Height, font, Text, PaddingLeft, PaddingRight, PaddingTop, PaddingBottom, BorderThickness, Foreground, Background, Angle);
  634. }
  635. }
  636. /// <summary>
  637. /// Method to add rectangle.
  638. /// </summary>
  639. public void AddRectangle(float x, float y, float Width, float Height,
  640. string Stroke, float StrokeThickness, string Fill, bool Rounded)
  641. {
  642. if (StrokeThickness == 0 && Fill == "none") return;
  643. string rgb_stroke;
  644. string rgb_fill;
  645. string fill_str = "";
  646. string border_col = "";
  647. string rect_stroke = "";
  648. string gsave = "gsave ";
  649. string grestore = "grestore ";
  650. if (StrokeThickness == 0)
  651. {
  652. gsave = ""; grestore = "";
  653. }
  654. else
  655. {
  656. rgb_stroke = ColorToPsRgb(Stroke);
  657. border_col = rgb_stroke + " setrgbcolor ";
  658. }
  659. if (Fill != "none")
  660. {
  661. rgb_fill = ColorToPsRgb(Fill);
  662. fill_str = gsave + rgb_fill + " setrgbcolor fill " + grestore;
  663. }
  664. rect_stroke = FloatToString(StrokeThickness) + " setlinewidth ";
  665. string internal_data;
  666. if (Rounded)
  667. {
  668. string x1 = FloatToString(x);
  669. string y1 = FloatToString(windowHeight - y - Height);
  670. string x2 = FloatToString(x);
  671. string y2 = FloatToString(windowHeight - y);
  672. string x3 = FloatToString(x + Width);
  673. string y3 = FloatToString(windowHeight - y); ;
  674. string x4 = FloatToString(x + Width);
  675. string y4 = FloatToString(windowHeight - y - Height);
  676. internal_data = FloatToString(StrokeThickness) + " setlinewidth " + FloatToString(x + Width / 2) + " " +
  677. FloatToString(windowHeight - y - Height) + " moveto " +
  678. x1 + " " + y1 + " " + x2 + " " + y2 + " 5 arct " +
  679. x2 + " " + y2 + " " + x3 + " " + y3 + " 5 arct " +
  680. x3 + " " + y3 + " " + x4 + " " + y4 + " 5 arct " +
  681. x4 + " " + y4 + " " + x1 + " " + y1 + " 5 arct closepath " + fill_str + border_col + "stroke";
  682. }
  683. else
  684. {
  685. internal_data = FloatToString(StrokeThickness) + " setlinewidth " + FloatToString(x) + " " +
  686. FloatToString(windowHeight - y - Height) + " newpath moveto " + FloatToString(x) + " " + FloatToString(windowHeight - y) +
  687. " lineto " + FloatToString(x + Width) + " " + FloatToString(windowHeight - y) +
  688. " lineto " + FloatToString(x + Width) + " " + FloatToString(windowHeight - y - Height) +
  689. " lineto closepath " + fill_str + border_col + "stroke";
  690. }
  691. psData.Append(internal_data + "\n");
  692. }
  693. /// <summary>
  694. /// Method for add ellips.
  695. /// </summary>
  696. public void AddEllipse(float x, float y, float Width, float Height,
  697. string Stroke, float StrokeThickness, string Fill)
  698. {
  699. if (StrokeThickness == 0 && Fill == "none") return;
  700. string rgb_stroke;
  701. string rgb_fill;
  702. string fill_str = "";
  703. string border_col = "";
  704. string ell_stroke = "";
  705. string gsave = "gsave ";
  706. string grestore = "grestore ";
  707. if (StrokeThickness == 0)
  708. {
  709. gsave = ""; grestore = "";
  710. }
  711. else
  712. {
  713. rgb_stroke = ColorToPsRgb(Stroke);
  714. border_col = rgb_stroke + " setrgbcolor ";
  715. }
  716. if (Fill != "none")
  717. {
  718. rgb_fill = ColorToPsRgb(Fill);
  719. fill_str = gsave + rgb_fill + " setrgbcolor fill " + grestore;
  720. }
  721. ell_stroke = FloatToString(StrokeThickness) + " setlinewidth ";
  722. string internal_data = "";
  723. internal_data = FloatToString(StrokeThickness) + " setlinewidth " +
  724. FloatToString(x + Width / 2) + " " + FloatToString(windowHeight - y - Height / 2) +
  725. " " + FloatToString(Width / 2) + " 0 360 arc closepath "
  726. + fill_str + border_col + "stroke";
  727. psData.Append(internal_data + "\n");
  728. }
  729. /// <summary>
  730. /// Method for add triangle.
  731. /// </summary>
  732. public void AddTriangle(float x, float y, float Width, float Height,
  733. string Stroke, float StrokeThickness, string Fill)
  734. {
  735. if (StrokeThickness == 0 && Fill == "none") return;
  736. float x2 = Width + x;
  737. float y2 = y;
  738. float x3 = x + Width / 2;
  739. float y3 = y;
  740. string rgb_stroke;
  741. string rgb_fill;
  742. string fill_str = "";
  743. string border_col = "";
  744. string tri_stroke = "";
  745. string gsave = "gsave ";
  746. string grestore = "grestore ";
  747. if (StrokeThickness == 0)
  748. { gsave = ""; grestore = ""; }
  749. else
  750. {
  751. rgb_stroke = ColorToPsRgb(Stroke);
  752. border_col = rgb_stroke + " setrgbcolor ";
  753. }
  754. if (Fill != "none")
  755. {
  756. rgb_fill = ColorToPsRgb(Fill);
  757. fill_str = gsave + rgb_fill + " setrgbcolor fill " + grestore;
  758. }
  759. tri_stroke = FloatToString(StrokeThickness) + " setlinewidth ";
  760. string internal_data = FloatToString(StrokeThickness) + " setlinewidth " + FloatToString(x) + " " +
  761. FloatToString(windowHeight - y - Height) + " newpath moveto " + FloatToString(x2) + " " + FloatToString(windowHeight - Height - y2) +
  762. " lineto " + FloatToString(x3) + " " + FloatToString(windowHeight - y3) +
  763. " lineto closepath " + fill_str + border_col + "stroke";
  764. psData.Append(internal_data + "\n");
  765. }
  766. /// <summary>
  767. /// Method for add Diamond.
  768. /// </summary>
  769. public void AddDiamond(float x, float y, float Width, float Height,
  770. string Stroke, float StrokeThickness, string Fill)
  771. {
  772. float x1 = Width / 2 + x;
  773. float y1 = y;
  774. float x2 = Width + x;
  775. float y2 = Height / 2 + y;
  776. float x3 = Width / 2 + x;
  777. float y3 = y;
  778. float x4 = x;
  779. float y4 = Height / 2 + y;
  780. string rgb_stroke;
  781. string rgb_fill;
  782. string fill_str = "";
  783. string border_col = "";
  784. string tri_stroke = "";
  785. string gsave = "gsave ";
  786. string grestore = "grestore ";
  787. if (StrokeThickness == 0)
  788. { gsave = ""; grestore = ""; }
  789. else
  790. {
  791. rgb_stroke = ColorToPsRgb(Stroke);
  792. border_col = rgb_stroke + " setrgbcolor ";
  793. }
  794. if (Fill != "none")
  795. {
  796. rgb_fill = ColorToPsRgb(Fill);
  797. fill_str = gsave + rgb_fill + " setrgbcolor fill " + grestore;
  798. }
  799. tri_stroke = FloatToString(StrokeThickness) + " setlinewidth ";
  800. string internal_data = FloatToString(StrokeThickness) + " setlinewidth " + FloatToString(x1) + " " +
  801. FloatToString(windowHeight - y1 - Height) + " newpath moveto " + FloatToString(x2) + " " + FloatToString(windowHeight - y2) +
  802. " lineto " + FloatToString(x3) + " " + FloatToString(windowHeight - y3) +
  803. " lineto " + FloatToString(x4) + " " + FloatToString(windowHeight - y4) +
  804. " lineto closepath " + fill_str + border_col + "stroke";
  805. psData.Append(internal_data + "\n");
  806. }
  807. ///<summary>
  808. ///Method for add line.
  809. /// </summary>
  810. public void AddLine(float x, float y, float x2, float y2, string Stroke, float StrokeThickness)
  811. {
  812. StartFig(StrokeThickness);
  813. MoveTo(x, y);
  814. AppendLine(x2, y2);
  815. EndFig(Stroke);
  816. }
  817. ///<summary>
  818. ///Method for add line with dash.
  819. /// </summary>
  820. public void AddLine(float x, float y, float x2, float y2, string Stroke, float StrokeThickness, Dashes dash)
  821. {
  822. string line_col = "";
  823. string line_stroke = "";
  824. string rgb = ColorToPsRgb(Stroke);
  825. string StrokeDashArray = "";
  826. if (dash == Dashes.Dash)
  827. {
  828. StrokeDashArray = " [5] 0 setdash ";
  829. }
  830. if (dash == Dashes.Dot)
  831. {
  832. StrokeDashArray = "[2 2] 0 setdash";
  833. }
  834. if (dash == Dashes.DashDot)
  835. {
  836. StrokeDashArray = "[2 2 5 2] 0 setdash";
  837. }
  838. if (dash == Dashes.DashDotDot)
  839. {
  840. StrokeDashArray = "[2 2 2 2 5 2] 0 setdash";
  841. }
  842. if (dash == Dashes.Double)
  843. {
  844. StrokeDashArray = "";
  845. AddLine(x + 10, y + 10, x2 + 10, y2 + 10, Stroke, StrokeThickness);
  846. }
  847. line_col = rgb + " setrgbcolor ";
  848. line_stroke = FloatToString(StrokeThickness) + " setlinewidth " + StrokeDashArray + " ";
  849. string internal_data = line_stroke + FloatToString(x) + " " +
  850. FloatToString(windowHeight - y) + " newpath moveto " + FloatToString(x2) + " " + FloatToString(windowHeight - y2) +
  851. " lineto " + line_col + "stroke [ ] 0 setdash";
  852. psData.Append(internal_data + "\n");
  853. }
  854. public void AddBezier(float x, float y, PointF p0, PointF p1, PointF p2, PointF p3,
  855. string Stroke, float StrokeThickness)
  856. {
  857. string line_col = "";
  858. string line_stroke = "";
  859. string rgb = ColorToPsRgb(Stroke);
  860. line_col = rgb + " setrgbcolor ";
  861. line_stroke = FloatToString(StrokeThickness) + " setlinewidth ";
  862. string internal_data = line_stroke + FloatToString(x + p0.X) + " " +
  863. FloatToString(windowHeight - y - p0.Y) + " newpath moveto " +
  864. FloatToString(x + p1.X) + " " + FloatToString(windowHeight - y - p1.Y) + " " +
  865. FloatToString(x + p2.X) + " " + FloatToString(windowHeight - y - p2.Y) + " " +
  866. FloatToString(x + p3.X) + " " + FloatToString(windowHeight - y - p3.Y) + " " +
  867. " curveto " + line_col + "stroke";
  868. psData.Append(internal_data + "\n");
  869. }
  870. private void AppendBezier(float x, float y, PointF p1, PointF p2, PointF p3)
  871. {
  872. string internal_data =
  873. FloatToString(x + p1.X * 0.75f) + " " + FloatToString(windowHeight - y - p1.Y * 0.75f) + " " +
  874. FloatToString(x + p2.X * 0.75f) + " " + FloatToString(windowHeight - y - p2.Y * 0.75f) + " " +
  875. FloatToString(x + p3.X * 0.75f) + " " + FloatToString(windowHeight - y - p3.Y * 0.75f) + " " +
  876. " curveto ";
  877. psData.Append(internal_data + "\n");
  878. }
  879. private void AppendLine(float x2, float y2)
  880. {
  881. string internal_data =
  882. FloatToString(x2) + " " + FloatToString(windowHeight - y2) +
  883. " lineto ";
  884. psData.Append(internal_data);
  885. }
  886. private void StartFig(float StrokeThickness)
  887. {
  888. string line_stroke = " ";
  889. line_stroke = FloatToString(StrokeThickness) + " setlinewidth ";
  890. psData.Append(line_stroke + "\n");
  891. }
  892. private void MoveTo(float x, float y)
  893. {
  894. psData.Append(" " + FloatToString(x) + " " +
  895. FloatToString(windowHeight - y) + " moveto ");
  896. }
  897. private void ClosePath()
  898. {
  899. psData.Append(" closepath ");
  900. }
  901. private void EndFig(string stroke, string fill)
  902. {
  903. string rgb_stroke = ColorToPsRgb(stroke);
  904. string l = "";
  905. string rgb_fill = ColorToPsRgb(stroke);
  906. l += /*" gsave " +*/ rgb_fill + " setrgbcolor fill "/*grestore "*/;
  907. psData.Append(l + rgb_stroke + "\n");
  908. }
  909. private void EndFig(string stroke)
  910. {
  911. string rgb_stroke = ColorToPsRgb(stroke);
  912. psData.Append(" gsave " + rgb_stroke + " setrgbcolor stroke grestore ");
  913. }
  914. public void DeclareImage(string name, string hex)
  915. {
  916. psData.Append("/" + name + " (" + hex + ") def \n");
  917. }
  918. /// <summary>
  919. /// Add image
  920. /// </summary>
  921. /// <param name="filename"></param>
  922. /// <param name="format"></param>
  923. /// <param name="left"></param>
  924. /// <param name="top"></param>
  925. /// <param name="width"></param>
  926. /// <param name="height"></param>
  927. public void AddImage(string filename, string format, float left, float top, float width, float height)
  928. {
  929. if (!String.IsNullOrEmpty(filename))
  930. {
  931. string s_picwidth = FloatToString((int)width);
  932. string s_picheight = FloatToString((int)height);
  933. width *= 0.75f;
  934. height *= 0.75f;
  935. string s_left = FloatToString(left);
  936. string s_top = FloatToString(windowHeight - height - top);
  937. string img = " gsave " +
  938. s_left + " " + s_top + " translate " + //set lower left of image at (360, 72)
  939. FloatToString(Math.Round(width)) + " " + FloatToString(Math.Round(height)) + " scale " + //size of rendered image is 175 points by 47 points
  940. s_picwidth + " " + //number of columns per row
  941. s_picheight + " " + //number of rows
  942. "8 " + //bits per color channel (1, 2, 4, or 8)
  943. "[" + s_picwidth + " 0 0 -" + s_picheight + " 0 " + s_picheight + "] " + //transform array... maps unit square to pixel
  944. "(" + filename + "." + format + ") (r) file /DCTDecode filter " + //opens the file and filters the image data
  945. "false " + //pull channels from separate sources
  946. "3 " + // 3 color channels (RGB)
  947. "colorimage " +
  948. "grestore ";
  949. psData.Append(img + "\n");
  950. }
  951. }
  952. /// <summary>
  953. /// Add image as hex code
  954. /// </summary>
  955. /// <param name="varname"></param>
  956. /// <param name="left"></param>
  957. /// <param name="top"></param>
  958. /// <param name="width"></param>
  959. /// <param name="height"></param>
  960. public void AddImage(string varname, float left, float top, float width, float height)
  961. {
  962. string s_picwidth = FloatToString((int)width);
  963. string s_picheight = FloatToString((int)height);
  964. width *= 0.75f;
  965. height *= 0.75f;
  966. string s_left = FloatToString(left);
  967. string s_top = FloatToString(windowHeight - height - top);
  968. string img = " gsave " +
  969. s_left + " " + s_top + " translate " + //set lower left of image at (360, 72)
  970. FloatToString(Math.Round(width)) + " " + FloatToString(Math.Round(height)) + " scale " + //size of rendered image is 175 points by 47 points
  971. s_picwidth + " " + //number of columns per row
  972. s_picheight + " " + //number of rows
  973. "8 " + //bits per color channel (1, 2, 4, or 8)
  974. "[" + s_picwidth + " 0 0 -" + s_picheight + " 0 " + s_picheight + "] " + //transform array... maps unit square to pixel
  975. varname + " /ASCIIHexDecode filter 0 dict" +
  976. " /DCTDecode filter " +
  977. "false " + //pull channels from separate sources
  978. "3 " + // 3 color channels (RGB)
  979. "colorimage " +
  980. "grestore ";
  981. psData.Append(img + "\n");
  982. }
  983. /// <summary>
  984. /// End of each page
  985. /// </summary>
  986. public void Finish()
  987. {
  988. psData.Append("showpage\n");
  989. }
  990. /// <summary>
  991. /// Save file.
  992. /// </summary>
  993. public void Save(string filename)
  994. {
  995. System.IO.File.WriteAllText(filename, psData.ToString());
  996. }
  997. /// <summary>
  998. /// Save stream.
  999. /// </summary>
  1000. public void Save(Stream stream)
  1001. {
  1002. stream.Write(System.Text.Encoding.UTF8.GetBytes(psData.ToString()), 0, System.Text.Encoding.UTF8.GetBytes(psData.ToString()).Length);
  1003. }
  1004. string ColorToPsRgb(string htmlcolor)
  1005. {
  1006. return FloatToString((double)System.Drawing.ColorTranslator.FromHtml(htmlcolor).R / 255) + " " +
  1007. FloatToString((double)System.Drawing.ColorTranslator.FromHtml(htmlcolor).G / 255) + " " +
  1008. FloatToString((double)System.Drawing.ColorTranslator.FromHtml(htmlcolor).B / 255);
  1009. }
  1010. private string FloatToString(double flt)
  1011. {
  1012. return ExportUtils.FloatToString(flt);
  1013. }
  1014. private string PSFont(string font)
  1015. {
  1016. return font.Replace(" ", "-");
  1017. }
  1018. /// <param name="Width"></param>
  1019. /// <param name="Height"></param>
  1020. public PSDocument(float Width, float Height)
  1021. {
  1022. windowHeight = Height;
  1023. windowWidth = Width;
  1024. CreateWindow();
  1025. }
  1026. public PSDocument()
  1027. {
  1028. windowHeight = 842;
  1029. windowWidth = 595;
  1030. }
  1031. }
  1032. }