Svg.php 7.9 KB

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