Svg.php 7.8 KB

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