VectorRenderModule.cs 34 KB

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