Svg.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\Response;
  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;
  28. /** @var string */
  29. public $font;
  30. /** @var int */
  31. public $fontSize;
  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(
  53. 'svg',
  54. '-//W3C//DTD SVG 1.1//EN',
  55. 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
  56. );
  57. $this->endDtd();
  58. }
  59. /**
  60. * Set document title
  61. *
  62. * @param string $value sets the title text
  63. *
  64. * @return void
  65. */
  66. public function setTitle($value)
  67. {
  68. $this->title = $value;
  69. }
  70. /**
  71. * Set document author
  72. *
  73. * @param string $value sets the author
  74. *
  75. * @return void
  76. */
  77. public function setAuthor($value)
  78. {
  79. $this->author = $value;
  80. }
  81. /**
  82. * Set document font
  83. *
  84. * @param string $value sets the font e.g Arial, Sans-serif etc
  85. *
  86. * @return void
  87. */
  88. public function setFont($value)
  89. {
  90. $this->font = $value;
  91. }
  92. /**
  93. * Get document font
  94. *
  95. * @return string returns the font name
  96. */
  97. public function getFont()
  98. {
  99. return $this->font;
  100. }
  101. /**
  102. * Set document font size
  103. *
  104. * @param int $value sets the font size in pixels
  105. *
  106. * @return void
  107. */
  108. public function setFontSize($value)
  109. {
  110. $this->fontSize = $value;
  111. }
  112. /**
  113. * Get document font size
  114. *
  115. * @return int returns the font size
  116. */
  117. public function getFontSize()
  118. {
  119. return $this->fontSize;
  120. }
  121. /**
  122. * Starts RelationStatsSvg Document
  123. *
  124. * svg document starts by first initializing svg tag
  125. * which contains all the attributes and namespace that needed
  126. * to define the svg document
  127. *
  128. * @see XMLWriter::startElement()
  129. * @see XMLWriter::writeAttribute()
  130. *
  131. * @param int $width total width of the RelationStatsSvg document
  132. * @param int $height total height of the RelationStatsSvg document
  133. * @param int $x min-x of the view box
  134. * @param int $y min-y of the view box
  135. *
  136. * @return void
  137. */
  138. public function startSvgDoc($width, $height, $x = 0, $y = 0)
  139. {
  140. $this->startElement('svg');
  141. if (! is_int($width)) {
  142. $width = intval($width);
  143. }
  144. if (! is_int($height)) {
  145. $height = intval($height);
  146. }
  147. if ($x != 0 || $y != 0) {
  148. $this->writeAttribute('viewBox', sprintf('%d %d %d %d', $x, $y, $width, $height));
  149. }
  150. $this->writeAttribute('width', ($width - $x) . 'px');
  151. $this->writeAttribute('height', ($height - $y) . 'px');
  152. $this->writeAttribute('xmlns', 'http://www.w3.org/2000/svg');
  153. $this->writeAttribute('version', '1.1');
  154. }
  155. /**
  156. * Ends RelationStatsSvg Document
  157. *
  158. * @see XMLWriter::endElement()
  159. * @see XMLWriter::endDocument()
  160. *
  161. * @return void
  162. */
  163. public function endSvgDoc()
  164. {
  165. $this->endElement();
  166. $this->endDocument();
  167. }
  168. /**
  169. * output RelationStatsSvg Document
  170. *
  171. * svg document prompted to the user for download
  172. * RelationStatsSvg document saved in .svg extension and can be
  173. * easily changeable by using any svg IDE
  174. *
  175. * @see XMLWriter::startElement()
  176. * @see XMLWriter::writeAttribute()
  177. *
  178. * @param string $fileName file name
  179. *
  180. * @return void
  181. */
  182. public function showOutput($fileName)
  183. {
  184. //ob_get_clean();
  185. $output = $this->flush();
  186. Response::getInstance()->disable();
  187. Core::downloadHeader(
  188. $fileName,
  189. 'image/svg+xml',
  190. strlen($output)
  191. );
  192. print $output;
  193. }
  194. /**
  195. * Draws RelationStatsSvg elements
  196. *
  197. * SVG has some predefined shape elements like rectangle & text
  198. * and other elements who have x,y co-ordinates are drawn.
  199. * specify their width and height and can give styles too.
  200. *
  201. * @see XMLWriter::startElement()
  202. * @see XMLWriter::writeAttribute()
  203. * @see XMLWriter::text()
  204. * @see XMLWriter::endElement()
  205. *
  206. * @param string $name RelationStatsSvg element name
  207. * @param int $x The x attr defines the left position of the element
  208. * (e.g. x="0" places the element 0 pixels from the
  209. * left of the browser window)
  210. * @param int $y The y attribute defines the top position of the
  211. * element (e.g. y="0" places the element 0 pixels
  212. * from the top of the browser window)
  213. * @param int|string $width The width attribute defines the width the element
  214. * @param int|string $height The height attribute defines the height the element
  215. * @param string|null $text The text attribute defines the text the element
  216. * @param string $styles The style attribute defines the style the element
  217. * styles can be defined like CSS styles
  218. *
  219. * @return void
  220. */
  221. public function printElement(
  222. $name,
  223. $x,
  224. $y,
  225. $width = '',
  226. $height = '',
  227. ?string $text = '',
  228. $styles = ''
  229. ) {
  230. $this->startElement($name);
  231. $this->writeAttribute('width', (string) $width);
  232. $this->writeAttribute('height', (string) $height);
  233. $this->writeAttribute('x', (string) $x);
  234. $this->writeAttribute('y', (string) $y);
  235. $this->writeAttribute('style', (string) $styles);
  236. if (isset($text)) {
  237. $this->writeAttribute('font-family', (string) $this->font);
  238. $this->writeAttribute('font-size', $this->fontSize . 'px');
  239. $this->text($text);
  240. }
  241. $this->endElement();
  242. }
  243. /**
  244. * Draws RelationStatsSvg Line element
  245. *
  246. * RelationStatsSvg line element is drawn for connecting the tables.
  247. * arrows are also drawn by specify its start and ending
  248. * co-ordinates
  249. *
  250. * @see XMLWriter::startElement()
  251. * @see XMLWriter::writeAttribute()
  252. * @see XMLWriter::endElement()
  253. *
  254. * @param string $name RelationStatsSvg element name i.e line
  255. * @param int $x1 Defines the start of the line on the x-axis
  256. * @param int $y1 Defines the start of the line on the y-axis
  257. * @param int $x2 Defines the end of the line on the x-axis
  258. * @param int $y2 Defines the end of the line on the y-axis
  259. * @param string $styles The style attribute defines the style the element
  260. * styles can be defined like CSS styles
  261. *
  262. * @return void
  263. */
  264. public function printElementLine($name, $x1, $y1, $x2, $y2, $styles)
  265. {
  266. $this->startElement($name);
  267. $this->writeAttribute('x1', (string) $x1);
  268. $this->writeAttribute('y1', (string) $y1);
  269. $this->writeAttribute('x2', (string) $x2);
  270. $this->writeAttribute('y2', (string) $y2);
  271. $this->writeAttribute('style', (string) $styles);
  272. $this->endElement();
  273. }
  274. }