/framework/experimental/graphic/GraphicParser.php

http://zoop.googlecode.com/ · PHP · 147 lines · 110 code · 19 blank · 18 comment · 34 complexity · 9ee8d9d00eac39937c8627fb98ac0349 MD5 · raw file

  1. <?php
  2. class GraphicParser extends XmlParser
  3. {
  4. private $context;
  5. private $pageSize;
  6. private $pageOrientation;
  7. private $documentSize = PdfContext::sizeA4;
  8. function __construct($contextType)
  9. {
  10. parent::__construct();
  11. switch($contextType)
  12. {
  13. case 'ZendPdf':
  14. $this->context = new ZendPdfContext();
  15. break;
  16. default:
  17. trigger_error("unhandled context type specified: $contextType");
  18. break;
  19. }
  20. }
  21. function parseText($xml)
  22. {
  23. $document = parent::parseText($xml);
  24. $document->getContext()->init($this->documentSize);
  25. $document->setMaxWidth($document->getContext()->getWidth());
  26. return $document;
  27. }
  28. protected function initRootContainer()
  29. {
  30. return new GraphicDocument($this->context);
  31. }
  32. protected function initExtra()
  33. {
  34. return new GraphicStyleStack();
  35. }
  36. function handleTextNode($node, $container, $styleStack)
  37. {
  38. $textRun = $container->getNewTextRun();
  39. echo_r($node->getTextContent());
  40. $textRun->setTextInfo($node->getTextContent());
  41. // echo $textRun;
  42. $textRun->setStyle($styleStack->getTopStyle());
  43. // echo 'handleTextNode<br>';
  44. // $this->container->drawRenderTree();
  45. // echo_r($container);
  46. // echo_r($node->getTextContent());
  47. // echo_r($extra);
  48. // die();
  49. }
  50. protected function preNodeHandler($node, $container, $styleStack)
  51. {
  52. $topStyle = $styleStack->cloneTop();
  53. $this->applyStyleInfo($node, $topStyle);
  54. }
  55. protected function postNodeHandler($node, $container, $styleStack)
  56. {
  57. $styleStack->pop();
  58. }
  59. private function applyStyleInfo($node, $style)
  60. {
  61. if($node->hasAttribute('style'))
  62. {
  63. $styleText = $node->getAttribute('style');
  64. $styleAttributes = explode(';', $style);
  65. $styleInfo = array();
  66. foreach($styleAttributes as $thisAttribute)
  67. {
  68. if(trim($thisAttribute))
  69. {
  70. $styleAttParts = explode(':', $thisAttribute);
  71. $styleAttName = strtolower(trim($styleAttParts[0]));
  72. $styleAttValue = strtolower(trim($styleAttParts[1]));
  73. $styleInfo[$styleAttName] = $styleAttValue;
  74. }
  75. }
  76. $style->add($styleInfo);
  77. }
  78. }
  79. protected function handleMeta($node, $container, $extra)
  80. {
  81. if(strtolower($node->getAttribute('name')) == 'pagesize')
  82. {
  83. if(strtolower($node->getAttribute('content')) == 'a4')
  84. $this->pageSize = 'a4';
  85. if(strtolower($node->getAttribute('content')) == 'letter')
  86. $this->pageSize = 'letter';
  87. else
  88. $this->documentSize = $node->getAttribute('content');
  89. }
  90. else if(strtolower($node->getAttribute('name')) == 'orientation')
  91. {
  92. if(strtolower($node->getAttribute('content')) == 'portrait')
  93. $this->pageOrientation = 'portrait';
  94. if(strtolower($node->getAttribute('content')) == 'landscape')
  95. $this->pageOrientation = 'landscape';
  96. else
  97. trigger_error("invalid orientation: " . $node->getAttribute('content'));
  98. }
  99. if($this->pageSize == 'a4' && $pageOrientation == 'portrait')
  100. $this->documentSize = PdfContext::sizeA4;
  101. else if($this->pageSize == 'a4' && $pageOrientation == 'landscape')
  102. $this->documentSize = PdfContext::sizeA4Landscape;
  103. else if($this->pageSize == 'letter' && $pageOrientation == 'portrait')
  104. $this->documentSize = PdfContext::sizeLetter;
  105. else if($this->pageSize == 'letter' && $pageOrientation == 'landscape')
  106. $this->documentSize = PdfContext::sizeLetterLandscape;
  107. }
  108. protected function handleDiv($node, $container, $extra)
  109. {
  110. $div = $container->getNewDiv();
  111. return $div;
  112. }
  113. protected function handleB($node, $container, $extra)
  114. {
  115. return $container;
  116. }
  117. /*
  118. private function __call($name, $arguments)
  119. {
  120. if(str_prefix($name, 'handle'))
  121. {
  122. }
  123. trigger_error("method call to $name not handled");
  124. }
  125. */
  126. }