/wp-content/plugins/dk-pdf/vendor/mpdf/mpdf/src/Tag.php

https://github.com/livinglab/openlab · PHP · 249 lines · 164 code · 24 blank · 61 comment · 111 complexity · 298568c8a8255bf88159aaeef1568795 MD5 · raw file

  1. <?php
  2. namespace Mpdf;
  3. use Mpdf\Color\ColorConverter;
  4. use Mpdf\Image\ImageProcessor;
  5. use Mpdf\Language\LanguageToFontInterface;
  6. class Tag
  7. {
  8. /**
  9. * @var \Mpdf\Mpdf
  10. */
  11. private $mpdf;
  12. /**
  13. * @var \Mpdf\Cache
  14. */
  15. private $cache;
  16. /**
  17. * @var \Mpdf\CssManager
  18. */
  19. private $cssManager;
  20. /**
  21. * @var \Mpdf\Form
  22. */
  23. private $form;
  24. /**
  25. * @var \Mpdf\Otl
  26. */
  27. private $otl;
  28. /**
  29. * @var \Mpdf\TableOfContents
  30. */
  31. private $tableOfContents;
  32. /**
  33. * @var \Mpdf\SizeConverter
  34. */
  35. private $sizeConverter;
  36. /**
  37. * @var \Mpdf\Color\ColorConverter
  38. */
  39. private $colorConverter;
  40. /**
  41. * @var \Mpdf\Image\ImageProcessor
  42. */
  43. private $imageProcessor;
  44. /**
  45. * @var \Mpdf\Language\LanguageToFontInterface
  46. */
  47. private $languageToFont;
  48. /**
  49. * @param \Mpdf\Mpdf $mpdf
  50. * @param \Mpdf\Cache $cache
  51. * @param \Mpdf\CssManager $cssManager
  52. * @param \Mpdf\Form $form
  53. * @param \Mpdf\Otl $otl
  54. * @param \Mpdf\TableOfContents $tableOfContents
  55. * @param \Mpdf\SizeConverter $sizeConverter
  56. * @param \Mpdf\Color\ColorConverter $colorConverter
  57. * @param \Mpdf\Image\ImageProcessor $imageProcessor
  58. * @param \Mpdf\Language\LanguageToFontInterface $languageToFont
  59. */
  60. public function __construct(
  61. Mpdf $mpdf,
  62. Cache $cache,
  63. CssManager $cssManager,
  64. Form $form,
  65. Otl $otl,
  66. TableOfContents $tableOfContents,
  67. SizeConverter $sizeConverter,
  68. ColorConverter $colorConverter,
  69. ImageProcessor $imageProcessor,
  70. LanguageToFontInterface $languageToFont
  71. ) {
  72. $this->mpdf = $mpdf;
  73. $this->cache = $cache;
  74. $this->cssManager = $cssManager;
  75. $this->form = $form;
  76. $this->otl = $otl;
  77. $this->tableOfContents = $tableOfContents;
  78. $this->sizeConverter = $sizeConverter;
  79. $this->colorConverter = $colorConverter;
  80. $this->imageProcessor = $imageProcessor;
  81. $this->languageToFont = $languageToFont;
  82. }
  83. /**
  84. * @param string $tag The tag name
  85. * @return \Mpdf\Tag\Tag
  86. */
  87. private function getTagInstance($tag)
  88. {
  89. $className = self::getTagClassName($tag);
  90. if (class_exists($className)) {
  91. return new $className(
  92. $this->mpdf,
  93. $this->cache,
  94. $this->cssManager,
  95. $this->form,
  96. $this->otl,
  97. $this->tableOfContents,
  98. $this->sizeConverter,
  99. $this->colorConverter,
  100. $this->imageProcessor,
  101. $this->languageToFont
  102. );
  103. }
  104. }
  105. /**
  106. * Returns the fully qualified name of the class handling the rendering of the given tag
  107. *
  108. * @param string $tag The tag name
  109. * @return string The fully qualified name
  110. */
  111. public static function getTagClassName($tag)
  112. {
  113. static $map = [
  114. 'BARCODE' => 'BarCode',
  115. 'BLOCKQUOTE' => 'BlockQuote',
  116. 'COLUMN_BREAK' => 'ColumnBreak',
  117. 'COLUMNBREAK' => 'ColumnBreak',
  118. 'DOTTAB' => 'DotTab',
  119. 'FIELDSET' => 'FieldSet',
  120. 'FIGCAPTION' => 'FigCaption',
  121. 'FORMFEED' => 'FormFeed',
  122. 'HGROUP' => 'HGroup',
  123. 'INDEXENTRY' => 'IndexEntry',
  124. 'INDEXINSET' => 'IndexInsert',
  125. 'NEWCOLUMN' => 'NewColumn',
  126. 'NEWPAGE' => 'NewPage',
  127. 'PAGEFOOTER' => 'PageFooter',
  128. 'PAGEHEADER' => 'PageHeader',
  129. 'PAGE_BREAK' => 'PageBreak',
  130. 'PAGEBREAK' => 'PageBreak',
  131. 'SETHTMLPAGEFOOTER' => 'SetHtmlPageFooter',
  132. 'SETHTMLPAGEHEADER' => 'SetHtmlPageHeader',
  133. 'SETPAGEFOOTER' => 'SetPageFooter',
  134. 'SETPAGEHEADER' => 'SetPageHeader',
  135. 'TBODY' => 'TBody',
  136. 'TFOOT' => 'TFoot',
  137. 'THEAD' => 'THead',
  138. 'TEXTAREA' => 'TextArea',
  139. 'TEXTCIRCLE' => 'TextCircle',
  140. 'TOCENTRY' => 'TocEntry',
  141. 'TOCPAGEBREAK' => 'TocPageBreak',
  142. 'VAR' => 'VarTag',
  143. 'WATERMARKIMAGE' => 'WatermarkImage',
  144. 'WATERMARKTEXT' => 'WatermarkText',
  145. ];
  146. $className = 'Mpdf\Tag\\';
  147. $className .= isset($map[$tag]) ? $map[$tag] : ucfirst(strtolower($tag));
  148. return $className;
  149. }
  150. public function OpenTag($tag, $attr, &$ahtml, &$ihtml)
  151. {
  152. // Correct for tags where HTML5 specifies optional end tags excluding table elements (cf WriteHTML() )
  153. if ($this->mpdf->allow_html_optional_endtags) {
  154. if (isset($this->mpdf->blk[$this->mpdf->blklvl]['tag'])) {
  155. $closed = false;
  156. // li end tag may be omitted if immediately followed by another li element
  157. if (!$closed && $this->mpdf->blk[$this->mpdf->blklvl]['tag'] == 'LI' && $tag == 'LI') {
  158. $this->CloseTag('LI', $ahtml, $ihtml);
  159. $closed = true;
  160. }
  161. // dt end tag may be omitted if immediately followed by another dt element or a dd element
  162. if (!$closed && $this->mpdf->blk[$this->mpdf->blklvl]['tag'] == 'DT' && ($tag == 'DT' || $tag == 'DD')) {
  163. $this->CloseTag('DT', $ahtml, $ihtml);
  164. $closed = true;
  165. }
  166. // dd end tag may be omitted if immediately followed by another dd element or a dt element
  167. if (!$closed && $this->mpdf->blk[$this->mpdf->blklvl]['tag'] == 'DD' && ($tag == 'DT' || $tag == 'DD')) {
  168. $this->CloseTag('DD', $ahtml, $ihtml);
  169. $closed = true;
  170. }
  171. // p end tag may be omitted if immediately followed by an address, article, aside, blockquote, div, dl,
  172. // fieldset, form, h1, h2, h3, h4, h5, h6, hgroup, hr, main, nav, ol, p, pre, section, table, ul
  173. if (!$closed && $this->mpdf->blk[$this->mpdf->blklvl]['tag'] == 'P'
  174. && ($tag == 'P' || $tag == 'DIV' || $tag == 'H1' || $tag == 'H2' || $tag == 'H3'
  175. || $tag == 'H4' || $tag == 'H5' || $tag == 'H6' || $tag == 'UL' || $tag == 'OL'
  176. || $tag == 'TABLE' || $tag == 'PRE' || $tag == 'FORM' || $tag == 'ADDRESS' || $tag == 'BLOCKQUOTE'
  177. || $tag == 'CENTER' || $tag == 'DL' || $tag == 'HR' || $tag == 'ARTICLE' || $tag == 'ASIDE'
  178. || $tag == 'FIELDSET' || $tag == 'HGROUP' || $tag == 'MAIN' || $tag == 'NAV' || $tag == 'SECTION')) {
  179. $this->CloseTag('P', $ahtml, $ihtml);
  180. $closed = true;
  181. }
  182. // option end tag may be omitted if immediately followed by another option element
  183. // (or if it is immediately followed by an optgroup element)
  184. if (!$closed && $this->mpdf->blk[$this->mpdf->blklvl]['tag'] == 'OPTION' && $tag == 'OPTION') {
  185. $this->CloseTag('OPTION', $ahtml, $ihtml);
  186. $closed = true;
  187. }
  188. // Table elements - see also WriteHTML()
  189. if (!$closed && ($tag == 'TD' || $tag == 'TH') && $this->mpdf->lastoptionaltag == 'TD') {
  190. $this->CloseTag($this->mpdf->lastoptionaltag, $ahtml, $ihtml);
  191. $closed = true;
  192. } // *TABLES*
  193. if (!$closed && ($tag == 'TD' || $tag == 'TH') && $this->mpdf->lastoptionaltag == 'TH') {
  194. $this->CloseTag($this->mpdf->lastoptionaltag, $ahtml, $ihtml);
  195. $closed = true;
  196. } // *TABLES*
  197. if (!$closed && $tag == 'TR' && $this->mpdf->lastoptionaltag == 'TR') {
  198. $this->CloseTag($this->mpdf->lastoptionaltag, $ahtml, $ihtml);
  199. $closed = true;
  200. } // *TABLES*
  201. if (!$closed && $tag == 'TR' && $this->mpdf->lastoptionaltag == 'TD') {
  202. $this->CloseTag($this->mpdf->lastoptionaltag, $ahtml, $ihtml);
  203. $this->CloseTag('TR', $ahtml, $ihtml);
  204. $this->CloseTag('THEAD', $ahtml, $ihtml);
  205. $closed = true;
  206. } // *TABLES*
  207. if (!$closed && $tag == 'TR' && $this->mpdf->lastoptionaltag == 'TH') {
  208. $this->CloseTag($this->mpdf->lastoptionaltag, $ahtml, $ihtml);
  209. $this->CloseTag('TR', $ahtml, $ihtml);
  210. $this->CloseTag('THEAD', $ahtml, $ihtml);
  211. $closed = true;
  212. } // *TABLES*
  213. }
  214. }
  215. if ($object = $this->getTagInstance($tag)) {
  216. return $object->open($attr, $ahtml, $ihtml);
  217. }
  218. }
  219. public function CloseTag($tag, &$ahtml, &$ihtml)
  220. {
  221. if ($object = $this->getTagInstance($tag)) {
  222. return $object->close($ahtml, $ihtml);
  223. }
  224. }
  225. }