VectorRenderModule.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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)
  315. bitmap = new Bitmap(width, height, PixelFormat.Format32bppRgb);
  316. else
  317. bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
  318. graph = Graphics.FromImage(bitmap);
  319. // this is really just to save people filling the
  320. // background color in their scripts, only do when fully opaque
  321. if (alpha >= 255)
  322. {
  323. using (SolidBrush bgFillBrush = new SolidBrush(bgColor))
  324. {
  325. graph.FillRectangle(bgFillBrush, 0, 0, width, height);
  326. }
  327. }
  328. for (int w = 0; w < bitmap.Width; w++)
  329. {
  330. if (alpha <= 255)
  331. {
  332. for (int h = 0; h < bitmap.Height; h++)
  333. {
  334. bitmap.SetPixel(w, h, Color.FromArgb(alpha, bitmap.GetPixel(w, h)));
  335. }
  336. }
  337. }
  338. GDIDraw(data, graph, altDataDelim, out reuseable);
  339. }
  340. byte[] imageJ2000 = new byte[0];
  341. // This code exists for testing purposes, please do not remove.
  342. // if (s_flipper)
  343. // imageJ2000 = s_asset1Data;
  344. // else
  345. // imageJ2000 = s_asset2Data;
  346. //
  347. // s_flipper = !s_flipper;
  348. try
  349. {
  350. imageJ2000 = OpenJPEG.EncodeFromImage(bitmap, true);
  351. }
  352. catch (Exception e)
  353. {
  354. m_log.ErrorFormat(
  355. "[VECTORRENDERMODULE]: OpenJpeg Encode Failed. Exception {0}{1}",
  356. e.Message, e.StackTrace);
  357. }
  358. return new OpenSim.Region.CoreModules.Scripting.DynamicTexture.DynamicTexture(
  359. data, extraParams, imageJ2000, new Size(width, height), reuseable);
  360. }
  361. finally
  362. {
  363. // XXX: In testing, it appears that if multiple threads dispose of separate GDI+ objects simultaneously,
  364. // the native malloc heap can become corrupted, possibly due to a double free(). This may be due to
  365. // bugs in the underlying libcairo used by mono's libgdiplus.dll on Linux/OSX. These problems were
  366. // seen with both libcario 1.10.2-6.1ubuntu3 and 1.8.10-2ubuntu1. They go away if disposal is perfomed
  367. // under lock.
  368. lock (this)
  369. {
  370. if (graph != null)
  371. graph.Dispose();
  372. if (bitmap != null)
  373. bitmap.Dispose();
  374. }
  375. }
  376. }
  377. private int parseIntParam(string strInt)
  378. {
  379. int parsed;
  380. try
  381. {
  382. parsed = Convert.ToInt32(strInt);
  383. }
  384. catch (Exception)
  385. {
  386. //Ckrinke: Add a WriteLine to remove the warning about 'e' defined but not used
  387. // m_log.Debug("Problem with Draw. Please verify parameters." + e.ToString());
  388. parsed = -1;
  389. }
  390. return parsed;
  391. }
  392. /*
  393. private void CairoDraw(string data, System.Drawing.Graphics graph)
  394. {
  395. using (Win32Surface draw = new Win32Surface(graph.GetHdc()))
  396. {
  397. Context contex = new Context(draw);
  398. contex.Antialias = Antialias.None; //fastest method but low quality
  399. contex.LineWidth = 7;
  400. char[] lineDelimiter = { ';' };
  401. char[] partsDelimiter = { ',' };
  402. string[] lines = data.Split(lineDelimiter);
  403. foreach (string line in lines)
  404. {
  405. string nextLine = line.Trim();
  406. if (nextLine.StartsWith("MoveTO"))
  407. {
  408. float x = 0;
  409. float y = 0;
  410. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  411. contex.MoveTo(x, y);
  412. }
  413. else if (nextLine.StartsWith("LineTo"))
  414. {
  415. float x = 0;
  416. float y = 0;
  417. GetParams(partsDelimiter, ref nextLine, ref x, ref y);
  418. contex.LineTo(x, y);
  419. contex.Stroke();
  420. }
  421. }
  422. }
  423. graph.ReleaseHdc();
  424. }
  425. */
  426. /// <summary>
  427. /// Split input data into discrete command lines.
  428. /// </summary>
  429. /// <returns></returns>
  430. /// <param name='data'></param>
  431. /// <param name='dataDelim'></param>
  432. private string[] GetLines(string data, char dataDelim)
  433. {
  434. char[] lineDelimiter = { dataDelim };
  435. return data.Split(lineDelimiter);
  436. }
  437. private void GDIDraw(string data, Graphics graph, char dataDelim, out bool reuseable)
  438. {
  439. reuseable = true;
  440. Point startPoint = new Point(0, 0);
  441. Point endPoint = new Point(0, 0);
  442. Pen drawPen = null;
  443. Font myFont = null;
  444. SolidBrush myBrush = null;
  445. try
  446. {
  447. drawPen = new Pen(Color.Black, 7);
  448. string fontName = m_fontName;
  449. float fontSize = 14;
  450. myFont = new Font(fontName, fontSize);
  451. myBrush = new SolidBrush(Color.Black);
  452. char[] partsDelimiter = {','};
  453. foreach (string line in GetLines(data, dataDelim))
  454. {
  455. string nextLine = line.Trim();
  456. // m_log.DebugFormat("[VECTOR RENDER MODULE]: Processing line '{0}'", nextLine);
  457. //replace with switch, or even better, do some proper parsing
  458. if (nextLine.StartsWith("MoveTo"))
  459. {
  460. float x = 0;
  461. float y = 0;
  462. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  463. startPoint.X = (int) x;
  464. startPoint.Y = (int) y;
  465. }
  466. else if (nextLine.StartsWith("LineTo"))
  467. {
  468. float x = 0;
  469. float y = 0;
  470. GetParams(partsDelimiter, ref nextLine, 6, ref x, ref y);
  471. endPoint.X = (int) x;
  472. endPoint.Y = (int) y;
  473. graph.DrawLine(drawPen, startPoint, endPoint);
  474. startPoint.X = endPoint.X;
  475. startPoint.Y = endPoint.Y;
  476. }
  477. else if (nextLine.StartsWith("Text"))
  478. {
  479. nextLine = nextLine.Remove(0, 4);
  480. nextLine = nextLine.Trim();
  481. graph.DrawString(nextLine, myFont, myBrush, startPoint);
  482. }
  483. else if (nextLine.StartsWith("Image"))
  484. {
  485. // We cannot reuse any generated texture involving fetching an image via HTTP since that image
  486. // can change.
  487. reuseable = false;
  488. float x = 0;
  489. float y = 0;
  490. GetParams(partsDelimiter, ref nextLine, 5, ref x, ref y);
  491. endPoint.X = (int) x;
  492. endPoint.Y = (int) y;
  493. using (Image image = ImageHttpRequest(nextLine))
  494. {
  495. if (image != null)
  496. {
  497. graph.DrawImage(image, (float)startPoint.X, (float)startPoint.Y, x, y);
  498. }
  499. else
  500. {
  501. using (Font errorFont = new Font(m_fontName,6))
  502. {
  503. graph.DrawString("URL couldn't be resolved or is", errorFont,
  504. myBrush, startPoint);
  505. graph.DrawString("not an image. Please check URL.", errorFont,
  506. myBrush, new Point(startPoint.X, 12 + startPoint.Y));
  507. }
  508. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  509. }
  510. }
  511. startPoint.X += endPoint.X;
  512. startPoint.Y += endPoint.Y;
  513. }
  514. else if (nextLine.StartsWith("Rectangle"))
  515. {
  516. float x = 0;
  517. float y = 0;
  518. GetParams(partsDelimiter, ref nextLine, 9, ref x, ref y);
  519. endPoint.X = (int) x;
  520. endPoint.Y = (int) y;
  521. graph.DrawRectangle(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  522. startPoint.X += endPoint.X;
  523. startPoint.Y += endPoint.Y;
  524. }
  525. else if (nextLine.StartsWith("FillRectangle"))
  526. {
  527. float x = 0;
  528. float y = 0;
  529. GetParams(partsDelimiter, ref nextLine, 13, ref x, ref y);
  530. endPoint.X = (int) x;
  531. endPoint.Y = (int) y;
  532. graph.FillRectangle(myBrush, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  533. startPoint.X += endPoint.X;
  534. startPoint.Y += endPoint.Y;
  535. }
  536. else if (nextLine.StartsWith("FillPolygon"))
  537. {
  538. PointF[] points = null;
  539. GetParams(partsDelimiter, ref nextLine, 11, ref points);
  540. graph.FillPolygon(myBrush, points);
  541. }
  542. else if (nextLine.StartsWith("Polygon"))
  543. {
  544. PointF[] points = null;
  545. GetParams(partsDelimiter, ref nextLine, 7, ref points);
  546. graph.DrawPolygon(drawPen, points);
  547. }
  548. else if (nextLine.StartsWith("Ellipse"))
  549. {
  550. float x = 0;
  551. float y = 0;
  552. GetParams(partsDelimiter, ref nextLine, 7, ref x, ref y);
  553. endPoint.X = (int)x;
  554. endPoint.Y = (int)y;
  555. graph.DrawEllipse(drawPen, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);
  556. startPoint.X += endPoint.X;
  557. startPoint.Y += endPoint.Y;
  558. }
  559. else if (nextLine.StartsWith("FontSize"))
  560. {
  561. nextLine = nextLine.Remove(0, 8);
  562. nextLine = nextLine.Trim();
  563. fontSize = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  564. myFont.Dispose();
  565. myFont = new Font(fontName, fontSize);
  566. }
  567. else if (nextLine.StartsWith("FontProp"))
  568. {
  569. nextLine = nextLine.Remove(0, 8);
  570. nextLine = nextLine.Trim();
  571. string[] fprops = nextLine.Split(partsDelimiter);
  572. foreach (string prop in fprops)
  573. {
  574. switch (prop)
  575. {
  576. case "B":
  577. if (!(myFont.Bold))
  578. {
  579. Font newFont = new Font(myFont, myFont.Style | FontStyle.Bold);
  580. myFont.Dispose();
  581. myFont = newFont;
  582. }
  583. break;
  584. case "I":
  585. if (!(myFont.Italic))
  586. {
  587. Font newFont = new Font(myFont, myFont.Style | FontStyle.Italic);
  588. myFont.Dispose();
  589. myFont = newFont;
  590. }
  591. break;
  592. case "U":
  593. if (!(myFont.Underline))
  594. {
  595. Font newFont = new Font(myFont, myFont.Style | FontStyle.Underline);
  596. myFont.Dispose();
  597. myFont = newFont;
  598. }
  599. break;
  600. case "S":
  601. if (!(myFont.Strikeout))
  602. {
  603. Font newFont = new Font(myFont, myFont.Style | FontStyle.Strikeout);
  604. myFont.Dispose();
  605. myFont = newFont;
  606. }
  607. break;
  608. case "R":
  609. // We need to place this newFont inside its own context so that the .NET compiler
  610. // doesn't complain about a redefinition of an existing newFont, even though there is none
  611. // The mono compiler doesn't produce this error.
  612. {
  613. Font newFont = new Font(myFont, FontStyle.Regular);
  614. myFont.Dispose();
  615. myFont = newFont;
  616. }
  617. break;
  618. }
  619. }
  620. }
  621. else if (nextLine.StartsWith("FontName"))
  622. {
  623. nextLine = nextLine.Remove(0, 8);
  624. fontName = nextLine.Trim();
  625. myFont.Dispose();
  626. myFont = new Font(fontName, fontSize);
  627. }
  628. else if (nextLine.StartsWith("PenSize"))
  629. {
  630. nextLine = nextLine.Remove(0, 7);
  631. nextLine = nextLine.Trim();
  632. float size = Convert.ToSingle(nextLine, CultureInfo.InvariantCulture);
  633. drawPen.Width = size;
  634. }
  635. else if (nextLine.StartsWith("PenCap"))
  636. {
  637. bool start = true, end = true;
  638. nextLine = nextLine.Remove(0, 6);
  639. nextLine = nextLine.Trim();
  640. string[] cap = nextLine.Split(partsDelimiter);
  641. if (cap[0].ToLower() == "start")
  642. end = false;
  643. else if (cap[0].ToLower() == "end")
  644. start = false;
  645. else if (cap[0].ToLower() != "both")
  646. return;
  647. string type = cap[1].ToLower();
  648. if (end)
  649. {
  650. switch (type)
  651. {
  652. case "arrow":
  653. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  654. break;
  655. case "round":
  656. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
  657. break;
  658. case "diamond":
  659. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
  660. break;
  661. case "flat":
  662. drawPen.EndCap = System.Drawing.Drawing2D.LineCap.Flat;
  663. break;
  664. }
  665. }
  666. if (start)
  667. {
  668. switch (type)
  669. {
  670. case "arrow":
  671. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
  672. break;
  673. case "round":
  674. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
  675. break;
  676. case "diamond":
  677. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
  678. break;
  679. case "flat":
  680. drawPen.StartCap = System.Drawing.Drawing2D.LineCap.Flat;
  681. break;
  682. }
  683. }
  684. }
  685. else if (nextLine.StartsWith("PenColour") || nextLine.StartsWith("PenColor"))
  686. {
  687. nextLine = nextLine.Remove(0, 9);
  688. nextLine = nextLine.Trim();
  689. int hex = 0;
  690. Color newColor;
  691. if (Int32.TryParse(nextLine, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hex))
  692. {
  693. newColor = Color.FromArgb(hex);
  694. }
  695. else
  696. {
  697. // this doesn't fail, it just returns black if nothing is found
  698. newColor = Color.FromName(nextLine);
  699. }
  700. myBrush.Color = newColor;
  701. drawPen.Color = newColor;
  702. }
  703. }
  704. }
  705. finally
  706. {
  707. if (drawPen != null)
  708. drawPen.Dispose();
  709. if (myFont != null)
  710. myFont.Dispose();
  711. if (myBrush != null)
  712. myBrush.Dispose();
  713. }
  714. }
  715. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref float x, ref float y)
  716. {
  717. line = line.Remove(0, startLength);
  718. string[] parts = line.Split(partsDelimiter);
  719. if (parts.Length == 2)
  720. {
  721. string xVal = parts[0].Trim();
  722. string yVal = parts[1].Trim();
  723. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  724. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  725. }
  726. else if (parts.Length > 2)
  727. {
  728. string xVal = parts[0].Trim();
  729. string yVal = parts[1].Trim();
  730. x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  731. y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  732. line = "";
  733. for (int i = 2; i < parts.Length; i++)
  734. {
  735. line = line + parts[i].Trim();
  736. line = line + " ";
  737. }
  738. }
  739. }
  740. private static void GetParams(char[] partsDelimiter, ref string line, int startLength, ref PointF[] points)
  741. {
  742. line = line.Remove(0, startLength);
  743. string[] parts = line.Split(partsDelimiter);
  744. if (parts.Length > 1 && parts.Length % 2 == 0)
  745. {
  746. points = new PointF[parts.Length / 2];
  747. for (int i = 0; i < parts.Length; i = i + 2)
  748. {
  749. string xVal = parts[i].Trim();
  750. string yVal = parts[i+1].Trim();
  751. float x = Convert.ToSingle(xVal, CultureInfo.InvariantCulture);
  752. float y = Convert.ToSingle(yVal, CultureInfo.InvariantCulture);
  753. PointF point = new PointF(x, y);
  754. points[i / 2] = point;
  755. // m_log.DebugFormat("[VECTOR RENDER MODULE]: Got point {0}", points[i / 2]);
  756. }
  757. }
  758. }
  759. private Bitmap ImageHttpRequest(string url)
  760. {
  761. try
  762. {
  763. WebRequest request = HttpWebRequest.Create(url);
  764. using (HttpWebResponse response = (HttpWebResponse)(request).GetResponse())
  765. {
  766. if (response.StatusCode == HttpStatusCode.OK)
  767. {
  768. using (Stream s = response.GetResponseStream())
  769. {
  770. Bitmap image = new Bitmap(s);
  771. return image;
  772. }
  773. }
  774. }
  775. }
  776. catch { }
  777. return null;
  778. }
  779. }
  780. }