Exemplo n.º 1
0
 /**
  * Takes the element's page-relative position and returns the left and top CSS offsets
  * These CSS offsets are relative to the closest positioned ancestor, which must be found by crawling the hierarchy
  * @param IdmlElement $element
  * @return array
  */
 protected function adjustOffsetsToRefPoint(IdmlElement $element)
 {
     // Get the element's page-relative position and first ancestor
     $xOffset = $element->getPosition('left');
     $yOffset = $element->getPosition('top');
     // If this element has no ancestors, return its page-relative offsets
     if (!isset($element->parentElement)) {
         return array($xOffset, $yOffset);
     }
     $ancestor = $element->parentElement;
     while (!is_null($ancestor)) {
         // SVG elements are produced as siblings, so they are never an ancestor in produced html
         // Stories aren't IdmlElements, and so have no position. But they may have ancestors that do.
         if (is_a($ancestor, 'IdmlSvgShape') || is_a($ancestor, 'IdmlStory')) {
             $ancestor = $ancestor->parentElement;
             continue;
         }
         // If the ancestor has no position, continue to the next ancestor
         if (!$ancestor->hasPosition('left') || !$ancestor->hasPosition('top')) {
             $ancestor = $ancestor->parentElement;
             continue;
         }
         // If we encountered an IdmlStory, use its text frame
         if (is_a($ancestor, 'IdmlStory')) {
             $ancestor = $ancestor->idmlTextFrame;
             continue;
         }
         // If the ancestor's positions are not set, get the next ancestor
         if (!$ancestor->hasPositions()) {
             $ancestor = $ancestor->parentElement;
             continue;
         }
         $ancestorLeft = $ancestor->getPosition('left');
         $ancestorTop = $ancestor->getPosition('top');
         // If the ancestor is positioned, return offsets calculated relative to the ancestor
         if (isset($ancestorLeft) && !is_null($ancestorLeft)) {
             return array($xOffset - $ancestorLeft, $yOffset - $ancestorTop);
         }
         // Step up the chain and reiterate
         $ancestor = $ancestor->parentElement;
     }
     // No positioned ancestor was found: return page-relative position
     return array($xOffset, $yOffset);
 }