PageRenderTime 32ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/classes/Plugins/Schema/Svg/Svg.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 279 lines | 115 code | 27 blank | 137 comment | 7 complexity | 468c01d98dc398888cce0f52029880b8 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. <?php
  2. /**
  3. * Classes to create relation schema in SVG format.
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Schema\Svg;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\ResponseRenderer;
  9. use XMLWriter;
  10. use function intval;
  11. use function is_int;
  12. use function sprintf;
  13. use function strlen;
  14. /**
  15. * This Class inherits the XMLwriter class and
  16. * helps in developing structure of SVG Schema Export
  17. *
  18. * @see https://www.php.net/manual/en/book.xmlwriter.php
  19. *
  20. * @access public
  21. */
  22. class Svg extends XMLWriter
  23. {
  24. /** @var string */
  25. public $title = '';
  26. /** @var string */
  27. public $author = 'phpMyAdmin';
  28. /** @var string */
  29. public $font = 'Arial';
  30. /** @var int */
  31. public $fontSize = 12;
  32. /**
  33. * Upon instantiation This starts writing the RelationStatsSvg XML document
  34. *
  35. * @see XMLWriter::openMemory()
  36. * @see XMLWriter::setIndent()
  37. * @see XMLWriter::startDocument()
  38. */
  39. public function __construct()
  40. {
  41. $this->openMemory();
  42. /*
  43. * Set indenting using three spaces,
  44. * so output is formatted
  45. */
  46. $this->setIndent(true);
  47. $this->setIndentString(' ');
  48. /*
  49. * Create the XML document
  50. */
  51. $this->startDocument('1.0', 'UTF-8');
  52. $this->startDtd('svg', '-//W3C//DTD SVG 1.1//EN', 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
  53. $this->endDtd();
  54. }
  55. /**
  56. * Set document title
  57. *
  58. * @param string $value sets the title text
  59. */
  60. public function setTitle($value): void
  61. {
  62. $this->title = $value;
  63. }
  64. /**
  65. * Set document author
  66. *
  67. * @param string $value sets the author
  68. */
  69. public function setAuthor($value): void
  70. {
  71. $this->author = $value;
  72. }
  73. /**
  74. * Set document font
  75. *
  76. * @param string $value sets the font e.g Arial, Sans-serif etc
  77. */
  78. public function setFont(string $value): void
  79. {
  80. $this->font = $value;
  81. }
  82. /**
  83. * Get document font
  84. *
  85. * @return string returns the font name
  86. */
  87. public function getFont(): string
  88. {
  89. return $this->font;
  90. }
  91. /**
  92. * Set document font size
  93. *
  94. * @param int $value sets the font size in pixels
  95. */
  96. public function setFontSize(int $value): void
  97. {
  98. $this->fontSize = $value;
  99. }
  100. /**
  101. * Get document font size
  102. *
  103. * @return int returns the font size
  104. */
  105. public function getFontSize(): int
  106. {
  107. return $this->fontSize;
  108. }
  109. /**
  110. * Starts RelationStatsSvg Document
  111. *
  112. * svg document starts by first initializing svg tag
  113. * which contains all the attributes and namespace that needed
  114. * to define the svg document
  115. *
  116. * @see XMLWriter::startElement()
  117. * @see XMLWriter::writeAttribute()
  118. *
  119. * @param int $width total width of the RelationStatsSvg document
  120. * @param int $height total height of the RelationStatsSvg document
  121. * @param int $x min-x of the view box
  122. * @param int $y min-y of the view box
  123. */
  124. public function startSvgDoc($width, $height, $x = 0, $y = 0): void
  125. {
  126. $this->startElement('svg');
  127. if (! is_int($width)) {
  128. $width = intval($width);
  129. }
  130. if (! is_int($height)) {
  131. $height = intval($height);
  132. }
  133. if ($x != 0 || $y != 0) {
  134. $this->writeAttribute('viewBox', sprintf('%d %d %d %d', $x, $y, $width, $height));
  135. }
  136. $this->writeAttribute('width', ($width - $x) . 'px');
  137. $this->writeAttribute('height', ($height - $y) . 'px');
  138. $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
  139. $this->writeAttribute('version', '1.1');
  140. }
  141. /**
  142. * Ends RelationStatsSvg Document
  143. *
  144. * @see XMLWriter::endElement()
  145. * @see XMLWriter::endDocument()
  146. */
  147. public function endSvgDoc(): void
  148. {
  149. $this->endElement();
  150. $this->endDocument();
  151. }
  152. /**
  153. * output RelationStatsSvg Document
  154. *
  155. * svg document prompted to the user for download
  156. * RelationStatsSvg document saved in .svg extension and can be
  157. * easily changeable by using any svg IDE
  158. *
  159. * @see XMLWriter::startElement()
  160. * @see XMLWriter::writeAttribute()
  161. *
  162. * @param string $fileName file name
  163. */
  164. public function showOutput($fileName): void
  165. {
  166. //ob_get_clean();
  167. $output = $this->flush();
  168. ResponseRenderer::getInstance()->disable();
  169. Core::downloadHeader(
  170. $fileName,
  171. 'image/svg+xml',
  172. strlen($output)
  173. );
  174. print $output;
  175. }
  176. /**
  177. * Draws RelationStatsSvg elements
  178. *
  179. * SVG has some predefined shape elements like rectangle & text
  180. * and other elements who have x,y co-ordinates are drawn.
  181. * specify their width and height and can give styles too.
  182. *
  183. * @see XMLWriter::startElement()
  184. * @see XMLWriter::writeAttribute()
  185. * @see XMLWriter::text()
  186. * @see XMLWriter::endElement()
  187. *
  188. * @param string $name RelationStatsSvg element name
  189. * @param int $x The x attr defines the left position of the element
  190. * (e.g. x="0" places the element 0 pixels from the
  191. * left of the browser window)
  192. * @param int $y The y attribute defines the top position of the
  193. * element (e.g. y="0" places the element 0 pixels
  194. * from the top of the browser window)
  195. * @param int|string $width The width attribute defines the width the element
  196. * @param int|string $height The height attribute defines the height the element
  197. * @param string|null $text The text attribute defines the text the element
  198. * @param string $styles The style attribute defines the style the element
  199. * styles can be defined like CSS styles
  200. */
  201. public function printElement(
  202. $name,
  203. $x,
  204. $y,
  205. $width = '',
  206. $height = '',
  207. ?string $text = '',
  208. $styles = ''
  209. ): void {
  210. $this->startElement($name);
  211. $this->writeAttribute('width', (string) $width);
  212. $this->writeAttribute('height', (string) $height);
  213. $this->writeAttribute('x', (string) $x);
  214. $this->writeAttribute('y', (string) $y);
  215. $this->writeAttribute('style', (string) $styles);
  216. if (isset($text)) {
  217. $this->writeAttribute('font-family', (string) $this->font);
  218. $this->writeAttribute('font-size', $this->fontSize . 'px');
  219. $this->text($text);
  220. }
  221. $this->endElement();
  222. }
  223. /**
  224. * Draws RelationStatsSvg Line element
  225. *
  226. * RelationStatsSvg line element is drawn for connecting the tables.
  227. * arrows are also drawn by specify its start and ending
  228. * co-ordinates
  229. *
  230. * @see XMLWriter::startElement()
  231. * @see XMLWriter::writeAttribute()
  232. * @see XMLWriter::endElement()
  233. *
  234. * @param string $name RelationStatsSvg element name i.e line
  235. * @param int $x1 Defines the start of the line on the x-axis
  236. * @param int $y1 Defines the start of the line on the y-axis
  237. * @param int $x2 Defines the end of the line on the x-axis
  238. * @param int $y2 Defines the end of the line on the y-axis
  239. * @param string $styles The style attribute defines the style the element
  240. * styles can be defined like CSS styles
  241. */
  242. public function printElementLine($name, $x1, $y1, $x2, $y2, $styles): void
  243. {
  244. $this->startElement($name);
  245. $this->writeAttribute('x1', (string) $x1);
  246. $this->writeAttribute('y1', (string) $y1);
  247. $this->writeAttribute('x2', (string) $x2);
  248. $this->writeAttribute('y2', (string) $y2);
  249. $this->writeAttribute('style', (string) $styles);
  250. $this->endElement();
  251. }
  252. }