VectorRenderModule.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. int temp = -1;
  186. foreach (string pair in nvps)
  187. {
  188. string[] nvp = pair.Split(nvpDelimiter);
  189. string name = "";
  190. string value = "";
  191. if (nvp[0] != null)
  192. {
  193. name = nvp[0].Trim();
  194. }
  195. if (nvp.Length == 2)
  196. {
  197. value = nvp[1].Trim();
  198. }
  199. switch (name)
  200. {
  201. case "width":
  202. temp = parseIntParam(value);
  203. if (temp != -1)
  204. {
  205. if (temp < 1)
  206. {
  207. width = 1;
  208. }
  209. else if (temp > 2048)
  210. {
  211. width = 2048;
  212. }
  213. else
  214. {
  215. width = temp;
  216. }
  217. }
  218. break;
  219. case "height":
  220. temp = parseIntParam(value);
  221. if (temp != -1)
  222. {
  223. if (temp < 1)
  224. {
  225. height = 1;
  226. }
  227. else if (temp > 2048)
  228. {
  229. height = 2048;
  230. }
  231. else
  232. {
  233. height = temp;
  234. }
  235. }
  236. break;
  237. case "alpha":
  238. temp = parseIntParam(value);
  239. if (temp != -1)
  240. {
  241. if (temp < 0)
  242. {
  243. alpha = 0;
  244. }
  245. else if (temp > 255)
  246. {
  247. alpha = 255;
  248. }
  249. else
  250. {
  251. alpha = temp;
  252. }
  253. }
  254. // Allow a bitmap w/o the alpha component to be created
  255. else if (value.ToLower() == "false") {
  256. alpha = 256;
  257. }
  258. break;
  259. case "bgcolor":
  260. case "bgcolour":
  261. int hex = 0;
  262. if (Int32.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
  263. {
  264. bgColor = Color.FromArgb(hex);
  265. }
  266. else
  267. {
  268. bgColor = Color.FromName(value);
  269. }
  270. break;
  271. case "altdatadelim":
  272. altDataDelim = value.ToCharArray()[0];
  273. break;
  274. case "":
  275. // blank string has been passed do nothing just use defaults
  276. break;
  277. default: // this is all for backwards compat, all a bit ugly hopfully can be removed in future
  278. // could be either set alpha or just an int
  279. if (name == "setalpha")
  280. {
  281. alpha = 0; // set the texture to have transparent background (maintains backwards compat)
  282. }
  283. else
  284. {
  285. // this function used to accept an int on its own that represented both
  286. // width and height, this is to maintain backwards compat, could be removed
  287. // but would break existing scripts
  288. temp = parseIntParam(name);
  289. if (temp != -1)
  290. {
  291. if (temp > 1024)
  292. temp = 1024;
  293. if (temp < 128)
  294. temp = 128;
  295. width = temp;
  296. height = temp;
  297. }
  298. }
  299. break;
  300. }
  301. }
  302. Bitmap bitmap = null;
  303. Graphics graph = null;
  304. bool reuseable = false;
  305. try
  306. {
  307. // XXX: In testing, it appears that if multiple threads dispose of separate GDI+ objects simultaneously,
  308. // the native malloc heap can become corrupted, possibly due to a double free(). This may be due to
  309. // bugs in the underlying libcairo used by mono's libgdiplus.dll on Linux/OSX. These problems were
  310. // seen with both libcario 1.10.2-6.1ubuntu3 and 1.8.10-2ubuntu1. They go away if disposal is perfomed
  311. // under lock.
  312. lock (this)
  313. {
  314. if (alpha == 256 && bgColor.A != 255)
  315. alpha = bgColor.A;
  316. if (alpha == 256)
  317. {
  318. bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
  319. graph = Graphics.FromImage(bitmap);
  320. graph.Clear(bgColor);
  321. }
  322. else
  323. {
  324. Color newbg = Color.FromArgb(alpha, bgColor);
  325. bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  326. graph = Graphics.FromImage(bitmap);
  327. graph.Clear(newbg);
  328. }
  329. GDIDraw(data, graph, altDataDelim, out reuseable);
  330. }
  331. byte[] imageJ2000 = new byte[0];
  332. // This code exists for testing purposes, please do not remove.
  333. // if (s_flipper)
  334. // imageJ2000 = s_asset1Data;
  335. // else
  336. // imageJ2000 = s_asset2Data;
  337. //
  338. // s_flipper = !s_flipper;
  339. try
  340. {
  341. imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, false);
  342. }
  343. catch (Exception e)
  344. {
  345. m_log.ErrorFormat(
  346. "[VECTORRENDERMODULE]: OpenJpeg Encode Failed. Exception {0}{1}",
  347. e.Message, e.StackTrace);
  348. }
  349. return new OpenSim.Region.CoreModules.Scripting.DynamicTexture.DynamicTexture(
  350. data, extraParams, imageJ2000, new Size(width, height), reuseable);
  351. }
  352. finally
  353. {
  354. // XXX: In testing, it appears that if multiple threads dispose of separate GDI+ objects simultaneously,
  355. // the native malloc heap can become corrupted, possibly due to a double free(). This may be due to
  356. // bugs in the underlying libcairo used by mono's libgdiplus.dll on Linux/OSX. These problems were
  357. // seen with both libcario 1.10.2-6.1ubuntu3 and 1.8.10-2ubuntu1. They go away if disposal is perfomed
  358. // under lock.
  359. lock (this)
  360. {
  361. if (graph != null)
  362. graph.Dispose();
  363. if (bitmap != null)
  364. bitmap.Dispose();
  365. }
  366. }
  367. }
  368. private int parseIntParam(string strInt)
  369. {
  370. int parsed;
  371. try
  372. {
  373. parsed = Convert.ToInt32(strInt);
  374. }
  375. catch (Exception)
  376. {
  377. //Ckrinke: Add a WriteLine to remove the warning about 'e' defined but not used
  378. // m_log.Debug("Problem with Draw. Please verify parameters." + e.ToString());
  379. parsed = -1;
  380. }
  381. return parsed;
  382. }
  383. /*
  384. private void CairoDraw(string data, System.Drawing.Graphics graph)
  385. {
  386. using (Win32Surface draw = new Win32Surface(graph.GetHdc()))
  387. {
  388. Context contex = new Context(draw);
  389. contex.Antialias = Antialias.None; //fastest method but low quality
  390. contex.LineWidth = 7;
  391. char[] lineDelimiter = { ';' };
  392. char[] partsDelimiter = { ',' };
  393. string[] lines = data.Split(lineDelimiter);
  394. foreach (string line in lines)
  395. {
  396. string nextLine = line.Trim();
  397. if (nextLine.StartsWith("MoveTO"))
  398. {
  399. float x = 0;
  400. float y = 0;
  401. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  402. contex.MoveTo(x, y);
  403. }
  404. else if (nextLine.StartsWith("LineTo"))
  405. {
  406. float x = 0;
  407. float y = 0;
  408. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  409. contex.LineTo(x, y);
  410. contex.Stroke();
  411. }
  412. }
  413. }
  414. graph.ReleaseHdc();
  415. }
  416. */
  417. /// <summary>
  418. /// Split input data into discrete command lines.
  419. /// </summary>
  420. /// <returns></returns>
  421. /// <param name='data'></param>
  422. /// <param name='dataDelim'></param>
  423. private string[] GetLines(string data, char dataDelim)
  424. {
  425. char[] lineDelimiter = { dataDelim };
  426. return data.Split(lineDelimiter);
  427. }
  428. private void GDIDraw(string data, Graphics graph, char dataDelim, out bool reuseable)
  429. {
  430. reuseable = true;
  431. Point startPoint = new Point(0, 0);
  432. Point endPoint = new Point(0, 0);
  433. Pen drawPen = null;
  434. Font myFont = null;
  435. SolidBrush myBrush = null;
  436. try
  437. {
  438. drawPen = new Pen(Color.Black, 7);
  439. string fontName = m_fontName;
  440. float fontSize = 14;
  441. myFont = new Font(fontName, fontSize);
  442. myBrush = new SolidBrush(Color.Black);
  443. char[] partsDelimiter = {','};
  444. foreach (string line in GetLines(data, dataDelim))
  445. {
  446. string nextLine = line.TrimStart();
  447. // m_log.DebugFormat("[VECTOR RENDER MODULE]: Processing line '{0}'", nextLine);
  448. if (nextLine.StartsWith("Text") && nextLine.Length > 5)
  449. {
  450. int start = 4;
  451. if (nextLine[4] == ' ')
  452. start++;
  453. if (start < nextLine.Length)
  454. {
  455. nextLine = nextLine.Substring(start);
  456. graph.DrawString(nextLine, myFont, myBrush, startPoint);
  457. }
  458. continue;
  459. }
  460. nextLine = nextLine.TrimEnd();
  461. if (nextLine.StartsWith("ResetTransf"))
  462. {
  463. graph.ResetTransform();
  464. }
  465. else if (nextLine.StartsWith("TransTransf"))
  466. {
  467. float x = 0;
  468. float y = 0;
  469. GetParams(partsDelimiter, ref nextLine, 11, ref x, ref y);
  470. graph.TranslateTransform(x, y);
  471. }
  472. else if (nextLine.StartsWith("ScaleTransf"))
  473. {
  474. float x = 0;
  475. float y = 0;
  476. GetParams(partsDelimiter, ref nextLine, 11, ref x, ref y);
  477. graph.ScaleTransform(x, y);
  478. }
  479. else if (nextLine.StartsWith("RotTransf"))
  480. {
  481. float x = 0;
  482. GetParams(partsDelimiter, ref nextLine, 9, ref x);
  483. graph.RotateTransform(x);
  484. }
  485. //replace with switch, or even better, do some proper parsing
  486. else if (nextLine.StartsWith("MoveTo"))
  487. {
  488. float x = 0;
  489. float y = 0;
  490. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  491. startPoint.X = (int) x;
  492. startPoint.Y = (int) y;
  493. }
  494. else if (nextLine.StartsWith("LineTo"))
  495. {
  496. float x = 0;
  497. float y = 0;
  498. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  499. endPoint.X = (int) x;
  500. endPoint.Y = (int) y;
  501. graph.DrawLine(drawPen, startPoint, endPoint);
  502. startPoint.X = endPoint.X;
  503. startPoint.Y = endPoint.Y;
  504. }
  505. else if (nextLine.StartsWith("Image"))
  506. {
  507. // We cannot reuse any generated texture involving fetching an image via HTTP since that image
  508. // can change.
  509. reuseable = false;
  510. float x = 0;
  511. float y = 0;
  512. GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y);
  513. endPoint.X = (int) x;
  514. endPoint.Y = (int) y;
  515. using (Image image = ImageHttpRequest(nextLine))
  516. {
  517. if (image != null)
  518. {
  519. graph.DrawImage(image, (float)startPoint.X, (float)startPoint.Y, x, y);
  520. }
  521. else
  522. {
  523. using (Font errorFont = new Font(m_fontName,6))
  524. {
  525. graph.DrawString("URL couldn't be resolved or is", errorFont,
  526. myBrush, startPoint);
  527. graph.DrawString("not an image. Please check URL.", errorFont,
  528. myBrush, new Point(startPoint.X, 12 + startPoint.Y));
  529. }
  530. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  531. }
  532. }
  533. startPoint.X += endPoint.X;
  534. startPoint.Y += endPoint.Y;
  535. }
  536. else if (nextLine.StartsWith("Rectangle"))
  537. {
  538. float x = 0;
  539. float y = 0;
  540. GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y);
  541. endPoint.X = (int) x;
  542. endPoint.Y = (int) y;
  543. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  544. startPoint.X += endPoint.X;
  545. startPoint.Y += endPoint.Y;
  546. }
  547. else if (nextLine.StartsWith("FillRectangle"))
  548. {
  549. float x = 0;
  550. float y = 0;
  551. GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y);
  552. endPoint.X = (int) x;
  553. endPoint.Y = (int) y;
  554. graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  555. startPoint.X += endPoint.X;
  556. startPoint.Y += endPoint.Y;
  557. }
  558. else if (nextLine.StartsWith("FillPolygon"))
  559. {
  560. PointF[] points = null;
  561. GetParams(partsDelimiter, ref nextLine, 11, ref points);
  562. graph.FillPolygon(myBrush, points);
  563. }
  564. else if (nextLine.StartsWith("Polygon"))
  565. {
  566. PointF[] points = null;
  567. GetParams(partsDelimiter, ref nextLine, 7, ref points);
  568. graph.DrawPolygon(drawPen, points);
  569. }
  570. else if (nextLine.StartsWith("Ellipse"))
  571. {
  572. float x = 0;
  573. float y = 0;
  574. GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y);
  575. endPoint.X = (int)x;
  576. endPoint.Y = (int)y;
  577. graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  578. startPoint.X += endPoint.X;
  579. startPoint.Y += endPoint.Y;
  580. }
  581. else if (nextLine.StartsWith("FillEllipse"))
  582. {
  583. float x = 0;
  584. float y = 0;
  585. GetParams(partsDelimiter, ref nextLine, 11, ref x, ref y);
  586. endPoint.X = (int)x;
  587. endPoint.Y = (int)y;
  588. graph.FillEllipse(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  589. startPoint.X += endPoint.X;
  590. startPoint.Y += endPoint.Y;
  591. }
  592. else if (nextLine.StartsWith("FontSize"))
  593. {
  594. nextLine = nextLine.Remove(0, 8);
  595. nextLine = nextLine.Trim();
  596. fontSize = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  597. myFont.Dispose();
  598. myFont = new Font(fontName, fontSize);
  599. }
  600. else if (nextLine.StartsWith("FontProp"))
  601. {
  602. FontStyle myFontStyle = myFont.Style;
  603. nextLine = nextLine.Remove(0, 8);
  604. nextLine = nextLine.Trim();
  605. string[] fprops = nextLine.Split(partsDelimiter);
  606. foreach (string prop in fprops)
  607. {
  608. switch (prop)
  609. {
  610. case "B":
  611. myFontStyle |= FontStyle.Bold;
  612. break;
  613. case "I":
  614. myFontStyle |= FontStyle.Italic;
  615. break;
  616. case "U":
  617. myFontStyle |= FontStyle.Underline;
  618. break;
  619. case "S":
  620. myFontStyle |= FontStyle.Strikeout;
  621. break;
  622. case "R": //This special case resets all font properties
  623. myFontStyle = FontStyle.Regular;
  624. break;
  625. }
  626. }
  627. if (myFontStyle != myFont.Style)
  628. {
  629. Font newFont = new Font(myFont, myFontStyle);
  630. myFont.Dispose();
  631. myFont = newFont;
  632. }
  633. }
  634. else if (nextLine.StartsWith("FontName"))
  635. {
  636. nextLine = nextLine.Remove(0, 8);
  637. fontName = nextLine.Trim();
  638. myFont.Dispose();
  639. myFont = new Font(fontName, fontSize);
  640. }
  641. else if (nextLine.StartsWith("PenSize"))
  642. {
  643. nextLine = nextLine.Remove(0, 7);
  644. nextLine = nextLine.Trim();
  645. float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  646. drawPen.Width = size;
  647. }
  648. else if (nextLine.StartsWith("PenCap"))
  649. {
  650. bool start = true, end = true;
  651. nextLine = nextLine.Remove(0, 6);
  652. nextLine = nextLine.Trim();
  653. string[] cap = nextLine.Split(partsDelimiter);
  654. if (cap[0].ToLower() == "start")
  655. end = false;
  656. else if (cap[0].ToLower() == "end")
  657. start = false;
  658. else if (cap[0].ToLower() != "both")
  659. return;
  660. string type = cap[1].ToLower();
  661. if (end)
  662. {
  663. switch (type)
  664. {
  665. case "arrow":
  666. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  667. break;
  668. case "round":
  669. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
  670. break;
  671. case "diamond":
  672. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
  673. break;
  674. case "flat":
  675. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.Flat;
  676. break;
  677. }
  678. }
  679. if (start)
  680. {
  681. switch (type)
  682. {
  683. case "arrow":
  684. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  685. break;
  686. case "round":
  687. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
  688. break;
  689. case "diamond":
  690. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
  691. break;
  692. case "flat":
  693. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.Flat;
  694. break;
  695. }
  696. }
  697. }
  698. else if (nextLine.StartsWith("PenColour") || nextLine.StartsWith("PenColor"))
  699. {
  700. nextLine = nextLine.Remove(0, 9);
  701. nextLine = nextLine.Trim();
  702. int hex = 0;
  703. Color newColor;
  704. if (Int32.TryParse(nextLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
  705. {
  706. newColor = Color.FromArgb(hex);
  707. }
  708. else
  709. {
  710. // this doesn't fail, it just returns black if nothing is found
  711. newColor = Color.FromName(nextLine);
  712. }
  713. myBrush.Color = newColor;
  714. drawPen.Color = newColor;
  715. }
  716. }
  717. }
  718. finally
  719. {
  720. if (drawPen != null)
  721. drawPen.Dispose();
  722. if (myFont != null)
  723. myFont.Dispose();
  724. if (myBrush != null)
  725. myBrush.Dispose();
  726. }
  727. }
  728. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x)
  729. {
  730. line = line.Remove(0, startLength);
  731. string[] parts = line.Split(partsDelimiter);
  732. if (parts.Length > 0)
  733. {
  734. string xVal = parts[0].Trim();
  735. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  736. }
  737. }
  738. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x, ref float y)
  739. {
  740. line = line.Remove(0, startLength);
  741. string[] parts = line.Split(partsDelimiter);
  742. if (parts.Length == 2)
  743. {
  744. string xVal = parts[0].Trim();
  745. string yVal = parts[1].Trim();
  746. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  747. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  748. }
  749. else if (parts.Length > 2)
  750. {
  751. string xVal = parts[0].Trim();
  752. string yVal = parts[1].Trim();
  753. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  754. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  755. line = "";
  756. for (int i = 2; i < parts.Length; i++)
  757. {
  758. line = line + parts[i].Trim();
  759. line = line + " ";
  760. }
  761. }
  762. }
  763. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref PointF[] points)
  764. {
  765. line = line.Remove(0, startLength);
  766. string[] parts = line.Split(partsDelimiter);
  767. if (parts.Length > 1 && parts.Length % 2 == 0)
  768. {
  769. points = new PointF[parts.Length / 2];
  770. for (int i = 0; i < parts.Length; i = i + 2)
  771. {
  772. string xVal = parts[i].Trim();
  773. string yVal = parts[i+1].Trim();
  774. float x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  775. float y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  776. PointF point = new PointF(x, y);
  777. points[i / 2] = point;
  778. // m_log.DebugFormat("[VECTOR RENDER MODULE]: Got point {0}", points[i / 2]);
  779. }
  780. }
  781. }
  782. private Bitmap ImageHttpRequest(string url)
  783. {
  784. try
  785. {
  786. WebRequest request = HttpWebRequest.Create(url);
  787. using (HttpWebResponse response = (HttpWebResponse)(request).GetResponse())
  788. {
  789. if (response.StatusCode == HttpStatusCode.OK)
  790. {
  791. using (Stream s = response.GetResponseStream())
  792. {
  793. Bitmap image = new Bitmap(s);
  794. return image;
  795. }
  796. }
  797. }
  798. }
  799. catch { }
  800. return null;
  801. }
  802. }
  803. }