VectorRenderModule.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Drawing;
  29. using System.Drawing.Imaging;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.Imaging;
  37. using OpenSim.Region.CoreModules.Scripting.DynamicTexture;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using log4net;
  41. using System.Reflection;
  42. using Mono.Addins;
  43. //using Cairo;
  44. namespace OpenSim.Region.CoreModules.Scripting.VectorRender
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "VectorRenderModule")]
  47. public class VectorRenderModule : ISharedRegionModule, IDynamicTextureRender
  48. {
  49. // These fields exist for testing purposes, please do not remove.
  50. // private static bool s_flipper;
  51. // private static byte[] s_asset1Data;
  52. // private static byte[] s_asset2Data;
  53. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. private Scene m_scene;
  55. private IDynamicTextureManager m_textureManager;
  56. private Graphics m_graph;
  57. private string m_fontName = "Arial";
  58. public VectorRenderModule()
  59. {
  60. }
  61. #region IDynamicTextureRender Members
  62. public string GetContentType()
  63. {
  64. return "vector";
  65. }
  66. public string GetName()
  67. {
  68. return Name;
  69. }
  70. public bool SupportsAsynchronous()
  71. {
  72. return true;
  73. }
  74. // public bool AlwaysIdenticalConversion(string bodyData, string extraParams)
  75. // {
  76. // string[] lines = GetLines(bodyData);
  77. // return lines.Any((str, r) => str.StartsWith("Image"));
  78. // }
  79. public IDynamicTexture ConvertUrl(string url, string extraParams)
  80. {
  81. return null;
  82. }
  83. public IDynamicTexture ConvertData(string bodyData, string extraParams)
  84. {
  85. return Draw(bodyData, extraParams);
  86. }
  87. public bool AsyncConvertUrl(UUID id, string url, string extraParams)
  88. {
  89. return false;
  90. }
  91. public bool AsyncConvertData(UUID id, string bodyData, string extraParams)
  92. {
  93. if (m_textureManager == null)
  94. {
  95. m_log.Warn("[VECTORRENDERMODULE]: No texture manager. Can't function");
  96. return false;
  97. }
  98. // XXX: This isn't actually being done asynchronously!
  99. m_textureManager.ReturnData(id, ConvertData(bodyData, extraParams));
  100. return true;
  101. }
  102. public void GetDrawStringSize(string text, string fontName, int fontSize,
  103. out double xSize, out double ySize)
  104. {
  105. lock (this)
  106. {
  107. using (Font myFont = new Font(fontName, fontSize))
  108. {
  109. SizeF stringSize = new SizeF();
  110. // XXX: This lock may be unnecessary.
  111. lock (m_graph)
  112. {
  113. stringSize = m_graph.MeasureString(text, myFont);
  114. xSize = stringSize.Width;
  115. ySize = stringSize.Height;
  116. }
  117. }
  118. }
  119. }
  120. #endregion
  121. #region ISharedRegionModule Members
  122. public void Initialise(IConfigSource config)
  123. {
  124. IConfig cfg = config.Configs["VectorRender"];
  125. if (null != cfg)
  126. {
  127. m_fontName = cfg.GetString("font_name", m_fontName);
  128. }
  129. m_log.DebugFormat("[VECTORRENDERMODULE]: using font \"{0}\" for text rendering.", m_fontName);
  130. // We won't dispose of these explicitly since this module is only removed when the entire simulator
  131. // is shut down.
  132. Bitmap bitmap = new Bitmap(1024, 1024, PixelFormat.Format32bppArgb);
  133. m_graph = Graphics.FromImage(bitmap);
  134. }
  135. public void PostInitialise()
  136. {
  137. }
  138. public void AddRegion(Scene scene)
  139. {
  140. if (m_scene == null)
  141. {
  142. m_scene = scene;
  143. }
  144. }
  145. public void RegionLoaded(Scene scene)
  146. {
  147. if (m_textureManager == null && m_scene == scene)
  148. {
  149. m_textureManager = m_scene.RequestModuleInterface<IDynamicTextureManager>();
  150. if (m_textureManager != null)
  151. {
  152. m_textureManager.RegisterRender(GetContentType(), this);
  153. }
  154. }
  155. }
  156. public void RemoveRegion(Scene scene)
  157. {
  158. }
  159. public void Close()
  160. {
  161. }
  162. public string Name
  163. {
  164. get { return "VectorRenderModule"; }
  165. }
  166. public Type ReplaceableInterface
  167. {
  168. get { return null; }
  169. }
  170. #endregion
  171. private IDynamicTexture Draw(string data, string extraParams)
  172. {
  173. // We need to cater for old scripts that didnt use extraParams neatly, they use either an integer size which represents both width and height, or setalpha
  174. // we will now support multiple comma seperated params in the form width:256,height:512,alpha:255
  175. int width = 256;
  176. int height = 256;
  177. int alpha = 255; // 0 is transparent
  178. Color bgColor = Color.White; // Default background color
  179. char altDataDelim = ';';
  180. char[] paramDelimiter = { ',' };
  181. char[] nvpDelimiter = { ':' };
  182. extraParams = extraParams.Trim();
  183. extraParams = extraParams.ToLower();
  184. string[] nvps = extraParams.Split(paramDelimiter);
  185. bool lossless = false;
  186. int temp = -1;
  187. foreach (string pair in nvps)
  188. {
  189. string[] nvp = pair.Split(nvpDelimiter);
  190. string name = "";
  191. string value = "";
  192. if (nvp[0] != null)
  193. {
  194. name = nvp[0].Trim();
  195. }
  196. if (nvp.Length == 2)
  197. {
  198. value = nvp[1].Trim();
  199. }
  200. switch (name)
  201. {
  202. case "width":
  203. temp = parseIntParam(value);
  204. if (temp != -1)
  205. {
  206. if (temp < 1)
  207. {
  208. width = 1;
  209. }
  210. else if (temp > 2048)
  211. {
  212. width = 2048;
  213. }
  214. else
  215. {
  216. width = temp;
  217. }
  218. }
  219. break;
  220. case "height":
  221. temp = parseIntParam(value);
  222. if (temp != -1)
  223. {
  224. if (temp < 1)
  225. {
  226. height = 1;
  227. }
  228. else if (temp > 2048)
  229. {
  230. height = 2048;
  231. }
  232. else
  233. {
  234. height = temp;
  235. }
  236. }
  237. break;
  238. case "alpha":
  239. temp = parseIntParam(value);
  240. if (temp != -1)
  241. {
  242. if (temp < 0)
  243. {
  244. alpha = 0;
  245. }
  246. else if (temp > 255)
  247. {
  248. alpha = 255;
  249. }
  250. else
  251. {
  252. alpha = temp;
  253. }
  254. }
  255. // Allow a bitmap w/o the alpha component to be created
  256. else if (value.ToLower() == "false") {
  257. alpha = 256;
  258. }
  259. break;
  260. case "bgcolor":
  261. case "bgcolour":
  262. int hex = 0;
  263. if (Int32.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
  264. {
  265. bgColor = Color.FromArgb(hex);
  266. }
  267. else
  268. {
  269. bgColor = Color.FromName(value);
  270. }
  271. break;
  272. case "altdatadelim":
  273. altDataDelim = value.ToCharArray()[0];
  274. break;
  275. case "lossless":
  276. if (value.ToLower() == "true")
  277. lossless = true;
  278. break;
  279. case "":
  280. // blank string has been passed do nothing just use defaults
  281. break;
  282. default: // this is all for backwards compat, all a bit ugly hopfully can be removed in future
  283. // could be either set alpha or just an int
  284. if (name == "setalpha")
  285. {
  286. alpha = 0; // set the texture to have transparent background (maintains backwards compat)
  287. }
  288. else
  289. {
  290. // this function used to accept an int on its own that represented both
  291. // width and height, this is to maintain backwards compat, could be removed
  292. // but would break existing scripts
  293. temp = parseIntParam(name);
  294. if (temp != -1)
  295. {
  296. if (temp > 1024)
  297. temp = 1024;
  298. if (temp < 128)
  299. temp = 128;
  300. width = temp;
  301. height = temp;
  302. }
  303. }
  304. break;
  305. }
  306. }
  307. Bitmap bitmap = null;
  308. Graphics graph = null;
  309. bool reuseable = false;
  310. try
  311. {
  312. // XXX: In testing, it appears that if multiple threads dispose of separate GDI+ objects simultaneously,
  313. // the native malloc heap can become corrupted, possibly due to a double free(). This may be due to
  314. // bugs in the underlying libcairo used by mono's libgdiplus.dll on Linux/OSX. These problems were
  315. // seen with both libcario 1.10.2-6.1ubuntu3 and 1.8.10-2ubuntu1. They go away if disposal is perfomed
  316. // under lock.
  317. lock (this)
  318. {
  319. if (alpha == 256 && bgColor.A != 255)
  320. alpha = bgColor.A;
  321. if (alpha == 256)
  322. {
  323. bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
  324. graph = Graphics.FromImage(bitmap);
  325. graph.Clear(bgColor);
  326. }
  327. else
  328. {
  329. Color newbg = Color.FromArgb(alpha, bgColor);
  330. bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  331. graph = Graphics.FromImage(bitmap);
  332. graph.Clear(newbg);
  333. }
  334. GDIDraw(data, graph, altDataDelim, out reuseable);
  335. }
  336. byte[] imageJ2000 = new byte[0];
  337. // This code exists for testing purposes, please do not remove.
  338. // if (s_flipper)
  339. // imageJ2000 = s_asset1Data;
  340. // else
  341. // imageJ2000 = s_asset2Data;
  342. //
  343. // s_flipper = !s_flipper;
  344. try
  345. {
  346. imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, lossless);
  347. }
  348. catch (Exception e)
  349. {
  350. m_log.ErrorFormat(
  351. "[VECTORRENDERMODULE]: OpenJpeg Encode Failed. Exception {0}{1}",
  352. e.Message, e.StackTrace);
  353. }
  354. return new OpenSim.Region.CoreModules.Scripting.DynamicTexture.DynamicTexture(
  355. data, extraParams, imageJ2000, new Size(width, height), reuseable);
  356. }
  357. finally
  358. {
  359. // XXX: In testing, it appears that if multiple threads dispose of separate GDI+ objects simultaneously,
  360. // the native malloc heap can become corrupted, possibly due to a double free(). This may be due to
  361. // bugs in the underlying libcairo used by mono's libgdiplus.dll on Linux/OSX. These problems were
  362. // seen with both libcario 1.10.2-6.1ubuntu3 and 1.8.10-2ubuntu1. They go away if disposal is perfomed
  363. // under lock.
  364. lock (this)
  365. {
  366. if (graph != null)
  367. graph.Dispose();
  368. if (bitmap != null)
  369. bitmap.Dispose();
  370. }
  371. }
  372. }
  373. private int parseIntParam(string strInt)
  374. {
  375. int parsed;
  376. try
  377. {
  378. parsed = Convert.ToInt32(strInt);
  379. }
  380. catch (Exception)
  381. {
  382. //Ckrinke: Add a WriteLine to remove the warning about 'e' defined but not used
  383. // m_log.Debug("Problem with Draw. Please verify parameters." + e.ToString());
  384. parsed = -1;
  385. }
  386. return parsed;
  387. }
  388. /*
  389. private void CairoDraw(string data, System.Drawing.Graphics graph)
  390. {
  391. using (Win32Surface draw = new Win32Surface(graph.GetHdc()))
  392. {
  393. Context contex = new Context(draw);
  394. contex.Antialias = Antialias.None; //fastest method but low quality
  395. contex.LineWidth = 7;
  396. char[] lineDelimiter = { ';' };
  397. char[] partsDelimiter = { ',' };
  398. string[] lines = data.Split(lineDelimiter);
  399. foreach (string line in lines)
  400. {
  401. string nextLine = line.Trim();
  402. if (nextLine.StartsWith("MoveTO"))
  403. {
  404. float x = 0;
  405. float y = 0;
  406. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  407. contex.MoveTo(x, y);
  408. }
  409. else if (nextLine.StartsWith("LineTo"))
  410. {
  411. float x = 0;
  412. float y = 0;
  413. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  414. contex.LineTo(x, y);
  415. contex.Stroke();
  416. }
  417. }
  418. }
  419. graph.ReleaseHdc();
  420. }
  421. */
  422. /// <summary>
  423. /// Split input data into discrete command lines.
  424. /// </summary>
  425. /// <returns></returns>
  426. /// <param name='data'></param>
  427. /// <param name='dataDelim'></param>
  428. private string[] GetLines(string data, char dataDelim)
  429. {
  430. char[] lineDelimiter = { dataDelim };
  431. return data.Split(lineDelimiter);
  432. }
  433. private void GDIDraw(string data, Graphics graph, char dataDelim, out bool reuseable)
  434. {
  435. reuseable = true;
  436. Point startPoint = new Point(0, 0);
  437. Point endPoint = new Point(0, 0);
  438. Pen drawPen = null;
  439. Font myFont = null;
  440. SolidBrush myBrush = null;
  441. try
  442. {
  443. drawPen = new Pen(Color.Black, 7);
  444. string fontName = m_fontName;
  445. float fontSize = 14;
  446. myFont = new Font(fontName, fontSize);
  447. myBrush = new SolidBrush(Color.Black);
  448. char[] partsDelimiter = {','};
  449. foreach (string line in GetLines(data, dataDelim))
  450. {
  451. string nextLine = line.TrimStart();
  452. // m_log.DebugFormat("[VECTOR RENDER MODULE]: Processing line '{0}'", nextLine);
  453. if (nextLine.StartsWith("Text") && nextLine.Length > 5)
  454. {
  455. int start = 4;
  456. if (nextLine[4] == ' ')
  457. start++;
  458. if (start < nextLine.Length)
  459. {
  460. nextLine = nextLine.Substring(start);
  461. graph.DrawString(nextLine, myFont, myBrush, startPoint);
  462. }
  463. continue;
  464. }
  465. nextLine = nextLine.TrimEnd();
  466. if (nextLine.StartsWith("ResetTransf"))
  467. {
  468. graph.ResetTransform();
  469. }
  470. else if (nextLine.StartsWith("TransTransf"))
  471. {
  472. float x = 0;
  473. float y = 0;
  474. GetParams(partsDelimiter, ref nextLine, 11, ref x, ref y);
  475. graph.TranslateTransform(x, y);
  476. }
  477. else if (nextLine.StartsWith("ScaleTransf"))
  478. {
  479. float x = 0;
  480. float y = 0;
  481. GetParams(partsDelimiter, ref nextLine, 11, ref x, ref y);
  482. graph.ScaleTransform(x, y);
  483. }
  484. else if (nextLine.StartsWith("RotTransf"))
  485. {
  486. float x = 0;
  487. GetParams(partsDelimiter, ref nextLine, 9, ref x);
  488. graph.RotateTransform(x);
  489. }
  490. //replace with switch, or even better, do some proper parsing
  491. else if (nextLine.StartsWith("MoveTo"))
  492. {
  493. float x = 0;
  494. float y = 0;
  495. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  496. startPoint.X = (int) x;
  497. startPoint.Y = (int) y;
  498. }
  499. else if (nextLine.StartsWith("LineTo"))
  500. {
  501. float x = 0;
  502. float y = 0;
  503. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  504. endPoint.X = (int) x;
  505. endPoint.Y = (int) y;
  506. graph.DrawLine(drawPen, startPoint, endPoint);
  507. startPoint.X = endPoint.X;
  508. startPoint.Y = endPoint.Y;
  509. }
  510. else if (nextLine.StartsWith("Image"))
  511. {
  512. // We cannot reuse any generated texture involving fetching an image via HTTP since that image
  513. // can change.
  514. reuseable = false;
  515. float x = 0;
  516. float y = 0;
  517. GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y);
  518. endPoint.X = (int) x;
  519. endPoint.Y = (int) y;
  520. using (Image image = ImageHttpRequest(nextLine))
  521. {
  522. if (image != null)
  523. {
  524. graph.DrawImage(image, (float)startPoint.X, (float)startPoint.Y, x, y);
  525. }
  526. else
  527. {
  528. using (Font errorFont = new Font(m_fontName,6))
  529. {
  530. graph.DrawString("URL couldn't be resolved or is", errorFont,
  531. myBrush, startPoint);
  532. graph.DrawString("not an image. Please check URL.", errorFont,
  533. myBrush, new Point(startPoint.X, 12 + startPoint.Y));
  534. }
  535. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  536. }
  537. }
  538. startPoint.X += endPoint.X;
  539. startPoint.Y += endPoint.Y;
  540. }
  541. else if (nextLine.StartsWith("Rectangle"))
  542. {
  543. float x = 0;
  544. float y = 0;
  545. GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y);
  546. endPoint.X = (int) x;
  547. endPoint.Y = (int) y;
  548. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  549. startPoint.X += endPoint.X;
  550. startPoint.Y += endPoint.Y;
  551. }
  552. else if (nextLine.StartsWith("FillRectangle"))
  553. {
  554. float x = 0;
  555. float y = 0;
  556. GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y);
  557. endPoint.X = (int) x;
  558. endPoint.Y = (int) y;
  559. graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  560. startPoint.X += endPoint.X;
  561. startPoint.Y += endPoint.Y;
  562. }
  563. else if (nextLine.StartsWith("FillPolygon"))
  564. {
  565. PointF[] points = null;
  566. GetParams(partsDelimiter, ref nextLine, 11, ref points);
  567. graph.FillPolygon(myBrush, points);
  568. }
  569. else if (nextLine.StartsWith("Polygon"))
  570. {
  571. PointF[] points = null;
  572. GetParams(partsDelimiter, ref nextLine, 7, ref points);
  573. graph.DrawPolygon(drawPen, points);
  574. }
  575. else if (nextLine.StartsWith("Ellipse"))
  576. {
  577. float x = 0;
  578. float y = 0;
  579. GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y);
  580. endPoint.X = (int)x;
  581. endPoint.Y = (int)y;
  582. graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  583. startPoint.X += endPoint.X;
  584. startPoint.Y += endPoint.Y;
  585. }
  586. else if (nextLine.StartsWith("FillEllipse"))
  587. {
  588. float x = 0;
  589. float y = 0;
  590. GetParams(partsDelimiter, ref nextLine, 11, ref x, ref y);
  591. endPoint.X = (int)x;
  592. endPoint.Y = (int)y;
  593. graph.FillEllipse(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  594. startPoint.X += endPoint.X;
  595. startPoint.Y += endPoint.Y;
  596. }
  597. else if (nextLine.StartsWith("FontSize"))
  598. {
  599. nextLine = nextLine.Remove(0, 8);
  600. nextLine = nextLine.Trim();
  601. fontSize = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  602. myFont.Dispose();
  603. myFont = new Font(fontName, fontSize);
  604. }
  605. else if (nextLine.StartsWith("FontProp"))
  606. {
  607. FontStyle myFontStyle = myFont.Style;
  608. nextLine = nextLine.Remove(0, 8);
  609. nextLine = nextLine.Trim();
  610. string[] fprops = nextLine.Split(partsDelimiter);
  611. foreach (string prop in fprops)
  612. {
  613. switch (prop)
  614. {
  615. case "B":
  616. myFontStyle |= FontStyle.Bold;
  617. break;
  618. case "I":
  619. myFontStyle |= FontStyle.Italic;
  620. break;
  621. case "U":
  622. myFontStyle |= FontStyle.Underline;
  623. break;
  624. case "S":
  625. myFontStyle |= FontStyle.Strikeout;
  626. break;
  627. case "R": //This special case resets all font properties
  628. myFontStyle = FontStyle.Regular;
  629. break;
  630. }
  631. }
  632. if (myFontStyle != myFont.Style)
  633. {
  634. Font newFont = new Font(myFont, myFontStyle);
  635. myFont.Dispose();
  636. myFont = newFont;
  637. }
  638. }
  639. else if (nextLine.StartsWith("FontName"))
  640. {
  641. nextLine = nextLine.Remove(0, 8);
  642. fontName = nextLine.Trim();
  643. myFont.Dispose();
  644. myFont = new Font(fontName, fontSize);
  645. }
  646. else if (nextLine.StartsWith("PenSize"))
  647. {
  648. nextLine = nextLine.Remove(0, 7);
  649. nextLine = nextLine.Trim();
  650. float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  651. drawPen.Width = size;
  652. }
  653. else if (nextLine.StartsWith("PenCap"))
  654. {
  655. bool start = true, end = true;
  656. nextLine = nextLine.Remove(0, 6);
  657. nextLine = nextLine.Trim();
  658. string[] cap = nextLine.Split(partsDelimiter);
  659. if (cap[0].ToLower() == "start")
  660. end = false;
  661. else if (cap[0].ToLower() == "end")
  662. start = false;
  663. else if (cap[0].ToLower() != "both")
  664. return;
  665. string type = cap[1].ToLower();
  666. if (end)
  667. {
  668. switch (type)
  669. {
  670. case "arrow":
  671. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  672. break;
  673. case "round":
  674. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
  675. break;
  676. case "diamond":
  677. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
  678. break;
  679. case "flat":
  680. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.Flat;
  681. break;
  682. }
  683. }
  684. if (start)
  685. {
  686. switch (type)
  687. {
  688. case "arrow":
  689. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  690. break;
  691. case "round":
  692. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
  693. break;
  694. case "diamond":
  695. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
  696. break;
  697. case "flat":
  698. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.Flat;
  699. break;
  700. }
  701. }
  702. }
  703. else if (nextLine.StartsWith("PenColour") || nextLine.StartsWith("PenColor"))
  704. {
  705. nextLine = nextLine.Remove(0, 9);
  706. nextLine = nextLine.Trim();
  707. int hex = 0;
  708. Color newColor;
  709. if (Int32.TryParse(nextLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
  710. {
  711. newColor = Color.FromArgb(hex);
  712. }
  713. else
  714. {
  715. // this doesn't fail, it just returns black if nothing is found
  716. newColor = Color.FromName(nextLine);
  717. }
  718. myBrush.Color = newColor;
  719. drawPen.Color = newColor;
  720. }
  721. }
  722. }
  723. finally
  724. {
  725. if (drawPen != null)
  726. drawPen.Dispose();
  727. if (myFont != null)
  728. myFont.Dispose();
  729. if (myBrush != null)
  730. myBrush.Dispose();
  731. }
  732. }
  733. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x)
  734. {
  735. line = line.Remove(0, startLength);
  736. string[] parts = line.Split(partsDelimiter);
  737. if (parts.Length > 0)
  738. {
  739. string xVal = parts[0].Trim();
  740. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  741. }
  742. }
  743. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x, ref float y)
  744. {
  745. line = line.Remove(0, startLength);
  746. string[] parts = line.Split(partsDelimiter);
  747. if (parts.Length == 2)
  748. {
  749. string xVal = parts[0].Trim();
  750. string yVal = parts[1].Trim();
  751. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  752. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  753. }
  754. else if (parts.Length > 2)
  755. {
  756. string xVal = parts[0].Trim();
  757. string yVal = parts[1].Trim();
  758. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  759. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  760. line = "";
  761. for (int i = 2; i < parts.Length; i++)
  762. {
  763. line = line + parts[i].Trim();
  764. line = line + " ";
  765. }
  766. }
  767. }
  768. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref PointF[] points)
  769. {
  770. line = line.Remove(0, startLength);
  771. string[] parts = line.Split(partsDelimiter);
  772. if (parts.Length > 1 && parts.Length % 2 == 0)
  773. {
  774. points = new PointF[parts.Length / 2];
  775. for (int i = 0; i < parts.Length; i = i + 2)
  776. {
  777. string xVal = parts[i].Trim();
  778. string yVal = parts[i+1].Trim();
  779. float x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  780. float y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  781. PointF point = new PointF(x, y);
  782. points[i / 2] = point;
  783. // m_log.DebugFormat("[VECTOR RENDER MODULE]: Got point {0}", points[i / 2]);
  784. }
  785. }
  786. }
  787. private Bitmap ImageHttpRequest(string url)
  788. {
  789. try
  790. {
  791. WebRequest request = HttpWebRequest.Create(url);
  792. using (HttpWebResponse response = (HttpWebResponse)(request).GetResponse())
  793. {
  794. if (response.StatusCode == HttpStatusCode.OK)
  795. {
  796. using (Stream s = response.GetResponseStream())
  797. {
  798. Bitmap image = new Bitmap(s);
  799. return image;
  800. }
  801. }
  802. }
  803. }
  804. catch { }
  805. return null;
  806. }
  807. }
  808. }